Skip to content

Commit 9e1b38b

Browse files
committed
Allow explicit disablement too
1 parent 2a4d1ec commit 9e1b38b

File tree

3 files changed

+143
-10
lines changed

3 files changed

+143
-10
lines changed

lib/init-action.js

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init.test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,110 @@ test("file coverage information enabled when env var enables it on PRs", async (
578578
);
579579
});
580580

581+
test("file coverage information disabled when env var disables it on PRs", async (t) => {
582+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
583+
process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "false";
584+
t.teardown(() => {
585+
delete process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS];
586+
});
587+
588+
const messages: LoggedMessage[] = [];
589+
const logger = getRecordingLogger(messages);
590+
591+
t.false(
592+
await getFileCoverageInformationEnabled(
593+
false, // debugMode
594+
parseRepositoryNwo("other-org/some-repo"),
595+
createFeatures([]),
596+
{},
597+
logger,
598+
),
599+
);
600+
601+
t.true(
602+
messages.some(
603+
(m) =>
604+
m.type === "info" &&
605+
typeof m.message === "string" &&
606+
m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS) &&
607+
m.message.includes("disabled"),
608+
),
609+
);
610+
});
611+
612+
test("file coverage information disabled when repository property disables it on PRs", async (t) => {
613+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
614+
615+
const messages: LoggedMessage[] = [];
616+
const logger = getRecordingLogger(messages);
617+
618+
t.false(
619+
await getFileCoverageInformationEnabled(
620+
false, // debugMode
621+
parseRepositoryNwo("other-org/some-repo"),
622+
createFeatures([]),
623+
{
624+
[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]: false,
625+
},
626+
logger,
627+
),
628+
);
629+
630+
t.true(
631+
messages.some(
632+
(m) =>
633+
m.type === "info" &&
634+
typeof m.message === "string" &&
635+
m.message.includes(
636+
RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS,
637+
) &&
638+
m.message.includes("disabled"),
639+
),
640+
);
641+
});
642+
643+
test("file coverage env var 'false' takes precedence over repository property 'true'", async (t) => {
644+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
645+
process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "false";
646+
t.teardown(() => {
647+
delete process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS];
648+
});
649+
650+
const messages: LoggedMessage[] = [];
651+
const logger = getRecordingLogger(messages);
652+
653+
t.false(
654+
await getFileCoverageInformationEnabled(
655+
false, // debugMode
656+
parseRepositoryNwo("github/codeql-action"),
657+
createFeatures([Feature.SkipFileCoverageOnPrs]),
658+
{
659+
[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]: true,
660+
},
661+
logger,
662+
),
663+
);
664+
665+
// Should mention the env var, not the repo property
666+
t.true(
667+
messages.some(
668+
(m) =>
669+
m.type === "info" &&
670+
typeof m.message === "string" &&
671+
m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS) &&
672+
m.message.includes("disabled"),
673+
),
674+
);
675+
t.false(
676+
messages.some(
677+
(m) =>
678+
m.type === "info" &&
679+
typeof m.message === "string" &&
680+
m.message.includes(RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS),
681+
),
682+
);
683+
});
684+
581685
test("file coverage env var takes precedence over repository property", async (t) => {
582686
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
583687
process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "true";

src/init.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,10 @@ export async function getFileCoverageInformationEnabled(
318318
// it is used to populate the status page.
319319
if (!isAnalyzingPullRequest()) return true;
320320

321-
// Allow users to opt in to file coverage on PRs via an environment variable.
322-
if (process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] === "true") {
321+
// Allow users to explicitly enable or disable file coverage on PRs via
322+
// an environment variable. This has the highest precedence.
323+
const envVarValue = process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS];
324+
if (envVarValue === "true") {
323325
logger.info(
324326
"File coverage information on pull requests has been enabled by the " +
325327
`'${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable. ` +
@@ -328,13 +330,19 @@ export async function getFileCoverageInformationEnabled(
328330
);
329331
return true;
330332
}
333+
if (envVarValue === "false") {
334+
logger.info(
335+
"File coverage information on pull requests has been disabled by the " +
336+
`'${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable.`,
337+
);
338+
return false;
339+
}
331340

332-
// Allow repository owners to opt in to file coverage on PRs via a
333-
// repository property.
334-
if (
335-
repositoryProperties[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS] ===
336-
true
337-
) {
341+
// Allow repository owners to explicitly enable or disable file coverage
342+
// on PRs via a repository property.
343+
const repoPropValue =
344+
repositoryProperties[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS];
345+
if (repoPropValue === true) {
338346
logger.info(
339347
"File coverage information on pull requests has been enabled by the " +
340348
`'${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property. ` +
@@ -343,6 +351,13 @@ export async function getFileCoverageInformationEnabled(
343351
);
344352
return true;
345353
}
354+
if (repoPropValue === false) {
355+
logger.info(
356+
"File coverage information on pull requests has been disabled by the " +
357+
`'${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property.`,
358+
);
359+
return false;
360+
}
346361

347362
// For now, restrict this feature to the GitHub org
348363
if (repositoryNwo.owner !== "github") return true;

0 commit comments

Comments
 (0)