Use actionsUtil.computeAutomationID on upload-lib

This commit is contained in:
David Verdeguer 2021-05-03 19:56:04 +02:00
parent c93cbc943a
commit 3b741b35ad
12 changed files with 67 additions and 157 deletions

11
lib/actions-util.js generated
View file

@ -355,8 +355,13 @@ async function getAnalysisKey() {
} }
exports.getAnalysisKey = getAnalysisKey; exports.getAnalysisKey = getAnalysisKey;
async function getAutomationID() { async function getAutomationID() {
let automationID = `${await getAnalysisKey()}/`; const analysis_key = await getAnalysisKey();
const environment = getOptionalInput("matrix"); const environment = getRequiredInput("matrix");
return computeAutomationID(analysis_key, environment);
}
exports.getAutomationID = getAutomationID;
function computeAutomationID(analysis_key, environment) {
let automationID = `${analysis_key}/`;
// the id has to be deterministic so we sort the fields // the id has to be deterministic so we sort the fields
if (environment !== undefined && environment !== "null") { if (environment !== undefined && environment !== "null") {
const environmentObject = JSON.parse(environment); const environmentObject = JSON.parse(environment);
@ -373,7 +378,7 @@ async function getAutomationID() {
} }
return automationID; return automationID;
} }
exports.getAutomationID = getAutomationID; exports.computeAutomationID = computeAutomationID;
/** /**
* Get the ref currently being analyzed. * Get the ref currently being analyzed.
*/ */

File diff suppressed because one or more lines are too long

View file

@ -64,32 +64,18 @@ ava_1.default("getAnalysisKey() when a local run", async (t) => {
const actualAnalysisKey = await actionsutil.getAnalysisKey(); const actualAnalysisKey = await actionsutil.getAnalysisKey();
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB"); t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
}); });
ava_1.default("getAutomationID() when a local run", async (t) => { ava_1.default("computeAutomationID()", async (t) => {
process.env.CODEQL_LOCAL_RUN = "true"; let actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"language": "javascript", "os": "linux"}');
process.env.CODEQL_ACTION_ANALYSIS_KEY = ""; t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/");
// TODO: setup the matrix as an input
process.env.MATRIX = '{"language": "javascript", "os": "linux"}';
actionsutil.prepareLocalRunEnvironment();
// TODO: uncomment once the matrix is setup
// const actualAutomationID = await actionsutil.getAutomationID();
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/language:javascript/os:linux/");
// check the environment sorting // check the environment sorting
process.env.MATRIX = '{"os": "linux", "language": "javascript"}'; actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"os": "linux", "language": "javascript"}');
actionsutil.prepareLocalRunEnvironment(); t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/");
// TODO: uncomment once the matrix is setup
// const actualAutomationID = await actionsutil.getAutomationID();
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/language:javascript/os:linux/");
// check that an empty environment produces the right results // check that an empty environment produces the right results
process.env.MATRIX = "{}"; actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", "{}");
actionsutil.prepareLocalRunEnvironment(); t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/");
const actualAutomationID = await actionsutil.getAutomationID();
t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/");
// check non string environment values // check non string environment values
process.env.MATRIX = '{"number": 1, "object": {"language": "javascript"}}'; actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"number": 1, "object": {"language": "javascript"}}');
actionsutil.prepareLocalRunEnvironment(); t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/number:/object:/");
// TODO: uncomment once the matrix is setup
// const actualAutomationID = await actionsutil.getAutomationID();
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/number:/object:/");
}); });
ava_1.default("prepareEnvironment() when a local run", (t) => { ava_1.default("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_LOCAL_RUN = "false"; process.env.CODEQL_LOCAL_RUN = "false";

File diff suppressed because one or more lines are too long

20
lib/upload-lib.js generated
View file

@ -70,25 +70,7 @@ function getAutomationID(category, analysis_key, environment) {
} }
return automationID; return automationID;
} }
return computeAutomationID(analysis_key, environment); return actionsUtil.computeAutomationID(analysis_key, environment);
}
function computeAutomationID(analysis_key, environment) {
let automationID = `${analysis_key}/`;
// the id has to be deterministic so we sort the fields
if (environment !== undefined && environment !== "null") {
const environmentObject = JSON.parse(environment);
for (const entry of Object.entries(environmentObject).sort()) {
if (typeof entry[1] === "string") {
automationID += `${entry[0]}:${entry[1]}/`;
}
else {
// In code scanning we just handle the string values,
// the rest get converted to the empty string
automationID += `${entry[0]}:/`;
}
}
}
return automationID;
} }
// Upload the given payload. // Upload the given payload.
// If the request fails then this will retry a small number of times. // If the request fails then this will retry a small number of times.

File diff suppressed because one or more lines are too long

17
lib/upload-lib.test.js generated
View file

@ -92,23 +92,6 @@ ava_1.default("populateRunAutomationDetails", (t) => {
// It doesn't matter if the category has a slash at the end or not // It doesn't matter if the category has a slash at the end or not
modifiedSarif = uploadLib.populateRunAutomationDetails(sarif, "language:javascript/os:linux/", analysisKey, ""); modifiedSarif = uploadLib.populateRunAutomationDetails(sarif, "language:javascript/os:linux/", analysisKey, "");
t.deepEqual(modifiedSarif, expectedSarif); 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"}');
t.deepEqual(modifiedSarif, expectedSarif);
// check the environment sorting
modifiedSarif = uploadLib.populateRunAutomationDetails(sarif, undefined, 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, undefined, analysisKey, "{}");
t.deepEqual(modifiedSarif, expectedSarif);
// check non string environment values
expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/number:/object:/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(sarif, undefined, analysisKey, '{"number": 1, "object": {"language": "javascript"}}');
t.deepEqual(modifiedSarif, expectedSarif);
// check that the automation details doesn't get overwritten // check that the automation details doesn't get overwritten
sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}'; sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}'; expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';

File diff suppressed because one or more lines are too long

View file

@ -73,36 +73,45 @@ test("getAnalysisKey() when a local run", async (t) => {
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB"); t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
}); });
test("getAutomationID() when a local run", async (t) => { test("computeAutomationID()", async (t) => {
process.env.CODEQL_LOCAL_RUN = "true"; let actualAutomationID = actionsutil.computeAutomationID(
process.env.CODEQL_ACTION_ANALYSIS_KEY = ""; ".github/workflows/codeql-analysis.yml:analyze",
'{"language": "javascript", "os": "linux"}'
// TODO: setup the matrix as an input );
process.env.MATRIX = '{"language": "javascript", "os": "linux"}'; t.deepEqual(
actionsutil.prepareLocalRunEnvironment(); actualAutomationID,
// TODO: uncomment once the matrix is setup ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"
// const actualAutomationID = await actionsutil.getAutomationID(); );
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/language:javascript/os:linux/");
// check the environment sorting // check the environment sorting
process.env.MATRIX = '{"os": "linux", "language": "javascript"}'; actualAutomationID = actionsutil.computeAutomationID(
actionsutil.prepareLocalRunEnvironment(); ".github/workflows/codeql-analysis.yml:analyze",
// TODO: uncomment once the matrix is setup '{"os": "linux", "language": "javascript"}'
// const actualAutomationID = await actionsutil.getAutomationID(); );
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/language:javascript/os:linux/"); t.deepEqual(
actualAutomationID,
".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"
);
// check that an empty environment produces the right results // check that an empty environment produces the right results
process.env.MATRIX = "{}"; actualAutomationID = actionsutil.computeAutomationID(
actionsutil.prepareLocalRunEnvironment(); ".github/workflows/codeql-analysis.yml:analyze",
const actualAutomationID = await actionsutil.getAutomationID(); "{}"
t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/"); );
t.deepEqual(
actualAutomationID,
".github/workflows/codeql-analysis.yml:analyze/"
);
// check non string environment values // check non string environment values
process.env.MATRIX = '{"number": 1, "object": {"language": "javascript"}}'; actualAutomationID = actionsutil.computeAutomationID(
actionsutil.prepareLocalRunEnvironment(); ".github/workflows/codeql-analysis.yml:analyze",
// TODO: uncomment once the matrix is setup '{"number": 1, "object": {"language": "javascript"}}'
// const actualAutomationID = await actionsutil.getAutomationID(); );
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/number:/object:/"); t.deepEqual(
actualAutomationID,
".github/workflows/codeql-analysis.yml:analyze/number:/object:/"
);
}); });
test("prepareEnvironment() when a local run", (t) => { test("prepareEnvironment() when a local run", (t) => {

View file

@ -426,8 +426,17 @@ export async function getAnalysisKey(): Promise<string> {
} }
export async function getAutomationID(): Promise<string> { export async function getAutomationID(): Promise<string> {
let automationID = `${await getAnalysisKey()}/`; const analysis_key = await getAnalysisKey();
const environment = getOptionalInput("matrix"); const environment = getRequiredInput("matrix");
return computeAutomationID(analysis_key, environment);
}
export 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 // the id has to be deterministic so we sort the fields
if (environment !== undefined && environment !== "null") { if (environment !== undefined && environment !== "null") {

View file

@ -157,47 +157,6 @@ test("populateRunAutomationDetails", (t) => {
); );
t.deepEqual(modifiedSarif, expectedSarif); 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"}'
);
t.deepEqual(modifiedSarif, expectedSarif);
// check the environment sorting
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
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,
undefined,
analysisKey,
"{}"
);
t.deepEqual(modifiedSarif, expectedSarif);
// check non string environment values
expectedSarif =
'{"runs":[{"automationDetails":{"id":".github/workflows/codeql-analysis.yml:analyze/number:/object:/"}}]}';
modifiedSarif = uploadLib.populateRunAutomationDetails(
sarif,
undefined,
analysisKey,
'{"number": 1, "object": {"language": "javascript"}}'
);
t.deepEqual(modifiedSarif, expectedSarif);
// check that the automation details doesn't get overwritten // check that the automation details doesn't get overwritten
sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}'; sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}'; expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';

View file

@ -78,30 +78,7 @@ function getAutomationID(
return automationID; return automationID;
} }
return computeAutomationID(analysis_key, environment); return actionsUtil.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
if (environment !== undefined && environment !== "null") {
const environmentObject = JSON.parse(environment);
for (const entry of Object.entries(environmentObject).sort()) {
if (typeof entry[1] === "string") {
automationID += `${entry[0]}:${entry[1]}/`;
} else {
// In code scanning we just handle the string values,
// the rest get converted to the empty string
automationID += `${entry[0]}:/`;
}
}
}
return automationID;
} }
// Upload the given payload. // Upload the given payload.