fix(server): make GitHub webhook registration idempotent (#68) - #71
Merged
Conversation
Auto-registration always POSTed a new hook instead of checking whether one with the same delivery URL already existed, so re-adds, reindexes, and restarts accumulated duplicate hooks on a repo — GitHub then fanned every push out N times. Add githubapi.ListWebhooks + EnsureWebhook: list existing hooks, match on config.url, PATCH the match (and prune same-URL duplicates) instead of creating, and only POST when none match. Wire both registration paths (tryAutoRegisterWebhook, reconciler.reconcileOne) through it. The reconciler now reuses an existing hook even when the stored WebhookID was lost, so a reregister sweep never leaves old + new hooks side by side. Covered by new githubapi tests (create / reuse / prune-duplicates / ignore-non-matching-URL) and a reconciler reuse test. Docs updated. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…doc caveat Address PR review: - When the matched hook is deleted between list and PATCH (404), the create-replacement branch now also prunes any other same-URL duplicates we listed, instead of leaving them behind. - WEBHOOKS.md §6: note that a reconcile sweep does NOT prune pre-existing same-URL duplicates when the stored hook id is still valid (it PATCHes that one and returns); re-adding the repo collapses them via EnsureWebhook. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This was referenced Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #68.
Problem
GitHub webhook auto-registration was not idempotent. Both registration paths created hooks blindly:
tryAutoRegisterWebhookalways calledCreateWebhook(a barePOST, no existence check).reconcileOneonlyPATCHed when it had a storedWebhookID; otherwise it created.There was no
ListWebhooksat all, so neither path could match onconfig.url. Every re-add / reindex / restart where the stored id was lost POSTed another identical hook → a repo accumulated multiple hooks with the same delivery URL, and GitHub fanned every push out N× (N× redundant clone/reindex + embedding-API calls). The docs even claimed dedup happened — it didn't.Fix
server/internal/githubapi/githubapi.goconfig.urltoHookResponse(newHookConfig).ListWebhooks(GET …/hooks, paginated).EnsureWebhook— idempotent entry point: lists hooks, matches on delivery URL, PATCHes the existing hook (refreshing secret/events) instead of creating, prunes any extra same-URL duplicates, and onlyPOSTs when none match. Returns acreated bool, handles the list→patch race (404 → create), and refuses to reuse a hook for a different URL.server/internal/httpapi/gitrepos.go— auto-register now goes throughEnsureWebhook.server/internal/tunnels/reconciler.go— the create fallback now goes throughEnsureWebhook(interface updated), so a repo whoseWebhookIDwas lost reuses its existing hook instead of duplicating. A reregister sweep never leaves old + new hooks side by side.Tests & docs
githubapitests (create / reuse / prune 3 duplicates → 1 / ignore non-matching URL) against a stateful in-memory hooks server.doc/WEBHOOKS.md§4 and §6 updated to describe the real idempotent behavior.go test ./...andgo vet ./...are green. Changes scoped toserver/anddoc/.Out of scope
Deliberately did not auto-delete hooks pointing at a different (stale) URL — that's the issue's "optional" cleanup and silently removing differently-targeted hooks is risky. Same-URL duplicate pruning (the reported bug) is handled.
🤖 Generated with Claude Code