Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add actions-util.getAutomationID()
David Verdeguer committed May 3, 2021

Unverified

No user is associated with the committer email.
1 parent c6e734c commit 519d077
Showing 6 changed files with 102 additions and 2 deletions.
20 changes: 20 additions & 0 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.

27 changes: 27 additions & 0 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.

32 changes: 32 additions & 0 deletions src/actions-util.test.ts
@@ -73,6 +73,38 @@ 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/");

// 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/");

// 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/");

// 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:/");
});

test("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_LOCAL_RUN = "false";
process.env.GITHUB_JOB = "YYY";
21 changes: 21 additions & 0 deletions src/actions-util.ts
@@ -425,6 +425,27 @@ export async function getAnalysisKey(): Promise<string> {
return analysisKey;
}

export async function getAutomationID(): Promise<string> {
let automationID = `${await getAnalysisKey()}/`;
const environment = getOptionalInput("matrix");

// 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;
}

/**
* Get the ref currently being analyzed.
*/

0 comments on commit 519d077

Please sign in to comment.