Skip to content

Commit

Permalink
fix: fix cache deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Mogyuchi <[email protected]>
  • Loading branch information
Mogyuchi committed Feb 6, 2024
1 parent b844bd7 commit 137fee4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
30 changes: 14 additions & 16 deletions dist/index.js

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"valibot": "^0.27.1"
},
"devDependencies": {
"@octokit/openapi-types": "^19.1.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.20.0",
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type NonArray<T> = T extends Array<infer U> ? U : never

Check failure on line 1 in src/internal/types.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Array type using 'Array<T>' is forbidden. Use 'T[]' instead

Check failure on line 1 in src/internal/types.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Array type using 'Array<T>' is forbidden. Use 'T[]' instead
35 changes: 20 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
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 type { NonArray } from './internal/types'

type Cache = NonArray<
types.components['schemas']['actions-cache-list']['actions_caches']
>

const deleteRefActionsCaches = async (
octokit: ReturnType<typeof github.getOctokit>,
repo: { owner: string; repo: string },
ref: string
): Promise<void> => {
// Get the list of cache IDs
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
})
}

// https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate
const iterator = octokit.paginate.iterator(
const caches = await octokit.paginate(
octokit.rest.actions.getActionsCacheList,
{
...repo,
ref
ref,
per_page: 100
}
)
core.info(`⌛ Deleting ${caches.length} caches on ${ref}`)

// https://github.com/octokit/octokit.js/tree/b831b6bce43d56b97e25a996e1b43525486d8bd3?tab=readme-ov-file#pagination
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
})
}
}
await Promise.all(caches.map(cache => deleteCache(cache)))

Check failure on line 36 in src/main.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Functions that return promises must be async

Check failure on line 36 in src/main.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Functions that return promises must be async
}

/**
Expand All @@ -51,7 +57,6 @@ export async function run(): Promise<void> {
)
return
}
core.info(`⌛ Deleting caches on ${ref}`)
await deleteRefActionsCaches(octokit, repo, ref)
core.info('✅ Done')
} catch (error) {
Expand Down

0 comments on commit 137fee4

Please sign in to comment.