Skip to content

Commit d781ca7

Browse files
committed
getOverlayDatabaseMode: use Feature.OverlayAnalysis
This commit changes getOverlayDatabaseMode so that, when Feature.OverlayAnalysis is enabled, it calculates the overlay database mode automatically based on analysis metadata. If we are analyzing the default branch, use OverlayBase, and if we are analyzing a PR, use Overlay. If CODEQL_OVERLAY_DATABASE_MODE is set to a valid overlay database mode, that environment variable still takes precedence.
1 parent 4e69877 commit d781ca7

File tree

1 file changed

+69
-27
lines changed

1 file changed

+69
-27
lines changed

src/config-utils.ts

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { performance } from "perf_hooks";
55
import * as yaml from "js-yaml";
66
import * as semver from "semver";
77

8+
import { isAnalyzingPullRequest } from "./actions-util";
89
import * as api from "./api-client";
910
import { CachingKind, getCachingKind } from "./caching-utils";
1011
import { CodeQL } from "./codeql";
1112
import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils";
1213
import { Feature, FeatureEnablement } from "./feature-flags";
13-
import { getGitRoot } from "./git-utils";
14+
import { getGitRoot, isAnalyzingDefaultBranch } from "./git-utils";
1415
import { Language, parseLanguage } from "./languages";
1516
import { Logger } from "./logging";
1617
import {
@@ -723,46 +724,87 @@ function parseQueriesFromInput(
723724
return trimmedInput.split(",").map((query) => ({ uses: query.trim() }));
724725
}
725726

727+
/**
728+
* Calculate and validate the overlay database mode to use.
729+
*
730+
* - If the environment variable `CODEQL_OVERLAY_DATABASE_MODE` is set, use it.
731+
* - Otherwise, if `Feature.OverlayAnalysis` is enabled, calculate the mode
732+
* based on what we are analyzing.
733+
* - If we are analyzing a pull request, use `Overlay`.
734+
* - If we are analyzing the default branch, use `OverlayBase`.
735+
* - Otherwise, use `None`.
736+
*
737+
* For `Overlay` and `OverlayBase`, the function performs further checks and
738+
* reverts to `None` if any check should fail.
739+
*/
726740
async function getOverlayDatabaseMode(
727741
codeql: CodeQL,
728742
features: FeatureEnablement,
729743
sourceRoot: string,
730744
buildMode: BuildMode | undefined,
731745
logger: Logger,
732746
): Promise<OverlayDatabaseMode> {
733-
const overlayDatabaseMode = process.env.CODEQL_OVERLAY_DATABASE_MODE;
747+
let overlayDatabaseMode = OverlayDatabaseMode.None;
734748

749+
const modeEnv = process.env.CODEQL_OVERLAY_DATABASE_MODE;
750+
// Any unrecognized CODEQL_OVERLAY_DATABASE_MODE value will be ignored and
751+
// treated as if the environment variable was not set.
735752
if (
736-
overlayDatabaseMode === OverlayDatabaseMode.Overlay ||
737-
overlayDatabaseMode === OverlayDatabaseMode.OverlayBase
753+
modeEnv === OverlayDatabaseMode.Overlay ||
754+
modeEnv === OverlayDatabaseMode.OverlayBase ||
755+
modeEnv === OverlayDatabaseMode.None
738756
) {
739-
if (buildMode !== BuildMode.None) {
740-
logger.warning(
741-
`Cannot build an ${overlayDatabaseMode} database because ` +
742-
`build-mode is set to "${buildMode}" instead of "none". ` +
743-
"Falling back to creating a normal full database instead.",
744-
);
745-
return OverlayDatabaseMode.None;
746-
}
747-
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
748-
logger.warning(
749-
`Cannot build an ${overlayDatabaseMode} database because ` +
750-
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
751-
"Falling back to creating a normal full database instead.",
757+
overlayDatabaseMode = modeEnv;
758+
logger.info(
759+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
760+
"from the CODEQL_OVERLAY_DATABASE_MODE environment variable.",
761+
);
762+
} else if (await features.getValue(Feature.OverlayAnalysis, codeql)) {
763+
if (isAnalyzingPullRequest()) {
764+
overlayDatabaseMode = OverlayDatabaseMode.Overlay;
765+
logger.info(
766+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
767+
"because we are analyzing a pull request.",
752768
);
753-
return OverlayDatabaseMode.None;
754-
}
755-
if ((await getGitRoot(sourceRoot)) === undefined) {
756-
logger.warning(
757-
`Cannot build an ${overlayDatabaseMode} database because ` +
758-
`the source root "${sourceRoot}" is not inside a git repository. ` +
759-
"Falling back to creating a normal full database instead.",
769+
} else if (await isAnalyzingDefaultBranch()) {
770+
overlayDatabaseMode = OverlayDatabaseMode.OverlayBase;
771+
logger.info(
772+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
773+
"because we are analyzing the default branch.",
760774
);
761-
return OverlayDatabaseMode.None;
762775
}
763-
return overlayDatabaseMode as OverlayDatabaseMode;
764776
}
765-
return OverlayDatabaseMode.None;
777+
778+
if (overlayDatabaseMode === OverlayDatabaseMode.None) {
779+
return OverlayDatabaseMode.None;
780+
}
781+
782+
if (buildMode !== BuildMode.None) {
783+
logger.warning(
784+
`Cannot build an ${overlayDatabaseMode} database because ` +
785+
`build-mode is set to "${buildMode}" instead of "none". ` +
786+
"Falling back to creating a normal full database instead.",
787+
);
788+
return OverlayDatabaseMode.None;
789+
}
790+
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
791+
logger.warning(
792+
`Cannot build an ${overlayDatabaseMode} database because ` +
793+
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
794+
"Falling back to creating a normal full database instead.",
795+
);
796+
return OverlayDatabaseMode.None;
797+
}
798+
if ((await getGitRoot(sourceRoot)) === undefined) {
799+
logger.warning(
800+
`Cannot build an ${overlayDatabaseMode} database because ` +
801+
`the source root "${sourceRoot}" is not inside a git repository. ` +
802+
"Falling back to creating a normal full database instead.",
803+
);
804+
return OverlayDatabaseMode.None;
805+
}
806+
807+
return overlayDatabaseMode;
766808
}
767809

768810
/**

0 commit comments

Comments
 (0)