Skip to content

Commit bbf0a22

Browse files
authored
Merge pull request #801 from github/aeisenberg/upload-by-category
Allow multiple uploads in a single job
2 parents ea8a175 + d7b5c61 commit bbf0a22

10 files changed

+101
-22
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ No user facing changes.
88

99
- The `init` step of the Action now supports `ram` and `threads` inputs to limit resource use of CodeQL extractors. These inputs also serve as defaults to the subsequent `analyze` step, which finalizes the database and executes queries. [#738](https://github.com/github/codeql-action/pull/738)
1010
- When used with CodeQL 2.7.1 or above, the Action now includes custom query help in the analysis results uploaded to GitHub code scanning, if available. To add help text for a custom query, create a Markdown file next to the `.ql` file containing the query, using the same base name but the file extension `.md`. [#804](https://github.com/github/codeql-action/pull/804)
11+
- The `upload-sarif` action now allows multiple uploads in a single job, as long as they have different categories. [#801](https://github.com/github/codeql-action/pull/801)
1112

1213
## 1.0.21 - 28 Oct 2021
1314

Diff for: lib/actions-util.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/actions-util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/upload-lib.js

+27-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/upload-lib.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/upload-lib.test.js

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: lib/upload-lib.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/actions-util.ts

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const getCommitOid = async function (ref = "HEAD"): Promise<string> {
8585
core.info(
8686
`Failed to call git to get current commit. Continuing with data from environment: ${e}`
8787
);
88+
core.info((e as Error).stack || "NO STACK");
8889
return getRequiredEnvParam("GITHUB_SHA");
8990
}
9091
};

Diff for: src/upload-lib.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,24 @@ test("populateRunAutomationDetails", (t) => {
175175
);
176176
t.deepEqual(modifiedSarif, expectedSarif);
177177
});
178+
179+
test("validateUniqueCategory", (t) => {
180+
t.notThrows(() => uploadLib.validateUniqueCategory(undefined));
181+
t.throws(() => uploadLib.validateUniqueCategory(undefined));
182+
183+
t.notThrows(() => uploadLib.validateUniqueCategory("abc"));
184+
t.throws(() => uploadLib.validateUniqueCategory("abc"));
185+
186+
t.notThrows(() => uploadLib.validateUniqueCategory("def"));
187+
t.throws(() => uploadLib.validateUniqueCategory("def"));
188+
189+
// Our category sanitization is not perfect. Here are some examples
190+
// of where we see false clashes
191+
t.notThrows(() => uploadLib.validateUniqueCategory("abc/def"));
192+
t.throws(() => uploadLib.validateUniqueCategory("abc@def"));
193+
t.throws(() => uploadLib.validateUniqueCategory("abc_def"));
194+
t.throws(() => uploadLib.validateUniqueCategory("abc def"));
195+
196+
// this one is fine
197+
t.notThrows(() => uploadLib.validateUniqueCategory("abc_ def"));
198+
});

Diff for: src/upload-lib.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,7 @@ async function uploadFiles(
343343
logger.startGroup("Uploading results");
344344
logger.info(`Processing sarif files: ${JSON.stringify(sarifFiles)}`);
345345

346-
if (util.isActions()) {
347-
// This check only works on actions as env vars don't persist between calls to the runner
348-
const sentinelEnvVar = "CODEQL_UPLOAD_SARIF";
349-
if (process.env[sentinelEnvVar]) {
350-
throw new Error(
351-
"Aborting upload: only one run of the codeql/analyze or codeql/upload-sarif actions is allowed per job"
352-
);
353-
}
354-
core.exportVariable(sentinelEnvVar, sentinelEnvVar);
355-
}
346+
validateUniqueCategory(category);
356347

357348
// Validate that the files we were asked to upload are all valid SARIF files
358349
for (const file of sarifFiles) {
@@ -409,3 +400,33 @@ async function uploadFiles(
409400
num_results_in_sarif: numResultInSarif,
410401
};
411402
}
403+
404+
export function validateUniqueCategory(category: string | undefined) {
405+
if (util.isActions()) {
406+
// This check only works on actions as env vars don't persist between calls to the runner
407+
const sentinelEnvVar = `CODEQL_UPLOAD_SARIF${
408+
category ? `_${sanitize(category)}` : ""
409+
}`;
410+
if (process.env[sentinelEnvVar]) {
411+
throw new Error(
412+
"Aborting upload: only one run of the codeql/analyze or codeql/upload-sarif actions is allowed per job per category. " +
413+
"Please specify a unique `category` to call this action multiple times. " +
414+
`Category: ${category ? category : "(none)"}`
415+
);
416+
}
417+
core.exportVariable(sentinelEnvVar, sentinelEnvVar);
418+
}
419+
}
420+
421+
/**
422+
* Santizes a string to be used as an environment variable name.
423+
* This will replace all non-alphanumeric characters with underscores.
424+
* There could still be some false category clashes if two uploads
425+
* occur that differ only in their non-alphanumeric characters. This is
426+
* unlikely.
427+
*
428+
* @param str the initial value to sanitize
429+
*/
430+
function sanitize(str: string) {
431+
return str.replace(/[^a-zA-Z0-9_]/g, "_");
432+
}

0 commit comments

Comments
 (0)