Skip to content

Commit 030a42a

Browse files
committed
Download overlay-base database from actions cache
1 parent e787bc7 commit 030a42a

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-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 {
@@ -395,6 +398,34 @@ async function run() {
395398
}
396399

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

src/overlay-database-utils.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,90 @@ export async function uploadOverlayBaseDatabaseToCache(
231231
return true;
232232
}
233233

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

0 commit comments

Comments
 (0)