Skip to content

Commit

Permalink
feat: add dry-run option
Browse files Browse the repository at this point in the history
Co-authored-by: hiraginoyuki <[email protected]>
  • Loading branch information
Mogyuchi and hiraginoyuki committed Apr 9, 2024
1 parent 8d76632 commit 933b50d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
description: 'The GitHub token used to manage repository action cache'
required: true
default: ${{ github.token }}
dry-run:
description: 'dry-run caches deletion'
required: false
default: 'false'

# Define your outputs here.

Expand Down
16 changes: 10 additions & 6 deletions dist/index.js

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

19 changes: 12 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ const deleteRefActionsCaches = async (
repo: { owner: string; repo: string },
ref: string
): Promise<void> => {
const isDryRun = core.getBooleanInput('dry-run')
const dryRunPrefix = isDryRun ? 'DRY-RUN MODE ' : ''

const deleteCache = async (cache: Cache): Promise<void> => {
if (!cache.id) return
core.info(` - Cache with key ${cache.key}`)
await octokit.rest.actions.deleteActionsCacheById({
...repo,
cache_id: cache.id
})
core.info(`${dryRunPrefix} - Cache with key ${cache.key}`)
if (!isDryRun) {
await octokit.rest.actions.deleteActionsCacheById({
...repo,
cache_id: cache.id
})
}
}

// https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate
Expand All @@ -29,7 +34,7 @@ const deleteRefActionsCaches = async (
per_page: 100
}
)
core.info(`⌛ Deleting ${caches.length} cache(s) on ${ref}`)
core.info(`${dryRunPrefix}⌛ Deleting ${caches.length} cache(s) on ${ref}`)

await Promise.all(caches.map(async cache => deleteCache(cache)))
}
Expand All @@ -47,7 +52,6 @@ export async function run(): Promise<void> {
const { repo, eventName, payload } = github.context

const ref = getRef({ eventName, payload })

if (ref === null) {
core.info('🤔 Could not determine deletion target.')
core.info(
Expand All @@ -56,6 +60,7 @@ export async function run(): Promise<void> {
return
}
await deleteRefActionsCaches(octokit, repo, ref)

core.info('✅ Done')
} catch (error) {
// Fail the workflow run if an error occurs
Expand Down

0 comments on commit 933b50d

Please sign in to comment.