Sub topic 13
Configuring the queue through the rulesets API
This chapter shows how to create, read, update, and delete a merge queue rule with the GitHub REST API instead of the settings page. It assumes you can already enable a queue in the UI, you know what each queue setting does, and you are comfortable with curl or the gh CLI and with JSON. You also need admin rights on the repository.
Why manage the queue as code
Clicking through Settings > Rules > Rulesets works for one repository. It stops working when you own thirty repositories that should share the same queue settings, or when you want a pull request to review a change such as raising max_entries_to_build from 5 to 10.
The REST API for repository rulesets fixes both problems. A ruleset is a JSON document, so you can:
- keep it in version control and review changes like any other code,
- apply the same document to many repositories with a loop,
- compare the live configuration against the file to detect drift,
- recreate a queue quickly after someone deletes the ruleset by mistake.
The merge queue is a rule type inside a ruleset. The classic branch protection API has no merge queue settings, so a queue managed as code is always a ruleset.
The ruleset document
A ruleset has four parts that matter here:
| Field | Purpose |
|---|---|
| `name` | A label shown in the UI and used to find the ruleset later. |
| `target` | What the ruleset applies to. Use `branch` for a queue. |
| `enforcement` | `active`, `evaluate`, or `disabled`. |
| `conditions.ref_name` | Which branches it covers, as `include` and `exclude` pattern lists. |
| `rules` | The list of rules, each with a `type` and usually a `parameters` object. |
The merge queue rule has the type merge_queue. This example puts a queue on main with squash merging, groups of up to five pull requests, and a 60-minute check timeout:
{
"name": "main-merge-queue",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": {
"include": ["refs/heads/main"],
"exclude": []
}
},
"rules": [
{
"type": "merge_queue",
"parameters": {
"merge_method": "SQUASH",
"grouping_strategy": "ALLGREEN",
"min_entries_to_merge": 1,
"max_entries_to_merge": 5,
"min_entries_to_merge_wait_minutes": 5,
"max_entries_to_build": 5,
"check_response_timeout_minutes": 60
}
}
]
}Branch patterns in include are full ref names such as refs/heads/main. You can also use ~DEFAULT_BRANCH to mean whatever the default branch is, or a pattern like refs/heads/release/*.
Parameter reference
Each parameter maps to a setting you already know from the UI.
| Parameter | Values | Effect |
|---|---|---|
| `merge_method` | `MERGE`, `SQUASH`, `REBASE` | How each group lands on the base branch. |
| `grouping_strategy` | `ALLGREEN`, `HEADGREEN` | Which commits must pass checks for a group to merge. |
| `max_entries_to_build` | 0 to 100 | How many merge group branches the queue builds and tests at once. |
| `min_entries_to_merge` | 0 to 100 | The smallest group the queue will merge. |
| `max_entries_to_merge` | 0 to 100 | The largest group the queue will merge in one step. |
| `min_entries_to_merge_wait_minutes` | 0 to 360 | How long the queue waits for the minimum group size before it merges a smaller group. |
| `check_response_timeout_minutes` | 1 to 360 | How long the queue waits for required checks before it counts the group as failed. |
With ALLGREEN, the merge commit prepared by the queue must pass all required checks. With HEADGREEN, only the commit at the head of the group must pass. That commit contains the changes of every pull request in the group.
The API rejects values outside these ranges with a 422 response. A common mistake is a lowercase merge method such as squash. The enum values are uppercase.
Creating the ruleset
Save the JSON above as merge-queue.json. Then create the ruleset with the GitHub CLI, which handles authentication and the required headers:
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/OWNER/REPO/rulesets \
--input merge-queue.jsonReplace OWNER/REPO with your repository. The response is the stored ruleset, including a numeric id. You need that id for every later update.
If you prefer curl, send the same body to the same URL with a token:
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/rulesets \
-d @merge-queue.jsonThe token needs the "Administration" repository permission set to write. A fine-grained token without it receives 403 or 404, and the 404 is easy to misread as a wrong URL.
Adding the rules a queue depends on
A queue without required status checks adds no safety, because the queue has nothing to test. You can put the checks in the same ruleset. Add a required_status_checks rule next to merge_queue:
{
"name": "main-merge-queue",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": {
"include": ["refs/heads/main"],
"exclude": []
}
},
"rules": [
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": false,
"require_code_owner_review": false,
"require_last_push_approval": false,
"required_review_thread_resolution": false
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": false,
"required_status_checks": [
{ "context": "test" }
]
}
},
{
"type": "merge_queue",
"parameters": {
"merge_method": "SQUASH",
"grouping_strategy": "ALLGREEN",
"min_entries_to_merge": 1,
"max_entries_to_merge": 5,
"min_entries_to_merge_wait_minutes": 5,
"max_entries_to_build": 5,
"check_response_timeout_minutes": 60
}
}
]
}The context value is the name of the check as it appears on the pull request. For a GitHub Actions job, that is the job name, such as test. If the name here does not match what the workflow reports, the queue waits for a check that never arrives until the timeout expires.
Keeping strict_required_status_checks_policy at false is normal with a queue. The queue already tests each pull request against the latest base branch, so you do not need to force authors to update their branches by hand.
Reading, updating, and deleting
List the rulesets on a repository to find the id:
gh api /repos/OWNER/REPO/rulesets --jq '.[] | {id, name, enforcement}'Fetch one ruleset in full:
gh api /repos/OWNER/REPO/rulesets/RULESET_IDUpdate it with PUT. This call replaces the stored ruleset with the body you send. It does not merge the two.
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/OWNER/REPO/rulesets/RULESET_ID \
--input merge-queue.jsonDelete it with DELETE on the same URL. Deleting removes the queue immediately. Pull requests that were queued lose their place.
A safe change workflow
Treat the JSON file as the source of truth and the repository setting as a deployment target.
Two habits make this workflow reliable:
- Create the ruleset with
"enforcement": "evaluate"first, where your plan allows it. In this mode GitHub records what the rules would have done without blocking anyone. Switch toactiveafter you check the results. - Apply changes to a scratch repository before production. Queue behavior depends on checks, timing, and group size, and the JSON schema cannot tell you if a setting suits your workload.
Applying one file to many repositories
A short loop applies the same settings to a list of repositories. This script creates the ruleset if the name is missing and updates it if it exists:
#!/usr/bin/env bash
set -euo pipefail
FILE="merge-queue.json"
NAME=$(jq -r '.name' "$FILE")
for repo in "$@"; do
id=$(gh api "/repos/$repo/rulesets" \
--jq ".[] | select(.name == \"$NAME\") | .id")
if [ -z "$id" ]; then
echo "Creating $NAME in $repo"
gh api --method POST "/repos/$repo/rulesets" --input "$FILE" > /dev/null
else
echo "Updating $NAME ($id) in $repo"
gh api --method PUT "/repos/$repo/rulesets/$id" --input "$FILE" > /dev/null
fi
doneRun it as ./apply-queue.sh my-org/api my-org/web. Matching on the ruleset name keeps the script safe to run repeatedly.
Common errors
| Symptom | Likely cause |
|---|---|
| `404 Not Found` on create | The token lacks Administration write access, or the repository's plan does not include merge queues. |
| `422 Unprocessable Entity` | An enum value is wrong, a number is out of range, or a required parameter is missing. The response body names the field. |
| The queue exists but pull requests show no "Merge when ready" button | The ruleset is `disabled` or `evaluate`, or the `ref_name` pattern does not match the target branch. |
| Checks never complete in the queue | The workflow lacks the `merge_group` trigger, or the `context` name does not match the reported check. |
| Other rules vanished after an update | The `PUT` body omitted them. Resend the full document. |
Limits of the API approach
The ruleset API configures the queue and the rules around it. It does not manage the workflows. Those live in the repository, so a drift check needs two parts: compare the live ruleset against your file, and confirm that every required check has a workflow with the merge_group trigger. Organization rulesets exist too and use a similar body under an organization endpoint. They cover many repositories with one document, but they add their own targeting conditions, so treat them as a separate topic.
Exercise
You send a PUT request to update max_entries_to_build and include only the merge_queue rule in the body. Your ruleset previously also had a required_status_checks rule. What happens, and how do you avoid it?
Show answer
PUT replaces the whole ruleset, so the required_status_checks rule is removed and the queue no longer has required checks to wait for. To avoid it, keep the complete ruleset in a JSON file, change only the value you need inside that file, and send the entire document. Fetching the live ruleset with GET before the update helps you confirm nothing is missing.
Exercise
A new queue is active, but every queued pull request fails after exactly 60 minutes with a timeout. The test job runs fine on pull requests. Name two things to check.
Show answer
First, check that the workflow containing the test job lists merge_group under on:, because without it no check runs on the queue's temporary branch. Second, check that the context in the required_status_checks rule matches the reported job name exactly, since a mismatch makes the queue wait for a check that never appears until check_response_timeout_minutes runs out.