@@ -230,6 +230,90 @@ 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 (
293+ `Downloaded overlay-base database in cache with key ${ foundKey } ` ,
294+ ) ;
295+ } catch ( error ) {
296+ logger . warning (
297+ "Failed to download overlay-base database from cache: " +
298+ `${ error instanceof Error ? error . message : String ( error ) } ` ,
299+ ) ;
300+ return false ;
301+ }
302+
303+ const databaseIsValid = checkOverlayBaseDatabase (
304+ config ,
305+ logger ,
306+ "Downloaded overlay-base database is invalid" ,
307+ ) ;
308+ if ( ! databaseIsValid ) {
309+ logger . warning ( "Downloaded overlay-base database failed validation" ) ;
310+ return false ;
311+ }
312+
313+ logger . info ( `Successfully downloaded overlay-base database to ${ dbLocation } ` ) ;
314+ return true ;
315+ }
316+
233317function generateCacheKey ( codeQlVersion : string ) : string {
234318 const sha = process . env . GITHUB_SHA || "unknown" ;
235319 return `${ getCacheRestoreKey ( codeQlVersion ) } ${ sha } ` ;
0 commit comments