A GitHub App that enforces approval policies on pull requests
A GitHub App that enforces approval policies on pull requests
policy-bot is a GitHub App for enforcing
approval policies on pull requests. It does this by creating a status check,
which can be configured as a required status check.
While GitHub natively supports required reviews, policy-bot provides more
complex approval features:
and and or conditionsBehavior is configured by a file in each repository. policy-bot also provides a
UI to view the detailed approval status of any pull request.
An approval policy is a set of rules that combine to determine when a pull request is approved. Policy Bot reports approval as a successful status check on the head commit of the pull request. The most common form of approval is code review from other developers, but Policy Bot supports many other types of approval.
Rules combine using the logical operators and and or. With no operators, a
policy requires that all of its rules are approved to approve the pull request.
Rules define the specific circumstances in which a pull request is approved. Each rule has four components:
Name. This is an arbitrary string that you use to refer to the rule in a policy. It also appears in the details view for a pull request. Try to pick names that make sense to humans and explain the purpose of the rule. A good name can complete the sentence "This pull request is approved if ..."
Predicates. A rule is only active when all of its predicates are true. If any predicate is false, the rule is skipped and does not influence the policy. Predicates allow you to create rules that are conditional on the state of the pull request, like requiring approval from a special team when specific important files change.
Options. Options control the behavior of the rule. This includes things like the approval methods, if pushing new changes invalidates previous approvals, and if the rule should request reviews from users or teams.
Requirements. These are the things that must be true for the rule to be approved. Requirements can be approvals from users with the right properties or can be conditions about the pull request, similar to predicates. If a rule has no requirements, it is always approved.
Only a name is required, but most rules need to include either predicates or requirements to be useful.
After evaluation, a rule can be in one of four states:
approved - all of the predicates and requirements are truepending - all of the predicates are true but one or more requirements are not trueskipped - one or more predicates are not trueerror - something went wrong while evaluating the ruleFor the purposes of the and and or operators:
approved is equivalent to truepending and error are equivalent to falseskipped completely removes the rule from the conditionWhen writing a rule, you can react to the state of pull request by using either predicates or required conditions. When would you use one over the other?
Use predicates to enable a rule when a pull request is in a specific state (or to skip the rule when a pull request is not in that state.) Predicates are useful when you want special approval requirements like requiring extra approval for files that involve security or automatically approving dependency updates.
Use required conditions when the pull request state itself is important for approval. For example, conditions are useful if you want to require that a pull request has certain status checks or specific labels.
Sometimes you can achieve a desired outcome using either approach. In this case, we prefer predicates. Policies that use predicates may define more rules, but tend to be flatter and use fewer logical operators. With well-chosen rule names, we find this style of policy easier to read and reason about.
By default, policies are defined in a .policy.yml file at the root of the
repository. You can change this path and file name when running your own
instance of the server.
The file is read from the most recent commit on the target branch of each pull request.
The file may contain a reference to a policy in a different repository (see Remote Policy Configuration.)
If the file does not exist in the repository, policy-bot tries to load a
shared policy.yml file at the root of the .github repository in the same
organization. You can change this path and repository name when running your
own instance of the server, or disable this feature by setting
options.shared_repository to an empty string ("") in the server
configuration.
If a policy does not exist in the repository or in the shared organization
repository, policy-bot does not post a status check on the pull request.
This means it is safe to enable policy-bot on all repositories in an
organization.
Server administrators can also set the force_shared_policy option to disable
loading policies from local repositories, requiring that all repositories use a
policy supplied in a shared organization repository.
The overall policy is expressed by:
Consider the following example, which allows changes to certain paths without
review, but all other changes require review from the palantir/devtools team.
Any member of the palantir organization can also disapprove changes.
# the high level policy
policy:
approval:
- or:
- the devtools team has approved
- only staging files have changed
disapproval:
requires:
organizations:
- "palantir"
# the list of rules
approval_rules:
- name: the devtools team has approved
requires:
count: 1
teams:
- "palantir/devtools"
- name: only staging files have changed
if:
only_changed_files:
paths:
- "^staging/.*$"
requires:
count: 0The YAML language specification supports flow scalars (basic values like strings
and numbers) in three formats:
single-quoted,
double-quoted, and
plain. Each support different
escape characters, which can cause confusion when used for regex strings
(which often contain the \\ character).
' is used as an escape character. Backslash characters do not need to be escaped.
e.g. '^BREAKING CHANGE: (\w| )+$'\ is used as an escape character. Backslash characters must
be escaped with a preceding \.
e.g. "^BREAKING CHANGE: (\\w| )+$"^BREAKING CHANGE: (\w| )+$You can also define a remote policy by specifying a repository, path, and ref
(only repository is required). Instead of defining a policy key, you would
define a remote key. Only 1 level of remote configuration is supported by design.
# The remote repository to read the policy file from. This is required, and must
# be in the form of "org/repo-name". The policy bot github app must have read
# access to this repository.
remote: org/repo-name
# The path to the policy config file in the remote repository. If none is
# specified, the default path in the server config is used.
path: path/to/policy.yml
# The branch (or tag, or commit hash) that should be used on the remote
# repository. If none is specified, the default branch of the repository is used.
ref: masterEach list entry in approval_rules has the following specification:
…Policies and server administrators can define new default values for approval rule options. This makes it easier to set common options across all rules. Policy Bot tries values in the following order until it finds a set value:
To define policy-level defaults, add the approval_defaults section at the top
level of the policy file:
…Option values are not merged. For example, defining the ignore_commits_by or
methods.comment_patterns options in a rule completely replaces any default
values for these options.
The only exception to this is the methods property, which supports limited
merging. This matches the legacy behavior for how rule values combined with the
hardcoded server defaults. For example:
# server defaults
options:
methods:
comments: ["==APPROVED=="]
# policy defaults
options:
methods:
comment_patterns: ["^LGTM$"]
github_review: false
# rule overrides
options:
methods:
comment_patterns: ["^:+1:$"]
# combined value used during evaluation
options:
methods:
comments: ["==APPROVED=="]
comment_patterns: ["^:+1:$"]
github_review: falseThe approval block in the policy section defines a list of rules that must
all be true:
policy:
approval:
- rule1
- rule2
- rule3
- ...Each list entry may be the name of a rule, or one of the following conjunctions:
or:
- rule1
- rule2
- ...and:
- rule1
- rule2
- ...Conjunctions can contain more conjunctions (up to a maximum depth of 5):
- or:
- rule1
- rule2
- and:
- rule3
- rule4Disapproval allows users to explicitly block pull requests if certain changes must be made. Any member of in the set of allowed users can disapprove a change or revoke another user's disapproval.
Unlike approval, all disapproval predicates and options are specified as part
of the policy. Effectively, there is a single disapproval rule. The disapproval
policy has the following specification:
…Sometimes it is useful to test if a given policy file is valid, especially in a CI environment.
An API endpoint exists at /api/validate to validate the syntax of the yaml and policy configuration,
however it cannot validate that the rules are semantically correct for a given use case.
The API
No open issues yet, or sync has not completed.