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 Feb 2, 2024
1 parent 6e4ecb5 commit c210eb2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 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: false
default: ${{ github.token }}
dry-run:
description: 'dry-run caches deletion'
required: false
default: 'false'

# Define your outputs here.

Expand Down
23 changes: 15 additions & 8 deletions dist/index.js

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

28 changes: 20 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as v from 'valibot'
import { getRef } from './ref'
import { DryRunSchema } from './schema'

const deleteRefActionsCaches = async (
octokit: ReturnType<typeof github.getOctokit>,
repo: { owner: string; repo: string },
ref: string
): Promise<void> => {
const isDryRun = v.parse(
DryRunSchema,
core.getInput('dry-run', { trimWhitespace: true })
)
const dryRunPrefix = isDryRun ? 'DRY-RUN MODE ' : ''

core.info(`${dryRunPrefix}⌛ Deleting caches on ${ref}`)

// Get the list of cache IDs
// https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate
const iterator = octokit.paginate.iterator(
Expand All @@ -21,11 +31,13 @@ const deleteRefActionsCaches = async (
for await (const { data: cacheList } of iterator) {
for (const cache of cacheList) {
if (!cache.id) continue
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
})
}
}
}
}
Expand All @@ -43,17 +55,17 @@ 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(
'ℹ️ If you suspect this is a bug, please consider raising an issue to help us address it promptly.'
)
return
}
core.info(`⌛ Deleting caches on ${ref}`)

await deleteRefActionsCaches(octokit, repo, ref)
core.info('✅ Done')

core.info(`✅ Done`)
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) {
Expand Down
6 changes: 5 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { nullable, optional, string } from 'valibot'
import { boolean, coerce, nullable, optional, string } from 'valibot'

export const StringSchema = string()
export const OptionalStringSchema = optional(string())
export const NullableStringSchema = nullable(string())
export const DryRunSchema = coerce(
boolean('The dry-run option must be either "true" or "false".'),
input => JSON.parse(`${input}`)
)

0 comments on commit c210eb2

Please sign in to comment.