From 9b09c13ec73bce2f0a4498cad80c21eb63bdbca5 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 16 Aug 2024 11:31:57 +0200 Subject: [PATCH] changes --- .github/workflows/clear-cache.yml | 9 +- dev-packages/clear-cache-gh-action/action.yml | 4 + dev-packages/clear-cache-gh-action/index.mjs | 96 +++++++++++-------- 3 files changed, 67 insertions(+), 42 deletions(-) diff --git a/.github/workflows/clear-cache.yml b/.github/workflows/clear-cache.yml index 16bc1c584701..b799e0a56553 100644 --- a/.github/workflows/clear-cache.yml +++ b/.github/workflows/clear-cache.yml @@ -3,13 +3,17 @@ on: workflow_dispatch: inputs: include_pending: - description: If caches of pending workflows should be deleted + description: Delete caches of pending workflows type: boolean default: false include_develop: - description: If caches of the develop branch should be deleted + description: Delete caches on develop branch type: boolean default: false + include_branches: + description: Delete caches on non-develop branches + type: boolean + default: true schedule: # Run every day at midnight - cron: '0 0 * * *' @@ -34,4 +38,5 @@ jobs: with: include_pending: ${{ inputs.include_pending }} include_develop: ${{ inputs.include_develop }} + include_branches: ${{ inputs.include_branches }} github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/dev-packages/clear-cache-gh-action/action.yml b/dev-packages/clear-cache-gh-action/action.yml index 8a973401e5e1..bb3e9ede0670 100644 --- a/dev-packages/clear-cache-gh-action/action.yml +++ b/dev-packages/clear-cache-gh-action/action.yml @@ -8,6 +8,10 @@ inputs: required: false default: "" description: "If set, also clear caches from develop branch." + clear_branches: + required: false + default: "" + description: "If set, also clear caches from non-develop branches." clear_pending: required: false default: "" diff --git a/dev-packages/clear-cache-gh-action/index.mjs b/dev-packages/clear-cache-gh-action/index.mjs index 6159a520f3ef..a6269ef1b7a3 100644 --- a/dev-packages/clear-cache-gh-action/index.mjs +++ b/dev-packages/clear-cache-gh-action/index.mjs @@ -9,6 +9,7 @@ async function run() { const githubToken = getInput('github_token'); const clearDevelop = getInput('clear_develop', { type: 'boolean' }); + const clearBranches = getInput('clear_branches', { type: 'boolean', default: true }); const clearPending = getInput('clear_pending', { type: 'boolean' }); const workflowName = getInput('workflow_name'); @@ -19,6 +20,7 @@ async function run() { owner, clearDevelop, clearPending, + clearBranches, workflowName, }); } @@ -27,9 +29,9 @@ async function run() { * Clear caches. * * @param {ReturnType } octokit - * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, workflowName: string}} options + * @param {{repo: string, owner: string, clearDevelop: boolean, clearPending: boolean, clearBranches: boolean, workflowName: string}} options */ -async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, workflowName }) { +async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPending, clearBranches, workflowName }) { for await (const response of octokit.paginate.iterator(octokit.rest.actions.getActionsCacheList, { owner, repo, @@ -46,48 +48,62 @@ async function clearGithubCaches(octokit, { repo, owner, clearDevelop, clearPend continue; } - // If clearPending is false, do not clear caches for pull requests that have pending checks. + // There are two fundamental paths here: + // If the cache belongs to a PR, we need to check if the PR has any pending workflows. + // Else, we assume the cache belongs to a branch, where we do not check for pending workflows const pull_number = /^refs\/pull\/(\d+)\/merge$/.exec(ref)?.[1]; - if (!clearPending && pull_number) { - const pr = await octokit.rest.pulls.get({ - owner, - repo, - pull_number, - }); - - const prBranch = pr.data.head.ref; - - // Check if PR has any pending workflows - const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ - repo, - owner, - branch: prBranch, - }); - - // We only care about the relevant workflow - const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter(workflow => workflow.name === workflowName); - - const latestWorkflowRun = relevantWorkflowRuns[0]; - - core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); - - // No relevant workflow? Clear caches! - if (!latestWorkflowRun) { - core.info('> Clearing cache because no relevant workflow was found.'); - continue; + if (pull_number) { + if (!clearPending) { + const pr = await octokit.rest.pulls.get({ + owner, + repo, + pull_number, + }); + + const prBranch = pr.data.head.ref; + + // Check if PR has any pending workflows + const workflowRuns = await octokit.rest.actions.listWorkflowRunsForRepo({ + repo, + owner, + branch: prBranch, + }); + + // We only care about the relevant workflow + const relevantWorkflowRuns = workflowRuns.data.workflow_runs.filter( + workflow => workflow.name === workflowName, + ); + + const latestWorkflowRun = relevantWorkflowRuns[0]; + + core.info(`> Latest relevant workflow run: ${latestWorkflowRun.html_url}`); + + // No relevant workflow? Clear caches! + if (!latestWorkflowRun) { + core.info('> Clearing cache because no relevant workflow was found.'); + continue; + } + + // If the latest run was not successful, keep caches + // as either the run may be in progress, + // or failed - in which case we may want to re-run the workflow + if (latestWorkflowRun.conclusion !== 'success') { + core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + continue; + } + + core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); + } else { + core.info('> Clearing cache of PR workflow run.'); } - - // If the latest run was not successful, keep caches - // as either the run may be in progress, - // or failed - in which case we may want to re-run the workflow - if (latestWorkflowRun.conclusion !== 'success') { - core.info(`> Keeping cache because latest workflow is ${latestWorkflowRun.status}.`); + } else { + // This means this is not a pull request, so check clearBranches + if (clearBranches) { + core.info('> Clearing cache because it is not a PR.'); + } else { + core.info('> Keeping cache for non-PR workflow run.'); continue; } - - core.info(`> Clearing cache because latest workflow run is ${latestWorkflowRun.status}.`); - } else { - core.info('Clearing cache because it is not a PR'); } // DRY RUN FOR NOW!