diff --git a/docs/operations.md b/docs/operations.md index 10f8bbd23..106c67283 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -28,10 +28,10 @@ The poll stays the backup and serves repos without the GitHub App. With webhooks - Webhook: Active. Webhook URL `/webhooks/github`, content type `application/json`, secret: the value of `github_app_webhook_secret` in the secrets file (GitHub returns it when the app is registered from the dashboard; if the key is empty, set a secret in the app's settings and store the same value as `github_app_webhook_secret` in the secrets file). The dashboard reads the secret at start, so restart it after the secret changes. - Permissions (Permissions & events): read access to Issues, Pull requests and Checks (and Commit statuses if `status` is wanted; AutoCraft ignores it). -- Subscribe to events: Issues, Issue comment, Pull request, Pull request review, Pull request review comment, Check suite, Check run. Check events count when their action is `completed`; a fork's pull request, which GitHub lists no pull requests for, is found through the event's head commit. +- Subscribe to events: Issues, Issue comment, Pull request, Pull request review, Pull request review comment, Check suite. A check suite counts when its action is `completed`; a fork's pull request, which GitHub lists no pull requests for, is found through the event's head commit. - Check Recent Deliveries after saving: the `ping` delivery answers 200. -`POST /webhooks/github` needs no session. It checks `X-Hub-Signature-256` (HMAC-SHA256 of the raw body, constant-time compare) before parsing and answers 401 for a wrong signature or when no secret is set, 413 over 1 MB. From a verified delivery it takes only the repository's full name and the item number (for completed check events, the pull requests the event lists whose base repository is this one, or the open ones at its head commit), and only for tracked repos. It writes one row per item to `sync_requests`, so a burst of events collapses, and answers 202. Payload text is neither stored nor logged and never reaches a prompt. +`POST /webhooks/github` needs no session. It checks `X-Hub-Signature-256` (HMAC-SHA256 of the raw body, constant-time compare) before parsing and answers 401 for a wrong signature or when no secret is set, 413 over 1 MB. From a verified delivery it takes only the repository's full name and the item number (for completed check suites, the pull requests the event lists whose base repository is this one, or the open ones at its head commit), and only for tracked repos. It writes one row per item to `sync_requests`, so a burst of events collapses, and answers 202. Payload text is neither stored nor logged and never reaches a prompt. The runner drains `sync_requests` every 5 seconds while rows exist (`webhookTick` in `src/runner/runner.ts`). It skips a pass while a sync run holds its lock and stops when fewer than 1,000 rate-limit points are left. Each item is read with the sync's single-item document and written by the sync's writer, so the live topics update open pages. A failed read stays queued until it is 6 hours old; the next poll reads the item anyway. Both the poll and the re-read write `item_reads.synced_at` (one statement per page), and a re-read rewrites the item's comments even when the item row is unchanged. The runner uses one GitHub client seeded from the last sync's rate-limit reading and also skips a pass while the full-sync lock is held. A read that fails for a reason other than a transient one is dropped from the queue and logged. The item page shows it as "updated hh:mm" beside "On GitHub". diff --git a/src/github/webhooks.ts b/src/github/webhooks.ts index 5e6bccf29..cfaf7986e 100644 --- a/src/github/webhooks.ts +++ b/src/github/webhooks.ts @@ -31,7 +31,7 @@ function trackedName(db: DatabaseSync, payload: Obj): string | null { } /** - * The item numbers an event names. A check event counts when it completes and names the pull requests of its head; + * The item numbers an event names. A completed check suite counts and names the pull requests of its head; * GitHub lists none for a fork's pull request, so the head commit is matched to this repository's open pull requests. */ export function itemNumbers(db: DatabaseSync, repo: string, event: string, payload: Obj): number[] { @@ -42,9 +42,8 @@ export function itemNumbers(db: DatabaseSync, repo: string, event: string, paylo case "pull_request": case "pull_request_review": case "pull_request_review_comment": return one(obj(payload.pull_request).number); - case "check_suite": - case "check_run": { - const body = obj(payload[event]); + case "check_suite": { + const body = obj(payload.check_suite); if (payload.action !== "completed") return []; const repoId = obj(payload.repository).id; const listed = Array.isArray(body.pull_requests) ? body.pull_requests : []; diff --git a/src/mirror.ts b/src/mirror.ts index 0495b9708..f5ce63c25 100644 --- a/src/mirror.ts +++ b/src/mirror.ts @@ -306,8 +306,11 @@ function writePage(db: DatabaseSync, repo: string, kind: Kind, detail: Detail, n if (!force && old && COLUMNS.every((c) => old[c] === row[c])) continue; insert.run(row); if (detail === "full") { - del.run(repo, n.number); - for (const row of commentRows(repo, kind, n, own)) insComment.run(...row); + const rows = commentRows(repo, kind, n, own); + // A forced re-read of an unchanged item upserts in place: an identical comment bumps no topic. Comments past the newest 20 go. + if (!force) del.run(repo, n.number); + for (const row of rows) insComment.run(...row); + if (force) db.prepare(`DELETE FROM main.comments WHERE repo = ? AND number = ? AND id NOT IN (${rows.map(() => "?").join(",") || "''"})`).run(repo, n.number, ...rows.map((r) => r[2] as string)); } } if (nodes.length) { diff --git a/test/webhooks.test.ts b/test/webhooks.test.ts index 03365be37..6e0d9c5df 100644 --- a/test/webhooks.test.ts +++ b/test/webhooks.test.ts @@ -70,3 +70,23 @@ test("a request that fails for a reason other than a transient one is dropped so assert.equal(calls, 2); assert.deepEqual(queued(db), []); }); + +test("re-reading an unchanged item bumps no items topic and keeps its newest comments", async () => { + const db = scratch(); + const comment = (i: number) => ({ id: `c${i}`, author: { login: "x", __typename: "User" }, authorAssociation: "NONE", createdAt: "2026-01-02T00:00:00Z", body: `b${i}`, url: "u" }); + const node = { + __typename: "Issue", repository: { name: "a", owner: { login: "storytold" } }, number: 5, title: "t", body: "", url: "u", author: { login: "x" }, + authorAssociation: "NONE", createdAt: "2026-01-01T00:00:00Z", updatedAt: "2026-11-02T10:00:00Z", closedAt: null, state: "OPEN", + labels: { nodes: [] }, assignees: { nodes: [] }, commentCount: { totalCount: 2 }, stateReason: null, comments: { nodes: [comment(1), comment(2)] }, + }; + const c = validating({ spent: 0, remaining: 4000 as number | null, async query() { return { repository: { issueOrPullRequest: node } }; } }); + const read = async () => { deliver(db, "issues", { repository: { full_name: "storytold/a", name: "a" }, issue: { number: 5 } }); await drainSyncRequests(db, c); }; + const seq = () => (db.prepare("SELECT seq FROM change_seq WHERE topic = 'items'").get() as { seq: number }).seq; + await read(); + const before = seq(); + await read(); + assert.equal(seq(), before); + node.comments = { nodes: [comment(2)] }; + await read(); + assert.deepEqual(db.prepare("SELECT id FROM comments WHERE repo = 'a' AND number = 5").all().map((r) => r.id), ["c2"]); +});