From 91e1b77782b0a0cae9fcbcec3959e1b3be6f3ec7 Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Tue, 11 Aug 2026 16:46:52 -0400 Subject: [PATCH] Fix repeated automerge grace periods Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com> --- README.md | 3 ++- src/automerge.ts | 14 ++++++++++---- tests/automerge.test.ts | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6416e40..e920bb1 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ latest commit status is `pending`. Once all reported work is terminal, it reques request, but GitHub rejects it when any required condition is unsatisfied. Before merging, the App waits until the active automerge label has been present for 10 seconds, then checks the pull request again. Removing the label -during this grace period cancels the merge. +during this grace period cancels the merge. If GitHub has not exposed the matching label event yet, the grace period starts when the App first observes +the label instead of waiting repeatedly for event history. When new commits are pushed, the App removes either automerge label. Re-add a label after the new commit is ready to opt in again. diff --git a/src/automerge.ts b/src/automerge.ts index a80b43c..2f7b4fc 100644 --- a/src/automerge.ts +++ b/src/automerge.ts @@ -28,7 +28,12 @@ export function mergeMethod(settings: RepositorySettings): MergeMethod | null { return null; } -export function gracePeriodRemaining(pullRequest: PullRequest, events: IssueEvent[], now: number): number { +export function gracePeriodRemaining( + pullRequest: PullRequest, + events: IssueEvent[], + now: number, + missingEventAddedAt = now, +): number { const activeLabels = new Set( pullRequest.labels.map(({ name }) => name.trim().toLowerCase()).filter((name) => AUTOMERGE_LABELS.has(name)), ); @@ -40,8 +45,8 @@ export function gracePeriodRemaining(pullRequest: PullRequest, events: IssueEven latestAdditions.set(label, Math.max(latestAdditions.get(label) || 0, createdAt)); } } - const addedAt = Math.min(...latestAdditions.values()); - if (!Number.isFinite(addedAt)) return AUTOMERGE_GRACE_PERIOD_MS; + const eventAddedAt = Math.min(...latestAdditions.values()); + const addedAt = Number.isFinite(eventAddedAt) ? eventAddedAt : missingEventAddedAt; return Math.min(AUTOMERGE_GRACE_PERIOD_MS, Math.max(0, AUTOMERGE_GRACE_PERIOD_MS - (now - addedAt))); } @@ -58,6 +63,7 @@ export async function evaluatePullRequest( const now = options.now || Date.now; const waitFor = options.wait || wait; let pullRequest = await github.pullRequest(number); + const labelObservedAt = now(); if (pullRequest.state !== "open" || pullRequest.draft || !hasAutomergeLabel(pullRequest)) return false; for (;;) { @@ -70,7 +76,7 @@ export async function evaluatePullRequest( return false; } - const remaining = gracePeriodRemaining(pullRequest, await github.issueEvents(number), now()); + const remaining = gracePeriodRemaining(pullRequest, await github.issueEvents(number), now(), labelObservedAt); if (remaining > 0) { console.log(`Waiting for automerge grace period on ${repository}#${number}`); await waitFor(remaining); diff --git a/tests/automerge.test.ts b/tests/automerge.test.ts index fce23a6..57c216e 100644 --- a/tests/automerge.test.ts +++ b/tests/automerge.test.ts @@ -113,6 +113,26 @@ test("merges when the label remains after the grace period", async () => { expect(github.merges).toEqual([{ number: 12, sha: "abc123", method: "squash" }]); }); +test("merges when the label event is not yet visible", async () => { + const github = new FakeGitHub(); + github.events = []; + const waits: number[] = []; + let now = 10_000; + + await expect( + evaluatePullRequest(github, 12, "owner/repository", { + now: () => now, + wait: async (milliseconds) => { + waits.push(milliseconds); + if (waits.length > 1) throw new Error("grace period repeated"); + now += milliseconds; + }, + }), + ).resolves.toBe(true); + expect(waits).toEqual([10_000]); + expect(github.merges).toEqual([{ number: 12, sha: "abc123", method: "squash" }]); +}); + test("waits until check runs and commit statuses finish", async () => { const github = new FakeGitHub(); github.checks = [{ status: "completed" }, { status: "in_progress" }];