# GitHub webhooks

> Have GitHub push pull request changes to Flow at once with a ghx webhook receiver, set up from Settings or the command line.

By default ghx polls GitHub for changes every few minutes. With webhooks, GitHub pushes them instead: a small **receiver** takes GitHub's webhook deliveries, and every Mac's ghx daemon **follows** it and updates its cache the moment a pull request, review, comment or check changes. Flow refreshes as soon as the cache does.

```text
GitHub ──POST /hook──▶ ghx webhooks serve ──GET /stream──▶ ghx daemon (each Mac)
         (Tailscale Funnel)    (one machine)
```

You can run the receiver on one of your Macs from Settings, or on a server you keep running.

## Set it up from Settings

Settings › General › Webhooks has three settings, all off by default:

| Setting | Default | Meaning |
|---|---|---|
| `webhooks.serve` | `false` | **Run the receiver on this Mac** while Flow is open (`ghx webhooks serve`). This Mac follows its own receiver. |
| `webhooks.funnel` | `false` | **Tailscale Funnel**: have the receiver bring a Funnel up so GitHub can reach it from the internet, and take it down when it stops. Only applies while the receiver runs on this Mac. |
| `webhooks.follow_url` | `""` | **Follow a webhook receiver**: the URL of a receiver another machine runs (`https://…` or `http://127.0.0.1:…`). Not used while this Mac runs the receiver. |

### Run the receiver on this Mac

1. Turn on **Run the receiver on this Mac**. Flow creates the receiver's secret and subscriber token the first time.
2. To let GitHub reach it from the internet, turn on **Tailscale Funnel** (needs Tailscale installed, logged in, and Funnel enabled for your tailnet). The row is disabled while Flow can't find Tailscale's CLI.
3. Under the settings, the **Payload URL** row shows the address GitHub should post to once the receiver runs, with **Copy URL** and **Copy secret** buttons.
4. Click **Register for OWNER/REPO…** to create the webhook on the active session's repository (you need admin rights on it), or add it on GitHub yourself (below).

The **Status** row shows how the receiver, the Funnel and the follower are doing, such as "Receiving · 12 deliveries · last 2m ago". Flow stops the receiver when you turn the setting off or quit Flow, taking its Funnel down first.

### Follow a receiver on another machine

1. Paste the receiver's URL into **Follow a webhook receiver** and press Enter.
2. A **Subscriber token** field appears. Paste the receiver's token (`ghx webhooks token` on the machine that runs it) and press Enter or **Follow**.

Flow hands the token to ghx once, on stdin, and never stores, shows or logs it. Paste a new one to replace it.

## Set it up from the command line

### 1. On the machine that runs the receiver

```sh
ghx webhooks init                 # creates ~/.ghx/webhooks.json (0600): a webhook secret and a subscriber token
ghx webhooks serve --funnel       # the receiver on 127.0.0.1:8477, plus a Tailscale Funnel to it
```

`serve --funnel` prints the payload URL (`https://NODE.TAILNET.ts.net/hook`) on stderr. Keep it running (with launchd, systemd or tmux).

| Option | Meaning |
|---|---|
| `--port N`, `--bind ADDR` | Listen somewhere other than `127.0.0.1:8477`. |
| `--funnel` | Bring a Tailscale Funnel up to the receiver, and take it down on exit. |
| `--funnel-port 443\|8443\|10000` | Use another public Funnel port. |

The Funnel is optional: without `--funnel`, put any HTTPS reverse proxy in front of `127.0.0.1:8477`. `--funnel` never replaces something your node already serves on that port, and on exit it removes only the mapping it added. If Tailscale is missing, stopped, logged out or Funnel isn't enabled, the receiver still runs and `ghx webhooks status` says why. Enabling Funnel is a one-time `tailscale funnel 8477`.

### 2. Point GitHub at it

Let ghx create the webhook (needs admin on the repository and the `admin:repo_hook` scope: `gh auth refresh -s admin:repo_hook`):

```sh
ghx webhooks register owner/repo                       # uses the running receiver's Funnel URL
ghx webhooks register owner/repo --url https://example.com/hook
```

It updates a hook already pointing at that URL instead of adding a second one. Or add it in the repository's settings on GitHub (Settings › Webhooks › Add webhook):

| Field | Value |
|---|---|
| Payload URL | the payload URL above |
| Content type | `application/json` |
| Secret | the output of `ghx webhooks secret` |
| Events | Pull requests, Pull request reviews, Pull request review comments, Issue comments, Issues, Pushes, Statuses, Check runs, Check suites |

GitHub's test ping shows up as `last ping` in `ghx webhooks status`.

### 3. On each Mac

```sh
ssh server ghx webhooks token | ghx webhooks follow https://NODE.TAILNET.ts.net --token-stdin
ghx webhooks status
```

The token is read from stdin (typed without echo at a terminal), never from the command line. The running ghx daemon picks the change up within a few seconds, with no restart, reconnects with a backoff and resumes where it left off. `ghx webhooks follow --off` stops following.

## Commands

| Command | What it does |
|---|---|
| `ghx webhooks init [--rotate]` | Create the secret and subscriber token; `--rotate` replaces both. |
| `ghx webhooks secret`, `ghx webhooks token` | Print the webhook secret or the subscriber token. The only commands that print a secret. |
| `ghx webhooks serve [--port N] [--bind ADDR] [--funnel] [--funnel-port P]` | Run the receiver. |
| `ghx webhooks follow URL --token-stdin`, `follow --self`, `follow --off` | Follow a receiver, follow this machine's own receiver, or stop. |
| `ghx webhooks register OWNER/REPO [--url URL] [--hostname H]` | Create or update the repository's webhook through `gh api`. |
| `ghx webhooks status [--json]` | The receiver, Funnel and follower state. Never prints a secret. |

`flow-cli read webhooks` prints Flow's view: the three settings and the last status, never a secret or token.

## Good to know

- **Rotating secrets.** `ghx webhooks init --rotate` replaces the secret and the token. Restart `serve`, re-run `register`, and run `follow` again on every other Mac (a machine following its own receiver with `follow --self` rotates with it).
- **Missed deliveries.** If a Mac was away longer than the receiver's replay buffer holds (512 deliveries or 64 MiB), or the receiver restarted, the daemon re-syncs its watched repositories once and carries on.
- **Keep polling on.** GitHub does not retry a delivery the receiver could not take, so leave ghx's polling as a safety net (`GHX_POLL_INTERVAL_S`, slower if you like).
- Repositories ghx neither watches nor caches are ignored. Set `GHX_WEBHOOKS_MERGE=0` to treat every delivery as a plain "fetch this" signal instead of merging its payload.
- Other knobs: `GHX_WEBHOOKS_FILE` (the state file, `~/.ghx/webhooks.json`), `GHX_WEBHOOKS_PORT` (8477), `GHX_TAILSCALE` (which Tailscale CLI to use).

## Limits

- The receiver is a small server on the public internet when exposed through a Funnel; a flood of requests can keep it busy.
- Flow only stops a receiver it started itself. A receiver you started in a terminal, or with launchd, keeps running when you turn the setting off.
- The secret and token live only in `~/.ghx/webhooks.json`, readable by you alone.

See [ghx and gitx](https://flow.worldbreakstudios.com/docs/cli/ghx-and-gitx.md) for the cache itself.
