Allow explicit disablement too

This commit is contained in:
Henry Mercer
2026-03-02 19:58:27 +01:00
parent 2a4d1eca6b
commit 9e1b38bcc6
3 changed files with 143 additions and 10 deletions
+16 -2
View File
@@ -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;
+104
View File
@@ -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";
+23 -8
View File
@@ -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;