Skip to content

Commit 6a51cdc

Browse files
committed
Download overlay-base database from actions cache
1 parent 321b161 commit 6a51cdc

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/init-action.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ import {
4242
} from "./init";
4343
import { Language } from "./languages";
4444
import { getActionsLogger, Logger } from "./logging";
45-
import { OverlayDatabaseMode } from "./overlay-database-utils";
45+
import {
46+
downloadOverlayBaseDatabaseFromCache,
47+
OverlayDatabaseMode,
48+
} from "./overlay-database-utils";
4649
import { getRepositoryNwo } from "./repository";
4750
import { ToolsSource } from "./setup-codeql";
4851
import {
@@ -400,6 +403,34 @@ async function run() {
400403
}
401404

402405
try {
406+
if (
407+
config.augmentationProperties.overlayDatabaseMode ===
408+
OverlayDatabaseMode.Overlay &&
409+
config.augmentationProperties.useOverlayDatabaseCaching
410+
) {
411+
// OverlayDatabaseMode.Overlay comes in two flavors: with database
412+
// caching, or without. The flavor with database caching is intended to be
413+
// an "automatic control" mode, which is supposed to be fail-safe. If we
414+
// cannot download an overlay-base database, we revert to
415+
// OverlayDatabaseMode.None so that the workflow can continue to run.
416+
//
417+
// The flavor without database caching is intended to be a "manual
418+
// control" mode, where the workflow is supposed to make all the
419+
// necessary preparations. So, in that mode, we would assume that
420+
// everything is in order and let the analysis fail if that turns out not
421+
// to be the case.
422+
const overlayDatabaseDownloaded =
423+
await downloadOverlayBaseDatabaseFromCache(codeql, config, logger);
424+
if (!overlayDatabaseDownloaded) {
425+
config.augmentationProperties.overlayDatabaseMode =
426+
OverlayDatabaseMode.None;
427+
logger.info(
428+
"No overlay-base database found in cache, " +
429+
`reverting overlay database mode to ${OverlayDatabaseMode.None}.`,
430+
);
431+
}
432+
}
433+
403434
if (
404435
config.augmentationProperties.overlayDatabaseMode !==
405436
OverlayDatabaseMode.Overlay

src/overlay-database-utils.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,88 @@ export async function uploadOverlayBaseDatabaseToCache(
230230
return true;
231231
}
232232

233+
/**
234+
* Downloads the overlay-base database from the GitHub Actions cache. If conditions
235+
* for downloading are not met, the function does nothing and returns false.
236+
*
237+
* @param codeql The CodeQL instance
238+
* @param config The configuration object
239+
* @param logger The logger instance
240+
* @returns A promise that resolves to true if the download was performed and
241+
* successfully completed, or false otherwise
242+
*/
243+
export async function downloadOverlayBaseDatabaseFromCache(
244+
codeql: CodeQL,
245+
config: Config,
246+
logger: Logger,
247+
): Promise<boolean> {
248+
const overlayDatabaseMode = config.augmentationProperties.overlayDatabaseMode;
249+
if (overlayDatabaseMode !== OverlayDatabaseMode.Overlay) {
250+
logger.debug(
251+
`Overlay database mode is ${overlayDatabaseMode}. ` +
252+
"Skip downloading overlay-base database from cache.",
253+
);
254+
return false;
255+
}
256+
if (!config.augmentationProperties.useOverlayDatabaseCaching) {
257+
logger.debug(
258+
"Overlay database caching is disabled. " +
259+
"Skip downloading overlay-base database from cache.",
260+
);
261+
return false;
262+
}
263+
if (isInTestMode()) {
264+
logger.debug(
265+
"In test mode. Skip downloading overlay-base database from cache.",
266+
);
267+
return false;
268+
}
269+
270+
const dbLocation = config.dbLocation;
271+
const codeQlVersion = (await codeql.getVersion()).version;
272+
const restoreKey = getCacheRestoreKey(codeQlVersion);
273+
274+
logger.info(
275+
`Looking in Actions cache for overlay-base database with restore key ${restoreKey}`,
276+
);
277+
278+
try {
279+
const foundKey = await withTimeout(
280+
MAX_CACHE_OPERATION_MS,
281+
actionsCache.restoreCache([dbLocation], restoreKey),
282+
() => {
283+
logger.info("Timed out downloading overlay-base database from cache");
284+
},
285+
);
286+
287+
if (foundKey === undefined) {
288+
logger.info("No overlay-base database found in Actions cache");
289+
return false;
290+
}
291+
292+
logger.info(`Downloaded overlay-base database in cache with key ${foundKey}`);
293+
} catch (error) {
294+
logger.warning(
295+
"Failed to download overlay-base database from cache: " +
296+
`${error instanceof Error ? error.message : String(error)}`,
297+
);
298+
return false;
299+
}
300+
301+
const databaseIsValid = checkOverlayBaseDatabase(
302+
config,
303+
logger,
304+
"Downloaded overlay-base database is invalid",
305+
);
306+
if (!databaseIsValid) {
307+
logger.warning("Downloaded overlay-base database failed validation");
308+
return false;
309+
}
310+
311+
logger.info(`Successfully downloaded overlay-base database to ${dbLocation}`);
312+
return true;
313+
}
314+
233315
function generateCacheKey(codeQlVersion: string): string {
234316
const sha = process.env.GITHUB_SHA || "unknown";
235317
return `${getCacheRestoreKey(codeQlVersion)}${sha}`;

0 commit comments

Comments
 (0)