mirror of
https://github.com/github/codeql-action
synced 2026-12-01 13:49:37 +08:00
Merge pull request #4345 from github/henrymercer/cancelled-runs-reporting
Report cancelled jobs with a separate job status in `init-post`
This commit is contained in:
4 files changed
+144
-17
No files matched your search
Generated
+20
-5
@@ -181009,8 +181009,12 @@ var JobStatus = /* @__PURE__ */ ((JobStatus2) => {
|
||||
JobStatus2["SuccessStatus"] = "JOB_STATUS_SUCCESS";
|
||||
JobStatus2["FailureStatus"] = "JOB_STATUS_FAILURE";
|
||||
JobStatus2["ConfigErrorStatus"] = "JOB_STATUS_CONFIGURATION_ERROR";
|
||||
JobStatus2["CancelledStatus"] = "JOB_STATUS_CANCELLED";
|
||||
return JobStatus2;
|
||||
})(JobStatus || {});
|
||||
function supportsCancelledJobStatus(gitHubVersion) {
|
||||
return gitHubVersion.type === "GitHub.com" /* DOTCOM */ || gitHubVersion.type === "GitHub Enterprise Cloud with data residency" /* GHEC_DR */;
|
||||
}
|
||||
function getActionsStatus(error3, otherFailureCause) {
|
||||
if (error3 || otherFailureCause) {
|
||||
return error3 instanceof ConfigurationError ? "user-error" : "failure";
|
||||
@@ -181028,6 +181032,8 @@ function getJobStatusDisplayName(status) {
|
||||
return "configuration error";
|
||||
case "JOB_STATUS_UNKNOWN" /* UnknownStatus */:
|
||||
return "unknown";
|
||||
case "JOB_STATUS_CANCELLED" /* CancelledStatus */:
|
||||
return "cancelled";
|
||||
default:
|
||||
assertNever(status);
|
||||
}
|
||||
@@ -197838,11 +197844,13 @@ async function removeUploadedSarif(uploadFailedSarifResult, logger2) {
|
||||
// src/init-action-post.ts
|
||||
async function run4(action) {
|
||||
const logger2 = action.logger;
|
||||
let actionsJobStatus;
|
||||
let config;
|
||||
let gitHubVersion;
|
||||
let uploadFailedSarifResult;
|
||||
let dependencyCachingUsage;
|
||||
try {
|
||||
const jobStatus2 = getOptionalInput("job-status");
|
||||
actionsJobStatus = getOptionalInput("job-status");
|
||||
restoreInputs(logger2);
|
||||
const tmpDir = getTemporaryDirectory();
|
||||
config = await getConfig(tmpDir, logger2);
|
||||
@@ -197852,7 +197860,7 @@ async function run4(action) {
|
||||
);
|
||||
config = getAbortedInitConfig(action, tmpDir);
|
||||
}
|
||||
const gitHubVersion = config?.gitHubVersion ?? await getGitHubVersion();
|
||||
gitHubVersion = config?.gitHubVersion ?? await getGitHubVersion();
|
||||
checkGitHubVersionInRange(gitHubVersion, logger2);
|
||||
const repositoryNwo = getRepositoryNwo();
|
||||
const features = initFeatures(
|
||||
@@ -197874,7 +197882,7 @@ async function run4(action) {
|
||||
config,
|
||||
repositoryNwo,
|
||||
features,
|
||||
jobStatus2,
|
||||
actionsJobStatus,
|
||||
action.env,
|
||||
logger2
|
||||
);
|
||||
@@ -197903,7 +197911,12 @@ async function run4(action) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
const jobStatus = getFinalJobStatus(action, config);
|
||||
const jobStatus = getFinalJobStatus(
|
||||
action,
|
||||
config,
|
||||
actionsJobStatus,
|
||||
gitHubVersion
|
||||
);
|
||||
logger2.info(`CodeQL job status was ${getJobStatusDisplayName(jobStatus)}.`);
|
||||
const statusReportBase = await createStatusReportBase(
|
||||
action.name,
|
||||
@@ -197925,7 +197938,7 @@ async function run4(action) {
|
||||
logger2.info("Status report sent for init-post step.");
|
||||
}
|
||||
}
|
||||
function getFinalJobStatus(action, config) {
|
||||
function getFinalJobStatus(action, config, actionsJobStatus, gitHubVersion) {
|
||||
const existingJobStatus = getJobStatusFromEnvironment(action.env);
|
||||
if (existingJobStatus !== void 0) {
|
||||
return existingJobStatus;
|
||||
@@ -197934,6 +197947,8 @@ function getFinalJobStatus(action, config) {
|
||||
if (action.env.getOptional("CODEQL_ACTION_ANALYZE_DID_COMPLETE_SUCCESSFULLY" /* ANALYZE_DID_COMPLETE_SUCCESSFULLY */) === "true") {
|
||||
action.env.export("CODEQL_ACTION_JOB_STATUS" /* JOB_STATUS */, "JOB_STATUS_SUCCESS" /* SuccessStatus */);
|
||||
jobStatus = "JOB_STATUS_SUCCESS" /* SuccessStatus */;
|
||||
} else if (actionsJobStatus === "cancelled" && supportsCancelledJobStatus(gitHubVersion)) {
|
||||
jobStatus = "JOB_STATUS_CANCELLED" /* CancelledStatus */;
|
||||
} else if (config !== void 0) {
|
||||
jobStatus = "JOB_STATUS_CONFIGURATION_ERROR" /* ConfigErrorStatus */;
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import test from "ava";
|
||||
|
||||
import { EnvVar } from "./environment";
|
||||
import { GitHubVariant, type GitHubVersion } from "./github";
|
||||
import { getFinalJobStatus } from "./init-action-post";
|
||||
import { JobStatus } from "./status-report";
|
||||
import { callee, createTestConfig, setupTests } from "./testing-utils";
|
||||
|
||||
setupTests(test);
|
||||
|
||||
const dotcom: GitHubVersion = { type: GitHubVariant.DOTCOM };
|
||||
|
||||
test("getFinalJobStatus reports a cancelled job status when the job was cancelled after init completed", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(createTestConfig({}), "cancelled", dotcom)
|
||||
.passes(t.is, JobStatus.CancelledStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus reports a cancelled job status when the job was cancelled before init completed", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(undefined, "cancelled", dotcom)
|
||||
.passes(t.is, JobStatus.CancelledStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus reports a cancelled job status on GitHub Enterprise Cloud with data residency", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(createTestConfig({}), "cancelled", {
|
||||
type: GitHubVariant.GHEC_DR,
|
||||
})
|
||||
.passes(t.is, JobStatus.CancelledStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus reports success when the job was cancelled after analyze completed", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withEnv((env) => {
|
||||
env.set(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
|
||||
})
|
||||
.withArgs(createTestConfig({}), "cancelled", dotcom)
|
||||
.passes(t.is, JobStatus.SuccessStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus reports the error from a CodeQL Action step when the job was cancelled afterwards", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withEnv((env) => {
|
||||
env.set(EnvVar.JOB_STATUS, JobStatus.FailureStatus);
|
||||
})
|
||||
.withArgs(createTestConfig({}), "cancelled", dotcom)
|
||||
.passes(t.is, JobStatus.FailureStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus reports a configuration error when a step outside the CodeQL Action failed", async (t) => {
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(createTestConfig({}), "failure", dotcom)
|
||||
.passes(t.is, JobStatus.ConfigErrorStatus);
|
||||
});
|
||||
|
||||
test("getFinalJobStatus doesn't report a cancelled job status on GitHub Enterprise Server", async (t) => {
|
||||
const ghes: GitHubVersion = {
|
||||
type: GitHubVariant.GHES,
|
||||
version: "3.23.0",
|
||||
};
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(createTestConfig({}), "cancelled", ghes)
|
||||
.passes(t.is, JobStatus.ConfigErrorStatus);
|
||||
await callee(getFinalJobStatus)
|
||||
.withArgs(undefined, "cancelled", ghes)
|
||||
.passes(t.is, JobStatus.UnknownStatus);
|
||||
});
|
||||
+37
-11
@@ -30,6 +30,7 @@ import {
|
||||
import { EnvVar, type ReadOnlyEnv } from "./environment";
|
||||
import { initFeatures } from "./feature-flags";
|
||||
import * as gitUtils from "./git-utils";
|
||||
import type { GitHubVersion } from "./github";
|
||||
import * as initActionPostHelper from "./init-action-post-helper";
|
||||
import { getRepositoryNwo } from "./repository";
|
||||
import {
|
||||
@@ -40,6 +41,7 @@ import {
|
||||
ActionName,
|
||||
getJobStatusDisplayName,
|
||||
JobStatus,
|
||||
supportsCancelledJobStatus,
|
||||
} from "./status-report";
|
||||
import { checkDiskUsage, checkGitHubVersionInRange, wrapError } from "./util";
|
||||
|
||||
@@ -55,7 +57,9 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
// possible, and only use safe functions outside.
|
||||
const logger = action.logger;
|
||||
|
||||
let actionsJobStatus: string | undefined;
|
||||
let config: Config | AbortedInitConfig | undefined;
|
||||
let gitHubVersion: GitHubVersion;
|
||||
let uploadFailedSarifResult:
|
||||
initActionPostHelper.UploadFailedSarifResult | undefined;
|
||||
let dependencyCachingUsage: DependencyCachingUsageReport | undefined;
|
||||
@@ -64,7 +68,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
// Read the job status before restoring inputs, since it is provided by the Actions runtime
|
||||
// environment for this step and would otherwise be overwritten by the value that the `init`
|
||||
// Action saw, which is always a success.
|
||||
const jobStatus = getOptionalInput("job-status");
|
||||
actionsJobStatus = getOptionalInput("job-status");
|
||||
|
||||
// Restore inputs from `init` Action.
|
||||
restoreInputs(logger);
|
||||
@@ -82,7 +86,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
config = getAbortedInitConfig(action, tmpDir);
|
||||
}
|
||||
|
||||
const gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion());
|
||||
gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion());
|
||||
checkGitHubVersionInRange(gitHubVersion, logger);
|
||||
|
||||
const repositoryNwo = getRepositoryNwo();
|
||||
@@ -107,7 +111,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
config,
|
||||
repositoryNwo,
|
||||
features,
|
||||
jobStatus,
|
||||
actionsJobStatus,
|
||||
action.env,
|
||||
logger,
|
||||
);
|
||||
@@ -146,7 +150,12 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
const jobStatus = getFinalJobStatus(action, config);
|
||||
const jobStatus = getFinalJobStatus(
|
||||
action,
|
||||
config,
|
||||
actionsJobStatus,
|
||||
gitHubVersion,
|
||||
);
|
||||
logger.info(`CodeQL job status was ${getJobStatusDisplayName(jobStatus)}.`);
|
||||
|
||||
const statusReportBase = await createStatusReportBase(
|
||||
@@ -175,11 +184,21 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
|
||||
*
|
||||
* If the job status has already been set by another step, we use that.
|
||||
* Otherwise, we determine the job status based on whether the analyze step
|
||||
* completed successfully and whether we have a valid CodeQL config.
|
||||
* completed successfully, whether the job was cancelled, and whether we have a
|
||||
* valid CodeQL config.
|
||||
*
|
||||
* Exported for testing.
|
||||
*
|
||||
* @param actionsJobStatus The status of the job, as reported by the Actions
|
||||
* runtime environment.
|
||||
* @param gitHubVersion The version of the GitHub instance that the status
|
||||
* report is sent to.
|
||||
*/
|
||||
function getFinalJobStatus(
|
||||
export function getFinalJobStatus(
|
||||
action: ActionState<["Env"]>,
|
||||
config: Partial<Config> | undefined,
|
||||
actionsJobStatus: string | undefined,
|
||||
gitHubVersion: GitHubVersion,
|
||||
): JobStatus {
|
||||
const existingJobStatus = getJobStatusFromEnvironment(action.env);
|
||||
if (existingJobStatus !== undefined) {
|
||||
@@ -193,21 +212,28 @@ function getFinalJobStatus(
|
||||
) {
|
||||
action.env.export(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
|
||||
jobStatus = JobStatus.SuccessStatus;
|
||||
} else if (
|
||||
actionsJobStatus === "cancelled" &&
|
||||
supportsCancelledJobStatus(gitHubVersion)
|
||||
) {
|
||||
// The job was cancelled before the analysis completed, and no CodeQL
|
||||
// Action step reported an error.
|
||||
jobStatus = JobStatus.CancelledStatus;
|
||||
} else if (config !== undefined) {
|
||||
// - We have computed a CodeQL config
|
||||
// - Analyze didn't complete successfully
|
||||
// - The job status hasn't already been set to Failure/ConfigurationError
|
||||
//
|
||||
// This means that something along the way failed in a step that is not
|
||||
// owned by the Action, for example a manual build step. We consider this a
|
||||
// configuration error.
|
||||
// owned by the Action, for example a manual build step, or that the job
|
||||
// was cancelled and the GitHub instance isn't known to accept
|
||||
// `JobStatus.CancelledStatus`. We consider this a configuration error.
|
||||
jobStatus = JobStatus.ConfigErrorStatus;
|
||||
} else {
|
||||
// If we didn't manage to compute a CodeQL config, it is unclear at this
|
||||
// point why the analyze Action didn't complete.
|
||||
// - One possibility is that the workflow run was cancelled. We could
|
||||
// consider determining workflow cancellation using the GitHub API, but
|
||||
// for now we treat all these cases as unknown.
|
||||
// - One possibility is that the job was cancelled and the GitHub instance
|
||||
// isn't known to accept `JobStatus.CancelledStatus`.
|
||||
// - Another possibility is that we're running a workflow that only runs
|
||||
// `init`, for instance a workflow that was created before `setup-codeql`
|
||||
// was available and uses `init` just to set up the CodeQL tools.
|
||||
|
||||
+19
-1
@@ -31,7 +31,7 @@ import {
|
||||
exportEnvVar,
|
||||
} from "./environment";
|
||||
import { getRef } from "./git-utils";
|
||||
import { GITHUB_DOTCOM_URL } from "./github";
|
||||
import { GITHUB_DOTCOM_URL, GitHubVariant, type GitHubVersion } from "./github";
|
||||
import * as json from "./json";
|
||||
import type { Logger } from "./logging";
|
||||
import type { OverlayBaseDatabaseDownloadStats } from "./overlay/caching";
|
||||
@@ -137,6 +137,22 @@ export enum JobStatus {
|
||||
SuccessStatus = "JOB_STATUS_SUCCESS",
|
||||
FailureStatus = "JOB_STATUS_FAILURE",
|
||||
ConfigErrorStatus = "JOB_STATUS_CONFIGURATION_ERROR",
|
||||
/** The job was cancelled before the analysis completed. */
|
||||
CancelledStatus = "JOB_STATUS_CANCELLED",
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the status report API of the GitHub instance is known to accept
|
||||
* `JobStatus.CancelledStatus`. The API rejects status reports with a job status
|
||||
* that it doesn't recognise.
|
||||
*/
|
||||
export function supportsCancelledJobStatus(
|
||||
gitHubVersion: GitHubVersion,
|
||||
): boolean {
|
||||
return (
|
||||
gitHubVersion.type === GitHubVariant.DOTCOM ||
|
||||
gitHubVersion.type === GitHubVariant.GHEC_DR
|
||||
);
|
||||
}
|
||||
|
||||
export interface StatusReportBase {
|
||||
@@ -268,6 +284,8 @@ export function getJobStatusDisplayName(status: JobStatus): string {
|
||||
return "configuration error";
|
||||
case JobStatus.UnknownStatus:
|
||||
return "unknown";
|
||||
case JobStatus.CancelledStatus:
|
||||
return "cancelled";
|
||||
default:
|
||||
assertNever(status);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user