Skip to content

Commit

Permalink
Truncate Gradle args when constructing cache key (#71)
Browse files Browse the repository at this point in the history
Cache keys have a hard limit of 512 characters, so we need to ensure that we don't generate a key longer than this.

- Remove excess whitespace
- Truncate to 400 characters

Fixes #70
  • Loading branch information
bigdaz committed Aug 24, 2021
1 parent e0c2736 commit b3afdc7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
36 changes: 31 additions & 5 deletions __tests__/crypto-utils.test.ts → __tests__/cache-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as cryptoUtils from '../src/crypto-utils'
import * as cacheUtils from '../src/cache-utils'
import * as path from 'path'

describe('crypto-utils', () => {
describe('cacheUtils-utils', () => {
describe('can hash', () => {
it('a directory', async () => {
const hash = await cryptoUtils.hashFiles(
const hash = await cacheUtils.hashFiles(
path.resolve('__tests__/data/crypto-utils-test/gradle')
)
expect(hash).toBe(
Expand All @@ -14,7 +14,7 @@ describe('crypto-utils', () => {
)
})
it('a directory with a glob', async () => {
const hash = await cryptoUtils.hashFiles(
const hash = await cacheUtils.hashFiles(
path.resolve('__tests__/data/crypto-utils-test/'),
['gradle/**']
)
Expand All @@ -25,7 +25,7 @@ describe('crypto-utils', () => {
)
})
it('a directory with globs', async () => {
const hash = await cryptoUtils.hashFiles(
const hash = await cacheUtils.hashFiles(
path.resolve('__tests__/data/crypto-utils-test/'),
['**/*.gradle', 'gradle/**']
)
Expand All @@ -36,4 +36,30 @@ describe('crypto-utils', () => {
)
})
})
describe('can truncate args', () => {
test('handles zero-length string', () => {
expect(cacheUtils.truncateArgs('')).toBe('')
})
test('leaves short string untouched', () => {
expect(
cacheUtils.truncateArgs('short string that-should-be-untouched')
).toBe('short string that-should-be-untouched')
})
test('truncates long string', () => {
const longString = 'a'.repeat(500)
expect(cacheUtils.truncateArgs(longString)).toBe('a'.repeat(400))
})
test('trims leading and trailing whitespace', () => {
expect(cacheUtils.truncateArgs(' this is an arg ')).toBe(
'this is an arg'
)
})
test('removes repeated whitespace', () => {
expect(
cacheUtils.truncateArgs(
' this one has long \t\n\t\r spaces '
)
).toBe('this one has long spaces')
})
})
})
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/cache-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'fs'
import * as core from '@actions/core'
import * as cache from '@actions/cache'

import * as crypto from './crypto-utils'
import * as cacheUtils from './cache-utils'

import {
inputCacheKeyGlobs,
Expand Down Expand Up @@ -35,10 +35,11 @@ export async function restoreCachedConfiguration(
const cacheKeyPrefix = 'configuration|'

const args = core.getInput('arguments')
const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|`
const argsKey = cacheUtils.truncateArgs(args)
const cacheKeyWithArgs = `${cacheKeyPrefix}${argsKey}|`

const cacheKeyGlobs = inputCacheKeyGlobs('configuration-cache-key')
const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
const hash = await cacheUtils.hashFiles(rootDir, cacheKeyGlobs)
const cacheKey = `${cacheKeyWithArgs}${hash}`

core.saveState(CONFIGURATION_CACHE_KEY, cacheKey)
Expand Down
7 changes: 4 additions & 3 deletions src/cache-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as os from 'os'
import * as core from '@actions/core'
import * as cache from '@actions/cache'

import * as crypto from './crypto-utils'
import * as cacheUtils from './cache-utils'

const DEPENDENCIES_CACHE_PATH = 'DEPENDENCIES_CACHE_PATH'
const DEPENDENCIES_CACHE_KEY = 'DEPENDENCIES_CACHE_KEY'
Expand All @@ -24,10 +24,11 @@ export async function restoreCachedDependencies(
const cacheKeyPrefix = 'dependencies|'

const args = core.getInput('arguments')
const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|`
const argsKey = cacheUtils.truncateArgs(args)
const cacheKeyWithArgs = `${cacheKeyPrefix}${argsKey}|`

const cacheKeyGlobs = inputCacheKeyGlobs('dependencies-cache-key')
const hash = await crypto.hashFiles(rootDir, cacheKeyGlobs)
const hash = await cacheUtils.hashFiles(rootDir, cacheKeyGlobs)
const cacheKey = `${cacheKeyWithArgs}${hash}`

core.saveState(DEPENDENCIES_CACHE_KEY, cacheKey)
Expand Down
4 changes: 4 additions & 0 deletions src/crypto-utils.ts → src/cache-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export async function hashFiles(
.join('\n')
return glob.hashFiles(combinedPatterns, {followSymbolicLinks})
}

export function truncateArgs(args: string): string {
return args.trim().replace(/\s+/g, ' ').substr(0, 400)
}

0 comments on commit b3afdc7

Please sign in to comment.