Skip to content

Commit

Permalink
Showing 12 changed files with 67 additions and 157 deletions.
11 changes: 8 additions & 3 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

32 changes: 9 additions & 23 deletions lib/actions-util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.test.js.map

Large diffs are not rendered by default.

20 changes: 1 addition & 19 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.js.map

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions lib/upload-lib.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/upload-lib.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 33 additions & 24 deletions src/actions-util.test.ts
@@ -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) => {
13 changes: 11 additions & 2 deletions src/actions-util.ts
@@ -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") {
41 changes: 0 additions & 41 deletions src/upload-lib.test.ts
@@ -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"}}]}';
25 changes: 1 addition & 24 deletions src/upload-lib.ts
@@ -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.

0 comments on commit 3b741b3

Please sign in to comment.