Skip to content

Commit 2ad0de5

Browse files
committed
AugmentationProperties: add useOverlayDatabaseCaching
This commit adds a useOverlayDatabaseCaching property to AugmentationProperties to indicate whether the action should upload overlay-base databases to the actions cache and to download a cached overlay-base database when creating an overlay database.
1 parent d781ca7 commit 2ad0de5

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

src/config-utils.ts

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ export interface AugmentationProperties {
196196
* The overlay database mode to use.
197197
*/
198198
overlayDatabaseMode: OverlayDatabaseMode;
199+
200+
/**
201+
* Whether to use caching for overlay databases. If it is true, the action
202+
* will upload the created overlay-base database to the actions cache, and
203+
* download an overlay-base database from the actions cache before it creates
204+
* a new overlay database. If it is false, the action assumes that the
205+
* workflow will be responsible for managing database storage and retrieval.
206+
*
207+
* This property has no effect unless `overlayDatabaseMode` is `Overlay` or
208+
* `OverlayBase`.
209+
*/
210+
useOverlayDatabaseCaching: boolean;
199211
}
200212

201213
/**
@@ -209,6 +221,7 @@ export const defaultAugmentationProperties: AugmentationProperties = {
209221
queriesInput: undefined,
210222
defaultQueryFilters: [],
211223
overlayDatabaseMode: OverlayDatabaseMode.None,
224+
useOverlayDatabaseCaching: false,
212225
};
213226
export type Packs = Partial<Record<Language, string[]>>;
214227

@@ -677,14 +690,18 @@ export async function calculateAugmentation(
677690
rawQueriesInput,
678691
queriesInputCombines,
679692
);
680-
const overlayDatabaseMode = await getOverlayDatabaseMode(
681-
codeql,
682-
features,
683-
sourceRoot,
684-
buildMode,
685-
logger,
693+
const { overlayDatabaseMode, useOverlayDatabaseCaching } =
694+
await getOverlayDatabaseMode(
695+
codeql,
696+
features,
697+
sourceRoot,
698+
buildMode,
699+
logger,
700+
);
701+
logger.info(
702+
`Using overlay database mode: ${overlayDatabaseMode} ` +
703+
`${useOverlayDatabaseCaching ? "with" : "without"} caching.`,
686704
);
687-
logger.info(`Using overlay database mode: ${overlayDatabaseMode}`);
688705

689706
const defaultQueryFilters: QueryFilter[] = [];
690707
if (await shouldPerformDiffInformedAnalysis(codeql, features, logger)) {
@@ -698,6 +715,7 @@ export async function calculateAugmentation(
698715
queriesInputCombines,
699716
defaultQueryFilters,
700717
overlayDatabaseMode,
718+
useOverlayDatabaseCaching,
701719
};
702720
}
703721

@@ -725,26 +743,38 @@ function parseQueriesFromInput(
725743
}
726744

727745
/**
728-
* Calculate and validate the overlay database mode to use.
746+
* Calculate and validate the overlay database mode and caching to use.
729747
*
730748
* - If the environment variable `CODEQL_OVERLAY_DATABASE_MODE` is set, use it.
749+
* In this case, the workflow is responsible for managing database storage and
750+
* retrieval, and the action will not perform overlay database caching. Think
751+
* of it as a "manual control" mode where the calling workflow is responsible
752+
* for making sure that everything is set up correctly.
731753
* - 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`.
754+
* based on what we are analyzing. Think of it as a "automatic control" mode
755+
* where the action will do the right thing by itself.
756+
* - If we are analyzing a pull request, use `Overlay` with caching.
757+
* - If we are analyzing the default branch, use `OverlayBase` with caching.
735758
* - Otherwise, use `None`.
736759
*
737760
* For `Overlay` and `OverlayBase`, the function performs further checks and
738761
* reverts to `None` if any check should fail.
762+
*
763+
* @returns An object containing the overlay database mode and whether the
764+
* action should perform overlay-base database caching.
739765
*/
740766
async function getOverlayDatabaseMode(
741767
codeql: CodeQL,
742768
features: FeatureEnablement,
743769
sourceRoot: string,
744770
buildMode: BuildMode | undefined,
745771
logger: Logger,
746-
): Promise<OverlayDatabaseMode> {
772+
): Promise<{
773+
overlayDatabaseMode: OverlayDatabaseMode;
774+
useOverlayDatabaseCaching: boolean;
775+
}> {
747776
let overlayDatabaseMode = OverlayDatabaseMode.None;
777+
let useOverlayDatabaseCaching = false;
748778

749779
const modeEnv = process.env.CODEQL_OVERLAY_DATABASE_MODE;
750780
// Any unrecognized CODEQL_OVERLAY_DATABASE_MODE value will be ignored and
@@ -762,21 +792,28 @@ async function getOverlayDatabaseMode(
762792
} else if (await features.getValue(Feature.OverlayAnalysis, codeql)) {
763793
if (isAnalyzingPullRequest()) {
764794
overlayDatabaseMode = OverlayDatabaseMode.Overlay;
795+
useOverlayDatabaseCaching = true;
765796
logger.info(
766797
`Setting overlay database mode to ${overlayDatabaseMode} ` +
767-
"because we are analyzing a pull request.",
798+
"with caching because we are analyzing a pull request.",
768799
);
769800
} else if (await isAnalyzingDefaultBranch()) {
770801
overlayDatabaseMode = OverlayDatabaseMode.OverlayBase;
802+
useOverlayDatabaseCaching = true;
771803
logger.info(
772804
`Setting overlay database mode to ${overlayDatabaseMode} ` +
773-
"because we are analyzing the default branch.",
805+
"with caching because we are analyzing the default branch.",
774806
);
775807
}
776808
}
777809

810+
const nonOverlayAnalysis = {
811+
overlayDatabaseMode: OverlayDatabaseMode.None,
812+
useOverlayDatabaseCaching: false,
813+
};
814+
778815
if (overlayDatabaseMode === OverlayDatabaseMode.None) {
779-
return OverlayDatabaseMode.None;
816+
return nonOverlayAnalysis;
780817
}
781818

782819
if (buildMode !== BuildMode.None) {
@@ -785,26 +822,29 @@ async function getOverlayDatabaseMode(
785822
`build-mode is set to "${buildMode}" instead of "none". ` +
786823
"Falling back to creating a normal full database instead.",
787824
);
788-
return OverlayDatabaseMode.None;
825+
return nonOverlayAnalysis;
789826
}
790827
if (!(await codeQlVersionAtLeast(codeql, CODEQL_OVERLAY_MINIMUM_VERSION))) {
791828
logger.warning(
792829
`Cannot build an ${overlayDatabaseMode} database because ` +
793830
`the CodeQL CLI is older than ${CODEQL_OVERLAY_MINIMUM_VERSION}. ` +
794831
"Falling back to creating a normal full database instead.",
795832
);
796-
return OverlayDatabaseMode.None;
833+
return nonOverlayAnalysis;
797834
}
798835
if ((await getGitRoot(sourceRoot)) === undefined) {
799836
logger.warning(
800837
`Cannot build an ${overlayDatabaseMode} database because ` +
801838
`the source root "${sourceRoot}" is not inside a git repository. ` +
802839
"Falling back to creating a normal full database instead.",
803840
);
804-
return OverlayDatabaseMode.None;
841+
return nonOverlayAnalysis;
805842
}
806843

807-
return overlayDatabaseMode;
844+
return {
845+
overlayDatabaseMode,
846+
useOverlayDatabaseCaching,
847+
};
808848
}
809849

810850
/**

0 commit comments

Comments
 (0)