Skip to content

Commit ff5f0b9

Browse files
committed
Support overlay database creation
This commit adds support for creating overlay-base and overlay databases, controlled via the CODEQL_OVERLAY_DATABASE_MODE environment variable.
1 parent 270886f commit ff5f0b9

File tree

4 files changed

+89
-8
lines changed

4 files changed

+89
-8
lines changed

src/git-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,28 @@ export const decodeGitFilePath = function (filePath: string): string {
300300
return filePath;
301301
};
302302

303+
/**
304+
* Get the root of the Git repository.
305+
*
306+
* @param sourceRoot The source root of the code being analyzed.
307+
* @returns The root of the Git repository.
308+
*/
309+
export const getGitRoot = async function (
310+
sourceRoot: string,
311+
): Promise<string | undefined> {
312+
try {
313+
const stdout = await runGitCommand(
314+
sourceRoot,
315+
["rev-parse", "--show-toplevel"],
316+
`Cannot find Git repository root from the source root ${sourceRoot}.`,
317+
);
318+
return stdout.trim();
319+
} catch {
320+
// Errors are already logged by runGitCommand()
321+
return undefined;
322+
}
323+
};
324+
303325
function getRefFromEnv(): string {
304326
// To workaround a limitation of Actions dynamic workflows not setting
305327
// the GITHUB_REF in some cases, we accept also the ref within the

src/init-action.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { Feature, featureConfig, Features } from "./feature-flags";
3636
import {
3737
checkInstallPython311,
3838
cleanupDatabaseClusterDirectory,
39+
getOverlayDatabaseMode,
3940
initCodeQL,
4041
initConfig,
4142
runInit,
@@ -396,7 +397,22 @@ async function run() {
396397
}
397398

398399
try {
399-
cleanupDatabaseClusterDirectory(config, logger);
400+
const sourceRoot = path.resolve(
401+
getRequiredEnvParam("GITHUB_WORKSPACE"),
402+
getOptionalInput("source-root") || "",
403+
);
404+
405+
const overlayDatabaseMode = await getOverlayDatabaseMode(
406+
(await codeql.getVersion()).version,
407+
config,
408+
sourceRoot,
409+
logger,
410+
);
411+
logger.info(`Using overlay database mode: ${overlayDatabaseMode}`);
412+
413+
if (overlayDatabaseMode !== OverlayDatabaseMode.Overlay) {
414+
cleanupDatabaseClusterDirectory(config, logger);
415+
}
400416

401417
if (zstdAvailability) {
402418
await recordZstdAvailability(config, zstdAvailability);
@@ -676,19 +692,14 @@ async function run() {
676692
}
677693
}
678694

679-
const sourceRoot = path.resolve(
680-
getRequiredEnvParam("GITHUB_WORKSPACE"),
681-
getOptionalInput("source-root") || "",
682-
);
683-
684695
const tracerConfig = await runInit(
685696
codeql,
686697
config,
687698
sourceRoot,
688699
"Runner.Worker.exe",
689700
getOptionalInput("registries"),
690701
apiDetails,
691-
OverlayDatabaseMode.None,
702+
overlayDatabaseMode,
692703
logger,
693704
);
694705
if (tracerConfig !== undefined) {

src/init.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import * as path from "path";
33

44
import * as toolrunner from "@actions/exec/lib/toolrunner";
55
import * as io from "@actions/io";
6+
import * as semver from "semver";
67

78
import { getOptionalInput, isSelfHostedRunner } from "./actions-util";
89
import { GitHubApiCombinedDetails, GitHubApiDetails } from "./api-client";
910
import { CodeQL, setupCodeQL } from "./codeql";
1011
import * as configUtils from "./config-utils";
1112
import { CodeQLDefaultVersionInfo, FeatureEnablement } from "./feature-flags";
13+
import { getGitRoot } from "./git-utils";
1214
import { Language, isScannedLanguage } from "./languages";
1315
import { Logger } from "./logging";
14-
import { OverlayDatabaseMode } from "./overlay-database-utils";
16+
import {
17+
CODEQL_OVERLAY_MINIMUM_VERSION,
18+
OverlayDatabaseMode,
19+
} from "./overlay-database-utils";
1520
import { ToolsSource } from "./setup-codeql";
1621
import { ZstdAvailability } from "./tar";
1722
import { ToolsDownloadStatusReport } from "./tools-download";
@@ -80,6 +85,47 @@ export async function initConfig(
8085
return config;
8186
}
8287

88+
export async function getOverlayDatabaseMode(
89+
codeqlVersion: string,
90+
config: configUtils.Config,
91+
sourceRoot: string,
92+
logger: Logger,
93+
): Promise<OverlayDatabaseMode> {
94+
const overlayDatabaseMode = process.env.CODEQL_OVERLAY_DATABASE_MODE;
95+
96+
if (
97+
overlayDatabaseMode === OverlayDatabaseMode.Overlay ||
98+
overlayDatabaseMode === OverlayDatabaseMode.OverlayBase
99+
) {
100+
if (config.buildMode !== util.BuildMode.None) {
101+
logger.warning(
102+
`Cannot build an ${overlayDatabaseMode} database because ` +
103+
`build-mode is set to "${config.buildMode}" instead of "none". ` +
104+
"Falling back to creating a normal full database instead.",
105+
);
106+
return OverlayDatabaseMode.None;
107+
}
108+
if (semver.lt(codeqlVersion, CODEQL_OVERLAY_MINIMUM_VERSION)) {
109+
logger.warning(
110+
`Cannot build an ${overlayDatabaseMode} database because ` +
111+
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
112+
"Falling back to creating a normal full database instead.",
113+
);
114+
return OverlayDatabaseMode.None;
115+
}
116+
if ((await getGitRoot(sourceRoot)) === undefined) {
117+
logger.warning(
118+
`Cannot build an ${overlayDatabaseMode} database because ` +
119+
`the source root "${sourceRoot}" is not inside a git repository. ` +
120+
"Falling back to creating a normal full database instead.",
121+
);
122+
return OverlayDatabaseMode.None;
123+
}
124+
return overlayDatabaseMode as OverlayDatabaseMode;
125+
}
126+
return OverlayDatabaseMode.None;
127+
}
128+
83129
export async function runInit(
84130
codeql: CodeQL,
85131
config: configUtils.Config,

src/overlay-database-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export enum OverlayDatabaseMode {
33
OverlayBase = "overlay-base",
44
None = "none",
55
}
6+
7+
export const CODEQL_OVERLAY_MINIMUM_VERSION = "2.20.5";

0 commit comments

Comments
 (0)