Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Bind obsolete CI cancellation to its closed pull request
pulse-triage[bot]d2a41bfff7a2
diff --git a/.github/scripts/reclaim-closed-pr-capacity.cjs b/.github/scripts/reclaim-closed-pr-capacity.cjsindex bb44817f20..138d7bd0b5 100644--- a/.github/scripts/reclaim-closed-pr-capacity.cjs+++ b/.github/scripts/reclaim-closed-pr-capacity.cjs@@ -2,6 +2,18 @@const ACTIVE_STATUSES = ['queued', 'in_progress'];+function belongsToClosedPullRequest(run, pullRequestNumber, headRepository, headBranch) {+ return (+ Number.isSafeInteger(run.id) && run.id > 0 &&+ run.event === 'pull_request' &&+ ACTIVE_STATUSES.includes(run.status) &&+ run.head_branch === headBranch &&+ run.head_repository?.full_name === headRepository &&+ Array.isArray(run.pull_requests) && run.pull_requests.length > 0 &&+ run.pull_requests.every((pullRequest) => pullRequest?.number === pullRequestNumber)+ );+}+async function cancelClosedPullRequestRuns({ github, context, core }) {const pullRequest = context.payload.pull_request;if (!pullRequest || !Number.isInteger(pullRequest.number)) {@@ -52,17 +64,43 @@ async function cancelClosedPullRequestRuns({ github, context, core }) {per_page: 100,});for (const run of runs) {- if (+ if (belongsToClosedPullRequest(run, pullRequest.number, headRepository, headBranch)) {+ candidates.set(run.id, run);+ } else if (run.head_branch === headBranch &&run.head_repository?.full_name === headRepository) {- candidates.set(run.id, run);+ core.info('A same-head run has no active, exclusive closed-PR association; leaving it alone.');}}}let cancellationRequests = 0;for (const run of candidates.values()) {+ // Lists are discovery, not mutation authority: a reused branch can have+ // another PR's runs, and a discovered run may have completed meanwhile.+ // Missing associations (including some fork responses) stay untouched.+ const observed = await github.rest.actions.getWorkflowRun({ owner, repo, run_id: run.id });+ if (+ observed.data.id !== run.id ||+ !belongsToClosedPullRequest(observed.data, pullRequest.number, headRepository, headBranch)+ ) {+ core.info(`Run ${run.id} no longer has an active, exclusive closed-PR association; leaving it alone.`);+ continue;+ }+ const latest = await github.rest.pulls.get({ owner, repo, pull_number: pullRequest.number });+ if (latest.data.state !== 'closed') {+ core.info(`PR #${pullRequest.number} is no longer closed; stopping cancellation.`);+ break;+ }+ const reused = await github.paginate(github.rest.pulls.list, {+ owner, repo, state: 'open', head: `${headOwner}:${headBranch}`, per_page: 100,+ });+ if (reused.length > 0) {+ core.info('The head now belongs to an open pull request; stopping cancellation.');+ break;+ }+try {await github.rest.actions.cancelWorkflowRun({ owner, repo, run_id: run.id });cancellationRequests += 1;diff --git a/.github/scripts/reclaim-closed-pr-capacity.test.cjs b/.github/scripts/reclaim-closed-pr-capacity.test.cjsindex a53a3097e0..4858582156 100644--- a/.github/scripts/reclaim-closed-pr-capacity.test.cjs+++ b/.github/scripts/reclaim-closed-pr-capacity.test.cjs@@ -8,17 +8,25 @@ const {cancelClosedPullRequestRuns,} = require('./reclaim-closed-pr-capacity.cjs');-function fixture({ state = 'closed', openForHead = [], runs = {}, cancelError, refreshed = 'completed', refreshError } = {}) {+function fixture({ state = 'closed', openForHead = [], runs = {}, cancelError, refreshed = 'completed', refreshError, observedRun, runReadError } = {}) {const cancelled = [];const cancellationAttempts = [];const readbacks = [];const messages = [];+ const lifecycleReads = [];+ const headReads = [];const github = {paginate: async (method, input) => method(input),rest: {pulls: {- get: async () => ({ data: { state } }),- list: async () => openForHead,+ get: async () => {+ lifecycleReads.push(1858);+ return { data: { state: Array.isArray(state) ? state.shift() : state } };+ },+ list: async ({ head }) => {+ headReads.push(head);+ return typeof openForHead === 'function' ? openForHead() : openForHead;+ },},actions: {listWorkflowRunsForRepo: async ({ status }) => runs[status] || [],@@ -29,8 +37,13 @@ function fixture({ state = 'closed', openForHead = [], runs = {}, cancelError, r},getWorkflowRun: async ({ run_id: runId }) => {readbacks.push(runId);- if (refreshError) throw refreshError;- return { data: { status: refreshed } };+ if (cancellationAttempts.includes(runId)) {+ if (refreshError) throw refreshError;+ return { data: { status: refreshed } };+ }+ if (runReadError) throw runReadError;+ const listed = Object.values(runs).flat().find((run) => run.id === runId);+ return { data: observedRun || listed };},},},@@ -52,7 +65,7 @@ function fixture({ state = 'closed', openForHead = [], runs = {}, cancelError, rwarning: (message) => messages.push(message),setFailed: (message) => messages.push(message),};- return { github, context, core, cancelled, cancellationAttempts, readbacks, messages };+ return { github, context, core, cancelled, cancellationAttempts, readbacks, messages, lifecycleReads, headReads };}function requestError(status) {@@ -63,6 +76,8 @@ function matchingRun(id = 10) {return {id,name: 'Core E2E Tests',+ event: 'pull_request',+ pull_requests: [{ number: 1858 }],status: 'in_progress',head_branch: 'topic/old',head_repository: { full_name: 'rcourtman/Pulse' },@@ -73,6 +88,8 @@ test('cancels only unfinished runs for the exact closed head', async () => {const matching = {id: 10,name: 'Build and Test',+ event: 'pull_request',+ pull_requests: [{ number: 1858 }],status: 'queued',head_branch: 'topic/old',head_repository: { full_name: 'rcourtman/Pulse' },@@ -115,7 +132,7 @@ test('accepts only a proven completion race', async () => {const raced = fixture({ runs: { in_progress: [run] }, cancelError: conflict });await cancelClosedPullRequestRuns(raced);assert.deepEqual(raced.cancellationAttempts, [10]);- assert.deepEqual(raced.readbacks, [10]);+ assert.deepEqual(raced.readbacks, [10, 10]);assert.match(raced.messages[0], /completed before cancellation/);assert.match(raced.messages.at(-1), /Requested cancellation for 0 of 1/);@@ -125,7 +142,7 @@ test('accepts only a proven completion race', async () => {refreshed: 'in_progress',});await assert.rejects(cancelClosedPullRequestRuns(failed), (error) => error === conflict);- assert.deepEqual(failed.readbacks, [10]);+ assert.deepEqual(failed.readbacks, [10, 10]);});for (const status of [400, 401, 403, 404, 429, 500, undefined, '409']) {@@ -139,7 +156,7 @@ for (const status of [400, 401, 403, 404, 429, 500, undefined, '409']) {await assert.rejects(cancelClosedPullRequestRuns(subject), (caught) => caught === error);assert.deepEqual(subject.cancellationAttempts, [10], 'stop before cancelling another run');assert.deepEqual(subject.cancelled, []);- assert.deepEqual(subject.readbacks, [], 'no follow-up API call after the failed cancellation');+ assert.deepEqual(subject.readbacks, [10], 'only admission read, no follow-up API call after the failed cancellation');assert.deepEqual(subject.messages, [], 'do not report the failed operation as a completion race');});}@@ -148,7 +165,7 @@ test('does not infer a conflict from an error message', async () => {const error = new Error('409');const subject = fixture({ runs: { in_progress: [matchingRun()] }, cancelError: error });await assert.rejects(cancelClosedPullRequestRuns(subject), (caught) => caught === error);- assert.deepEqual(subject.readbacks, []);+ assert.deepEqual(subject.readbacks, [10]);});test('stops when the conflict readback fails', async () => {@@ -159,7 +176,131 @@ test('stops when the conflict readback fails', async () => {refreshError: error,});await assert.rejects(cancelClosedPullRequestRuns(subject), (caught) => caught === error);- assert.deepEqual(subject.readbacks, [10]);+ assert.deepEqual(subject.readbacks, [10, 10]);assert.deepEqual(subject.cancellationAttempts, [10]);assert.deepEqual(subject.messages, []);});+++for (const [label, change] of [+ ['different PR', { pull_requests: [{ number: 1900 }] }],+ ['missing association', { pull_requests: undefined }],+ ['empty fork association', { pull_requests: [] }],+ ['shared association', { pull_requests: [{ number: 1858 }, { number: 1900 }] }],+ ['malformed association', { pull_requests: [null] }],+ ['push event', { event: 'push' }],+ ['completed status', { status: 'completed' }],+ ['invalid run ID', { id: '10' }],+]) {+ test(`identity: ignores ${label} even on the same head branch`, async () => {+ const subject = fixture({ runs: { in_progress: [{ ...matchingRun(), ...change }] } });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.deepEqual(subject.readbacks, [], 'do not pursue an unrelated or unbound run');+ });+}++for (const [label, change] of [+ ['reassociated', { pull_requests: [{ number: 1900 }] }],+ ['association unavailable', { pull_requests: [] }],+ ['completed', { status: 'completed' }],+ ['changed event', { event: 'push' }],+ ['different returned ID', { id: 11 }],+ ['different returned branch', { head_branch: 'topic/current' }],+ ['different returned repository', { head_repository: { full_name: 'contributor/Pulse' } }],+]) {+ test(`identity: leaves a ${label} run alone at mutation readback`, async () => {+ const subject = fixture({+ runs: { in_progress: [matchingRun()] },+ observedRun: { ...matchingRun(), ...change },+ });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.deepEqual(subject.readbacks, [10]);+ assert.deepEqual(subject.lifecycleReads, [1858], 'no further reads for the excluded run');+ });+}++test('identity: stops unchanged on a refused run admission read', async () => {+ const error = requestError(403);+ const subject = fixture({+ runs: { in_progress: [matchingRun(), matchingRun(11)] }, runReadError: error,+ });+ await assert.rejects(cancelClosedPullRequestRuns(subject), (caught) => caught === error);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.deepEqual(subject.readbacks, [10]);+ assert.deepEqual(subject.lifecycleReads, [1858]);+});++test('lifecycle: stops if the PR reopens during run discovery', async () => {+ const subject = fixture({ state: ['closed', 'open'], runs: { in_progress: [matchingRun()] } });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.deepEqual(subject.lifecycleReads, [1858, 1858]);+});++test('lifecycle: stops if an open PR reuses the branch during discovery', async () => {+ let lists = 0;+ const subject = fixture({+ openForHead: () => ++lists === 1 ? [] : [{ number: 1900 }],+ runs: { in_progress: [matchingRun()] },+ });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.equal(lists, 2);+});++test('lifecycle: rechecks closure before every cancellation, not just the first', async () => {+ const subject = fixture({+ state: ['closed', 'closed', 'open'],+ runs: { in_progress: [matchingRun(), matchingRun(11)] },+ });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, [10]);+ assert.deepEqual(subject.readbacks, [10, 11]);+ assert.deepEqual(subject.lifecycleReads, [1858, 1858, 1858]);+});++test('lifecycle: rechecks branch reuse before every cancellation', async () => {+ let lists = 0;+ const subject = fixture({+ openForHead: () => ++lists < 3 ? [] : [{ number: 1900 }],+ runs: { in_progress: [matchingRun(), matchingRun(11)] },+ });+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancellationAttempts, [10]);+ assert.equal(lists, 3);+});+++for (const status of [403, 429]) {+ for (const phase of ['closure', 'head reuse']) {+ test(`lifecycle: stops on ${status} from the fresh ${phase} read`, async () => {+ const error = requestError(status);+ const subject = fixture({ runs: { in_progress: [matchingRun(), matchingRun(11)] } });+ const method = phase === 'closure' ? 'get' : 'list';+ const original = subject.github.rest.pulls[method];+ let calls = 0;+ subject.github.rest.pulls[method] = async (input) => {+ if (++calls === 2) throw error;+ return original(input);+ };+ await assert.rejects(cancelClosedPullRequestRuns(subject), (caught) => caught === error);+ assert.deepEqual(subject.cancellationAttempts, []);+ assert.deepEqual(subject.readbacks, [10], 'no later run or fallback read after refusal');+ });+ }+}++test('identity: still reclaims every exclusively associated run, including a fork', async () => {+ const run = { ...matchingRun(), head_repository: { full_name: 'contributor/Pulse' } };+ const subject = fixture({ runs: { in_progress: [run, { ...run, id: 11 }] } });+ subject.context.payload.pull_request.head.repo = {+ full_name: 'contributor/Pulse', owner: { login: 'contributor' },+ };+ await cancelClosedPullRequestRuns(subject);+ assert.deepEqual(subject.cancelled, [10, 11]);+ assert.deepEqual(subject.readbacks, [10, 11]);+ assert.deepEqual(subject.headReads, Array(3).fill('contributor:topic/old'));+ assert.match(subject.messages.at(-1), /Requested cancellation for 2 of 2/);+});