Skip to content
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

Upload per-database diagnostic SARIFs on green and red runs #1556

Merged
merged 28 commits into from
Mar 20, 2023

Conversation

angelapwen
Copy link
Contributor

@angelapwen angelapwen commented Mar 3, 2023

On failed runs we now call codeql database diagnostic-export to create and upload a SARIF file for per-database diagnostics. If this file was not created, we fall back on the original behavior of uploading the result of codeql export diagnostics.

On successful runs, we add the --sarif-include-diagnostics option to the codeql database interpret-results command.

Both the addition of the option to the interpret-results call, and the codeql database diagnostic-export call, are gated behind the ExportDiagnosticsEnabled feature flag.

We test the green run scenario with an integration test that adds a diagnostic prior to the analyze step, with the feature flag environment variable override to enable the feature. We check the SARIF file generated to make sure it includes the manually added diagnostic.

[GitHub internal only]: we test the red run scenario by writing a similar workflow as the one in the integration test, but failing the workflow after adding the diagnostic. See run and corresponding tool status page for the surfaced diagnostic.

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.

Move the definition of the variable from util to shared-environment, and rename.
Adds `codeql database export-diagnostics` command and `CODEQL_ACTION_IS_DATABASE_CLUSTER` internal environment variable
Copy link
Contributor

@henrymercer henrymercer left a comment

Choose a reason for hiding this comment

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

In the green runs path, we call codeql database interpret-results once per database to produce a SARIF file per DB, combine all these SARIF files together, and then submit a single SARIF file. This has different behaviour to uploading each SARIF file separately, since code scanning will overwrite the existing data for that category each time you upload a new SARIF file.

I think for red runs we can do something simpler — config.dbLocation should always be a DB cluster (at least for all CodeQL versions that support diagnostics). Therefore we should be able to run codeql database export-diagnostics once on that DB cluster and submit the resulting SARIF file.

@angelapwen
Copy link
Contributor Author

Unrelated CI failures that I'll fix in a separate PR!

Copy link
Contributor

@henrymercer henrymercer left a comment

Choose a reason for hiding this comment

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

Looking good so far. An integration test might be tricky to write due to most of the work happening in post-init. We could work around that by creating a separate Action for the test (like we did for the query filters tests), but I'd be happy to merge this with detailed unit tests instead.

src/init-action-post-helper.ts Outdated Show resolved Hide resolved
src/init-action-post-helper.ts Outdated Show resolved Hide resolved
src/codeql.ts Show resolved Hide resolved
@angelapwen angelapwen marked this pull request as ready for review March 14, 2023 00:00
@angelapwen angelapwen requested a review from a team as a code owner March 14, 2023 00:00
@angelapwen angelapwen changed the title [WIP] Upload per-database diagnostic SARIFs on failed runs Upload per-database diagnostic SARIFs on failed runs Mar 14, 2023
@angelapwen
Copy link
Contributor Author

I've addressed the existing comments other than testing. I need to update some of the existing unit tests to reflect the new behavior, as well as write new ones to confirm that the appropriate export command is called as expected ✍️

@angelapwen angelapwen dismissed stale reviews from ghost via 3f5acf0 March 17, 2023 21:36
@angelapwen angelapwen changed the title Upload per-database diagnostic SARIFs on failed runs Upload per-database diagnostic SARIFs on green and red runs Mar 17, 2023
henrymercer
henrymercer previously approved these changes Mar 20, 2023
Copy link
Contributor

@henrymercer henrymercer left a comment

Choose a reason for hiding this comment

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

Nice!

pr-checks/checks/config-export.yml Show resolved Hide resolved
pr-checks/checks/diagnostics-export.yml Outdated Show resolved Hide resolved
Comment on lines 41 to 44
const diagnosticToolExecutionNotification = toolExecutionNotifications[toolExecutionNotifications.length - 1];
if (diagnosticToolExecutionNotification.descriptor.id !== 'lang/diagnostics/example' || diagnosticToolExecutionNotification.message.text !== 'Plaintext message') {
core.setFailed('`toolExecutionNotification` related to this diagnostic was not found in SARIF');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will work with the current implementation, but we probably don't want to rely on the ordering of the SARIF file in general. Could we convert this to instead check inclusion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! Done. Not sure if there's a cleaner way to check for inclusion (maybe if I used a filter and checked if there were any elements left afterwards..?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Filtering might be slightly neater. You could then also check we have exactly one diagnostic with that ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that's a benefit — done!

pr-checks/checks/diagnostics-export.yml Outdated Show resolved Hide resolved
pr-checks/checks/diagnostics-export.yml Outdated Show resolved Hide resolved
src/init-action-post-helper.test.ts Outdated Show resolved Hide resolved
src/init-action-post-helper.test.ts Outdated Show resolved Hide resolved
src/init-action-post-helper.test.ts Show resolved Hide resolved
src/codeql.ts Show resolved Hide resolved
@angelapwen angelapwen dismissed stale reviews from ghost and henrymercer via 1954584 March 20, 2023 19:26
angelapwen and others added 2 commits March 20, 2023 12:29
Co-authored-by: Henry Mercer <henry.mercer@me.com>
henrymercer
henrymercer previously approved these changes Mar 20, 2023
Copy link
Contributor

@henrymercer henrymercer left a comment

Choose a reason for hiding this comment

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

I'd recommend using some functional list methods to simplify the tests, otherwise LGTM

@angelapwen angelapwen enabled auto-merge (squash) March 20, 2023 20:23
@angelapwen angelapwen merged commit 3cbd063 into github:main Mar 20, 2023
@angelapwen angelapwen deleted the failed-database-diagnostic branch March 21, 2023 18:12
@github-actions github-actions bot mentioned this pull request Mar 22, 2023
6 tasks
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