You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitHub创建Pull Request时自动要求指定数量的审批者?

GitHub Pull Request 自动设置审批者数量要求

一、全局设置最少审批者数量

要让所有针对特定分支的PR自动要求最少审批者,可通过分支保护规则实现:

  • 进入仓库的「Settings」→「Branches」→「Branch protection rules」页面
  • 点击「Add rule」,填写需要保护的分支名称(如main、develop,支持通配符)
  • 勾选「Require pull request reviews before merging」选项
  • 在「Required number of approvals」输入框中设置最少审批者数量(比如1)
  • 保存规则后,所有向该分支提交的PR都必须达到指定审批数才能合并

二、针对特定Pull Request要求2位审批者

根据不同场景,有两种实现方式:

场景1:针对特定分支的PR(如发布分支)

如果是向特定分支(如release/*)提交的PR需要2位审批者,同样用分支保护规则:

  • 新建一条分支保护规则,分支名称填写release/*(匹配所有发布分支)
  • 勾选「Require pull request reviews before merging」,将「Required number of approvals」设置为2
  • 保存后,所有目标分支为release/*的PR都必须获得2位审批者的同意才能合并

场景2:针对带特定标记的PR(如重大变更PR)

如果需要给标记为特定标签(如critical)的PR强制要求2位审批者,可通过GitHub Actions实现:

  1. 在仓库.github/workflows/目录下创建工作流文件,比如require-2-approvals.yml
  2. 写入以下配置:
name: Require 2 Approvals for Critical PRs
on:
  pull_request:
    types: [opened, labeled, synchronize, review_requested, review_request_removed]

jobs:
  check-approvals:
    runs-on: ubuntu-latest
    if: contains(github.event.pull_request.labels.*.name, 'critical')
    steps:
      - name: Verify approval count
        uses: actions/github-script@v6
        with:
          script: |
            const reviews = await github.rest.pulls.listReviews({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            });
            // 过滤出有效审批(排除PR作者自己的审批)
            const validApprovals = reviews.data.filter(review => 
              review.state === 'APPROVED' && review.user.login !== context.payload.pull_request.user.login
            );
            if (validApprovals.length < 2) {
              core.setFailed('此重大变更PR需要至少2位审批者的同意');
            }
  1. 回到仓库的分支保护规则,勾选「Require status checks to pass before merging」,将上述工作流的状态检查项(Require 2 Approvals for Critical PRs)添加进去
  2. 之后,只要PR被打上critical标签,就会自动检查审批者数量,未达到2位时会阻止合并

内容的提问来源于stack exchange,提问作者itye1970

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 17:19:02