Add category input

This commit is contained in:
David Verdeguer 2021-04-28 14:32:16 +02:00
parent 03f029c2a1
commit 40fb1f3f00
8 changed files with 111 additions and 31 deletions

View file

@ -137,10 +137,31 @@ test("populateRunAutomationDetails", (t) => {
const analysisKey = ".github/workflows/codeql-analysis.yml:analyze";
let expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"}}]}';
'{"runs":[{"automationDetails":{"id":"language:javascript/os:linux/"}}]}';
// Category has priority over analysis_key/environment
let modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
"language:javascript/os:linux",
analysisKey,
'{"language": "other", "os": "other"}'
);
t.deepEqual(modifiedSarif, expectedSarif);
// It doesn't matter if the category has a slash at the end or not
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
"language:javascript/os:linux/",
analysisKey,
""
);
t.deepEqual(modifiedSarif, expectedSarif);
expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
'{"language": "javascript", "os": "linux"}'
);
@ -149,6 +170,7 @@ test("populateRunAutomationDetails", (t) => {
// check the environment sorting
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
'{"os": "linux", "language": "javascript"}'
);
@ -159,6 +181,7 @@ test("populateRunAutomationDetails", (t) => {
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
"{}"
);
@ -169,6 +192,7 @@ test("populateRunAutomationDetails", (t) => {
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/number:/object:/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
'{"number": 1, "object": {"language": "javascript"}}'
);
@ -179,6 +203,7 @@ test("populateRunAutomationDetails", (t) => {
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
'{"os": "linux", "language": "javascript"}'
);

View file

@ -44,12 +44,47 @@ export function combineSarifFiles(sarifFiles: string[]): string {
// and return an updated sarif file contents.
export function populateRunAutomationDetails(
sarifContents: string,
category: string | undefined,
analysis_key: string | undefined,
environment: string | undefined
): string {
if (analysis_key === undefined) {
return sarifContents;
}
const automationID = getAutomationID(category, analysis_key, environment);
const sarif = JSON.parse(sarifContents);
for (const run of sarif.runs || []) {
if (run.automationDetails === undefined) {
run.automationDetails = {
id: automationID,
};
}
}
return JSON.stringify(sarif);
}
function getAutomationID(
category: string | undefined,
analysis_key: string | undefined,
environment: string | undefined
): string {
if (category !== undefined) {
let automationID = category;
if (!automationID.endsWith("/")) {
automationID += "/";
}
return automationID;
}
return computeAutomationID(analysis_key, environment);
}
function computeAutomationID(
analysis_key: string | undefined,
environment: string | undefined
): string {
let automationID = `${analysis_key}/`;
// the id has to be deterministic so we sort the fields
@ -66,16 +101,7 @@ export function populateRunAutomationDetails(
}
}
const sarif = JSON.parse(sarifContents);
for (const run of sarif.runs || []) {
if (run.automationDetails === undefined) {
run.automationDetails = {
id: automationID,
};
}
}
return JSON.stringify(sarif);
return automationID;
}
// Upload the given payload.
@ -361,6 +387,7 @@ async function uploadFiles(
);
sarifPayload = populateRunAutomationDetails(
sarifPayload,
actionsUtil.getOptionalInput("category"),
analysisKey,
environment
);