From 7118d28b34ab3c3cf13486e93a49dae8e8838338 Mon Sep 17 00:00:00 2001 From: Fledge Platform Dev-3 Date: Tue, 24 Nov 2026 11:26:00 +0000 Subject: [PATCH 1/2] test(deliveries): pin a merge to the pull request's merged_at, not receipt (#1290) Red at this tip: the ledger row and the work item are stamped with nowIso() at receipt. The first case fails here; the second (no merged_at in the payload, one row at receipt) already passes and is kept as the regression guard. --- test/deliveryOutcome.test.ts | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/deliveryOutcome.test.ts b/test/deliveryOutcome.test.ts index b983a2c0..2cfe499a 100644 --- a/test/deliveryOutcome.test.ts +++ b/test/deliveryOutcome.test.ts @@ -384,6 +384,76 @@ test('a merged pull request webhook writes the delivery the repository page read } }) +// #1290: the payload carries the instant GitHub merged the pull request, so a +// redelivered or replayed event must not move the merge to the day Fledge heard +// about it. The receipt time stays as the fallback for a payload without one. +test("a merge is recorded at the pull request's merged_at, not at receipt", async () => { + const { built, store, repoId, github } = await webhookApp('merged-at') + try { + const fullName = 'o/delivery-webhook-merged-at' + const prUrl = `https://github.com/${fullName}/pull/21` + const mergedAt = '2026-11-20T10:00:00Z' + const send = async (action: string, pull: Record, delivery: string): Promise => { + const body = Buffer.from(JSON.stringify({ + action, installation: { id: 4242 }, repository: { full_name: fullName }, pull_request: pull, + })) + const response = await built.app.inject({ + method: 'POST', + url: '/api/github/webhook', + headers: { 'content-type': 'application/json', 'x-github-event': 'pull_request', 'x-github-delivery': delivery, 'x-hub-signature-256': signWebhook(body) }, + payload: body, + }) + return response.statusCode + } + // A close for a pull request Fledge never saw is dropped on purpose, so the + // opened event comes first and creates the item the close attaches to. + assert.equal(await send('opened', { number: 21, html_url: prUrl, title: 'date the merge', merged: false, state: 'open' }, 'delivery-opened-21'), 200) + assert.equal(await send('closed', { number: 21, html_url: prUrl, title: 'date the merge', merged: true, state: 'closed', merge_commit_sha: MERGE_A, merged_at: mergedAt }, 'delivery-merged-21'), 200) + const rows = store.listDeliveryOutcomes(repoId, '2000-01-01T00:00:00.000Z') + assert.equal(rows.length, 1, 'the merge is one delivery') + assert.equal(rows[0]?.merged_at, mergedAt, "the row is dated by the pull request's merged_at, not by the webhook's receipt") + assert.equal(store.findWorkItemByPrUrl(repoId, prUrl)?.merged_at, mergedAt, 'the board item reads the same instant as the ledger') + } finally { + // built.close() closes the store it was given. + await built.close() + await github.close() + } +}) + +test('a merge with no merged_at in the payload is still dated at receipt', async () => { + const { built, store, repoId, github } = await webhookApp('no-merged-at') + try { + const fullName = 'o/delivery-webhook-no-merged-at' + const prUrl = `https://github.com/${fullName}/pull/22` + const send = async (action: string, pull: Record, delivery: string): Promise => { + const body = Buffer.from(JSON.stringify({ + action, installation: { id: 4242 }, repository: { full_name: fullName }, pull_request: pull, + })) + const response = await built.app.inject({ + method: 'POST', + url: '/api/github/webhook', + headers: { 'content-type': 'application/json', 'x-github-event': 'pull_request', 'x-github-delivery': delivery, 'x-hub-signature-256': signWebhook(body) }, + payload: body, + }) + return response.statusCode + } + assert.equal(await send('opened', { number: 22, html_url: prUrl, title: 'no merged_at', merged: false, state: 'open' }, 'delivery-opened-22'), 200) + const before = Date.now() + const closed = { number: 22, html_url: prUrl, title: 'no merged_at', merged: true, state: 'closed', merge_commit_sha: MERGE_B } + assert.equal(await send('closed', closed, 'delivery-merged-22'), 200) + const after = Date.now() + assert.equal(await send('closed', closed, 'delivery-merged-22-replay'), 200) + const rows = store.listDeliveryOutcomes(repoId, '2000-01-01T00:00:00.000Z') + assert.equal(rows.length, 1, 'a replayed merge delivery inserts nothing') + const recorded = Date.parse(rows[0]?.merged_at ?? '') + assert.ok(recorded >= before - 1000 && recorded <= after + 1000, 'without a merged_at the row keeps the receipt instant') + } finally { + // built.close() closes the store it was given. + await built.close() + await github.close() + } +}) + test('a failing default-branch run at a merge commit counts one post-merge failure per run', async () => { const { built, store, repoId, github } = await webhookApp('fail') try { From 67ade554913a6183d6f0dcf9450b9f2b962afb0f Mon Sep 17 00:00:00 2001 From: Fledge Platform Dev-3 Date: Tue, 24 Nov 2026 11:27:33 +0000 Subject: [PATCH 2/2] fix(deliveries): date a merge by the pull request's merged_at (#1290) The recorder and both work-item frames stamped nowIso() with the payload in hand, so a redelivered merge landed on the day of receipt. The app closure now takes one mergedAt parameter, threaded from pull_request.merged_at at both call sites, and keeps the receipt instant only when the payload carries no usable value. docs/architecture.md says which instant the ledger holds. --- changelog.d/1290.fixed.md | 1 + docs/architecture.md | 5 ++++- src/app.ts | 16 ++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 changelog.d/1290.fixed.md diff --git a/changelog.d/1290.fixed.md b/changelog.d/1290.fixed.md new file mode 100644 index 00000000..477c5d6a --- /dev/null +++ b/changelog.d/1290.fixed.md @@ -0,0 +1 @@ +A merged pull request is now dated by GitHub's own `merged_at` instead of the moment Fledge received the webhook, so a redelivered or replayed event no longer moves a merge to today on the repository's delivery ledger and board item. A payload that carries no `merged_at` keeps the receipt instant, and a replayed delivery still writes one row. diff --git a/docs/architecture.md b/docs/architecture.md index eadedee8..b04e1e71 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1650,7 +1650,10 @@ model and work type — and the facts that arrive later attach to it: a revert branch's recent commits, recorded once with its first instant), a follow-up issue whose body names the pull request, and each failing default-branch run whose head is the merge commit. A replayed webhook event inserts nothing because the unique -key is `(repo_id, pr_url)`. The repository's delivery tab prints the window: +key is `(repo_id, pr_url)`. The instant is GitHub's `merged_at` from the merge +payload, so a redelivered or replayed event dates the delivery by the merge and +not by the receipt; a payload that carries no `merged_at` falls back to the +receipt time. The repository's delivery tab prints the window: merged, reverted, the three rates and the cost per kept change, with `—` where a number was never recorded and a "too few deliveries" label on a single sample. Each rate is a count of deliveries over merged: the post-merge failure rate is diff --git a/src/app.ts b/src/app.ts index eeb09d28..537444e9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7163,9 +7163,12 @@ export async function buildApp(options: BuildOptions): Promise { // beside it and is best-effort, so a token or network failure leaves the row // un-reverted instead of failing the webhook delivery GitHub will not resend // for the same reason twice. - const recordMergedDelivery = (repo: RepoRow, jobId: string | null, prUrl: string, title: string, mergeCommit: string | null): void => { + const recordMergedDelivery = (repo: RepoRow, jobId: string | null, prUrl: string, title: string, mergeCommit: string | null, mergedAt: string | null): void => { if (mergeCommit === null || mergeCommit === '') return - jobs.recordMergedDelivery({ repoId: repo.id, jobId, prUrl, mergeCommit, mergedAt: nowIso(), title }) + // #1290: the payload carries the instant GitHub merged the pull request, so a + // redelivery dates the row by the merge and not by the day Fledge heard of it. + // A payload without one keeps the receipt instant. + jobs.recordMergedDelivery({ repoId: repo.id, jobId, prUrl, mergeCommit, mergedAt: mergedAt ?? nowIso(), title }) void scanDeliveryReverts(repo).catch((error: unknown) => log.warn('delivery revert scan failed', { repo: repo.id, error: String(error) })) } @@ -7241,21 +7244,22 @@ export async function buildApp(options: BuildOptions): Promise { if (event === 'pull_request') { const title = typeof pull.title === 'string' ? pull.title.slice(0, 200) : prUrl const merged = pull.merged === true + const pullMergedAt = typeof pull.merged_at === 'string' && Number.isFinite(Date.parse(pull.merged_at)) ? pull.merged_at : null const closed = action === 'closed' || pull.state === 'closed' store.updateJobsPrState(prUrl, merged ? 'merged' : closed ? 'closed' : 'open') if (existing === undefined && action !== 'opened') return if (existing === undefined) { - const created = store.createWorkItem({ id: randomId('work'), repoId: repo.id, userId: repo.user_id, title, prUrl, state: merged ? 'merged' : closed ? 'closed' : 'pr_ready', prState: merged ? 'merged' : closed ? 'closed' : 'open', mergedAt: merged ? nowIso() : null, mergeCommit: typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null }) - if (merged) recordMergedDelivery(repo, created.job_id, prUrl, title, typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null) + const created = store.createWorkItem({ id: randomId('work'), repoId: repo.id, userId: repo.user_id, title, prUrl, state: merged ? 'merged' : closed ? 'closed' : 'pr_ready', prState: merged ? 'merged' : closed ? 'closed' : 'open', mergedAt: merged ? (pullMergedAt ?? nowIso()) : null, mergeCommit: typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null }) + if (merged) recordMergedDelivery(repo, created.job_id, prUrl, title, typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null, pullMergedAt) return } if (closed) { store.updateWorkItem(existing.id, { state: merged ? 'merged' : 'closed', pr_state: merged ? 'merged' : 'closed', - ...(merged ? { merged_at: nowIso(), merge_commit: typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null } : {}), + ...(merged ? { merged_at: pullMergedAt ?? nowIso(), merge_commit: typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null } : {}), }) - if (merged) recordMergedDelivery(repo, existing.job_id, prUrl, title, typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null) + if (merged) recordMergedDelivery(repo, existing.job_id, prUrl, title, typeof pull.merge_commit_sha === 'string' ? pull.merge_commit_sha : null, pullMergedAt) } else { store.updateWorkItem(existing.id, { state: existing.state === 'in_review' ? 'in_review' : 'pr_ready', pr_state: 'open', pr_url: prUrl, title }) }