From 9e1b38bcc6ee333370429099fc0130e3d17eeeb3 Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Mon, 2 Mar 2026 19:58:27 +0100 Subject: [PATCH] Allow explicit disablement too --- lib/init-action.js | 18 +++++++- src/init.test.ts | 104 +++++++++++++++++++++++++++++++++++++++++++++ src/init.ts | 31 ++++++++++---- 3 files changed, 143 insertions(+), 10 deletions(-) diff --git a/lib/init-action.js b/lib/init-action.js index b824100a1..b2f69bca3 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -109056,18 +109056,32 @@ function cleanupDatabaseClusterDirectory(config, logger, options = {}, rmSync2 = async function getFileCoverageInformationEnabled(debugMode, repositoryNwo, features, repositoryProperties, logger) { if (debugMode) return true; if (!isAnalyzingPullRequest()) return true; - if (process.env["CODEQL_ACTION_ENABLE_FILE_COVERAGE_ON_PRS" /* ENABLE_FILE_COVERAGE_ON_PRS */] === "true") { + const envVarValue = process.env["CODEQL_ACTION_ENABLE_FILE_COVERAGE_ON_PRS" /* ENABLE_FILE_COVERAGE_ON_PRS */]; + if (envVarValue === "true") { logger.info( `File coverage information on pull requests has been enabled by the '${"CODEQL_ACTION_ENABLE_FILE_COVERAGE_ON_PRS" /* ENABLE_FILE_COVERAGE_ON_PRS */}' environment variable. This may increase the time it takes to analyze pull requests, particularly on large repositories.` ); return true; } - if (repositoryProperties["github-codeql-enable-file-coverage-on-prs" /* ENABLE_FILE_COVERAGE_ON_PRS */] === true) { + if (envVarValue === "false") { + logger.info( + `File coverage information on pull requests has been disabled by the '${"CODEQL_ACTION_ENABLE_FILE_COVERAGE_ON_PRS" /* ENABLE_FILE_COVERAGE_ON_PRS */}' environment variable.` + ); + return false; + } + const repoPropValue = repositoryProperties["github-codeql-enable-file-coverage-on-prs" /* ENABLE_FILE_COVERAGE_ON_PRS */]; + if (repoPropValue === true) { logger.info( `File coverage information on pull requests has been enabled by the '${"github-codeql-enable-file-coverage-on-prs" /* ENABLE_FILE_COVERAGE_ON_PRS */}' repository property. This may increase the time it takes to analyze pull requests, particularly on large repositories.` ); return true; } + if (repoPropValue === false) { + logger.info( + `File coverage information on pull requests has been disabled by the '${"github-codeql-enable-file-coverage-on-prs" /* ENABLE_FILE_COVERAGE_ON_PRS */}' repository property.` + ); + return false; + } if (repositoryNwo.owner !== "github") return true; if (!await features.getValue("skip_file_coverage_on_prs" /* SkipFileCoverageOnPrs */)) return true; return false; diff --git a/src/init.test.ts b/src/init.test.ts index 264fa7862..2c81e7eb8 100644 --- a/src/init.test.ts +++ b/src/init.test.ts @@ -578,6 +578,110 @@ test("file coverage information enabled when env var enables it on PRs", async ( ); }); +test("file coverage information disabled when env var disables it on PRs", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "false"; + t.teardown(() => { + delete process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS]; + }); + + const messages: LoggedMessage[] = []; + const logger = getRecordingLogger(messages); + + t.false( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("other-org/some-repo"), + createFeatures([]), + {}, + logger, + ), + ); + + t.true( + messages.some( + (m) => + m.type === "info" && + typeof m.message === "string" && + m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS) && + m.message.includes("disabled"), + ), + ); +}); + +test("file coverage information disabled when repository property disables it on PRs", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + + const messages: LoggedMessage[] = []; + const logger = getRecordingLogger(messages); + + t.false( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("other-org/some-repo"), + createFeatures([]), + { + [RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]: false, + }, + logger, + ), + ); + + t.true( + messages.some( + (m) => + m.type === "info" && + typeof m.message === "string" && + m.message.includes( + RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS, + ) && + m.message.includes("disabled"), + ), + ); +}); + +test("file coverage env var 'false' takes precedence over repository property 'true'", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "false"; + t.teardown(() => { + delete process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS]; + }); + + const messages: LoggedMessage[] = []; + const logger = getRecordingLogger(messages); + + t.false( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("github/codeql-action"), + createFeatures([Feature.SkipFileCoverageOnPrs]), + { + [RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]: true, + }, + logger, + ), + ); + + // Should mention the env var, not the repo property + t.true( + messages.some( + (m) => + m.type === "info" && + typeof m.message === "string" && + m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS) && + m.message.includes("disabled"), + ), + ); + t.false( + messages.some( + (m) => + m.type === "info" && + typeof m.message === "string" && + m.message.includes(RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS), + ), + ); +}); + test("file coverage env var takes precedence over repository property", async (t) => { sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "true"; diff --git a/src/init.ts b/src/init.ts index 81e379b1e..f0fa2c05e 100644 --- a/src/init.ts +++ b/src/init.ts @@ -318,8 +318,10 @@ export async function getFileCoverageInformationEnabled( // it is used to populate the status page. if (!isAnalyzingPullRequest()) return true; - // Allow users to opt in to file coverage on PRs via an environment variable. - if (process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] === "true") { + // Allow users to explicitly enable or disable file coverage on PRs via + // an environment variable. This has the highest precedence. + const envVarValue = process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS]; + if (envVarValue === "true") { logger.info( "File coverage information on pull requests has been enabled by the " + `'${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable. ` + @@ -328,13 +330,19 @@ export async function getFileCoverageInformationEnabled( ); return true; } + if (envVarValue === "false") { + logger.info( + "File coverage information on pull requests has been disabled by the " + + `'${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable.`, + ); + return false; + } - // Allow repository owners to opt in to file coverage on PRs via a - // repository property. - if ( - repositoryProperties[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS] === - true - ) { + // Allow repository owners to explicitly enable or disable file coverage + // on PRs via a repository property. + const repoPropValue = + repositoryProperties[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]; + if (repoPropValue === true) { logger.info( "File coverage information on pull requests has been enabled by the " + `'${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property. ` + @@ -343,6 +351,13 @@ export async function getFileCoverageInformationEnabled( ); return true; } + if (repoPropValue === false) { + logger.info( + "File coverage information on pull requests has been disabled by the " + + `'${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property.`, + ); + return false; + } // For now, restrict this feature to the GitHub org if (repositoryNwo.owner !== "github") return true;