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
1 change: 1 addition & 0 deletions changelog.d/1290.fixed.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7163,9 +7163,12 @@ export async function buildApp(options: BuildOptions): Promise<BuiltApp> {
// 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) }))
}

Expand Down Expand Up @@ -7241,21 +7244,22 @@ export async function buildApp(options: BuildOptions): Promise<BuiltApp> {
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 })
}
Expand Down
70 changes: 70 additions & 0 deletions test/deliveryOutcome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>, delivery: string): Promise<number> => {
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<string, unknown>, delivery: string): Promise<number> => {
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 {
Expand Down
Loading