# Scopes and levels

> branchd's feature scopes, what each one allows, and the read, write, maintain and admin levels that bundle them.
A scope names **one feature at one level**. The verbs are `read`, `edit`
and `admin`, and each implies the ones before it: `admin` ⊃ `edit` ⊃
`read`. Ask for, or issue, only the scopes the job needs. A key that can
pull images cannot clone, and a token that can clone cannot push.

The same scopes are checked everywhere: the API, git over HTTPS, the
container registry, Go modules and release downloads.

## Feature scopes

| Scope                         | Allows                                                                                                                                                                                                        |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `forge:contents:read`         | Clone and fetch, read files, commits and blame, compare, fetch Go modules                                                                                                                                     |
| `forge:contents:edit`         | Push, and create or delete branches and tags                                                                                                                                                                  |
| `forge:repository:read`       | Read repositories and their settings                                                                                                                                                                          |
| `forge:repository:edit`       | Change repository settings                                                                                                                                                                                    |
| `forge:repository:admin`      | Create, delete, rename and transfer repositories; visibility, access and grants, webhooks and mirrors                                                                                                         |
| `forge:group:read`            | See groups and their settings                                                                                                                                                                                 |
| `forge:group:edit`            | Create and change groups, their labels and settings                                                                                                                                                           |
| `forge:group:admin`           | A group's access, bots and runner pools                                                                                                                                                                       |
| `forge:pull-request:read`     | Read pull requests and their comments                                                                                                                                                                         |
| `forge:pull-request:edit`     | Open, edit, close and comment on pull requests                                                                                                                                                                |
| `forge:pull-request:merge`    | Merge pull requests, where branch rules allow                                                                                                                                                                 |
| `forge:review:read`           | Read reviews                                                                                                                                                                                                  |
| `forge:review:edit`           | Review and approve pull requests                                                                                                                                                                              |
| `forge:check:read`            | Read checks on commits                                                                                                                                                                                        |
| `forge:check:edit`            | Report checks on commits. A check reported this way never counts as a required check.                                                                                                                         |
| `forge:branch-rule:read`      | Read branch rules                                                                                                                                                                                             |
| `forge:branch-rule:edit`      | Change branch rules                                                                                                                                                                                           |
| `forge:release:read`          | Read releases and download their assets                                                                                                                                                                       |
| `forge:release:edit`          | Create, edit and publish releases                                                                                                                                                                             |
| `forge:package:read`          | Pull container images, download generic packages and install npm packages; see tags, versions and retention                                                                                                   |
| `forge:package:edit`          | Push container images; publish generic and npm packages; move aliases and dist-tags                                                                                                                           |
| `forge:package:admin`         | Set and apply retention; delete images and generic packages; deprecate and unpublish npm versions; claim npm scopes. Deleting also needs a key at the maintain level (it must hold `forge:branch-rule:edit`). |
| `forge:terraform-state:read`  | Read Terraform states, secrets included (also needs write access on the repository)                                                                                                                           |
| `forge:terraform-state:edit`  | Write, lock and unlock Terraform states, as Terraform does                                                                                                                                                    |
| `forge:terraform-state:admin` | Download, restore and delete Terraform state versions and states; force-unlock them                                                                                                                           |
| `forge:pipeline:read`         | Read runs, logs and drafts                                                                                                                                                                                    |
| `forge:pipeline:edit`         | Run, retry and cancel pipelines, fire schedules, edit drafts                                                                                                                                                  |
| `forge:pipeline:admin`        | Change pipeline settings, variables and secrets                                                                                                                                                               |

Webhooks and mirrors are under `forge:repository:admin` because both hold
or send secrets.

Some scopes are never put on a key: your SSH keys (`forge:ssh-key:*`),
session credentials and your profile. A key can never manage keys, SSH
keys, session credentials, bots or access grants, whatever scopes it has.

## Levels

A level is a named bundle of scopes, offered by the console and by the
CLI's `--level` as a shortcut. Each level contains the one before it.

| Level        | Adds                                                                                                                                                               |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **read**     | Every feature's `:read`: `group`, `repository`, `contents`, `pull-request`, `review`, `check`, `branch-rule`, `release`, `package`, `pipeline`                     |
| **write**    | `contents:edit`, `pull-request:edit`, `review:edit`, `check:edit`, `release:edit`, `package:edit`, `pipeline:edit`, `terraform-state:read`, `terraform-state:edit` |
| **maintain** | `pull-request:merge`, `branch-rule:edit`, `repository:edit`, `pipeline:admin`, `terraform-state:admin`                                                             |
| **admin**    | `repository:admin`, `group:edit`, `group:admin`, `package:admin`                                                                                                   |

A key below the maintain level stays at write access even where your role
is higher. It cannot create or force-update protected branches or tags, or
delete images.

## Choosing scopes: examples

| Job                                                                            | Scopes                                                     |
| ------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| Clone repositories in CI                                                       | `forge:contents:read`                                      |
| Pull images from the registry, download generic packages, install npm packages | `forge:package:read`                                       |
| Push images, publish generic and npm packages                                  | `forge:package:edit`                                       |
| Run `terraform plan` and `apply` against a [state](https://docs.branchd.run/branchd/terraform-state)   | `forge:terraform-state:read`, `forge:terraform-state:edit` |
| Publish releases from a build                                                  | `forge:release:edit`, `forge:contents:read`                |
| Report a status check from an external system                                  | `forge:check:edit`                                         |
| A review bot that comments on pull requests                                    | `forge:pull-request:edit`, `forge:contents:read`           |

Source: https://docs.branchd.run/branchd/access/scopes
