Sub topic 13
Configuring the queue through the rulesets API
This chapter assumes you can enable a merge queue in the repository settings, and that you know which settings the queue exposes (merge method, group size, build concurrency, timeout, grouping strategy). It also assumes you are comfortable calling the GitHub REST API with gh or curl. Here you will define the same settings as a JSON document and apply it through the API.
Why configure the queue as code
Clicking through settings works for one repository. It becomes a problem when you manage twenty repositories, or when you want a reviewable record of who changed the queue timeout and when.
A repository ruleset is a named set of rules that targets branches. The merge queue is one rule type inside a ruleset. Because the API accepts a ruleset as JSON, you can keep that JSON in version control, review changes in a pull request, and apply the same file to many repositories.
The ruleset body
This body applies a merge queue to 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
}
}
]
}The top-level fields choose where the ruleset applies. enforcement can be active, disabled, or evaluate. The ref_name condition selects branches by full ref name.
The parameters object maps to the queue settings you already know:
| Parameter | Setting it controls |
|---|---|
| `merge_method` | `MERGE`, `SQUASH`, or `REBASE` |
| `grouping_strategy` | `ALLGREEN` or `HEADGREEN`, which decides how a group's checks are judged |
| `max_entries_to_build` | How many merge groups build concurrently |
| `min_entries_to_merge` and `max_entries_to_merge` | The size range of a group that merges together |
| `min_entries_to_merge_wait_minutes` | How long the queue waits to reach the minimum group size |
| `check_response_timeout_minutes` | How long the queue waits for required checks before removing the entry |
Applying and updating it
Save the JSON as ruleset.json. Then create the ruleset. Replace OWNER/REPO with your repository.
gh api --method POST repos/OWNER/REPO/rulesets --input ruleset.jsonThe response includes an id. To change a setting later, edit the file and send it with PUT to that ID. To find the ID of an existing ruleset, list them.
gh api repos/OWNER/REPO/rulesets
gh api --method PUT repos/OWNER/REPO/rulesets/RULESET_ID --input ruleset.jsonThe credentials you use need administration write permission on the repository. A token without it gets a 403 or 404 response.
Common mistakes
- Creating a second ruleset instead of updating the first. Two rulesets can both target
main, and GitHub combines their rules. List existing rulesets before youPOST. - Treating the ruleset as the whole setup. The queue only produces results if your workflows trigger on
merge_groupand the required checks are configured. The ruleset does not edit workflow files. - Starting with
enforcement: activeon a busy repository. Test the file on a low-traffic repository first, or useevaluatewhere your plan supports it.
Exercise
Your team keeps its ruleset in a file and wants to lower check_response_timeout_minutes from 60 to 30 on an existing repository. Which HTTP method and endpoint do you use, and why is POST to /rulesets the wrong choice?
Show answer
Use PUT repos/OWNER/REPO/rulesets/RULESET_ID with the edited JSON body. You can find the ID by listing rulesets with GET repos/OWNER/REPO/rulesets. POST to /rulesets creates a new ruleset. That leaves two rulesets targeting main, and GitHub combines their rules instead of replacing the old one.