# GitHub App

URL: https://swb.one/docs/blocks/github

## Make the app [#make-the-app]

Create a GitHub App on your account. Set the webhook address to your public SwB address plus `/github/webhook`. For example, use `https://swb.example.com/github/webhook`. Generate a webhook secret in a private file. SwB accepts secrets of at least 16 characters.

```bash
(umask 077; set -C; openssl rand -hex 32 > ~/.swb-github-secret)
```

Enter the secret in **Webhook secret**. Ask an agent to connect GitHub. Save the same secret through its claim link. Delete the private file. Restart SwB to load the secret. Ask the agent to call `connect` again for any remaining App ID or private key links. Paste the full contents of the `.pem` file in the private key field.

## Permissions [#permissions]

Under **Repository permissions**, use the access levels in the table.

| Repository resource | Access | Events |
| - | - | - |
| **Issues** | **Read & write** | `issues`, `issue_comment` |
| **Pull requests** | **Read & write** | `pull_request`, `pull_request_review`, `pull_request_review_comment`, `pull_request_review_thread` |
| **Contents** | **Read-only** | `push` |
| **Checks** | **Read-only** | `check_run`, `check_suite` |
| **Actions** | **Read-only** | `workflow_run` |
| **Metadata** | **Read-only** automatically | |

Install the app on the repositories you want.

## Route events [#route-events]

```yaml
blocks:
  github:
    use: github
    sinks:
      team: {url_env: SWB_EXAMPLE_GITHUB_SINK_URL}
    routes:
      "acme/app": [team]
      "acme/*": [team]
```

Set `SWB_EXAMPLE_GITHUB_SINK_URL` to the receiver's address in your server environment.

A **sink** is a URL that receives a copy of an event. SwB sends each matching sink a compact JSON event in `watch` mode. A repository with a sink route uses `watch` by default. Channel and space modes take priority. SwB quotes additional details in `metadata`. Each request uses the event's delivery ID as its `Idempotency-Key`. SwB retries connection failures, timeouts and HTTP 5xx responses.

If Cloudflare Access protects your host, let `/github/webhook` through. The GitHub signature protects that path.

## Owner accounts [#owner-accounts]

In `off` mode, SwB drops all GitHub events for the space, including events from owner accounts.

Use `work` mode to accept events from configured owner accounts only. Replace `USERNAME` in `https://api.github.com/users/USERNAME` with your GitHub username. Open the URL. Copy the numeric `id` into the space's `owner_ids` list. Restart SwB to apply the setting.

```yaml
blocks:
  github:
    use: github
    spaces:
      acme/app:
        owner_ids: [4242]
        mode:
          mode: work
          target: agent:beta
```

Use `blocks.github.owner_ids` to set the default owner IDs. A space's `owner_ids` list replaces that default. An empty space list clears it.

SwB checks the author of each title, body or comment. The text counts as `owner` only when its own author is in `owner_ids` and is a person. SwB quotes branch names, refs, workflow titles and check names as `external`.

A `check_run` with a check name carries `external` trust, even when the owner triggers it. A pull request from a fork can set the check name. Use IDs such as `github:4242` in access rules. These rules give access separately from trust.

For GitHub conversation actions, pass the SwB conversation ID in `conversation` or `handle`.

## Config [#config]

| Key | Use |
| - | - |
| `blocks.github` | Settings for GitHub events and actions |
| `blocks.github.use` | Block type. Use `github` |
| `blocks.github.enabled` | Set `false` to skip this block |
| `blocks.github.app_id_env` | Name of the variable that holds the App ID |
| `blocks.github.private_key_env` | Name of the variable that holds the App private key |
| `blocks.github.token_env` | Name of the variable that holds a fine-grained token as an alternative to App credentials |
| `blocks.github.secret_env` | Name of the variable that holds the webhook secret |
| `blocks.github.sinks` | Settings by event receiver name |
| `blocks.github.sinks.*.url_env` | Name of the variable that holds the receiver address |
| `blocks.github.routes` | Map from repo names or `owner/*` to lists of event receivers |
| `blocks.github.owner_ids` | Default list of numeric GitHub user IDs trusted as owners |
| `blocks.github.spaces` | Named sets of repositories that share settings. A name such as `acme/app` selects that repository |
| `blocks.github.spaces.*.repo` | Repository name in `owner/repo` form |
| `blocks.github.spaces.*.owner_ids` | List of numeric GitHub user IDs that replaces the default for this space |
| `blocks.github.spaces.*.mode` | Default [channel mode](/docs/channel-modes) for the repositories in this space |
| `blocks.github.spaces.*.mode.target` | Target in the mode settings. Choose an address such as `agent:beta` |
| `blocks.github.spaces.*.project` | Default SwB project for events from this space |

Bind an agent to a repository with `space_mode(block="github", space="acme/app", mode="triage", target="agent:beta")`. Use `mode="assist"` for assist mode. Choose a target from `targets`. The linked project route is the fallback.

For outside senders, grant access to both `channel:github/acme/app` and `target:beta`. See [channel visibility](/docs/channel-modes#allow-outside-github-senders) for the access rules.

In `watch` mode, SwB sends a digest to the target mailbox after `modes.digest_minutes` or once it collects 50 events for that mailbox and space. See [Watch digests](/docs/channel-modes#read-a-watch-digest).
