# Rulesets

> For AI agents: the complete documentation index is at https://buddy.works/llms.txt

Rulesets in Buddy Git: required pull requests, required checks from pipelines and tests, blocked force pushes, deletions and ref creation, and bypass rules for people and Buds.

A ruleset defines rules for branches and tags in a Buddy Git repository. Buddy checks the rules on every push, commit made in the editor, pull request merge, and ref deletion. A change that violates a rule is rejected, and the error message identifies the ruleset and the reason.

Rulesets replace the old push permissions that were set separately for each branch. One ruleset covers many branches or tags at once.

## Creating a ruleset

1. In the project, go to Branches and open the Rulesets tab. Rulesets for tags are in Tags > Rulesets.
2. Click **New ruleset**.
3. Enter a name and an identifier, and list the branches or tags in the **Refs** field.
4. Select the rules in the **Rules** section.
5. In the **Bypass** tab, choose who can bypass the rules.
6. Click **Add ruleset**.

![Ruleset Settings tab with rules and the required build check](https://buddy.works/docs/version-control/rulesets/ruleset-settings.png)

> **Success**
>
> You can also create a ruleset for a single branch from the branch list: in All branches, open the menu next to the branch and select **Limit pushing...**. The form opens with the branch name in **Refs** and the **Restrict updates** and **Restrict deletions** rules selected.

The ruleset list shows the refs, rules, and bypass entries for each ruleset. The toggle next to a ruleset disables it without deleting it. The **Disable this ruleset** option in the form also disables the ruleset. The **RULES** column in All branches shows which branches are covered by rulesets.

![Ruleset list in the Rulesets tab](https://buddy.works/docs/version-control/rulesets/rulesets-list.png)

Rulesets have no test mode. An enabled ruleset blocks changes immediately, so check the patterns in the **RULES** column before you enable rules on a production branch.

## Refs

The **Refs** field accepts 1 to 20 patterns. The syntax is the same as in pipeline triggers:

| Pattern | Matches |
| :--- | :--- |
| `refs/heads/main` | the `main` branch |
| `refs/tags/v*` | tags starting with `v` |
| `main` | the branch and the tag named `main` |
| `refs/heads/agent/*` | branches under `agent/` |
| `:default` | the default branch of the repository, also after it changes |
| `(regex)` | refs matching the regular expression in parentheses |

Full pattern reference: [Wildcards](https://buddy.works/docs/pipelines/introduction/triggering-pipelines/wildcards.md).

## Rules

| Rule in the UI | YAML field | Effect |
| :--- | :--- | :--- |
| Restrict creations | `restrict_creation` | blocks creating new refs that match the pattern |
| Restrict updates | `restrict_update` | blocks pushes to an existing ref, including pull request merges |
| Restrict deletions | `restrict_deletion` | blocks deleting the ref |
| Require a pull request before merging | `require_pull_request` | commits are made on another branch and reach the protected one only through a pull request merge; direct pushes are rejected |
| Block force pushes | `block_force_push` | blocks pushes that rewrite history |
| Require checks | `required_checks` | a pull request merge requires successful pipelines and test sessions |

`require_pull_request`, `block_force_push`, and `required_checks` apply only to branches. A tag ruleset saves them, but Buddy does not enforce them.

The first push to an empty repository is not subject to `restrict_creation`.

## Required checks

After you select **Require checks**, add status sources in the **REQUIRED CHECKS** table:

- pipelines from this project
- [unit test](https://buddy.works/docs/tests/unit-tests.md) suites
- [visual test](https://buddy.works/docs/tests/visual-tests.md) suites

A pull request can be merged only after every required source reports success for the latest commit on the branch. The **Require branch to be up to date before merging** option (`require_up_to_date`) also requires the branch to contain the latest changes from the base branch. After the branch is updated, the required checks must pass again on the new latest commit.

Only a pipeline that sets the commit status can be a required check. A pipeline with `do_not_create_commit_status: true` is rejected:

```
Required check source does not mark commits with its status: pipeline lint
```

You also cannot turn off the commit status in a pipeline that is a required check:

```
Required by ruleset "Protect main" - remove it from the ruleset first
```

### Missing sources

You can delete a pipeline that is a required check. The ruleset then lists that pipeline in the `missing_sources` field, and the CLI prints `Missing sources: build (PIPELINE)`. The entry disappears when the project has a pipeline with the same identifier again.

A new ruleset with a source that does not exist cannot be saved:

```
Required check source not found: pipeline build
```

## Bypass

The **Bypass** tab defines who can bypass the ruleset's rules:

- **Administrators & Project Managers** (`admins`) - workspace administrators and project managers
- **Others** (`others`) - all other project members
- **Member** (`users`) - selected workspace members
- **Bud** (`users`) - [Buds](https://buddy.works/docs/basics/collaboration/roles.md), AI agents with their own account in the workspace
- **Group** (`groups`) - groups assigned to the project

Each entry is **Allowed** (`ALLOWED`) or **None (Deny)** (`DENIED`). A member or Bud must have access to the project, and a group must be assigned to it. Otherwise Buddy rejects the ruleset:

```
User 31 is not a member of the project
```

![Bypass tab: a member and a Bud set to Allowed; in the GROUP section, Administrators & Project Managers set to Allowed and Others set to None (Deny)](https://buddy.works/docs/version-control/rulesets/ruleset-bypass.png)

Buddy checks the entries in this order:

1. A personal entry in **Member** or **Bud**. If one exists, its setting takes precedence.
2. Groups, including **Administrators & Project Managers**. If there is no personal entry, Buddy checks all groups the person belongs to.
3. **Others** - for members who do not match any of the entries above.

> **Info**
>
> **Exception for one person**
>
> An administrator with a `DENIED` entry in `users` is subject to the rules despite `admins: true`, and a Bud with `ALLOWED` in `users` bypasses them despite `others: DENIED`. The group settings stay unchanged.

## Notes for humans and agents

A ruleset has two notes:

- **Note for humans** (`note`) - an explanation for team members
- **Note for agents** (`agent_note`) - an instruction for AI agents, for example where to create branches

The API, the CLI (`bdy ruleset get`), and the YAML export return both notes. The rejection message does not include them, so an agent should read the ruleset before performing operations on the protected refs. More about notes: [Resource notes](https://buddy.works/docs/ai-agents/resource-notes.md).

![Ruleset with notes for humans and for agents](https://buddy.works/docs/version-control/rulesets/ruleset-notes.png)

## Rejection messages

| Situation | Message |
| :--- | :--- |
| Commit directly to a protected branch | `Ruleset "Protect main": changes must be made through a pull request` |
| Merge while a check is still running | `Ruleset "Protect main": required checks have not passed: pipeline build: in_progress` |
| Merge after a failed check | `Ruleset "Protect main": required checks have not passed: pipeline build: failed` |
| Branch out of date with the base branch | `Ruleset "Protect main": branch is out of date with the base branch - update it and wait for the checks of the new commit` |
| Deleting a protected branch | `Ruleset "Protect main": deleting this ref is not allowed` |
| Creating a protected tag | `Ruleset "Release tags": creating this ref is not allowed` |

A rejected push in the terminal:

```bash
$ git push origin master
To https://app.buddy.works/my-workspace/my-project
 ! [remote rejected] master -> master (Ruleset "Protect main": changes must be made through a pull request)
error: failed to push some refs to 'https://app.buddy.works/my-workspace/my-project'
```

## YAML

You can save a ruleset as YAML. This example protects the default branch: changes go in only through a pull request with a successful `build` pipeline, and force pushes and branch deletion are blocked for everyone.

```yaml
identifier: protect-main
name: Protect main
refs:
  - ":default"
bypass:
  admins: false
  others: DENIED
rules:
  required_checks:
    pipelines:
      - build
    unit_tests_suites: []
    visual_tests_suites: []
    require_up_to_date: true
  require_pull_request: true
  block_force_push: true
  restrict_deletion: true
note: Main changes only through pull requests with a green build
agent_note: Open a pull request instead of pushing to main
```

The second example reserves branches for an agent. Only the Bud with ID `46` and administrators can create and delete branches under `agent/`:

```yaml
identifier: agent-branches
name: Agent branches
refs:
  - refs/heads/agent/*
bypass:
  admins: true
  others: DENIED
  users:
    - id: 46
      access: ALLOWED
rules:
  restrict_creation: true
  restrict_deletion: true
agent_note: Create your branches under agent/ and open a pull request to main
```

Import the file in the **Import YAML** tab of the **New ruleset** form. A ruleset with the same identifier is replaced. To get an existing ruleset as YAML, use **Download YAML** in the ruleset menu.

> **Warning**
>
> **Updates replace whole sections**
>
> When you edit through YAML, the CLI or the API, the `rules` and `bypass` objects are saved as a whole. A rule missing from the object you send is turned off.

## CLI and API

The [CLI](https://buddy.works/docs/cli.md) manages rulesets with `bdy ruleset` (alias `bdy rs`):

```bash
bdy ruleset list -w my-workspace -p my-project
bdy ruleset create --yaml @protect-main.yml -w my-workspace -p my-project
bdy ruleset update protect-main --yaml @protect-main.yml -w my-workspace -p my-project
bdy ruleset delete protect-main -w my-workspace -p my-project
```

Workspace administrators and project managers create and edit rulesets. An API token needs the `RULESET_READ` scope to read and `RULESET_MANAGE` to make changes. Full endpoint list: [Rulesets API](https://buddy.works/docs/api/version-control/rulesets.md).

See also:

- [Pull requests](https://buddy.works/docs/git-hosting/pull-requests.md) - what a blocked merge looks like and how to update a branch
- [Branches](https://buddy.works/docs/git-hosting/branches.md) - creating and merging branches
- [Wildcards](https://buddy.works/docs/pipelines/introduction/triggering-pipelines/wildcards.md) - ref pattern syntax
- [Resource notes](https://buddy.works/docs/ai-agents/resource-notes.md) - notes for AI agents in Buddy resources


---
Original source: https://buddy.works/docs/git-hosting/rulesets