From 933b50db1319c8cfab2942bf3b61b43dd86cfb6f Mon Sep 17 00:00:00 2001 From: Mogyuchi Date: Wed, 31 Jan 2024 03:49:51 +0900 Subject: [PATCH] feat: add dry-run option Co-authored-by: hiraginoyuki --- action.yml | 4 ++++ dist/index.js | 16 ++++++++++------ src/main.ts | 19 ++++++++++++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/action.yml b/action.yml index 1ca5a70f..51d41201 100644 --- a/action.yml +++ b/action.yml @@ -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. diff --git a/dist/index.js b/dist/index.js index 0bf081bb..1ad2e21b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29680,14 +29680,18 @@ const core = __importStar(__nccwpck_require__(9093)); const github = __importStar(__nccwpck_require__(5942)); const ref_1 = __nccwpck_require__(2636); const deleteRefActionsCaches = async (octokit, repo, ref) => { + const isDryRun = core.getBooleanInput('dry-run'); + const dryRunPrefix = isDryRun ? 'DRY-RUN MODE ' : ''; const deleteCache = async (cache) => { if (!cache.id) return; - 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 + }); + } }; // https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate const caches = await octokit.paginate(octokit.rest.actions.getActionsCacheList, { @@ -29695,7 +29699,7 @@ const deleteRefActionsCaches = async (octokit, repo, ref) => { ref, per_page: 100 }); - core.info(`⌛ Deleting ${caches.length} cache(s) on ${ref}`); + core.info(`${dryRunPrefix}⌛ Deleting ${caches.length} cache(s) on ${ref}`); await Promise.all(caches.map(async (cache) => deleteCache(cache))); }; /** diff --git a/src/main.ts b/src/main.ts index dccce7c9..01bb4a5a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,13 +11,18 @@ const deleteRefActionsCaches = async ( repo: { owner: string; repo: string }, ref: string ): Promise => { + const isDryRun = core.getBooleanInput('dry-run') + const dryRunPrefix = isDryRun ? 'DRY-RUN MODE ' : '' + const deleteCache = async (cache: Cache): Promise => { if (!cache.id) return - 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 + }) + } } // https://github.com/octokit/plugin-paginate-rest.js#octokitpaginate @@ -29,7 +34,7 @@ const deleteRefActionsCaches = async ( per_page: 100 } ) - core.info(`⌛ Deleting ${caches.length} cache(s) on ${ref}`) + core.info(`${dryRunPrefix}⌛ Deleting ${caches.length} cache(s) on ${ref}`) await Promise.all(caches.map(async cache => deleteCache(cache))) } @@ -47,7 +52,6 @@ export async function run(): Promise { const { repo, eventName, payload } = github.context const ref = getRef({ eventName, payload }) - if (ref === null) { core.info('🤔 Could not determine deletion target.') core.info( @@ -56,6 +60,7 @@ export async function run(): Promise { return } await deleteRefActionsCaches(octokit, repo, ref) + core.info('✅ Done') } catch (error) { // Fail the workflow run if an error occurs