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

如何通过GitHub REST API创建分支匹配模式的保护规则?

使用GitHub REST API创建基于模式的分支保护规则

你需要使用分支保护规则专用API端点,而非针对单个分支的保护端点。具体操作如下:

  • 调用PUT /repos/{owner}/{repo}/branch-protection/rules端点,在请求体中指定pattern参数为你的分支匹配模式(比如release/*),同时配置其他保护规则参数(如required_pull_request_reviews、required_status_checks等)。

示例请求体(JSON格式):

{
  "pattern": "release/*",
  "required_pull_request_reviews": {
    "required_approving_review_count": 2,
    "dismiss_stale_reviews": true
  },
  "required_status_checks": {
    "strict": true,
    "contexts": ["ci/build", "ci/test"]
  },
  "enforce_admins": true,
  "restrictions": null
}
  • 注意:这个端点和你之前用的单个分支保护端点(PUT /repos/{owner}/{repo}/branches/{branch}/protection)是不同的。后者仅针对具体分支,前者支持通配符模式匹配。

  • 权限要求:需要仓库的admin权限,或者拥有管理分支保护规则的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 23:58:16