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 11, 2024
1 parent 8d76632 commit 84b1f40
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 29 deletions.
18 changes: 10 additions & 8 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const runMock = jest.spyOn(main, 'run')
// Mock the GitHub Actions core library
// let debugMock: jest.SpyInstance
let errorMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
let getBooleanInputMock: jest.SpyInstance
// let setFailedMock: jest.SpyInstance
// let setOutputMock: jest.SpyInstance

Expand All @@ -28,19 +28,21 @@ describe('action', () => {

// debugMock = jest.spyOn(core, 'debug').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
getBooleanInputMock = jest
.spyOn(core, 'getBooleanInput')
.mockImplementation()
// setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
// setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
})

it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
it('sets the dry-run', async () => {
// Set the action's inputs as return values from core.getBooleanInput()
getBooleanInputMock.mockImplementation((name: string): boolean => {
switch (name) {
case 'milliseconds':
return '500'
case 'dry-run':
return true
default:
return ''
return false
}
})

Expand Down
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
65 changes: 55 additions & 10 deletions dist/index.js

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

6 changes: 6 additions & 0 deletions src/get-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as core from '@actions/core'

export const getInputs = () => ({

Check failure on line 3 in src/get-inputs.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Missing return type on function

Check failure on line 3 in src/get-inputs.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Missing return type on function
token: core.getInput('repo-token', { required: true }),
dryRun: core.getBooleanInput('dry-run')
})
33 changes: 22 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import { getRef } from './ref'
import type * as types from '@octokit/openapi-types'
import { getInputs } from './get-inputs'

type Cache =
types.components['schemas']['actions-cache-list']['actions_caches'][number]

const ansi = { reset: '\x1B[0m', dryRun: '\x1B[38;2;90;185;255m' }

const prefix = ({ isDryRun = false }: { isDryRun: boolean }) =>

Check failure on line 12 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Missing return type on function

Check failure on line 12 in src/main.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Missing return type on function
isDryRun ? `${ansi.dryRun}DRY-RUN MODE ${ansi.reset}` : ''

const deleteRefActionsCaches = async (
octokit: ReturnType<typeof github.getOctokit>,
repo: { owner: string; repo: string },
ref: string
ref: string,
isDryRun: boolean
): Promise<void> => {
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(`${prefix({ isDryRun })} - 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 +38,9 @@ const deleteRefActionsCaches = async (
per_page: 100
}
)
core.info(`⌛ Deleting ${caches.length} cache(s) on ${ref}`)
core.info(
`${prefix({ isDryRun })}⌛ Deleting ${caches.length} cache(s) on ${ref}`
)

await Promise.all(caches.map(async cache => deleteCache(cache)))
}
Expand All @@ -40,23 +51,23 @@ const deleteRefActionsCaches = async (
*/
export async function run(): Promise<void> {
try {
const token = core.getInput('repo-token', { required: true })
const { token, dryRun: isDryRun } = getInputs()
const octokit = github.getOctokit(token)

// get repostiory information
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
}
await deleteRefActionsCaches(octokit, repo, ref)
core.info('✅ Done')
await deleteRefActionsCaches(octokit, repo, ref, isDryRun)

core.info(`${prefix({ isDryRun })}✅ Done`)
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) {
Expand Down

0 comments on commit 84b1f40

Please sign in to comment.