From 57f3f23714db016f61164a03dd4ca83a4befd039 Mon Sep 17 00:00:00 2001 From: daz Date: Fri, 22 Dec 2023 13:54:10 -0700 Subject: [PATCH] Improve non-restore behavior for config-cache - Avoid logging "not restoring" message when no entries exist to restore - Clear the entries from metadata when they are not restored. This ensures that the non-restored entries are correctly purged. --- src/cache-extract-entries.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/cache-extract-entries.ts b/src/cache-extract-entries.ts index 27731ad..adbccb0 100644 --- a/src/cache-extract-entries.ts +++ b/src/cache-extract-entries.ts @@ -257,7 +257,7 @@ abstract class AbstractEntryExtractor { } const filedata = fs.readFileSync(cacheMetadataFile, 'utf-8') - cacheDebug(`Loaded cache metadata: ${filedata}`) + cacheDebug(`Loaded cache metadata for ${this.extractorName}: ${filedata}`) const extractedCacheEntryMetadata = JSON.parse(filedata) as ExtractedCacheEntryMetadata return extractedCacheEntryMetadata.entries } @@ -265,12 +265,12 @@ abstract class AbstractEntryExtractor { /** * Saves information about the extracted cache entries into the 'cache-metadata.json' file. */ - private saveMetadataForCacheResults(results: ExtractedCacheEntry[]): void { + protected saveMetadataForCacheResults(results: ExtractedCacheEntry[]): void { const extractedCacheEntryMetadata = new ExtractedCacheEntryMetadata() extractedCacheEntryMetadata.entries = results.filter(x => x.cacheKey !== undefined) const filedata = JSON.stringify(extractedCacheEntryMetadata) - cacheDebug(`Saving cache metadata: ${filedata}`) + cacheDebug(`Saving cache metadata for ${this.extractorName}: ${filedata}`) fs.writeFileSync(this.getCacheMetadataFile(), filedata, 'utf-8') } @@ -353,18 +353,12 @@ export class ConfigurationCacheEntryExtractor extends AbstractEntryExtractor { */ async restore(listener: CacheListener): Promise { if (!listener.fullyRestored) { - core.info('Not restoring configuration-cache state, as Gradle User Home was not fully restored') - for (const cacheEntry of this.loadExtractedCacheEntries()) { - listener.entry(cacheEntry.pattern).markNotRestored('Gradle User Home not fully restored') - } + this.markNotRestored(listener, 'Gradle User Home was not fully restored') return } if (!params.getCacheEncryptionKey()) { - core.info('Not restoring configuration-cache state, as no encryption key was provided') - for (const cacheEntry of this.loadExtractedCacheEntries()) { - listener.entry(cacheEntry.pattern).markNotRestored('No encryption key provided') - } + this.markNotRestored(listener, 'Encryption Key was not provided') return } @@ -373,6 +367,19 @@ export class ConfigurationCacheEntryExtractor extends AbstractEntryExtractor { return await super.restore(listener) } + private markNotRestored(listener: CacheListener, reason: string): void { + const cacheEntries = this.loadExtractedCacheEntries() + if (cacheEntries.length > 0) { + core.info(`Not restoring configuration-cache state, as ${reason}`) + for (const cacheEntry of cacheEntries) { + listener.entry(cacheEntry.pattern).markNotRestored(reason).markNotSaved(reason) + } + + // Update the results file based on no entries restored + this.saveMetadataForCacheResults([]) + } + } + async extract(listener: CacheListener): Promise { if (!params.getCacheEncryptionKey()) { core.info('Not saving configuration-cache state, as no encryption key was provided')