Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

policy-bot

> 开发工具
Open source

A GitHub App that enforces approval policies on pull requests

1.0K stars0 likes0 views
WebsiteGitHub

About

A GitHub App that enforces approval policies on pull requests

policy-bot

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:

  • Require reviews from specific users, organizations, or teams
  • Apply rules based on the files, authors, or branches involved in a pull request
  • Combine multiple approval rules with and and or conditions
  • Automatically approve pull requests that meet specific conditions

Behavior is configured by a file in each repository. policy-bot also provides a UI to view the detailed approval status of any pull request.

  • Designing Policies
  • Configuration
    • policy.yml Specification
    • Approval Rules
    • Approval Policies
    • Disapproval Policy
    • Testing and Debugging Policies
      • Simulation API
    • Caveats and Notes
      • Disapproval is Disabled by Default
      • Interactions with GitHub Reviews
      • or, and, and if (Rule Predicates)
      • Cross-organization Membership Tests
      • Update Merges
      • Automatically Requesting Reviewers
  • Security
  • Deployment
  • Development
  • Contributing
  • License

Designing Policies

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

Rules define the specific circumstances in which a pull request is approved. Each rule has four components:

  1. 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 ..."

  2. 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.

  3. 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.

  4. 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 true
  • pending - all of the predicates are true but one or more requirements are not true
  • skipped - one or more predicates are not true
  • error - something went wrong while evaluating the rule

For the purposes of the and and or operators:

  • approved is equivalent to true
  • pending and error are equivalent to false
  • skipped completely removes the rule from the condition

Predicates vs Required Conditions

When 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.

Configuration

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.

policy.yml Specification

The overall policy is expressed by:

  • Lists of rule definitions
  • A set of policies that combine the rules or define additional options

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.

yaml
# 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: 0

Notes on YAML Syntax

The 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).

  • Single Quoted: ' is used as an escape character. Backslash characters do not need to be escaped. e.g. '^BREAKING CHANGE: (\w| )+$'
  • Double Quoted: \ is used as an escape character. Backslash characters must be escaped with a preceding \. e.g. "^BREAKING CHANGE: (\\w| )+$"
  • Plain: There are no escape characters. Backslash characters do not need to be escaped. e.g. ^BREAKING CHANGE: (\w| )+$

Remote Policy Configuration

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.

yaml
# 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: master

Approval Rules

Each list entry in approval_rules has the following specification:

…

Default Options

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:

  1. Explicit rule options
  2. Policy-level default options
  3. Configurable server-level default options
  4. Hardcoded server-level default options

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:

yaml
# 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: false

Approval Policies

The approval block in the policy section defines a list of rules that must all be true:

yaml
policy:
  approval:
    - rule1
    - rule2
    - rule3
    - ...

Each list entry may be the name of a rule, or one of the following conjunctions:

yaml
or:
  - rule1
  - rule2
  - ...
yaml
and:
  - rule1
  - rule2
  - ...

Conjunctions can contain more conjunctions (up to a maximum depth of 5):

yaml
- or:
    - rule1
    - rule2
    - and:
        - rule3
        - rule4

Disapproval Policy

Disapproval 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:

…

Testing and Debugging Policies

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Goapprovalcode-reviewgithub-appgolang

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具