Skip to content

Commit c0d9de1

Browse files
Merge pull request #11 from github/analysisName
Start uploading analysis_key parameter
2 parents f668f5f + 52cd1f2 commit c0d9de1

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

lib/shared-environment.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/shared-environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const CODEQL_ACTION_CMD = 'CODEQL_ACTION_CMD';
22
export const CODEQL_ACTION_DATABASE_DIR = 'CODEQL_ACTION_DATABASE_DIR';
33
export const CODEQL_ACTION_LANGUAGES = 'CODEQL_ACTION_LANGUAGES';
4+
export const CODEQL_ACTION_ANALYSIS_KEY = 'CODEQL_ACTION_ANALYSIS_KEY';
45
export const ODASA_TRACER_CONFIGURATION = 'ODASA_TRACER_CONFIGURATION';
56
export const CODEQL_ACTION_SCANNED_LANGUAGES = 'CODEQL_ACTION_SCANNED_LANGUAGES';
67
export const CODEQL_ACTION_TRACED_LANGUAGES = 'CODEQL_ACTION_TRACED_LANGUAGES';

src/upload-lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ async function uploadFiles(sarifFiles: string[]): Promise<boolean> {
140140
const commitOid = util.getRequiredEnvParam('GITHUB_SHA');
141141
const workflowRunIDStr = util.getRequiredEnvParam('GITHUB_RUN_ID');
142142
const ref = util.getRequiredEnvParam('GITHUB_REF'); // it's in the form "refs/heads/master"
143+
const analysisKey = await util.getAnalysisKey();
143144
const analysisName = util.getRequiredEnvParam('GITHUB_WORKFLOW');
144145
const startedAt = process.env[sharedEnv.CODEQL_ACTION_STARTED_AT];
145146

@@ -167,6 +168,7 @@ async function uploadFiles(sarifFiles: string[]): Promise<boolean> {
167168
const payload = JSON.stringify({
168169
"commit_oid": commitOid,
169170
"ref": ref,
171+
"analysis_key": analysisKey,
170172
"analysis_name": analysisName,
171173
"sarif": zipped_sarif,
172174
"workflow_run_id": workflowRunID,

src/util.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,54 @@ export async function getLanguages(): Promise<string[]> {
152152
return languages;
153153
}
154154

155+
/**
156+
* Get the path of the currently executing workflow.
157+
*/
158+
async function getWorkflowPath(): Promise<string> {
159+
const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/");
160+
const owner = repo_nwo[0];
161+
const repo = repo_nwo[1];
162+
const run_id = getRequiredEnvParam('GITHUB_RUN_ID');
163+
164+
const ok = new octokit.Octokit({
165+
auth: core.getInput('token'),
166+
userAgent: "CodeQL Action",
167+
log: consoleLogLevel({ level: 'debug' })
168+
});
169+
170+
const runsResponse = await ok.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
171+
owner,
172+
repo,
173+
run_id
174+
});
175+
const workflowUrl = runsResponse.data.workflow_url;
176+
177+
const workflowResponse = await ok.request('GET ' + workflowUrl);
178+
179+
return workflowResponse.data.path;
180+
}
181+
182+
/**
183+
* Get the analysis key paramter for the current job.
184+
*
185+
* This will combine the workflow path and current job name.
186+
* Computing this the first time requires making requests to
187+
* the github API, but after that the result will be cached.
188+
*/
189+
export async function getAnalysisKey(): Promise<string> {
190+
let analysisKey = process.env[sharedEnv.CODEQL_ACTION_ANALYSIS_KEY];
191+
if (analysisKey !== undefined) {
192+
return analysisKey;
193+
}
194+
195+
const workflowPath = await getWorkflowPath();
196+
const jobName = getRequiredEnvParam('GITHUB_JOB');
197+
198+
analysisKey = workflowPath + ':' + jobName;
199+
core.exportVariable(sharedEnv.CODEQL_ACTION_ANALYSIS_KEY, analysisKey);
200+
return analysisKey;
201+
}
202+
155203
interface StatusReport {
156204
"workflow_run_id": number;
157205
"workflow_name": string;

0 commit comments

Comments
 (0)