Use actionsUtil.computeAutomationID on upload-lib
This commit is contained in:
parent
c93cbc943a
commit
3b741b35ad
12 changed files with 67 additions and 157 deletions
11
lib/actions-util.js
generated
11
lib/actions-util.js
generated
|
|
@ -355,8 +355,13 @@ async function getAnalysisKey() {
|
|||
}
|
||||
exports.getAnalysisKey = getAnalysisKey;
|
||||
async function getAutomationID() {
|
||||
let automationID = `${await getAnalysisKey()}/`;
|
||||
const environment = getOptionalInput("matrix");
|
||||
const analysis_key = await getAnalysisKey();
|
||||
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
|
||||
if (environment !== undefined && environment !== "null") {
|
||||
const environmentObject = JSON.parse(environment);
|
||||
|
|
@ -373,7 +378,7 @@ async function getAutomationID() {
|
|||
}
|
||||
return automationID;
|
||||
}
|
||||
exports.getAutomationID = getAutomationID;
|
||||
exports.computeAutomationID = computeAutomationID;
|
||||
/**
|
||||
* Get the ref currently being analyzed.
|
||||
*/
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
32
lib/actions-util.test.js
generated
32
lib/actions-util.test.js
generated
|
|
@ -64,32 +64,18 @@ ava_1.default("getAnalysisKey() when a local run", async (t) => {
|
|||
const actualAnalysisKey = await actionsutil.getAnalysisKey();
|
||||
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
|
||||
});
|
||||
ava_1.default("getAutomationID() when a local run", async (t) => {
|
||||
process.env.CODEQL_LOCAL_RUN = "true";
|
||||
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
|
||||
// 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/");
|
||||
ava_1.default("computeAutomationID()", async (t) => {
|
||||
let actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"language": "javascript", "os": "linux"}');
|
||||
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/");
|
||||
// check the environment sorting
|
||||
process.env.MATRIX = '{"os": "linux", "language": "javascript"}';
|
||||
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/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"os": "linux", "language": "javascript"}');
|
||||
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/");
|
||||
// check that an empty environment produces the right results
|
||||
process.env.MATRIX = "{}";
|
||||
actionsutil.prepareLocalRunEnvironment();
|
||||
const actualAutomationID = await actionsutil.getAutomationID();
|
||||
t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", "{}");
|
||||
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/");
|
||||
// check non string environment values
|
||||
process.env.MATRIX = '{"number": 1, "object": {"language": "javascript"}}';
|
||||
actionsutil.prepareLocalRunEnvironment();
|
||||
// TODO: uncomment once the matrix is setup
|
||||
// const actualAutomationID = await actionsutil.getAutomationID();
|
||||
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/number:/object:/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"number": 1, "object": {"language": "javascript"}}');
|
||||
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/number:/object:/");
|
||||
});
|
||||
ava_1.default("prepareEnvironment() when a local run", (t) => {
|
||||
process.env.CODEQL_LOCAL_RUN = "false";
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
20
lib/upload-lib.js
generated
20
lib/upload-lib.js
generated
|
|
@ -70,25 +70,7 @@ function getAutomationID(category, analysis_key, environment) {
|
|||
}
|
||||
return automationID;
|
||||
}
|
||||
return 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;
|
||||
return actionsUtil.computeAutomationID(analysis_key, environment);
|
||||
}
|
||||
// Upload the given payload.
|
||||
// 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
17
lib/upload-lib.test.js
generated
|
|
@ -92,23 +92,6 @@ ava_1.default("populateRunAutomationDetails", (t) => {
|
|||
// 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"}');
|
||||
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
|
||||
sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
|
||||
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -73,36 +73,45 @@ test("getAnalysisKey() when a local run", async (t) => {
|
|||
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
|
||||
});
|
||||
|
||||
test("getAutomationID() when a local run", async (t) => {
|
||||
process.env.CODEQL_LOCAL_RUN = "true";
|
||||
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
|
||||
|
||||
// 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/");
|
||||
test("computeAutomationID()", async (t) => {
|
||||
let actualAutomationID = actionsutil.computeAutomationID(
|
||||
".github/workflows/codeql-analysis.yml:analyze",
|
||||
'{"language": "javascript", "os": "linux"}'
|
||||
);
|
||||
t.deepEqual(
|
||||
actualAutomationID,
|
||||
".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"
|
||||
);
|
||||
|
||||
// check the environment sorting
|
||||
process.env.MATRIX = '{"os": "linux", "language": "javascript"}';
|
||||
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/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(
|
||||
".github/workflows/codeql-analysis.yml:analyze",
|
||||
'{"os": "linux", "language": "javascript"}'
|
||||
);
|
||||
t.deepEqual(
|
||||
actualAutomationID,
|
||||
".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"
|
||||
);
|
||||
|
||||
// check that an empty environment produces the right results
|
||||
process.env.MATRIX = "{}";
|
||||
actionsutil.prepareLocalRunEnvironment();
|
||||
const actualAutomationID = await actionsutil.getAutomationID();
|
||||
t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(
|
||||
".github/workflows/codeql-analysis.yml:analyze",
|
||||
"{}"
|
||||
);
|
||||
t.deepEqual(
|
||||
actualAutomationID,
|
||||
".github/workflows/codeql-analysis.yml:analyze/"
|
||||
);
|
||||
|
||||
// check non string environment values
|
||||
process.env.MATRIX = '{"number": 1, "object": {"language": "javascript"}}';
|
||||
actionsutil.prepareLocalRunEnvironment();
|
||||
// TODO: uncomment once the matrix is setup
|
||||
// const actualAutomationID = await actionsutil.getAutomationID();
|
||||
// t.deepEqual(actualAutomationID, "LOCAL-RUN:UNKNOWN-JOB/number:/object:/");
|
||||
actualAutomationID = actionsutil.computeAutomationID(
|
||||
".github/workflows/codeql-analysis.yml:analyze",
|
||||
'{"number": 1, "object": {"language": "javascript"}}'
|
||||
);
|
||||
t.deepEqual(
|
||||
actualAutomationID,
|
||||
".github/workflows/codeql-analysis.yml:analyze/number:/object:/"
|
||||
);
|
||||
});
|
||||
|
||||
test("prepareEnvironment() when a local run", (t) => {
|
||||
|
|
|
|||
|
|
@ -426,8 +426,17 @@ export async function getAnalysisKey(): Promise<string> {
|
|||
}
|
||||
|
||||
export async function getAutomationID(): Promise<string> {
|
||||
let automationID = `${await getAnalysisKey()}/`;
|
||||
const environment = getOptionalInput("matrix");
|
||||
const analysis_key = await getAnalysisKey();
|
||||
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
|
||||
if (environment !== undefined && environment !== "null") {
|
||||
|
|
|
|||
|
|
@ -157,47 +157,6 @@ test("populateRunAutomationDetails", (t) => {
|
|||
);
|
||||
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
|
||||
sarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
|
||||
expectedSarif = '{"runs":[{"automationDetails":{"id":"my_id"}}]}';
|
||||
|
|
|
|||
|
|
@ -78,30 +78,7 @@ function getAutomationID(
|
|||
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
|
||||
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;
|
||||
return actionsUtil.computeAutomationID(analysis_key, environment);
|
||||
}
|
||||
|
||||
// Upload the given payload.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue