Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reduce duplication of getFileCoverageInformationEnabled
  • Loading branch information
henrymercer committed Mar 10, 2026
commit 55ae11793af2e75e1cc9550ee2a16baf5baee055
37 changes: 21 additions & 16 deletions lib/init-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 9 additions & 14 deletions src/init-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getOptionalInput,
getRequiredInput,
getTemporaryDirectory,
isAnalyzingPullRequest,
persistInputs,
} from "./actions-util";
import { AnalysisKind, getAnalysisKinds } from "./analyses";
Expand Down Expand Up @@ -43,7 +42,6 @@ import { Feature, FeatureEnablement, initFeatures } from "./feature-flags";
import {
loadPropertiesFromApi,
RepositoryProperties,
RepositoryPropertyName,
} from "./feature-flags/properties";
import {
checkInstallPython311,
Expand Down Expand Up @@ -349,6 +347,13 @@ async function run(startedAt: Date) {
analysisKinds = await getAnalysisKinds(logger);
const debugMode = getOptionalInput("debug") === "true" || core.isDebug();
const repositoryProperties = repositoryPropertiesResult.orElse({});
const fileCoverageResult = await getFileCoverageInformationEnabled(
debugMode,
codeql,
features,
repositoryProperties,
);

config = await initConfig(features, {
analysisKinds,
languagesInput: getOptionalInput("languages"),
Expand Down Expand Up @@ -379,12 +384,7 @@ async function run(startedAt: Date) {
apiDetails,
features,
repositoryProperties,
enableFileCoverageInformation: await getFileCoverageInformationEnabled(
debugMode,
codeql,
features,
repositoryProperties,
),
enableFileCoverageInformation: fileCoverageResult.enabled,
logger,
});

Expand All @@ -401,12 +401,7 @@ async function run(startedAt: Date) {
);
}

if (
config.enableFileCoverageInformation &&
isAnalyzingPullRequest() &&
(await features.getValue(Feature.SkipFileCoverageOnPrs, codeql)) &&
repositoryProperties[RepositoryPropertyName.FILE_COVERAGE_ON_PRS] === true
) {
if (fileCoverageResult.enabledByRepositoryProperty) {
addNoLanguageDiagnostic(
config,
makeTelemetryDiagnostic(
Expand Down
74 changes: 37 additions & 37 deletions src/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,29 +452,29 @@ test(
);

test("file coverage information enabled when debugMode is true", async (t) => {
t.true(
await getFileCoverageInformationEnabled(
true, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
),
const result = await getFileCoverageInformationEnabled(
true, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
);
t.true(result.enabled);
t.false(result.enabledByRepositoryProperty);
});

test.serial(
"file coverage information enabled when not analyzing a pull request",
async (t) => {
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(false);

t.true(
await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
),
const result = await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
);
t.true(result.enabled);
t.false(result.enabledByRepositoryProperty);
},
);

Expand All @@ -483,14 +483,14 @@ test.serial(
async (t) => {
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);

t.true(
await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([]),
{},
),
const result = await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([]),
{},
);
t.true(result.enabled);
t.false(result.enabledByRepositoryProperty);
},
);

Expand All @@ -499,16 +499,16 @@ test.serial(
async (t) => {
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);

t.true(
await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{
"github-codeql-file-coverage-on-prs": true,
},
),
const result = await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{
"github-codeql-file-coverage-on-prs": true,
},
);
t.true(result.enabled);
t.true(result.enabledByRepositoryProperty);
},
);

Expand All @@ -517,13 +517,13 @@ test.serial(
async (t) => {
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);

t.false(
await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
),
const result = await getFileCoverageInformationEnabled(
false, // debugMode
createStubCodeQL({}),
createFeatures([Feature.SkipFileCoverageOnPrs]),
{},
);
t.false(result.enabled);
t.false(result.enabledByRepositoryProperty);
},
);
42 changes: 28 additions & 14 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,32 @@ export async function getFileCoverageInformationEnabled(
codeql: CodeQL,
features: FeatureEnablement,
repositoryProperties: RepositoryProperties,
): Promise<boolean> {
return (
// Always enable file coverage information in debug mode
debugMode ||
// We're most interested in speeding up PRs, and we want to keep
// submitting file coverage information for the default branch since
// it is used to populate the status page.
!isAnalyzingPullRequest() ||
// Allow repositories to opt in to file coverage information on PRs
// using a repository property.
repositoryProperties[RepositoryPropertyName.FILE_COVERAGE_ON_PRS] ===
true ||
!(await features.getValue(Feature.SkipFileCoverageOnPrs, codeql))
);
): Promise<{
enabled: boolean;
enabledByRepositoryProperty: boolean;
}> {
// Always enable file coverage information in debug mode
if (debugMode) {
return { enabled: true, enabledByRepositoryProperty: false };
}
// We're most interested in speeding up PRs, and we want to keep
// submitting file coverage information for the default branch since
// it is used to populate the status page.
if (!isAnalyzingPullRequest()) {
return { enabled: true, enabledByRepositoryProperty: false };
}
// If the feature is disabled, then maintain the previous behavior of
// unconditionally computing file coverage information.
if (!(await features.getValue(Feature.SkipFileCoverageOnPrs, codeql))) {
return { enabled: true, enabledByRepositoryProperty: false };
}
// Allow repositories to opt in to file coverage information on PRs
// using a repository property.
if (
repositoryProperties[RepositoryPropertyName.FILE_COVERAGE_ON_PRS] === true
) {
return { enabled: true, enabledByRepositoryProperty: true };
}
// Otherwise, disable file coverage information on PRs to speed up analysis.
return { enabled: false, enabledByRepositoryProperty: false };
}
Loading