Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to control cache invalidation interval #1031

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ with:
# ...
```

### `cache-invalidation-interval`

(optional)

Periodically invalidate the cache every `cache-invalidation-interval` days to ensure that outdated data is removed and fresh data is loaded.

The default value is `7`.

```yml
uses: golangci/golangci-lint-action@v5
with:
cache-invalidation-interval: 15
# ...
```

If set the number is `<= 0`, the cache will be always invalidate (Not recommended).

### `annotations`

(optional)
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ inputs:
description: "golangci-lint command line arguments"
default: ""
required: false
cache-invalidation-interval:
description: "Periodically invalidate a cache because a new code being added. (number of days)"
default: '7'
required: false
runs:
using: "node20"
main: "dist/run/index.js"
Expand Down
7 changes: 5 additions & 2 deletions dist/post_run/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions dist/run/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,40 @@ const getLintCacheDir = (): string => {

const getIntervalKey = (invalidationIntervalDays: number): string => {
const now = new Date()

if (invalidationIntervalDays <= 0) {
return `${now.getTime()}`
}

const secondsSinceEpoch = now.getTime() / 1000
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400))
return intervalNumber.toString()
}

async function buildCacheKeys(): Promise<string[]> {
const keys = []

const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim())

// Periodically invalidate a cache because a new code being added.
// TODO: configure it via inputs.
let cacheKey = `golangci-lint.cache-${getIntervalKey(7)}-`
let cacheKey = `golangci-lint.cache-${getIntervalKey(invalidationIntervalDays)}-`
keys.push(cacheKey)

// Get working directory from input
const workingDirectory = core.getInput(`working-directory`)

// create path to go.mod prepending the workingDirectory if it exists
const goModPath = path.join(workingDirectory, `go.mod`)

core.info(`Checking for go.mod: ${goModPath}`)

if (await pathExists(goModPath)) {
// Add checksum to key to invalidate a cache when dependencies change.
cacheKey += await checksumFile(`sha1`, goModPath)
} else {
cacheKey += `nogomod`
}

keys.push(cacheKey)

return keys
Expand Down
Loading