@@ -30,6 +30,7 @@ import {
3030import { EnvVar , type ReadOnlyEnv } from "./environment" ;
3131import { initFeatures } from "./feature-flags" ;
3232import * as gitUtils from "./git-utils" ;
33+ import type { GitHubVersion } from "./github" ;
3334import * as initActionPostHelper from "./init-action-post-helper" ;
3435import { getRepositoryNwo } from "./repository" ;
3536import {
@@ -40,6 +41,7 @@ import {
4041 ActionName ,
4142 getJobStatusDisplayName ,
4243 JobStatus ,
44+ supportsCancelledJobStatus ,
4345} from "./status-report" ;
4446import { 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.
0 commit comments