Avoid printing "reason unknown" for extract entries

This was happening when the main Gradle User Home entry was not saved due to
having an unchanged cache key.

Fixes #309
This commit is contained in:
Daz DeBoer 2022-06-06 14:57:03 -06:00
parent e88ed3e650
commit 7a15005377
No known key found for this signature in database
GPG key ID: DD6B9F0B06683D5D
3 changed files with 17 additions and 13 deletions

View file

@ -94,11 +94,18 @@ export class GradleStateCache {
async save(listener: CacheListener): Promise<void> {
const cacheKey = generateCacheKey(this.cacheName).key
const restoredCacheKey = core.getState(RESTORED_CACHE_KEY_KEY)
const entryListener = listener.entry(this.cacheDescription)
const gradleHomeEntryListener = listener.entry(this.cacheDescription)
if (restoredCacheKey && cacheKey === restoredCacheKey) {
core.info(`Cache hit occurred on the cache key ${cacheKey}, not saving cache.`)
entryListener.markUnchanged('cache key not changed')
for (const entryListener of listener.cacheEntries) {
if (entryListener === gradleHomeEntryListener) {
entryListener.markUnsaved('cache key not changed')
} else {
entryListener.markUnsaved(`referencing '${this.cacheDescription}' cache entry not saved`)
}
}
return
}
@ -111,7 +118,7 @@ export class GradleStateCache {
core.info(`Caching ${this.cacheDescription} with cache key: ${cacheKey}`)
const cachePath = this.getCachePath()
await saveCache(cachePath, cacheKey, entryListener)
await saveCache(cachePath, cacheKey, gradleHomeEntryListener)
return
}

View file

@ -213,7 +213,7 @@ abstract class AbstractEntryExtractor {
if (previouslyRestoredKey === cacheKey) {
cacheDebug(`No change to previously restored ${artifactType}. Not saving.`)
entryListener.markUnchanged('contents unchanged')
entryListener.markUnsaved('contents unchanged')
} else {
core.info(`Caching ${artifactType} with path '${pattern}' and cache key: ${cacheKey}`)
await saveCache([pattern], cacheKey, entryListener)

View file

@ -64,7 +64,7 @@ export class CacheEntryListener {
savedKey: string | undefined
savedSize: number | undefined
unchanged: string | undefined
unsaved: string | undefined
constructor(entryName: string) {
this.entryName = entryName
@ -98,8 +98,8 @@ export class CacheEntryListener {
return this
}
markUnchanged(message: string): CacheEntryListener {
this.unchanged = message
markUnsaved(message: string): CacheEntryListener {
this.unsaved = message
return this
}
}
@ -159,8 +159,8 @@ function getRestoredMessage(entry: CacheEntryListener, isCacheWriteOnly: boolean
}
function getSavedMessage(entry: CacheEntryListener, isCacheReadOnly: boolean): string {
if (entry.unchanged) {
return `(Entry not saved: ${entry.unchanged})`
if (entry.unsaved) {
return `(Entry not saved: ${entry.unsaved})`
}
if (entry.savedKey === undefined) {
if (isCacheReadOnly) {
@ -190,11 +190,8 @@ function getSize(
}
function formatSize(bytes: number | undefined): string {
if (bytes === undefined) {
if (bytes === undefined || bytes === 0) {
return ''
}
if (bytes === 0) {
return '0 (Entry already exists)'
}
return `${Math.round(bytes / (1024 * 1024))} MB (${bytes} B)`
}