Skip to content

Commit 173df96

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 e610460 commit 173df96

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

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

769+
const modeEnv = process.env.CODEQL_OVERLAY_DATABASE_MODE;
770+
// Any unrecognized CODEQL_OVERLAY_DATABASE_MODE value will be ignored and
771+
// treated as if the environment variable was not set.
755772
if (
756-
overlayDatabaseMode === OverlayDatabaseMode.Overlay ||
757-
overlayDatabaseMode === OverlayDatabaseMode.OverlayBase
773+
modeEnv === OverlayDatabaseMode.Overlay ||
774+
modeEnv === OverlayDatabaseMode.OverlayBase ||
775+
modeEnv === OverlayDatabaseMode.None
758776
) {
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.",
777+
overlayDatabaseMode = modeEnv;
778+
logger.info(
779+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
780+
"from the CODEQL_OVERLAY_DATABASE_MODE environment variable.",
781+
);
782+
} else if (await features.getValue(Feature.OverlayAnalysis, codeql)) {
783+
if (isAnalyzingPullRequest()) {
784+
overlayDatabaseMode = OverlayDatabaseMode.Overlay;
785+
logger.info(
786+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
787+
"because we are analyzing a pull request.",
772788
);
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.",
789+
} else if (await isAnalyzingDefaultBranch()) {
790+
overlayDatabaseMode = OverlayDatabaseMode.OverlayBase;
791+
logger.info(
792+
`Setting overlay database mode to ${overlayDatabaseMode} ` +
793+
"because we are analyzing the default branch.",
780794
);
781-
return OverlayDatabaseMode.None;
782795
}
783-
return overlayDatabaseMode as OverlayDatabaseMode;
784796
}
785-
return OverlayDatabaseMode.None;
797+
798+
if (overlayDatabaseMode === OverlayDatabaseMode.None) {
799+
return OverlayDatabaseMode.None;
800+
}
801+
802+
if (buildMode !== BuildMode.None) {
803+
logger.warning(
804+
`Cannot build an ${overlayDatabaseMode} database because ` +
805+
`build-mode is set to "${buildMode}" instead of "none". ` +
806+
"Falling back to creating a normal full database instead.",
807+
);
808+
return OverlayDatabaseMode.None;
809+
}
810+
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
811+
logger.warning(
812+
`Cannot build an ${overlayDatabaseMode} database because ` +
813+
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
814+
"Falling back to creating a normal full database instead.",
815+
);
816+
return OverlayDatabaseMode.None;
817+
}
818+
if ((await getGitRoot(sourceRoot)) === undefined) {
819+
logger.warning(
820+
`Cannot build an ${overlayDatabaseMode} database because ` +
821+
`the source root "${sourceRoot}" is not inside a git repository. ` +
822+
"Falling back to creating a normal full database instead.",
823+
);
824+
return OverlayDatabaseMode.None;
825+
}
826+
827+
return overlayDatabaseMode;
786828
}
787829

788830
/**

0 commit comments

Comments
 (0)