Skip to content

Commit 2a4d1ec

Browse files
committed
Add env var to enable file coverage on PRs
1 parent 003c59c commit 2a4d1ec

File tree

6 files changed

+119
-8
lines changed

6 files changed

+119
-8
lines changed

lib/analyze-action.js

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyze.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import * as github from "@actions/github";
66
import * as io from "@actions/io";
77
import * as yaml from "js-yaml";
88

9-
import { getTemporaryDirectory, PullRequestBranches } from "./actions-util";
9+
import {
10+
getTemporaryDirectory,
11+
isDefaultSetup,
12+
PullRequestBranches,
13+
} from "./actions-util";
1014
import * as analyses from "./analyses";
1115
import { setupCppAutobuild } from "./autobuild";
1216
import { type CodeQL } from "./codeql";
@@ -506,9 +510,14 @@ export async function runQueries(
506510
if (!config.enableFileCoverageInformation) {
507511
const isOrgOwned =
508512
github.context.payload.repository?.owner.type === "Organization";
509-
const reenableMessage = isOrgOwned
510-
? ` To enable file coverage information on pull requests, set the '${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property to 'true'.`
511-
: "";
513+
let reenableMessage: string;
514+
if (isOrgOwned) {
515+
reenableMessage = ` To enable file coverage information on pull requests, set the '${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property to 'true'.`;
516+
} else if (!isDefaultSetup()) {
517+
reenableMessage = ` To enable file coverage information on pull requests, set the '${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable to 'true'.`;
518+
} else {
519+
reenableMessage = "";
520+
}
512521
logger.info(
513522
`To speed up pull request analysis, file coverage information is only enabled when analyzing the default branch and protected branches.${reenableMessage}`,
514523
);

src/environment.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export enum EnvVar {
142142
*/
143143
ANALYSIS_KEY = "CODEQL_ACTION_ANALYSIS_KEY",
144144

145+
/**
146+
* Whether to enable file coverage information on pull requests.
147+
*/
148+
ENABLE_FILE_COVERAGE_ON_PRS = "CODEQL_ACTION_ENABLE_FILE_COVERAGE_ON_PRS",
149+
145150
/** Used by Code Scanning Risk Assessment to communicate the assessment ID to the CodeQL Action. */
146151
RISK_ASSESSMENT_ID = "CODEQL_ACTION_RISK_ASSESSMENT_ID",
147152
}

src/init.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as sinon from "sinon";
66

77
import * as actionsUtil from "./actions-util";
88
import { createStubCodeQL } from "./codeql";
9+
import { EnvVar } from "./environment";
910
import { Feature } from "./feature-flags";
1011
import { RepositoryPropertyName } from "./feature-flags/properties";
1112
import {
@@ -546,3 +547,74 @@ test("file coverage information enabled when repository property enables it on P
546547
),
547548
);
548549
});
550+
551+
test("file coverage information enabled when env var enables it on PRs", async (t) => {
552+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
553+
process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "true";
554+
t.teardown(() => {
555+
delete process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS];
556+
});
557+
558+
const messages: LoggedMessage[] = [];
559+
const logger = getRecordingLogger(messages);
560+
561+
t.true(
562+
await getFileCoverageInformationEnabled(
563+
false, // debugMode
564+
parseRepositoryNwo("github/codeql-action"),
565+
createFeatures([Feature.SkipFileCoverageOnPrs]),
566+
{},
567+
logger,
568+
),
569+
);
570+
571+
t.true(
572+
messages.some(
573+
(m) =>
574+
m.type === "info" &&
575+
typeof m.message === "string" &&
576+
m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS),
577+
),
578+
);
579+
});
580+
581+
test("file coverage env var takes precedence over repository property", async (t) => {
582+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
583+
process.env[EnvVar.ENABLE_FILE_COVERAGE_ON_PRS] = "true";
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.true(
592+
await getFileCoverageInformationEnabled(
593+
false, // debugMode
594+
parseRepositoryNwo("github/codeql-action"),
595+
createFeatures([Feature.SkipFileCoverageOnPrs]),
596+
{
597+
[RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS]: true,
598+
},
599+
logger,
600+
),
601+
);
602+
603+
// Should mention the env var, not the repo property
604+
t.true(
605+
messages.some(
606+
(m) =>
607+
m.type === "info" &&
608+
typeof m.message === "string" &&
609+
m.message.includes(EnvVar.ENABLE_FILE_COVERAGE_ON_PRS),
610+
),
611+
);
612+
t.false(
613+
messages.some(
614+
(m) =>
615+
m.type === "info" &&
616+
typeof m.message === "string" &&
617+
m.message.includes(RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS),
618+
),
619+
);
620+
});

src/init.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { GitHubApiDetails } from "./api-client";
1414
import { CodeQL, setupCodeQL } from "./codeql";
1515
import * as configUtils from "./config-utils";
16+
import { EnvVar } from "./environment";
1617
import {
1718
CodeQLDefaultVersionInfo,
1819
Feature,
@@ -317,6 +318,17 @@ export async function getFileCoverageInformationEnabled(
317318
// it is used to populate the status page.
318319
if (!isAnalyzingPullRequest()) return true;
319320

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") {
323+
logger.info(
324+
"File coverage information on pull requests has been enabled by the " +
325+
`'${EnvVar.ENABLE_FILE_COVERAGE_ON_PRS}' environment variable. ` +
326+
"This may increase the time it takes to analyze pull requests, " +
327+
"particularly on large repositories.",
328+
);
329+
return true;
330+
}
331+
320332
// Allow repository owners to opt in to file coverage on PRs via a
321333
// repository property.
322334
if (
@@ -326,8 +338,8 @@ export async function getFileCoverageInformationEnabled(
326338
logger.info(
327339
"File coverage information on pull requests has been enabled by the " +
328340
`'${RepositoryPropertyName.ENABLE_FILE_COVERAGE_ON_PRS}' repository property. ` +
329-
"This will increase the time it takes to analyze pull requests, particularly on " +
330-
"large repositories.",
341+
"This may increase the time it takes to analyze pull requests, " +
342+
"particularly on large repositories.",
331343
);
332344
return true;
333345
}

0 commit comments

Comments
 (0)