GitHub webhooks for the Firstmate watcher
Many Firstmate homes share one GitHub account, so polling pull requests drains the shared REST quota. With webhooks, GitHub tells the host when a pull request merges, and the watcher reads the API only as a slow fallback.
Three parts, all on the Firstmate profile with development on:
fm-gh-webhook(apps/fm-gh-webhook/) is a small Rust service on127.0.0.1:8788. It acceptsPOST /gh-webhook, checksX-Hub-Signature-256(HMAC-SHA256 underGH_WEBHOOK_SECRET), and stores a file for each merged pull request (pull_request). It also records each completedcheck_suite,check_runandworkflow_runas one file per check underci/<owner>__<repo>__<sha>/. The watcher reads onlymerged/for now. The secret is never logged. Without it the service exits 78 and stays down.Firstmate patch
0007-watch-webhook-events.patch: the merged-PR poll reads these files first. It calls the API only once perFM_GH_FALLBACK_POLLseconds for each pull request (default 600). Before it does, it readsrate_limit(at most once a minute). Below 20 percent remaining, the interval is four times longer. Below 5 percent, it makes no call. Events live in$XDG_STATE_HOME/crewship/gh-webhook, or~/.local/state/crewship/gh-webhookwhenXDG_STATE_HOMEis unset (FM_GH_EVENTS_DIRfor the poll,GH_WEBHOOK_DIRfor the receiver). The receiver uses the same rule and exits 78 when none ofGH_WEBHOOK_DIR,XDG_STATE_HOMEandHOMEis set.Merge attempts and manual PR commands still call the API when you run them. They are not polled.
Enable it
The playbook installs and starts the service. It never registers a webhook, opens the funnel or writes the secret.
Add
GH_WEBHOOK_SECRET=<long random string>to~/super.env, then runsystemctl --user restart fm-gh-webhook.Publish the port on the host's Tailscale funnel:
tailscale funnel --bg --set-path /gh-webhook http://127.0.0.1:8788/gh-webhook. The payload URL ishttps://<host>.<tailnet>.ts.net/gh-webhook.In each repository, open Settings, Webhooks, Add webhook:
Payload URL: the URL from step 2.
Content type:
application/json.Secret: the same string as in step 1.
Events: Pull requests, Check suites, Check runs, Workflow runs.
Check the Recent Deliveries tab: a merge shows
204; a bad secret shows401.
Until a repository has the webhook, its pull requests are found by the fallback poll, so merge detection takes up to FM_GH_FALLBACK_POLL seconds. Set the variable in the watcher's environment to change it.