mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 01:30:10 +08:00
As we move towards analysing the merge commit for pull requests by default, we should stop sending `/refs/pull/n/head` rather than `refs/pull/n/merge` _unless_ the checked-out SHA has actually changed. Here we assume that any change (compared to GITHUB_SHA) indicates that `git checkout HEAD^2` has been run earlier. This may sometimes be incorrect (e.g. `git checkout mybranch`), but in that case the ref would be wrong either way.
267 lines
10 KiB
JavaScript
Generated
267 lines
10 KiB
JavaScript
Generated
"use strict";
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
result["default"] = mod;
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const core = __importStar(require("@actions/core"));
|
|
const toolrunnner = __importStar(require("@actions/exec/lib/toolrunner"));
|
|
const api = __importStar(require("./api-client"));
|
|
const sharedEnv = __importStar(require("./shared-environment"));
|
|
const util_1 = require("./util");
|
|
/**
|
|
* Wrapper around core.getInput for inputs that always have a value.
|
|
* Also see getOptionalInput.
|
|
*
|
|
* This allows us to get stronger type checking of required/optional inputs
|
|
* and make behaviour more consistent between actions and the runner.
|
|
*/
|
|
function getRequiredInput(name) {
|
|
return core.getInput(name, { required: true });
|
|
}
|
|
exports.getRequiredInput = getRequiredInput;
|
|
/**
|
|
* Wrapper around core.getInput that converts empty inputs to undefined.
|
|
* Also see getRequiredInput.
|
|
*
|
|
* This allows us to get stronger type checking of required/optional inputs
|
|
* and make behaviour more consistent between actions and the runner.
|
|
*/
|
|
function getOptionalInput(name) {
|
|
const value = core.getInput(name);
|
|
return value.length > 0 ? value : undefined;
|
|
}
|
|
exports.getOptionalInput = getOptionalInput;
|
|
/**
|
|
* Get an environment parameter, but throw an error if it is not set.
|
|
*/
|
|
function getRequiredEnvParam(paramName) {
|
|
const value = process.env[paramName];
|
|
if (value === undefined || value.length === 0) {
|
|
throw new Error(`${paramName} environment variable must be set`);
|
|
}
|
|
core.debug(`${paramName}=${value}`);
|
|
return value;
|
|
}
|
|
exports.getRequiredEnvParam = getRequiredEnvParam;
|
|
/**
|
|
* Ensures all required environment variables are set in the context of a local run.
|
|
*/
|
|
function prepareLocalRunEnvironment() {
|
|
if (!util_1.isLocalRun()) {
|
|
return;
|
|
}
|
|
core.debug("Action is running locally.");
|
|
if (!process.env.GITHUB_JOB) {
|
|
core.exportVariable("GITHUB_JOB", "UNKNOWN-JOB");
|
|
}
|
|
}
|
|
exports.prepareLocalRunEnvironment = prepareLocalRunEnvironment;
|
|
/**
|
|
* Gets the SHA of the commit that is currently checked out.
|
|
*/
|
|
exports.getCommitOid = async function () {
|
|
// Try to use git to get the current commit SHA. If that fails then
|
|
// log but otherwise silently fall back to using the SHA from the environment.
|
|
// The only time these two values will differ is during analysis of a PR when
|
|
// the workflow has changed the current commit to the head commit instead of
|
|
// the merge commit, which must mean that git is available.
|
|
// Even if this does go wrong, it's not a huge problem for the alerts to
|
|
// reported on the merge commit.
|
|
try {
|
|
let commitOid = "";
|
|
await new toolrunnner.ToolRunner("git", ["rev-parse", "HEAD"], {
|
|
silent: true,
|
|
listeners: {
|
|
stdout: (data) => {
|
|
commitOid += data.toString();
|
|
},
|
|
stderr: (data) => {
|
|
process.stderr.write(data);
|
|
},
|
|
},
|
|
}).exec();
|
|
return commitOid.trim();
|
|
}
|
|
catch (e) {
|
|
core.info(`Failed to call git to get current commit. Continuing with data from environment: ${e}`);
|
|
return getRequiredEnvParam("GITHUB_SHA");
|
|
}
|
|
};
|
|
/**
|
|
* 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 = Number(getRequiredEnvParam("GITHUB_RUN_ID"));
|
|
const apiClient = api.getActionsApiClient();
|
|
const runsResponse = await apiClient.request("GET /repos/:owner/:repo/actions/runs/:run_id", {
|
|
owner,
|
|
repo,
|
|
run_id,
|
|
});
|
|
const workflowUrl = runsResponse.data.workflow_url;
|
|
const workflowResponse = await apiClient.request(`GET ${workflowUrl}`);
|
|
return workflowResponse.data.path;
|
|
}
|
|
/**
|
|
* Get the workflow run ID.
|
|
*/
|
|
function getWorkflowRunID() {
|
|
const workflowRunID = parseInt(getRequiredEnvParam("GITHUB_RUN_ID"), 10);
|
|
if (Number.isNaN(workflowRunID)) {
|
|
throw new Error("GITHUB_RUN_ID must define a non NaN workflow run ID");
|
|
}
|
|
return workflowRunID;
|
|
}
|
|
exports.getWorkflowRunID = getWorkflowRunID;
|
|
/**
|
|
* 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() {
|
|
const analysisKeyEnvVar = "CODEQL_ACTION_ANALYSIS_KEY";
|
|
let analysisKey = process.env[analysisKeyEnvVar];
|
|
if (analysisKey !== undefined) {
|
|
return analysisKey;
|
|
}
|
|
const workflowPath = await getWorkflowPath();
|
|
const jobName = getRequiredEnvParam("GITHUB_JOB");
|
|
analysisKey = `${workflowPath}:${jobName}`;
|
|
core.exportVariable(analysisKeyEnvVar, analysisKey);
|
|
return analysisKey;
|
|
}
|
|
exports.getAnalysisKey = getAnalysisKey;
|
|
/**
|
|
* Get the ref currently being analyzed.
|
|
*/
|
|
async function getRef() {
|
|
// Will be in the form "refs/heads/master" on a push event
|
|
// or in the form "refs/pull/N/merge" on a pull_request event
|
|
const ref = getRequiredEnvParam("GITHUB_REF");
|
|
// For pull request refs we want to detect whether the workflow
|
|
// has run `git checkout HEAD^2` to analyze the 'head' ref rather
|
|
// than the 'merge' ref. If so, we want to convert the ref that
|
|
// we report back.
|
|
const pull_ref_regex = /refs\/pull\/(\d+)\/merge/;
|
|
const checkoutSha = await exports.getCommitOid();
|
|
if (pull_ref_regex.test(ref) &&
|
|
checkoutSha !== getRequiredEnvParam("GITHUB_SHA")) {
|
|
return ref.replace(pull_ref_regex, "refs/pull/$1/head");
|
|
}
|
|
else {
|
|
return ref;
|
|
}
|
|
}
|
|
exports.getRef = getRef;
|
|
/**
|
|
* Compose a StatusReport.
|
|
*
|
|
* @param actionName The name of the action, e.g. 'init', 'finish', 'upload-sarif'
|
|
* @param status The status. Must be 'success', 'failure', or 'starting'
|
|
* @param startedAt The time this action started executing.
|
|
* @param cause Cause of failure (only supply if status is 'failure')
|
|
* @param exception Exception (only supply if status is 'failure')
|
|
*/
|
|
async function createStatusReportBase(actionName, status, actionStartedAt, cause, exception) {
|
|
const commitOid = process.env["GITHUB_SHA"] || "";
|
|
const ref = await getRef();
|
|
const workflowRunIDStr = process.env["GITHUB_RUN_ID"];
|
|
let workflowRunID = -1;
|
|
if (workflowRunIDStr) {
|
|
workflowRunID = parseInt(workflowRunIDStr, 10);
|
|
}
|
|
const workflowName = process.env["GITHUB_WORKFLOW"] || "";
|
|
const jobName = process.env["GITHUB_JOB"] || "";
|
|
const analysis_key = await getAnalysisKey();
|
|
let workflowStartedAt = process.env[sharedEnv.CODEQL_WORKFLOW_STARTED_AT];
|
|
if (workflowStartedAt === undefined) {
|
|
workflowStartedAt = actionStartedAt.toISOString();
|
|
core.exportVariable(sharedEnv.CODEQL_WORKFLOW_STARTED_AT, workflowStartedAt);
|
|
}
|
|
const statusReport = {
|
|
workflow_run_id: workflowRunID,
|
|
workflow_name: workflowName,
|
|
job_name: jobName,
|
|
analysis_key,
|
|
commit_oid: commitOid,
|
|
ref,
|
|
action_name: actionName,
|
|
action_oid: "unknown",
|
|
started_at: workflowStartedAt,
|
|
action_started_at: actionStartedAt.toISOString(),
|
|
status,
|
|
};
|
|
// Add optional parameters
|
|
if (cause) {
|
|
statusReport.cause = cause;
|
|
}
|
|
if (exception) {
|
|
statusReport.exception = exception;
|
|
}
|
|
if (status === "success" || status === "failure" || status === "aborted") {
|
|
statusReport.completed_at = new Date().toISOString();
|
|
}
|
|
const matrix = getRequiredInput("matrix");
|
|
if (matrix) {
|
|
statusReport.matrix_vars = matrix;
|
|
}
|
|
return statusReport;
|
|
}
|
|
exports.createStatusReportBase = createStatusReportBase;
|
|
/**
|
|
* Send a status report to the code_scanning/analysis/status endpoint.
|
|
*
|
|
* Optionally checks the response from the API endpoint and sets the action
|
|
* as failed if the status report failed. This is only expected to be used
|
|
* when sending a 'starting' report.
|
|
*
|
|
* Returns whether sending the status report was successful of not.
|
|
*/
|
|
async function sendStatusReport(statusReport, ignoreFailures) {
|
|
if (getRequiredEnvParam("GITHUB_SERVER_URL") !== util_1.GITHUB_DOTCOM_URL) {
|
|
core.debug("Not sending status report to GitHub Enterprise");
|
|
return true;
|
|
}
|
|
if (util_1.isLocalRun()) {
|
|
core.debug("Not sending status report because this is a local run");
|
|
return true;
|
|
}
|
|
const statusReportJSON = JSON.stringify(statusReport);
|
|
core.debug(`Sending status report: ${statusReportJSON}`);
|
|
const nwo = getRequiredEnvParam("GITHUB_REPOSITORY");
|
|
const [owner, repo] = nwo.split("/");
|
|
const client = api.getActionsApiClient();
|
|
const statusResponse = await client.request("PUT /repos/:owner/:repo/code-scanning/analysis/status", {
|
|
owner,
|
|
repo,
|
|
data: statusReportJSON,
|
|
});
|
|
if (!ignoreFailures) {
|
|
// If the status report request fails with a 403 or a 404, then this is a deliberate
|
|
// message from the endpoint that the SARIF upload can be expected to fail too,
|
|
// so the action should fail to avoid wasting actions minutes.
|
|
//
|
|
// Other failure responses (or lack thereof) could be transitory and should not
|
|
// cause the action to fail.
|
|
if (statusResponse.status === 403) {
|
|
core.setFailed("The repo on which this action is running is not opted-in to CodeQL code scanning.");
|
|
return false;
|
|
}
|
|
if (statusResponse.status === 404) {
|
|
core.setFailed("Not authorized to used the CodeQL code scanning feature on this repo.");
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
exports.sendStatusReport = sendStatusReport;
|
|
//# sourceMappingURL=actions-util.js.map
|