diff --git a/lib/shared-environment.js b/lib/shared-environment.js index 1a6a3d329a..fabb260ddf 100644 --- a/lib/shared-environment.js +++ b/lib/shared-environment.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.CODEQL_ACTION_CMD = 'CODEQL_ACTION_CMD'; exports.CODEQL_ACTION_DATABASE_DIR = 'CODEQL_ACTION_DATABASE_DIR'; exports.CODEQL_ACTION_LANGUAGES = 'CODEQL_ACTION_LANGUAGES'; +exports.CODEQL_ACTION_ANALYSIS_KEY = 'CODEQL_ACTION_ANALYSIS_KEY'; exports.ODASA_TRACER_CONFIGURATION = 'ODASA_TRACER_CONFIGURATION'; exports.CODEQL_ACTION_SCANNED_LANGUAGES = 'CODEQL_ACTION_SCANNED_LANGUAGES'; exports.CODEQL_ACTION_TRACED_LANGUAGES = 'CODEQL_ACTION_TRACED_LANGUAGES'; diff --git a/lib/upload-lib.js b/lib/upload-lib.js index 6c8100d587..c371c330d5 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -137,6 +137,7 @@ async function uploadFiles(sarifFiles) { const commitOid = util.getRequiredEnvParam('GITHUB_SHA'); const workflowRunIDStr = util.getRequiredEnvParam('GITHUB_RUN_ID'); const ref = util.getRequiredEnvParam('GITHUB_REF'); // it's in the form "refs/heads/master" + const analysisKey = await util.getAnalysisKey(); const analysisName = util.getRequiredEnvParam('GITHUB_WORKFLOW'); const startedAt = process.env[sharedEnv.CODEQL_ACTION_STARTED_AT]; core.info("Uploading sarif files: " + JSON.stringify(sarifFiles)); @@ -158,6 +159,7 @@ async function uploadFiles(sarifFiles) { const payload = JSON.stringify({ "commit_oid": commitOid, "ref": ref, + "analysis_key": analysisKey, "analysis_name": analysisName, "sarif": zipped_sarif, "workflow_run_id": workflowRunID, diff --git a/lib/util.js b/lib/util.js index a9d79bb416..ae0ea412bf 100644 --- a/lib/util.js +++ b/lib/util.js @@ -151,6 +151,47 @@ async function getLanguages() { return languages; } exports.getLanguages = getLanguages; +/** + * Get the path of the currently executing workflow. + */ +async function getWorkflowPath() { + const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/"); + const owner = repo_nwo[0]; + const repo = repo_nwo[1]; + const run_id = getRequiredEnvParam('GITHUB_RUN_ID'); + const ok = new octokit.Octokit({ + auth: core.getInput('token'), + userAgent: "CodeQL Action", + log: console_log_level_1.default({ level: 'debug' }) + }); + const runsResponse = await ok.request('GET /repos/:owner/:repo/actions/runs/:run_id', { + owner, + repo, + run_id + }); + const workflowUrl = runsResponse.data.workflow_url; + const workflowResponse = await ok.request('GET ' + workflowUrl); + return workflowResponse.data.path; +} +/** + * Get the analysis key paramter for the current job. + * + * This will combine the workflow path and current job name. + * Computing this the first time requires making requests to + * the github API, but after that the result will be cached. + */ +async function getAnalysisKey() { + let analysisKey = process.env[sharedEnv.CODEQL_ACTION_ANALYSIS_KEY]; + if (analysisKey !== undefined) { + return analysisKey; + } + const workflowPath = await getWorkflowPath(); + const jobName = getRequiredEnvParam('GITHUB_JOB'); + analysisKey = workflowPath + ':' + jobName; + core.exportVariable(sharedEnv.CODEQL_ACTION_ANALYSIS_KEY, analysisKey); + return analysisKey; +} +exports.getAnalysisKey = getAnalysisKey; /** * Compose a StatusReport. * diff --git a/src/shared-environment.ts b/src/shared-environment.ts index c9c16e20ed..fbc94edb9f 100644 --- a/src/shared-environment.ts +++ b/src/shared-environment.ts @@ -1,6 +1,7 @@ export const CODEQL_ACTION_CMD = 'CODEQL_ACTION_CMD'; export const CODEQL_ACTION_DATABASE_DIR = 'CODEQL_ACTION_DATABASE_DIR'; export const CODEQL_ACTION_LANGUAGES = 'CODEQL_ACTION_LANGUAGES'; +export const CODEQL_ACTION_ANALYSIS_KEY = 'CODEQL_ACTION_ANALYSIS_KEY'; export const ODASA_TRACER_CONFIGURATION = 'ODASA_TRACER_CONFIGURATION'; export const CODEQL_ACTION_SCANNED_LANGUAGES = 'CODEQL_ACTION_SCANNED_LANGUAGES'; export const CODEQL_ACTION_TRACED_LANGUAGES = 'CODEQL_ACTION_TRACED_LANGUAGES'; diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 7eb44a635f..f1d9fba244 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -140,6 +140,7 @@ async function uploadFiles(sarifFiles: string[]): Promise { const commitOid = util.getRequiredEnvParam('GITHUB_SHA'); const workflowRunIDStr = util.getRequiredEnvParam('GITHUB_RUN_ID'); const ref = util.getRequiredEnvParam('GITHUB_REF'); // it's in the form "refs/heads/master" + const analysisKey = await util.getAnalysisKey(); const analysisName = util.getRequiredEnvParam('GITHUB_WORKFLOW'); const startedAt = process.env[sharedEnv.CODEQL_ACTION_STARTED_AT]; @@ -167,6 +168,7 @@ async function uploadFiles(sarifFiles: string[]): Promise { const payload = JSON.stringify({ "commit_oid": commitOid, "ref": ref, + "analysis_key": analysisKey, "analysis_name": analysisName, "sarif": zipped_sarif, "workflow_run_id": workflowRunID, diff --git a/src/util.ts b/src/util.ts index d17571d5d5..6c23d4fdf9 100644 --- a/src/util.ts +++ b/src/util.ts @@ -152,6 +152,54 @@ export async function getLanguages(): Promise { return languages; } +/** + * Get the path of the currently executing workflow. + */ +async function getWorkflowPath(): Promise { + const repo_nwo = getRequiredEnvParam('GITHUB_REPOSITORY').split("/"); + const owner = repo_nwo[0]; + const repo = repo_nwo[1]; + const run_id = getRequiredEnvParam('GITHUB_RUN_ID'); + + const ok = new octokit.Octokit({ + auth: core.getInput('token'), + userAgent: "CodeQL Action", + log: consoleLogLevel({ level: 'debug' }) + }); + + const runsResponse = await ok.request('GET /repos/:owner/:repo/actions/runs/:run_id', { + owner, + repo, + run_id + }); + const workflowUrl = runsResponse.data.workflow_url; + + const workflowResponse = await ok.request('GET ' + workflowUrl); + + return workflowResponse.data.path; +} + +/** + * Get the analysis key paramter for the current job. + * + * This will combine the workflow path and current job name. + * Computing this the first time requires making requests to + * the github API, but after that the result will be cached. + */ +export async function getAnalysisKey(): Promise { + let analysisKey = process.env[sharedEnv.CODEQL_ACTION_ANALYSIS_KEY]; + if (analysisKey !== undefined) { + return analysisKey; + } + + const workflowPath = await getWorkflowPath(); + const jobName = getRequiredEnvParam('GITHUB_JOB'); + + analysisKey = workflowPath + ':' + jobName; + core.exportVariable(sharedEnv.CODEQL_ACTION_ANALYSIS_KEY, analysisKey); + return analysisKey; +} + interface StatusReport { "workflow_run_id": number; "workflow_name": string;