diff --git a/dist/index.js b/dist/index.js index 5b670192..b0b52c0e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29652,17 +29652,14 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.run = void 0; const core = __importStar(__nccwpck_require__(9093)); const github = __importStar(__nccwpck_require__(5942)); -const getActionsCacheList = (octokit, repo, ref) => { +const deleteRefActionsCache = async (octokit, repo, ref) => { // Get the list of cache IDs // https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate const iterator = octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, { ...repo, - ref: ref + ref }); - return iterator; -}; -const deleteRefActionsCache = async (octokit, repo, ref) => { - const iterator = getActionsCacheList(octokit, repo, ref); + // https://github.com/octokit/octokit.js/tree/b831b6bce43d56b97e25a996e1b43525486d8bd3?tab=readme-ov-file#pagination for await (const { data: cacheList } of iterator) { for (const { id: cacheId } of cacheList) { if (!cacheId) @@ -29682,15 +29679,31 @@ async function run() { try { const token = core.getInput('repo-token'); const octokit = github.getOctokit(token); - // get context - // MEMO: git contextからheadRefは取得できなそう - const { repo, ref } = github.context; - const headRef = process.env.GITHUB_HEAD_REF; - core.debug(`headRef: ${headRef}`); + // get repostiory information + const { repo } = github.context; + // MEMO: payloadから取得できるのは確認したけど、型何もついてない + const payload = github.context.payload; + const prNumber = payload.pull_request?.number; + const headRef = payload.pull_request?.head?.ref; + const ref = payload.ref; + if (prNumber) { + // fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment + core.info(`delete cache for refs/pull/${prNumber}/merge`); + await deleteRefActionsCache(octokit, repo, `refs/pull/${prNumber}/merge`); + core.info('done ✅'); + } if (headRef) { - deleteRefActionsCache(octokit, repo, headRef); + // fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment + core.info(`delete cache for refs/heads/${headRef}`); + await deleteRefActionsCache(octokit, repo, `refs/heads/${headRef}`); + core.info('done ✅'); + } + if (ref) { + // fire when event is workflow_dispatch or push + core.info(`delete cache for ${ref}`); + await deleteRefActionsCache(octokit, repo, ref); + core.info('done ✅'); } - deleteRefActionsCache(octokit, repo, ref); } catch (error) { // Fail the workflow run if an error occurs diff --git a/src/main.ts b/src/main.ts index d5af8688..2c0783b4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,6 +16,7 @@ const deleteRefActionsCache = async ( } ) + // https://github.com/octokit/octokit.js/tree/b831b6bce43d56b97e25a996e1b43525486d8bd3?tab=readme-ov-file#pagination for await (const { data: cacheList } of iterator) { for (const { id: cacheId } of cacheList) { if (!cacheId) continue @@ -35,17 +36,34 @@ export async function run(): Promise { try { const token = core.getInput('repo-token') const octokit = github.getOctokit(token) - // get context - // MEMO: git contextからheadRefは取得できなそう - const { repo, ref } = github.context - const headRef = process.env.GITHUB_HEAD_REF - core.debug(`headRef: ${headRef}`) + // get repostiory information + const { repo } = github.context + // MEMO: payloadから取得できるのは確認したけど、型何もついてない + const payload = github.context.payload + const prNumber = payload.pull_request?.number as number | undefined + const headRef = payload.pull_request?.head?.ref as string | undefined + const ref = payload.ref as string | undefined + + if (prNumber) { + // fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment + core.info(`delete cache for refs/pull/${prNumber}/merge`) + await deleteRefActionsCache(octokit, repo, `refs/pull/${prNumber}/merge`) + core.info('done ✅') + } if (headRef) { - deleteRefActionsCache(octokit, repo, headRef) + // fire when event is pull_request or pull_request_target or pull_request_review or pull_request_review_comment + core.info(`delete cache for refs/heads/${headRef}`) + await deleteRefActionsCache(octokit, repo, `refs/heads/${headRef}`) + core.info('done ✅') + } + if (ref) { + // fire when event is workflow_dispatch or push + core.info(`delete cache for ${ref}`) + await deleteRefActionsCache(octokit, repo, ref) + core.info('done ✅') } - deleteRefActionsCache(octokit, repo, ref) } catch (error) { // Fail the workflow run if an error occurs if (error instanceof Error) core.setFailed(error.message)