@@ -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+
233315function generateCacheKey ( codeQlVersion : string ) : string {
234316 const sha = process . env . GITHUB_SHA || "unknown" ;
235317 return `${ getCacheRestoreKey ( codeQlVersion ) } ${ sha } ` ;
0 commit comments