Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/entry-points.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/init-action-post.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
48 changes: 37 additions & 11 deletions src/init-action-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,6 +41,7 @@ import {
ActionName,
getJobStatusDisplayName,
JobStatus,
supportsCancelledJobStatus,
} from "./status-report";
import { checkDiskUsage, checkGitHubVersionInRange, wrapError } from "./util";

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -107,7 +111,7 @@ async function run(action: ActionState<["Base", "Logger", "Env"]>) {
config,
repositoryNwo,
features,
jobStatus,
actionsJobStatus,
action.env,
logger,
);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion src/status-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
Loading