Add automationdetails id to runs

This commit is contained in:
David Verdeguer 2021-04-15 16:20:49 +02:00
parent 6aebd1b98a
commit 47755f0910
6 changed files with 111 additions and 2 deletions

View file

@ -131,3 +131,36 @@ test("finding SARIF files", async (t) => {
]);
});
});
test("populateRunAutomationDetails", (t) => {
const sarif = '{"runs": [{}]}';
const analysisKey = ".github/workflows/codeql-analysis.yml:analyze";
let expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"}}]}';
let modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
'{"language": "javascript", "os": "linux"}'
);
t.deepEqual(modifiedSarif, expectedSarif);
// check the environment sorting
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
'{"os": "linux", "language": "javascript"}'
);
t.deepEqual(modifiedSarif, expectedSarif);
// check that an empty environment produces the right results
expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
analysisKey,
"{}"
);
t.deepEqual(modifiedSarif, expectedSarif);
});

View file

@ -40,6 +40,37 @@ export function combineSarifFiles(sarifFiles: string[]): string {
return JSON.stringify(combinedSarif);
}
// Populates the run.automationDetails.id field using the analysis_key and environment
// and return an updated sarif file contents.
export function populateRunAutomationDetails(
sarifContents: string,
analysis_key: string | undefined,
environment: string | undefined
): string {
if (analysis_key === undefined) {
return sarifContents;
}
let automationID = `${analysis_key}/`;
// the id has to be deterministic so we sort the fields
if (environment !== undefined) {
console.log(environment);
const environmentObject = JSON.parse(environment);
for (const entry of Object.entries(environmentObject).sort()) {
automationID += `${entry[0]}:${entry[1]}/`; //automationID + entry[0] + ':' + entry[1] + '/';
}
}
const sarif = JSON.parse(sarifContents);
for (const run of sarif.runs || []) {
run.automationDetails = {
id: automationID,
};
}
return JSON.stringify(sarif);
}
// Upload the given payload.
// If the request fails then this will retry a small number of times.
async function uploadPayload(
@ -321,6 +352,11 @@ async function uploadFiles(
checkoutPath,
logger
);
sarifPayload = populateRunAutomationDetails(
sarifPayload,
analysisKey,
environment
);
const zippedSarif = zlib.gzipSync(sarifPayload).toString("base64");
const checkoutURI = fileUrl(checkoutPath);