Skip to content

Commit 6cc7ec8

Browse files
committed
Delete analysis after uploading
The analysis is purposefully failing. We don't want a failed analysis sitting in the security center since this can cause some internal checks to erroneously fail.
1 parent 137a1e0 commit 6cc7ec8

File tree

8 files changed

+84
-0
lines changed

8 files changed

+84
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Delete uploaded SARIF
2+
description: Deletes an already uploaded SARIF analysis that was uploaded by the CodeQL Action in a previous step
3+
inputs:
4+
token:
5+
description: The GitHub token to use. It must have enough privileges to checkout the repository as well as the submodule.
6+
required: true
7+
8+
runs:
9+
using: node16
10+
main: index.js
11+
post: post.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("No action required.");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const core = require('@actions/core')
2+
3+
const sarifId = process.env.CODEQL_ACTION_TESTING_ENVIRONMENT;
4+
5+
if (!sarifId) {
6+
core.setFailed('CODEQL_ACTION_TESTING_ENVIRONMENT not set');
7+
process.exit(1);
8+
}
9+
10+
function initializeOctokit(token) {
11+
if (!token) {
12+
throw new Error('Missing GitHub Token.');
13+
}
14+
return github.getOctokit(token, {userAgent: "CodeQL-CI (github/semmle-code; submodule-check)"});
15+
}
16+
17+
const token = core.getInput('token', { required: true });
18+
core.setSecret(token);
19+
const octokit = initializeOctokit(token);
20+
21+
new Promise(async (resolve) => {
22+
await octokit.request("DELETE /repos/{owner}/{repo}/code-scanning/analyses/{sarifId}?confirm_delete", {
23+
owner: "github",
24+
repo: "codeql-action",
25+
sarifId
26+
});
27+
console.log(`Deleted uploaded SARIF analysis with ID ${sarifId}.`);
28+
resolve();
29+
}).catch((error) => {
30+
core.setFailed(error.message);
31+
process.exit(1);
32+
});

.github/workflows/__submit-sarif-failure.yml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/python312-windows.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ jobs:
3838
- name: Analyze
3939
uses: ./../action/analyze
4040
with:
41+
upload: false
4142
upload-database: false

pr-checks/checks/submit-sarif-failure.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ env:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19+
# This step ensures that the invalid uploaded SARIF file is deleted at the end of the
20+
# workflow run. This avoids any erroneous alerts with our internal reporting.
21+
- uses: ./delete-uploaded-sarif
22+
with:
23+
token: ${{ secrets.GITHUB_TOKEN }}
1924
- uses: ./init
2025
with:
2126
languages: javascript
@@ -27,8 +32,19 @@ steps:
2732
continue-on-error: true
2833
run: exit 1
2934
- uses: ./analyze
35+
id: analyze
3036
# In a real workflow, this step wouldn't run. Since we used `continue-on-error`
3137
# above, we manually disable it with an `if` condition.
3238
if: false
3339
with:
3440
category: "/test-codeql-version:${{ matrix.version }}"
41+
42+
- name: Delete uploaded analysis
43+
env:
44+
UPLOADED_SARIF_ID: ${{ steps.analyze.outputs.sarif-id }}
45+
shell: bash
46+
run: |
47+
echo "Deleting SARIF analysis with ID ${UPLOADED_SARIF_ID}"
48+
gh api \
49+
--method DELETE \
50+
/repos/github/codeql-action/code-scanning/analyses//${UPLOADED_SARIF_ID}?confirm_delete

src/environment.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ export enum EnvVar {
6868
* We check this later to ensure that it hasn't been tampered with by a late e.g. `setup-go` step.
6969
*/
7070
GO_BINARY_LOCATION = "CODEQL_ACTION_GO_BINARY",
71+
72+
/**
73+
* The internal SARIF ID of the uploaded SARIF file for the most recent uploaded SARIF file.
74+
*/
75+
UPLOADED_SARIF_ID = "CODEQL_ACTION_UPLOADED_SARIF_ID",
7176
}

src/upload-lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ async function uploadFiles(
407407
// Make the upload
408408
const sarifID = await uploadPayload(payload, repositoryNwo, logger);
409409

410+
// Make the sarif ID available to later steps
411+
core.exportVariable(EnvVar.UPLOADED_SARIF_ID, sarifID);
410412
logger.endGroup();
411413

412414
return {

0 commit comments

Comments
 (0)