Sub topic 4
Choosing which repositories a ruleset applies to
This chapter assumes you can read a ruleset JSON body and that you know the difference between an organisation and a repository ruleset. It covers the repository_name condition and the other ways an organisation ruleset picks its repositories.
Why repository targeting exists
A repository ruleset lives in one repository and applies only there. An organisation ruleset lives in the organisation and has a second question to answer: which repositories does it cover? A ruleset answers two separate questions. The ref_name condition selects branches or tags. The repository condition selects the repositories those refs belong to. A ref is only protected when both conditions match.
Ways to select repositories
An organisation ruleset can select repositories in three ways in the web interface. The REST API exposes three matching condition keys.
| Selection | Condition key | Typical use |
|---|---|---|
| All repositories | `repository_name` with `~ALL` | Baseline rules for the whole organisation |
| Name patterns | `repository_name` with `include` and `exclude` | Cover `service-*`, skip `sandbox-*` |
| Repository IDs | `repository_id` | Cover a fixed list of repositories |
| Custom properties | `repository_property` | Cover repositories by label, such as `tier=production` |
Use only one selection key in a ruleset's conditions. Do not mix repository_name and repository_id. GitHub rejects a request that sets more than one.
Name patterns
The repository_name condition takes two lists, include and exclude. Each entry is either the special value ~ALL or an fnmatch pattern such as service-*.
{
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
}
}This fragment covers the default branch of every repository except those whose names start with sandbox-. An exclude entry always wins over an include entry, so a repository matching both is left out.
Name patterns have one weakness. If someone renames a repository to sandbox-payments, it silently leaves the ruleset. Patterns match names, not intent.
Selecting by custom property
Custom properties are labels that organisation owners define and set on each repository. A ruleset can match on them, so the scope follows the repository's classification instead of its name.
{
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_property": {
"include": [
{
"name": "tier",
"source": "custom",
"property_values": ["production"]
}
],
"exclude": []
}
}
}Any repository with tier set to production is covered, including repositories created later. This makes properties better than name patterns when a team can create repositories freely.
Why targeting matters for required workflows
The workflows rule from the example in this tutorial is only useful when the target set is right. The workflow file lives in one repository (repository_id: 123456), but the rule applies to every repository the ruleset selects. If the target set is too wide, repositories without the files or settings the workflow expects will fail every pull request. Start with a narrow target, such as one custom property value, and widen it after the workflow proves stable.
Common errors
- Empty
includelist. A repository condition with no includes matches nothing. Use~ALLto cover everything. - Pattern typos.
sandbox*andsandbox-*match different names. Check the list of matched repositories in the ruleset's page before you enable it. - Wrong key combination. Setting both
repository_nameandrepository_idfails with a validation error. - Forgetting the ref condition. A correct repository scope with a wrong
ref_namestill leaves the branch unprotected.
Exercise
Your organisation labels repositories with a custom property tier. You want a ruleset to cover the default branch of every repository where tier is production, including ones created next month. Which condition key do you use for the repositories, and why is it better than the name pattern prod-*?
Show answer
Use repository_property with an include entry for name tier and property value production. It matches on the repository's classification, so new production repositories are covered automatically even if their names do not start with prod-, and a rename cannot remove a repository from scope.