Skip to content

Commit dbb7ebc

Browse files
authored
feat: add option to control cache invalidation interval (#1031)
1 parent ecb3292 commit dbb7ebc

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ with:
195195
# ...
196196
```
197197

198+
### `cache-invalidation-interval`
199+
200+
(optional)
201+
202+
Periodically invalidate the cache every `cache-invalidation-interval` days to ensure that outdated data is removed and fresh data is loaded.
203+
204+
The default value is `7`.
205+
206+
```yml
207+
uses: golangci/golangci-lint-action@v5
208+
with:
209+
cache-invalidation-interval: 15
210+
# ...
211+
```
212+
213+
If set the number is `<= 0`, the cache will be always invalidate (Not recommended).
214+
198215
### `annotations`
199216

200217
(optional)

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ inputs:
4444
description: "golangci-lint command line arguments"
4545
default: ""
4646
required: false
47+
cache-invalidation-interval:
48+
description: "Periodically invalidate a cache because a new code being added. (number of days)"
49+
default: '7'
50+
required: false
4751
runs:
4852
using: "node20"
4953
main: "dist/run/index.js"

dist/post_run/index.js

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/run/index.js

+5-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,40 @@ const getLintCacheDir = (): string => {
2525

2626
const getIntervalKey = (invalidationIntervalDays: number): string => {
2727
const now = new Date()
28+
29+
if (invalidationIntervalDays <= 0) {
30+
return `${now.getTime()}`
31+
}
32+
2833
const secondsSinceEpoch = now.getTime() / 1000
2934
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400))
3035
return intervalNumber.toString()
3136
}
3237

3338
async function buildCacheKeys(): Promise<string[]> {
3439
const keys = []
40+
41+
const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim())
42+
3543
// Periodically invalidate a cache because a new code being added.
36-
// TODO: configure it via inputs.
37-
let cacheKey = `golangci-lint.cache-${getIntervalKey(7)}-`
44+
let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-`
3845
keys.push(cacheKey)
46+
3947
// Get working directory from input
4048
const workingDirectory = core.getInput(`working-directory`)
49+
4150
// create path to go.mod prepending the workingDirectory if it exists
4251
const goModPath = path.join(workingDirectory, `go.mod`)
52+
4353
core.info(`Checking for go.mod: ${goModPath}`)
54+
4455
if (await pathExists(goModPath)) {
4556
// Add checksum to key to invalidate a cache when dependencies change.
4657
cacheKey += await checksumFile(`sha1`, goModPath)
4758
} else {
4859
cacheKey += `nogomod`
4960
}
61+
5062
keys.push(cacheKey)
5163

5264
return keys

0 commit comments

Comments
 (0)