Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
14 changes: 10 additions & 4 deletions src/automerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
Expand All @@ -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)));
}

Expand All @@ -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 (;;) {
Expand All @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions tests/automerge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }];
Expand Down