From d7ed6d7e8dc62d7369659aea23efd014b36bff7e Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 5 Sep 2021 17:10:47 -0600 Subject: [PATCH] Extract cache key generation into common function --- src/cache-gradle-user-home.ts | 25 +++++++++---------------- src/cache-project-dot-gradle.ts | 20 ++++++-------------- src/cache-utils.ts | 31 +++++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index cf21b9f..86d8fb0 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -4,11 +4,10 @@ import os from 'os' import * as core from '@actions/core' import * as cache from '@actions/cache' -import * as github from '@actions/github' import { + generateCacheKey, isCacheReadEnabled, - isCacheSaveEnabled, - truncateArgs + isCacheSaveEnabled } from './cache-utils' const CACHE_NAME = 'gradle-user-home' @@ -28,21 +27,15 @@ export async function restore(): Promise { return } - const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || '' - const runnerOs = process.env[`RUNNER_OS`] || '' - const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|gradle|` + const cacheKey = generateCacheKey('gradle') - const args = truncateArgs(core.getInput('arguments')) - const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|` + core.saveState(CACHE_KEY, cacheKey.key) - const cacheKey = `${cacheKeyWithArgs}${github.context.sha}` - - core.saveState(CACHE_KEY, cacheKey) - - const cacheResult = await cache.restoreCache(CACHE_PATH, cacheKey, [ - cacheKeyWithArgs, - cacheKeyPrefix - ]) + const cacheResult = await cache.restoreCache( + CACHE_PATH, + cacheKey.key, + cacheKey.restoreKeys + ) if (!cacheResult) { core.info( diff --git a/src/cache-project-dot-gradle.ts b/src/cache-project-dot-gradle.ts index 732d92f..e05dc79 100644 --- a/src/cache-project-dot-gradle.ts +++ b/src/cache-project-dot-gradle.ts @@ -3,11 +3,10 @@ import fs from 'fs' import * as core from '@actions/core' import * as cache from '@actions/cache' -import * as github from '@actions/github' import { + generateCacheKey, isCacheReadEnabled, - isCacheSaveEnabled, - truncateArgs + isCacheSaveEnabled } from './cache-utils' const CACHE_NAME = 'project-dot-gradle' @@ -27,21 +26,14 @@ export async function restore(rootDir: string): Promise { return } - const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || '' - const runnerOs = process.env[`RUNNER_OS`] || '' - const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|project|` + const cacheKey = generateCacheKey('project') - const args = truncateArgs(core.getInput('arguments')) - const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|` - - const cacheKey = `${cacheKeyWithArgs}${github.context.sha}` - - core.saveState(CACHE_KEY, cacheKey) + core.saveState(CACHE_KEY, cacheKey.key) const cacheResult = await cache.restoreCache( getCachePath(rootDir), - cacheKey, - [cacheKeyWithArgs, cacheKeyPrefix] + cacheKey.key, + cacheKey.restoreKeys ) if (!cacheResult) { diff --git a/src/cache-utils.ts b/src/cache-utils.ts index 4f78055..b5dcfe6 100644 --- a/src/cache-utils.ts +++ b/src/cache-utils.ts @@ -1,8 +1,5 @@ import * as core from '@actions/core' - -export function truncateArgs(args: string): string { - return args.trim().replace(/\s+/g, ' ').substr(0, 400) -} +import * as github from '@actions/github' export function isCacheReadEnabled(cacheName: string): boolean { const configValue = getCacheEnabledValue(cacheName) @@ -26,3 +23,29 @@ function getCacheEnabledValue(cacheName: string): string { `Invalid cache-enabled parameter '${configValue}'. Valid values are ['true', 'false', 'read-only']` ) } + +export function generateCacheKey(cacheName: string): CacheKey { + const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || '' + const runnerOs = process.env[`RUNNER_OS`] || '' + const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|${cacheName}|` + + const args = truncateArgs(core.getInput('arguments')) + const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|` + + const cacheKey = `${cacheKeyWithArgs}${github.context.sha}` + return new CacheKey(cacheKey, [cacheKeyWithArgs, cacheKeyPrefix]) +} + +function truncateArgs(args: string): string { + return args.trim().replace(/\s+/g, ' ').substr(0, 400) +} + +export class CacheKey { + key: string + restoreKeys: string[] + + constructor(key: string, restoreKeys: string[]) { + this.key = key + this.restoreKeys = restoreKeys + } +}