Skip to content

Commit b69d569

Browse files
henrymercerCopilot
andcommitted
Report cancelled jobs with a separate job status in init-post
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 3811262 commit b69d569

4 files changed

Lines changed: 144 additions & 17 deletions

File tree

‎lib/entry-points.js‎

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

‎src/init-action-post.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import test from "ava";
2+
3+
import { EnvVar } from "./environment";
4+
import { GitHubVariant, type GitHubVersion } from "./github";
5+
import { getFinalJobStatus } from "./init-action-post";
6+
import { JobStatus } from "./status-report";
7+
import { callee, createTestConfig, setupTests } from "./testing-utils";
8+
9+
setupTests(test);
10+
11+
const dotcom: GitHubVersion = { type: GitHubVariant.DOTCOM };
12+
13+
test("getFinalJobStatus reports a cancelled job status when the job was cancelled after init completed", async (t) => {
14+
await callee(getFinalJobStatus)
15+
.withArgs(createTestConfig({}), "cancelled", dotcom)
16+
.passes(t.is, JobStatus.CancelledStatus);
17+
});
18+
19+
test("getFinalJobStatus reports a cancelled job status when the job was cancelled before init completed", async (t) => {
20+
await callee(getFinalJobStatus)
21+
.withArgs(undefined, "cancelled", dotcom)
22+
.passes(t.is, JobStatus.CancelledStatus);
23+
});
24+
25+
test("getFinalJobStatus reports a cancelled job status on GitHub Enterprise Cloud with data residency", async (t) => {
26+
await callee(getFinalJobStatus)
27+
.withArgs(createTestConfig({}), "cancelled", {
28+
type: GitHubVariant.GHEC_DR,
29+
})
30+
.passes(t.is, JobStatus.CancelledStatus);
31+
});
32+
33+
test("getFinalJobStatus reports success when the job was cancelled after analyze completed", async (t) => {
34+
await callee(getFinalJobStatus)
35+
.withEnv((env) => {
36+
env.set(EnvVar.ANALYZE_DID_COMPLETE_SUCCESSFULLY, "true");
37+
})
38+
.withArgs(createTestConfig({}), "cancelled", dotcom)
39+
.passes(t.is, JobStatus.SuccessStatus);
40+
});
41+
42+
test("getFinalJobStatus reports the error from a CodeQL Action step when the job was cancelled afterwards", async (t) => {
43+
await callee(getFinalJobStatus)
44+
.withEnv((env) => {
45+
env.set(EnvVar.JOB_STATUS, JobStatus.FailureStatus);
46+
})
47+
.withArgs(createTestConfig({}), "cancelled", dotcom)
48+
.passes(t.is, JobStatus.FailureStatus);
49+
});
50+
51+
test("getFinalJobStatus reports a configuration error when a step outside the CodeQL Action failed", async (t) => {
52+
await callee(getFinalJobStatus)
53+
.withArgs(createTestConfig({}), "failure", dotcom)
54+
.passes(t.is, JobStatus.ConfigErrorStatus);
55+
});
56+
57+
test("getFinalJobStatus doesn't report a cancelled job status on GitHub Enterprise Server", async (t) => {
58+
const ghes: GitHubVersion = {
59+
type: GitHubVariant.GHES,
60+
version: "3.23.0",
61+
};
62+
await callee(getFinalJobStatus)
63+
.withArgs(createTestConfig({}), "cancelled", ghes)
64+
.passes(t.is, JobStatus.ConfigErrorStatus);
65+
await callee(getFinalJobStatus)
66+
.withArgs(undefined, "cancelled", ghes)
67+
.passes(t.is, JobStatus.UnknownStatus);
68+
});

‎src/init-action-post.ts‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import { EnvVar, type ReadOnlyEnv } from "./environment";
3131
import { initFeatures } from "./feature-flags";
3232
import * as gitUtils from "./git-utils";
33+
import type { GitHubVersion } from "./github";
3334
import * as initActionPostHelper from "./init-action-post-helper";
3435
import { getRepositoryNwo } from "./repository";
3536
import {
@@ -40,6 +41,7 @@ import {
4041
ActionName,
4142
getJobStatusDisplayName,
4243
JobStatus,
44+
supportsCancelledJobStatus,
4345
} from "./status-report";
4446
import { checkDiskUsage, checkGitHubVersionInRange, wrapError } from "./util";
4547

@@ -55,7 +57,9 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
5557
// possible, and only use safe functions outside.
5658
const logger = action.logger;
5759

60+
let actionsJobStatus: string | undefined;
5861
let config: Config | AbortedInitConfig | undefined;
62+
let gitHubVersion: GitHubVersion;
5963
let uploadFailedSarifResult:
6064
initActionPostHelper.UploadFailedSarifResult | undefined;
6165
let dependencyCachingUsage: DependencyCachingUsageReport | undefined;
@@ -64,7 +68,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
6468
// Read the job status before restoring inputs, since it is provided by the Actions runtime
6569
// environment for this step and would otherwise be overwritten by the value that the `init`
6670
// Action saw, which is always a success.
67-
const jobStatus = getOptionalInput("job-status");
71+
actionsJobStatus = getOptionalInput("job-status");
6872

6973
// Restore inputs from `init` Action.
7074
restoreInputs(logger);
@@ -82,7 +86,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
8286
config = getAbortedInitConfig(action, tmpDir);
8387
}
8488

85-
const gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion());
89+
gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion());
8690
checkGitHubVersionInRange(gitHubVersion, logger);
8791

8892
const repositoryNwo = getRepositoryNwo();
@@ -107,7 +111,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
107111
config,
108112
repositoryNwo,
109113
features,
110-
jobStatus,
114+
actionsJobStatus,
111115
action.env,
112116
logger,
113117
);
@@ -146,7 +150,12 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
146150
}
147151
return;
148152
}
149-
const jobStatus = getFinalJobStatus(action, config);
153+
const jobStatus = getFinalJobStatus(
154+
action,
155+
config,
156+
actionsJobStatus,
157+
gitHubVersion,
158+
);
150159
logger.info(`CodeQL job status was ${getJobStatusDisplayName(jobStatus)}.`);
151160

152161
const statusReportBase = await createStatusReportBase(
@@ -175,11 +184,21 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
175184
*
176185
* If the job status has already been set by another step, we use that.
177186
* Otherwise, we determine the job status based on whether the analyze step
178-
* completed successfully and whether we have a valid CodeQL config.
187+
* completed successfully, whether the job was cancelled, and whether we have a
188+
* valid CodeQL config.
189+
*
190+
* Exported for testing.
191+
*
192+
* @param actionsJobStatus The status of the job, as reported by the Actions
193+
* runtime environment.
194+
* @param gitHubVersion The version of the GitHub instance that the status
195+
* report is sent to.
179196
*/
180-
function getFinalJobStatus(
197+
export function getFinalJobStatus(
181198
action: ActionState<["Env"]>,
182199
config: Partial<Config> | undefined,
200+
actionsJobStatus: string | undefined,
201+
gitHubVersion: GitHubVersion,
183202
): JobStatus {
184203
const existingJobStatus = getJobStatusFromEnvironment(action.env);
185204
if (existingJobStatus !== undefined) {
@@ -193,21 +212,28 @@ function getFinalJobStatus(
193212
) {
194213
action.env.export(EnvVar.JOB_STATUS, JobStatus.SuccessStatus);
195214
jobStatus = JobStatus.SuccessStatus;
215+
} else if (
216+
actionsJobStatus === "cancelled" &&
217+
supportsCancelledJobStatus(gitHubVersion)
218+
) {
219+
// The job was cancelled before the analysis completed, and no CodeQL
220+
// Action step reported an error.
221+
jobStatus = JobStatus.CancelledStatus;
196222
} else if (config !== undefined) {
197223
// - We have computed a CodeQL config
198224
// - Analyze didn't complete successfully
199225
// - The job status hasn't already been set to Failure/ConfigurationError
200226
//
201227
// This means that something along the way failed in a step that is not
202-
// owned by the Action, for example a manual build step. We consider this a
203-
// configuration error.
228+
// owned by the Action, for example a manual build step, or that the job
229+
// was cancelled and the GitHub instance isn't known to accept
230+
// `JobStatus.CancelledStatus`. We consider this a configuration error.
204231
jobStatus = JobStatus.ConfigErrorStatus;
205232
} else {
206233
// If we didn't manage to compute a CodeQL config, it is unclear at this
207234
// point why the analyze Action didn't complete.
208-
// - One possibility is that the workflow run was cancelled. We could
209-
// consider determining workflow cancellation using the GitHub API, but
210-
// for now we treat all these cases as unknown.
235+
// - One possibility is that the job was cancelled and the GitHub instance
236+
// isn't known to accept `JobStatus.CancelledStatus`.
211237
// - Another possibility is that we're running a workflow that only runs
212238
// `init`, for instance a workflow that was created before `setup-codeql`
213239
// was available and uses `init` just to set up the CodeQL tools.

‎src/status-report.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
exportEnvVar,
3232
} from "./environment";
3333
import { getRef } from "./git-utils";
34-
import { GITHUB_DOTCOM_URL } from "./github";
34+
import { GITHUB_DOTCOM_URL, GitHubVariant, type GitHubVersion } from "./github";
3535
import * as json from "./json";
3636
import type { Logger } from "./logging";
3737
import type { OverlayBaseDatabaseDownloadStats } from "./overlay/caching";
@@ -137,6 +137,22 @@ export enum JobStatus {
137137
SuccessStatus = "JOB_STATUS_SUCCESS",
138138
FailureStatus = "JOB_STATUS_FAILURE",
139139
ConfigErrorStatus = "JOB_STATUS_CONFIGURATION_ERROR",
140+
/** The job was cancelled before the analysis completed. */
141+
CancelledStatus = "JOB_STATUS_CANCELLED",
142+
}
143+
144+
/**
145+
* Whether the status report API of the GitHub instance is known to accept
146+
* `JobStatus.CancelledStatus`. The API rejects status reports with a job status
147+
* that it doesn't recognise.
148+
*/
149+
export function supportsCancelledJobStatus(
150+
gitHubVersion: GitHubVersion,
151+
): boolean {
152+
return (
153+
gitHubVersion.type === GitHubVariant.DOTCOM ||
154+
gitHubVersion.type === GitHubVariant.GHEC_DR
155+
);
140156
}
141157

142158
export interface StatusReportBase {
@@ -268,6 +284,8 @@ export function getJobStatusDisplayName(status: JobStatus): string {
268284
return "configuration error";
269285
case JobStatus.UnknownStatus:
270286
return "unknown";
287+
case JobStatus.CancelledStatus:
288+
return "cancelled";
271289
default:
272290
assertNever(status);
273291
}

0 commit comments

Comments
 (0)