Unverified Commit 75cec40e authored by Daz DeBoer's avatar Daz DeBoer
Browse files

Rename 'report' classes to 'listener'

- `CachingReport` -> `CacheListener`
- `CacheEntryReport` -> `CacheEntryListener`
parent c317ccac
Loading
Loading
Loading
Loading
+36 −36
Changes for __tests__/cache-base.test.ts: 36 added lines, 36 removed lines.
Original line number Diff line number Diff line
import {CacheEntryReport, CachingReport} from '../src/cache-base'
import {CacheEntryListener, CacheListener} from '../src/cache-base'

describe('caching report', () => {
    describe('reports not fully restored', () => {
        it('with one requested entry report', async () => {
            const report = new CachingReport()
            report.entryReport('foo').markRequested('1', ['2'])
            report.entryReport('bar').markRequested('3').markRestored('4')
            const report = new CacheListener()
            report.entry('foo').markRequested('1', ['2'])
            report.entry('bar').markRequested('3').markRestored('4')
            expect(report.fullyRestored).toBe(false)
        })
    })
    describe('reports fully restored', () => {
        it('when empty', async () => {
            const report = new CachingReport()
            const report = new CacheListener()
            expect(report.fullyRestored).toBe(true)
        })
        it('with empty entry reports', async () => {
            const report = new CachingReport()
            report.entryReport('foo')
            report.entryReport('bar')
            const report = new CacheListener()
            report.entry('foo')
            report.entry('bar')
            expect(report.fullyRestored).toBe(true)
        })
        it('with restored entry report', async () => {
            const report = new CachingReport()
            report.entryReport('bar').markRequested('3').markRestored('4')
            const report = new CacheListener()
            report.entry('bar').markRequested('3').markRestored('4')
            expect(report.fullyRestored).toBe(true)
        })
        it('with multiple restored entry reportss', async () => {
            const report = new CachingReport()
            report.entryReport('foo').markRestored('4')
            report.entryReport('bar').markRequested('3').markRestored('4')
            const report = new CacheListener()
            report.entry('foo').markRestored('4')
            report.entry('bar').markRequested('3').markRestored('4')
            expect(report.fullyRestored).toBe(true)
        })
    })
    describe('can be stringified and rehydrated', () => {
        it('when empty', async () => {
            const report = new CachingReport()
            const report = new CacheListener()

            const stringRep = report.stringify()
            const reportClone: CachingReport = CachingReport.rehydrate(stringRep)
            const reportClone: CacheListener = CacheListener.rehydrate(stringRep)

            expect(reportClone.cacheEntryReports).toEqual([])
            expect(reportClone.cacheEntries).toEqual([])

            // Can call methods on rehydrated
            expect(reportClone.entryReport('foo')).toBeInstanceOf(CacheEntryReport)
            expect(reportClone.entry('foo')).toBeInstanceOf(CacheEntryListener)
        })
        it('with entry reports', async () => {
            const report = new CachingReport()
            report.entryReport('foo')
            report.entryReport('bar')
            report.entryReport('baz')
            const report = new CacheListener()
            report.entry('foo')
            report.entry('bar')
            report.entry('baz')

            const stringRep = report.stringify()
            const reportClone: CachingReport = CachingReport.rehydrate(stringRep)
            const reportClone: CacheListener = CacheListener.rehydrate(stringRep)

            expect(reportClone.cacheEntryReports.length).toBe(3)
            expect(reportClone.cacheEntryReports[0].entryName).toBe('foo')
            expect(reportClone.cacheEntryReports[1].entryName).toBe('bar')
            expect(reportClone.cacheEntryReports[2].entryName).toBe('baz')
            expect(reportClone.cacheEntries.length).toBe(3)
            expect(reportClone.cacheEntries[0].entryName).toBe('foo')
            expect(reportClone.cacheEntries[1].entryName).toBe('bar')
            expect(reportClone.cacheEntries[2].entryName).toBe('baz')

            expect(reportClone.entryReport('foo')).toBe(reportClone.cacheEntryReports[0])
            expect(reportClone.entry('foo')).toBe(reportClone.cacheEntries[0])
        })
        it('with rehydrated entry report', async () => {
            const report = new CachingReport()
            const entryReport = report.entryReport('foo')
            const report = new CacheListener()
            const entryReport = report.entry('foo')
            entryReport.markRequested('1', ['2', '3'])
            entryReport.markSaved('4')

            const stringRep = report.stringify()
            const reportClone: CachingReport = CachingReport.rehydrate(stringRep)
            const entryClone = reportClone.entryReport('foo')
            const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
            const entryClone = reportClone.entry('foo')

            expect(entryClone.requestedKey).toBe('1')
            expect(entryClone.requestedRestoreKeys).toEqual(['2', '3'])
            expect(entryClone.savedKey).toBe('4')
        })
        it('with live entry report', async () => {
            const report = new CachingReport()
            const entryReport = report.entryReport('foo')
            const report = new CacheListener()
            const entryReport = report.entry('foo')
            entryReport.markRequested('1', ['2', '3'])

            const stringRep = report.stringify()
            const reportClone: CachingReport = CachingReport.rehydrate(stringRep)
            const entryClone = reportClone.entryReport('foo')
            const reportClone: CacheListener = CacheListener.rehydrate(stringRep)
            const entryClone = reportClone.entry('foo')

            // Check type and call method on rehydrated entry report
            expect(entryClone).toBeInstanceOf(CacheEntryReport)
            expect(entryClone).toBeInstanceOf(CacheEntryListener)
            entryClone.markSaved('4')

            expect(entryClone.requestedKey).toBe('1')
+28 −28
Changes for src/cache-base.ts: 28 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -40,41 +40,41 @@ class CacheKey {
    }
}

export class CachingReport {
    cacheEntryReports: CacheEntryReport[] = []
export class CacheListener {
    cacheEntries: CacheEntryListener[] = []

    get fullyRestored(): boolean {
        return this.cacheEntryReports.every(x => !x.wasRequestedButNotRestored())
        return this.cacheEntries.every(x => !x.wasRequestedButNotRestored())
    }

    entryReport(name: string): CacheEntryReport {
        for (const report of this.cacheEntryReports) {
            if (report.entryName === name) {
                return report
    entry(name: string): CacheEntryListener {
        for (const entry of this.cacheEntries) {
            if (entry.entryName === name) {
                return entry
            }
        }

        const newReport = new CacheEntryReport(name)
        this.cacheEntryReports.push(newReport)
        return newReport
        const newEntry = new CacheEntryListener(name)
        this.cacheEntries.push(newEntry)
        return newEntry
    }

    stringify(): string {
        return JSON.stringify(this)
    }

    static rehydrate(stringRep: string): CachingReport {
        const rehydrated: CachingReport = Object.assign(new CachingReport(), JSON.parse(stringRep))
        const entryReports = rehydrated.cacheEntryReports
        for (let index = 0; index < entryReports.length; index++) {
            const rawEntryReport = entryReports[index]
            entryReports[index] = Object.assign(new CacheEntryReport(rawEntryReport.entryName), rawEntryReport)
    static rehydrate(stringRep: string): CacheListener {
        const rehydrated: CacheListener = Object.assign(new CacheListener(), JSON.parse(stringRep))
        const entries = rehydrated.cacheEntries
        for (let index = 0; index < entries.length; index++) {
            const rawEntry = entries[index]
            entries[index] = Object.assign(new CacheEntryListener(rawEntry.entryName), rawEntry)
        }
        return rehydrated
    }
}

export class CacheEntryReport {
export class CacheEntryListener {
    entryName: string
    requestedKey: string | undefined
    requestedRestoreKeys: string[] | undefined
@@ -92,18 +92,18 @@ export class CacheEntryReport {
        return this.requestedKey !== undefined && this.restoredKey === undefined
    }

    markRequested(key: string, restoreKeys: string[] = []): CacheEntryReport {
    markRequested(key: string, restoreKeys: string[] = []): CacheEntryListener {
        this.requestedKey = key
        this.requestedRestoreKeys = restoreKeys
        return this
    }

    markRestored(key: string): CacheEntryReport {
    markRestored(key: string): CacheEntryListener {
        this.restoredKey = key
        return this
    }

    markSaved(key: string): CacheEntryReport {
    markSaved(key: string): CacheEntryListener {
        this.savedKey = key
        return this
    }
@@ -125,14 +125,14 @@ export abstract class AbstractCache {
        this.cacheDebuggingEnabled = isCacheDebuggingEnabled()
    }

    async restore(report: CachingReport): Promise<void> {
    async restore(listener: CacheListener): Promise<void> {
        if (this.cacheOutputExists()) {
            core.info(`${this.cacheDescription} already exists. Not restoring from cache.`)
            return
        }

        const cacheKey = this.prepareCacheKey()
        const entryReport = report.entryReport(this.cacheName)
        const entryReport = listener.entry(this.cacheName)
        entryReport.markRequested(cacheKey.key, cacheKey.restoreKeys)

        this.debug(
@@ -153,7 +153,7 @@ export abstract class AbstractCache {
        core.info(`Restored ${this.cacheDescription} from cache key: ${cacheResult}`)

        try {
            await this.afterRestore(report)
            await this.afterRestore(listener)
        } catch (error) {
            core.warning(`Restore ${this.cacheDescription} failed in 'afterRestore': ${error}`)
        }
@@ -184,9 +184,9 @@ export abstract class AbstractCache {
        }
    }

    protected async afterRestore(_report: CachingReport): Promise<void> {}
    protected async afterRestore(_listener: CacheListener): Promise<void> {}

    async save(report: CachingReport): Promise<void> {
    async save(listener: CacheListener): Promise<void> {
        if (!this.cacheOutputExists()) {
            this.debug(`No ${this.cacheDescription} to cache.`)
            return
@@ -206,7 +206,7 @@ export abstract class AbstractCache {
        }

        try {
            await this.beforeSave(report)
            await this.beforeSave(listener)
        } catch (error) {
            core.warning(`Save ${this.cacheDescription} failed in 'beforeSave': ${error}`)
            return
@@ -216,12 +216,12 @@ export abstract class AbstractCache {
        const cachePath = this.getCachePath()
        await this.saveCache(cachePath, cacheKey)

        report.entryReport(this.cacheName).markSaved(cacheKey)
        listener.entry(this.cacheName).markSaved(cacheKey)

        return
    }

    protected async beforeSave(_report: CachingReport): Promise<void> {}
    protected async beforeSave(_listener: CacheListener): Promise<void> {}

    protected async saveCache(cachePath: string[], cacheKey: string): Promise<void> {
        try {
+21 −17
Changes for src/cache-gradle-user-home.ts: 21 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ import * as core from '@actions/core'
import * as glob from '@actions/glob'
import * as exec from '@actions/exec'

import {AbstractCache, CacheEntryReport, CachingReport} from './cache-base'
import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base'
import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils'

const META_FILE_DIR = '.gradle-build-action'
@@ -22,13 +22,13 @@ export class GradleUserHomeCache extends AbstractCache {
        this.gradleUserHome = this.determineGradleUserHome(rootDir)
    }

    async afterRestore(report: CachingReport): Promise<void> {
    async afterRestore(listener: CacheListener): Promise<void> {
        await this.reportGradleUserHomeSize('as restored from cache')
        await this.restoreArtifactBundles(report)
        await this.restoreArtifactBundles(listener)
        await this.reportGradleUserHomeSize('after restoring common artifacts')
    }

    private async restoreArtifactBundles(report: CachingReport): Promise<void> {
    private async restoreArtifactBundles(listener: CacheListener): Promise<void> {
        const processes: Promise<void>[] = []

        const bundleMetaFiles = await this.getBundleMetaFiles()
@@ -37,16 +37,16 @@ export class GradleUserHomeCache extends AbstractCache {
        // Iterate over all bundle meta files and try to restore
        for (const bundleMetaFile of bundleMetaFiles) {
            const bundle = path.basename(bundleMetaFile, '.cache')
            const bundleEntryReport = report.entryReport(bundle)
            const entryListener = listener.entry(bundle)
            const bundlePattern = bundlePatterns.get(bundle)

            // Handle case where the 'artifactBundlePatterns' have been changed
            if (bundlePattern === undefined) {
                core.info(`Found bundle metafile for ${bundle} but no such bundle defined`)
                bundleEntryReport.markRequested('BUNDLE_NOT_CONFIGURED')
                entryListener.markRequested('BUNDLE_NOT_CONFIGURED')
                tryDelete(bundleMetaFile)
            } else {
                const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, bundleEntryReport)
                const p = this.restoreArtifactBundle(bundle, bundlePattern, bundleMetaFile, entryListener)
                // Run sequentially when debugging enabled
                if (this.cacheDebuggingEnabled) {
                    await p
@@ -62,15 +62,15 @@ export class GradleUserHomeCache extends AbstractCache {
        bundle: string,
        bundlePattern: string,
        bundleMetaFile: string,
        report: CacheEntryReport
        listener: CacheEntryListener
    ): Promise<void> {
        const cacheKey = fs.readFileSync(bundleMetaFile, 'utf-8').trim()
        report.markRequested(cacheKey)
        listener.markRequested(cacheKey)

        const restoredKey = await this.restoreCache([bundlePattern], cacheKey)
        if (restoredKey) {
            core.info(`Restored ${bundle} with key ${cacheKey} to ${bundlePattern}`)
            report.markRestored(restoredKey)
            listener.markRestored(restoredKey)
        } else {
            core.info(`Did not restore ${bundle} with key ${cacheKey} to ${bundlePattern}`)
            tryDelete(bundleMetaFile)
@@ -88,10 +88,10 @@ export class GradleUserHomeCache extends AbstractCache {
        return bundleFiles
    }

    async beforeSave(report: CachingReport): Promise<void> {
    async beforeSave(listener: CacheListener): Promise<void> {
        await this.reportGradleUserHomeSize('before saving common artifacts')
        this.removeExcludedPaths()
        await this.saveArtifactBundles(report)
        await this.saveArtifactBundles(listener)
        await this.reportGradleUserHomeSize(
            "after saving common artifacts (only 'caches' and 'notifications' will be stored)"
        )
@@ -107,12 +107,12 @@ export class GradleUserHomeCache extends AbstractCache {
        }
    }

    private async saveArtifactBundles(report: CachingReport): Promise<void> {
    private async saveArtifactBundles(listener: CacheListener): Promise<void> {
        const processes: Promise<void>[] = []
        for (const [bundle, pattern] of this.getArtifactBundles()) {
            const bundleEntryReport = report.entryReport(bundle)
            const entryListener = listener.entry(bundle)

            const p = this.saveArtifactBundle(bundle, pattern, bundleEntryReport)
            const p = this.saveArtifactBundle(bundle, pattern, entryListener)
            // Run sequentially when debugging enabled
            if (this.cacheDebuggingEnabled) {
                await p
@@ -123,7 +123,11 @@ export class GradleUserHomeCache extends AbstractCache {
        await Promise.all(processes)
    }

    private async saveArtifactBundle(bundle: string, artifactPath: string, report: CacheEntryReport): Promise<void> {
    private async saveArtifactBundle(
        bundle: string,
        artifactPath: string,
        listener: CacheEntryListener
    ): Promise<void> {
        const bundleMetaFile = this.getBundleMetaFile(bundle)

        const globber = await glob.create(artifactPath, {
@@ -152,7 +156,7 @@ export class GradleUserHomeCache extends AbstractCache {
            core.info(`Caching ${bundle} with cache key: ${cacheKey}`)
            await this.saveCache([artifactPath], cacheKey)
            this.writeBundleMetaFile(bundleMetaFile, cacheKey)
            report.markSaved(cacheKey)
            listener.markSaved(cacheKey)
        }

        for (const file of bundleFiles) {
+8 −8
Changes for src/caches.ts: 8 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ import {GradleUserHomeCache} from './cache-gradle-user-home'
import {ProjectDotGradleCache} from './cache-project-dot-gradle'
import * as core from '@actions/core'
import {isCacheDisabled, isCacheReadOnly} from './cache-utils'
import {CachingReport} from './cache-base'
import {CacheListener} from './cache-base'

const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR'
const CACHING_REPORT = 'CACHING_REPORT'
@@ -16,20 +16,20 @@ export async function restore(buildRootDirectory: string): Promise<void> {
    await core.group('Restore Gradle state from cache', async () => {
        core.saveState(BUILD_ROOT_DIR, buildRootDirectory)

        const cachingReport = new CachingReport()
        await new GradleUserHomeCache(buildRootDirectory).restore(cachingReport)
        const cacheListener = new CacheListener()
        await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener)

        const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory)
        if (cachingReport.fullyRestored) {
        if (cacheListener.fullyRestored) {
            // Only restore the configuration-cache if the Gradle Home is fully restored
            await projectDotGradleCache.restore(cachingReport)
            await projectDotGradleCache.restore(cacheListener)
        } else {
            // Otherwise, prepare the cache key for later save()
            core.info('Gradle Home cache not fully restored: not restoring configuration-cache state')
            projectDotGradleCache.prepareCacheKey()
        }

        core.saveState(CACHING_REPORT, cachingReport.stringify())
        core.saveState(CACHING_REPORT, cacheListener.stringify())
    })
}

@@ -39,7 +39,7 @@ export async function save(): Promise<void> {
        return
    }

    const cachingReport: CachingReport = CachingReport.rehydrate(core.getState(CACHING_REPORT))
    const cachingReport: CacheListener = CacheListener.rehydrate(core.getState(CACHING_REPORT))

    await core.group('Caching Gradle state', async () => {
        const buildRootDirectory = core.getState(BUILD_ROOT_DIR)
@@ -52,6 +52,6 @@ export async function save(): Promise<void> {
    logCachingReport(cachingReport)
}

function logCachingReport(report: CachingReport): void {
function logCachingReport(report: CacheListener): void {
    core.info(JSON.stringify(report, null, 2))
}