From 7c3b40b1c41dbfa31715f61962bbd28dce22e048 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Thu, 19 Nov 2026 08:29:15 +0800 Subject: [PATCH] ci: find closed PRs' runs without the API status filter (#4384) The status filter of the workflow-runs API lags behind newly created runs, so the first run found nothing to cancel while the PR's CI was still in progress. List the branch's runs and skip completed ones instead. --- .github/workflows/cancel-on-close.yml | 33 +++++++++++++-------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cancel-on-close.yml b/.github/workflows/cancel-on-close.yml index 47ce78248..0a0f652ba 100644 --- a/.github/workflows/cancel-on-close.yml +++ b/.github/workflows/cancel-on-close.yml @@ -19,24 +19,23 @@ jobs: const pr = context.payload.pull_request; const { owner, repo } = context.repo; const headRepo = pr.head.repo?.full_name; + // The API's status filter lags behind new runs: list the branch and check each one. + const runs = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, { + owner, + repo, + branch: pr.head.ref, + per_page: 100, + }); let cancelled = 0; - for (const status of ['queued', 'in_progress', 'waiting', 'pending', 'requested']) { - const runs = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, { - owner, - repo, - branch: pr.head.ref, - status, - per_page: 100, - }); - for (const run of runs) { - if (run.id === context.runId || run.head_repository?.full_name !== headRepo) continue; - if (!['pull_request', 'pull_request_target'].includes(run.event)) continue; - try { - await github.rest.actions.cancelWorkflowRun({ owner, repo, run_id: run.id }); - cancelled++; - } catch (e) { - core.warning(`run ${run.id}: ${e.message}`); - } + for (const run of runs) { + if (run.id === context.runId || run.status === 'completed') continue; + if (run.head_repository?.full_name !== headRepo) continue; + if (!['pull_request', 'pull_request_target'].includes(run.event)) continue; + try { + await github.rest.actions.cancelWorkflowRun({ owner, repo, run_id: run.id }); + cancelled++; + } catch (e) { + core.warning(`run ${run.id}: ${e.message}`); } } core.info(`Cancelled ${cancelled} run(s) of #${pr.number}`);