From 6e9aec682d5a2cb314aac734a431d598d774e438 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Thu, 15 Oct 2026 14:40:34 +0100 Subject: [PATCH] Add and use `shouldSkipGenerateFailedSarif` --- lib/entry-points.js | 8 ++++++- src/environment.ts | 7 ++++++ src/init-action-post-helper.ts | 4 ++-- src/util.test.ts | 39 ++++++++++++++++++++++++++++++++++ src/util.ts | 15 +++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index 72559eea9..6a1e50a4c 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -169889,6 +169889,12 @@ async function delay(milliseconds, opts) { function isGoodVersion(versionSpec) { return !BROKEN_VERSIONS.includes(versionSpec); } +function shouldSkipGenerateFailedSarif(env = getEnv()) { + const skipGenerateFailedSarif = env.getOptional( + "CODEQL_ACTION_SKIP_GENERATE_FAILED_SARIF" /* SKIP_GENERATE_FAILED_SARIF */ + ); + return skipGenerateFailedSarif !== "false" && (shouldSkipSarifUpload(env) || skipGenerateFailedSarif === "true"); +} function shouldSkipSarifUpload(env = getEnv()) { const skipSarifUpload = env.getOptional("CODEQL_ACTION_SKIP_SARIF_UPLOAD" /* SKIP_SARIF_UPLOAD */); return skipSarifUpload !== "false" && (env.isSkippingUploadsInTests() || skipSarifUpload === "true"); @@ -186904,7 +186910,7 @@ async function prepareFailedSarif(logger2, features, config) { } const jobName = getRequiredEnvParam("GITHUB_JOB"); const matrix = parseMatrixInput(getRequiredInput("matrix")); - if (shouldSkipSarifUpload()) { + if (shouldSkipGenerateFailedSarif()) { return new Failure({ upload_failed_run_skipped_because: "SARIF upload is disabled" }); diff --git a/src/environment.ts b/src/environment.ts index 9f6107025..32140baf0 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -165,6 +165,13 @@ export enum EnvVar { */ SARIF_DUMP_DIR = "CODEQL_ACTION_SARIF_DUMP_DIR", + /** + * Whether to skip generating a failed SARIF file. Intended for testing purposes. + * This setting is more specific than `CODEQL_ACTION_TEST_MODE` and + * `CODEQL_ACTION_SKIP_SARIF_UPLOAD`, which imply this option. + */ + SKIP_GENERATE_FAILED_SARIF = "CODEQL_ACTION_SKIP_GENERATE_FAILED_SARIF", + /** * Whether to skip uploading SARIF results to GitHub. Intended for testing purposes. * This setting is more specific than `CODEQL_ACTION_TEST_MODE`, which implies this option. diff --git a/src/init-action-post-helper.ts b/src/init-action-post-helper.ts index f9021b789..38ff07871 100644 --- a/src/init-action-post-helper.ts +++ b/src/init-action-post-helper.ts @@ -44,7 +44,7 @@ import { getRequiredEnvParam, parseMatrixInput, Result, - shouldSkipSarifUpload, + shouldSkipGenerateFailedSarif, Success, wrapError, } from "./util"; @@ -115,7 +115,7 @@ async function prepareFailedSarif( const jobName = getRequiredEnvParam("GITHUB_JOB"); const matrix = parseMatrixInput(actionsUtil.getRequiredInput("matrix")); - if (shouldSkipSarifUpload()) { + if (shouldSkipGenerateFailedSarif()) { return new Failure({ upload_failed_run_skipped_because: "SARIF upload is disabled", }); diff --git a/src/util.test.ts b/src/util.test.ts index 1a1f7677e..e7a7d3abd 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -494,6 +494,45 @@ test("Failure.orElse returns the default value for a failure result", (t) => { t.is(result.orElse("default value"), "default value"); }); +test("shouldSkipGenerateFailedSarif", (t) => { + const env = getTestEnv(); + + // Don't skip by default + t.is(util.shouldSkipGenerateFailedSarif(env), false); + + // Skip in test mode + env.setAll({ + [EnvVar.TEST_MODE]: "true", + [EnvVar.SKIP_SARIF_UPLOAD]: undefined, + [EnvVar.SKIP_GENERATE_FAILED_SARIF]: undefined, + }); + t.is(util.shouldSkipGenerateFailedSarif(env), true); + + // Skip if explicitly requested + env.setAll({ + [EnvVar.TEST_MODE]: undefined, + [EnvVar.SKIP_SARIF_UPLOAD]: undefined, + [EnvVar.SKIP_GENERATE_FAILED_SARIF]: "true", + }); + t.is(util.shouldSkipGenerateFailedSarif(env), true); + + // Skip if not uploading SARIF + env.setAll({ + [EnvVar.TEST_MODE]: undefined, + [EnvVar.SKIP_SARIF_UPLOAD]: "true", + [EnvVar.SKIP_GENERATE_FAILED_SARIF]: undefined, + }); + t.is(util.shouldSkipGenerateFailedSarif(env), true); + + // Don't skip if explicitly requested + env.setAll({ + [EnvVar.TEST_MODE]: "true", + [EnvVar.SKIP_SARIF_UPLOAD]: "true", + [EnvVar.SKIP_GENERATE_FAILED_SARIF]: "false", + }); + t.is(util.shouldSkipGenerateFailedSarif(env), false); +}); + test("shouldSkipSarifUpload", (t) => { const env = getTestEnv(); diff --git a/src/util.ts b/src/util.ts index 073ecf0a0..ba0c3c7d4 100644 --- a/src/util.ts +++ b/src/util.ts @@ -684,6 +684,21 @@ export function isGoodVersion(versionSpec: string) { return !BROKEN_VERSIONS.includes(versionSpec); } +/** + * Returns whether we specifically want to skip generating a failed SARIF. + */ +export function shouldSkipGenerateFailedSarif( + env: ReadOnlyEnv = getEnv(), +): boolean { + const skipGenerateFailedSarif = env.getOptional( + EnvVar.SKIP_GENERATE_FAILED_SARIF, + ); + return ( + skipGenerateFailedSarif !== "false" && + (shouldSkipSarifUpload(env) || skipGenerateFailedSarif === "true") + ); +} + /** * Returns whether we specifically want to skip uploading SARIF files. */