Skip to content

Commit 5a36cc1

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 6b6727f commit 5a36cc1

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 {
@@ -741,46 +742,87 @@ function parseQueriesFromInput(
741742
return trimmedInput.split(",").map((query) => ({ uses: query.trim() }));
742743
}
743744

745+
/**
746+
* Calculate and validate the overlay database mode to use.
747+
*
748+
* - If the environment variable `CODEQL_OVERLAY_DATABASE_MODE` is set, use it.
749+
* - Otherwise, if `Feature.OverlayAnalysis` is enabled, calculate the mode
750+
* based on what we are analyzing.
751+
* - If we are analyzing a pull request, use `Overlay`.
752+
* - If we are analyzing the default branch, use `OverlayBase`.
753+
* - Otherwise, use `None`.
754+
*
755+
* For `Overlay` and `OverlayBase`, the function performs further checks and
756+
* reverts to `None` if any check should fail.
757+
*/
744758
async function getOverlayDatabaseMode(
745759
codeql: CodeQL,
746760
features: FeatureEnablement,
747761
sourceRoot: string,
748762
buildMode: BuildMode | undefined,
749763
logger: Logger,
750764
): Promise<OverlayDatabaseMode> {
751-
const overlayDatabaseMode = process.env.CODEQL_OVERLAY_DATABASE_MODE;
765+
let overlayDatabaseMode = OverlayDatabaseMode.None;
752766

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

786828
/**

0 commit comments

Comments
 (0)