Skip to content

Produce separate SARIF file for quality-queries alerts #2935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

mbg
Copy link
Member

@mbg mbg commented Jun 16, 2025

Follows on from #2917.

This PR modifies the action to produce separate SARIF files for code quality queries (as specified as arguments to the quality-queries input) and uploads them to the code quality API. The approach here is that:

  • We remove the logic from Add new quality-queries input #2917 that adds the quality-queries input to the code scanning configuration file that's used when initialising the database.
  • That means that the quality queries are no longer included in the config-queries.qls that's generated by the CLI.
  • When we call database run-queries, and if quality queries are enabled, we explicitly specify config-queries.qls (which the CLI normally does by default if nothing is specified) + the quality queries.
  • Since the quality queries are only specified as argument to database run-queries, the existing call to database interpret-results generates a SARIF that only includes results relating to security queries (i.e. config-queries.qls).
  • We make an additional call to database interpret-results for queries that were specified as arguments to the quality-queries input (if any). This results in SARIF files for those queries.
  • We then upload them to the CQ API endpoint.

Notes

  • The changes aim to be as backwards-compatible as possible. That includes:
    • SARIF files are output to the same place as before (e.g. as opposed to having separate subdirectories for security and quality SARIFs). SARIF files containing quality results are identified by the .quality.sarif extension.
  • We don't support query-filter options with respect to code quality results.

Merge / deployment checklist

  • Confirm this change is backwards compatible with existing workflows.
  • Confirm the readme has been updated if necessary.
  • Confirm the changelog has been updated if necessary.

@mbg mbg force-pushed the mbg/interpret-cq-results branch 2 times, most recently from 87b6687 to fe76653 Compare June 17, 2025 13:37
@mbg mbg force-pushed the mbg/interpret-cq-results branch from 2bf7322 to 24e4f58 Compare June 23, 2025 17:35
@mbg mbg force-pushed the mbg/interpret-cq-results branch from 7e59f77 to f7fbaa0 Compare June 24, 2025 12:09
@mbg mbg marked this pull request as ready for review June 24, 2025 13:08
@mbg mbg requested a review from a team as a code owner June 24, 2025 13:08
@mbg mbg requested a review from Copilot June 24, 2025 13:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables separate generation and upload of SARIF files for code-quality queries specified via the quality-queries input, in addition to the existing code-scanning SARIF flow.

  • Introduces SARIF_UPLOAD_TARGET enum and UploadTarget interface to distinguish code scanning vs. code quality uploads.
  • Extends file discovery (findSarifFilesInDir, getSarifFilePaths) and upload logic (uploadFiles, uploadPayload, validateUniqueCategory) to filter and handle .quality.sarif files.
  • Updates analyze.ts/analyze-action.ts to produce and upload quality SARIFs when quality-queries are provided, and adjusts CI workflows to upload/check both SARIF artifacts.

Reviewed Changes

Copilot reviewed 12 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/upload-lib.ts Added enum and targets for separate SARIF uploads, filtering logic for .quality.sarif.
src/upload-lib.test.ts Added tests for filtering .quality.sarif files and validateUniqueCategory prefix.
src/analyze.ts Introduced default query suites and resolveQuerySuiteAlias; generate quality SARIF.
src/analyze.test.ts Added tests for resolveQuerySuiteAlias.
src/analyze-action.ts Uploads quality SARIF and sets quality-sarif-id output when quality-queries present.
pr-checks/checks/quality-queries.yml CI updates to upload security vs. quality SARIF artifacts and adjust checks.
Comments suppressed due to low confidence (2)

src/upload-lib.ts:427

  • The getSarifFilePaths function bypasses the isSarif filter when sarifPath is a single file. This can lead to uploading unwanted files (e.g., a .quality.sarif when targeting code-scanning). Apply isSarif to the single-file case or throw if it does not match.
    sarifFiles = [sarifPath];

pr-checks/checks/quality-queries.yml:29

  • [nitpick] The CI step now only checks config properties in the quality SARIF, omitting the original security SARIF check. Consider adding a separate check for ${{ runner.temp }}/results/javascript.sarif to ensure both artifacts are validated.
      SARIF_PATH: "${{ runner.temp }}/results/javascript.quality.sarif"

@mbg mbg force-pushed the mbg/interpret-cq-results branch 2 times, most recently from 3393b4e to 81969d7 Compare June 25, 2025 13:24
@mbg mbg force-pushed the mbg/interpret-cq-results branch 2 times, most recently from 4088317 to b403ba2 Compare June 25, 2025 13:38
@mbg mbg force-pushed the mbg/interpret-cq-results branch from b403ba2 to 79049d9 Compare June 25, 2025 13:42
Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, and pleasantly surgical.

I think this does the right thing, except for a missing quality.sarif predicate in the very end.

Other than that it's mostly nits, and some concerns about maintenance in the long term. We have already discussed them elsewhere. Perhaps we should update the discussion document with our current choice for query resolution.

@@ -567,6 +581,30 @@ export function buildPayload(
return payloadObj;
}

// Represents configurations for different services that we can upload SARIF to.
export interface UploadTarget {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding the target/endpoint comment above: this is a much better place to call something target. I'd probably prefer SarifUploadConfig but that is splitting hairs.

sentinelPrefix: string;
}

// Represents the Code Scanning upload target.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray thought. I have seen both line comments and jsdoc comments on your new functions. Is it deliberate that you do not use jsdoc here? If yes, OK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the existing codebase is fairly inconsistent in this regard. I probably just subconsciously mirrored what adjacent definitions do. I'll make this more consistent for my changes.

@mbg mbg force-pushed the mbg/interpret-cq-results branch from a4d5138 to 362ebf8 Compare June 27, 2025 11:33
@mbg mbg requested a review from esbena June 27, 2025 11:42
esbena
esbena previously approved these changes Jun 27, 2025
Copy link
Contributor

@esbena esbena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! There's one naming nit left, but it is quite minor.

Comment on lines 310 to 314
export enum SARIF_UPLOAD_ENDPOINT {
CODE_SCANNING_UPLOAD_TARGET = "PUT /repos/:owner/:repo/code-scanning/analysis",
CODE_QUALITY_UPLOAD_TARGET = "PUT /repos/:owner/:repo/code-quality/analysis",
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export enum SARIF_UPLOAD_ENDPOINT {
CODE_SCANNING_UPLOAD_TARGET = "PUT /repos/:owner/:repo/code-scanning/analysis",
CODE_QUALITY_UPLOAD_TARGET = "PUT /repos/:owner/:repo/code-quality/analysis",
}
export enum SARIF_UPLOAD_ENDPOINT {
CODE_SCANNING = "PUT /repos/:owner/:repo/code-scanning/analysis",
CODE_QUALITY = "PUT /repos/:owner/:repo/code-quality/analysis",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants