Skip to content

Commit 93e8729

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 da758dc commit 93e8729

File tree

1 file changed

+72
-28
lines changed

1 file changed

+72
-28
lines changed

src/config-utils.ts

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ 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 { Language, parseLanguage } from "./languages";
14+
import { getGitRoot, isAnalyzingDefaultBranch } from "./git-utils";
15+
import { isTracedLanguage, Language, parseLanguage } from "./languages";
1516
import { Logger } from "./logging";
1617
import {
1718
CODEQL_OVERLAY_MINIMUM_VERSION,
@@ -691,6 +692,7 @@ export async function calculateAugmentation(
691692
const overlayDatabaseMode = await getOverlayDatabaseMode(
692693
codeql,
693694
features,
695+
languages,
694696
sourceRoot,
695697
buildMode,
696698
logger,
@@ -743,46 +745,88 @@ function parseQueriesFromInput(
743745
return trimmedInput.split(",").map((query) => ({ uses: query.trim() }));
744746
}
745747

748+
/**
749+
* Calculate and validate the overlay database mode to use.
750+
*
751+
* - If the environment variable `CODEQL_OVERLAY_DATABASE_MODE` is set, use it.
752+
* - Otherwise, if `Feature.OverlayAnalysis` is enabled, calculate the mode
753+
* based on what we are analyzing.
754+
* - If we are analyzing a pull request, use `Overlay`.
755+
* - If we are analyzing the default branch, use `OverlayBase`.
756+
* - Otherwise, use `None`.
757+
*
758+
* For `Overlay` and `OverlayBase`, the function performs further checks and
759+
* reverts to `None` if any check should fail.
760+
*/
746761
async function getOverlayDatabaseMode(
747762
codeql: CodeQL,
748763
features: FeatureEnablement,
764+
languages: Language[],
749765
sourceRoot: string,
750766
buildMode: BuildMode | undefined,
751767
logger: Logger,
752768
): Promise<OverlayDatabaseMode> {
753-
const overlayDatabaseMode = process.env.CODEQL_OVERLAY_DATABASE_MODE;
769+
let overlayDatabaseMode = OverlayDatabaseMode.None;
754770

771+
const modeEnv = process.env.CODEQL_OVERLAY_DATABASE_MODE;
772+
// Any unrecognized CODEQL_OVERLAY_DATABASE_MODE value will be ignored and
773+
// treated as if the environment variable was not set.
755774
if (
756-
overlayDatabaseMode === OverlayDatabaseMode.Overlay ||
757-
overlayDatabaseMode === OverlayDatabaseMode.OverlayBase
775+
modeEnv === OverlayDatabaseMode.Overlay ||
776+
modeEnv === OverlayDatabaseMode.OverlayBase ||
777+
modeEnv === OverlayDatabaseMode.None
758778
) {
759-
if (buildMode !== BuildMode.None) {
760-
logger.warning(
761-
`Cannot build an ${overlayDatabaseMode} database because ` +
762-
`build-mode is set to "${buildMode}" instead of "none". ` +
763-
"Falling back to creating a normal full database instead.",
764-
);
765-
return OverlayDatabaseMode.None;
766-
}
767-
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
768-
logger.warning(
769-
`Cannot build an ${overlayDatabaseMode} database because ` +
770-
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
771-
"Falling back to creating a normal full database instead.",
779+
overlayDatabaseMode = modeEnv;
780+
logger.info(
781+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
782+
"from the CODEQL_OVERLAY_DATABASE_MODE environment variable.",
783+
);
784+
} else if (await features.getValue(Feature.OverlayAnalysis, codeql)) {
785+
if (isAnalyzingPullRequest()) {
786+
overlayDatabaseMode = OverlayDatabaseMode.Overlay;
787+
logger.info(
788+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
789+
"because we are analyzing a pull request.",
772790
);
773-
return OverlayDatabaseMode.None;
774-
}
775-
if ((await getGitRoot(sourceRoot)) === undefined) {
776-
logger.warning(
777-
`Cannot build an ${overlayDatabaseMode} database because ` +
778-
`the source root "${sourceRoot}" is not inside a git repository. ` +
779-
"Falling back to creating a normal full database instead.",
791+
} else if (await isAnalyzingDefaultBranch()) {
792+
overlayDatabaseMode = OverlayDatabaseMode.OverlayBase;
793+
logger.info(
794+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
795+
"because we are analyzing the default branch.",
780796
);
781-
return OverlayDatabaseMode.None;
782797
}
783-
return overlayDatabaseMode as OverlayDatabaseMode;
784798
}
785-
return OverlayDatabaseMode.None;
799+
800+
if (overlayDatabaseMode === OverlayDatabaseMode.None) {
801+
return OverlayDatabaseMode.None;
802+
}
803+
804+
if (buildMode !== BuildMode.None && languages.some(isTracedLanguage)) {
805+
logger.warning(
806+
`Cannot build an ${overlayDatabaseMode} database because ` +
807+
`build-mode is set to "${buildMode}" instead of "none". ` +
808+
"Falling back to creating a normal full database instead.",
809+
);
810+
return OverlayDatabaseMode.None;
811+
}
812+
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
813+
logger.warning(
814+
`Cannot build an ${overlayDatabaseMode} database because ` +
815+
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
816+
"Falling back to creating a normal full database instead.",
817+
);
818+
return OverlayDatabaseMode.None;
819+
}
820+
if ((await getGitRoot(sourceRoot)) === undefined) {
821+
logger.warning(
822+
`Cannot build an ${overlayDatabaseMode} database because ` +
823+
`the source root "${sourceRoot}" is not inside a git repository. ` +
824+
"Falling back to creating a normal full database instead.",
825+
);
826+
return OverlayDatabaseMode.None;
827+
}
828+
829+
return overlayDatabaseMode;
786830
}
787831

788832
/**

0 commit comments

Comments
 (0)