From 148befd826853763cf8c3bcfdcc79bc8fcb1d21a Mon Sep 17 00:00:00 2001 From: hulk510 Date: Wed, 17 Jan 2024 22:49:05 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20ref=E3=81=AE=E6=8C=87=E5=AE=9A=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=82=92=E4=BF=AE=E6=AD=A3=20pull=5Frequest=E3=82=84i?= =?UTF-8?q?ssue=E3=81=AE=E7=95=AA=E5=8F=B7=E3=82=92=E3=81=84=E3=81=84?= =?UTF-8?q?=E6=84=9F=E3=81=98=E3=81=AB=E5=8F=96=E3=82=8B=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=81=8C=E8=A6=8B=E3=81=A4=E3=81=8B=E3=82=89=E3=81=AA=E3=81=8B?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=81=AE=E3=81=A7payload=E3=81=8B=E3=82=89?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/index.js | 39 ++++++++++++++++++++++++++------------- src/main.ts | 32 +++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 20 deletions(-) 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)