在Github组织级别启用分支保护规则

18

在Github中,是否可以在组织级别启用分支保护规则,以便该组织的所有存储库继承这些规则应用于所应用的分支?目前,对于相同的分支集合,逐个存储库启用相同的规则确实很麻烦。

4个回答

8

我使用一个简单的Ruby脚本,利用GitHub API使其工作:

require "json"
require "logger"

LOGGER = Logger.new(STDOUT)

def run(cmd)
  LOGGER.debug("Running: #{cmd}")
  output = `#{cmd}`
  raise "Error: #{$?}" unless $?.success?
  output
end


def repos(page = 1, list = [])
  cmd = %Q{curl -s --user "user:pwd" https://github_url/api/v3/orgs/org_name/repos?page=#{page}}
  data = JSON.parse(run(cmd))
  list.concat(data)
  repos(page + 1, list) unless data.empty?
  list
end

repos.each do |repo|
  require 'net/http'
require 'uri'
require 'json'

uri = URI.parse("https://github_url/api/v3/repos/org_name/#{repo["name"]}/branches/master/protection")
request = Net::HTTP::Put.new(uri)
request.basic_auth("user", "pwd")
request["Accept"] = "application/vnd.github.luke-cage-preview+jso"
request.body = JSON.dump({
  "required_status_checks" => {
    "strict" => true,
    "contexts" => [
      "continuous-integration/travis-ci"
    ]
  },
  "enforce_admins" => true,
  "required_pull_request_reviews" => {
    "dismiss_stale_reviews" => true
  },
  "restrictions" => nil
})

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end
end


我以这段代码为基础,做了一些调整,现在它运行得很好,谢谢Ashley。 - gvasquez

4
从 @Ashley 的答案中提取,稍微更新了一下,并进行了轻微更改以反映当前 Github 的 API URL,同时添加了使用 GITHUB_ORG 和 GITHUB_ACCESS_TOKEN 环境变量的自定义功能。
require "json"
require "logger"

$org = ENV["GITHUB_ORG"]
$token = ENV["GITHUB_ACCESS_TOKEN"]

LOGGER = Logger.new(STDOUT)

def run(cmd)
  LOGGER.debug("Running: #{cmd}")
  output = `#{cmd}`
  raise "Error: #{$?}" unless $?.success?
  output
end


def repos(page = 1, list = [])
  cmd = %Q{curl -s -u dummy:#{$token} https://api.github.com/orgs/#{$org}/repos?page=#{page}}
  data = JSON.parse(run(cmd))
  list.concat(data)
  repos(page + 1, list) unless data.empty?
  list
end

repos.each do |repo|
p(repo["name"])
  require 'net/http'
require 'uri'
require 'json'

uri = URI.parse("https://api.github.com/repos/#{$org}/#{repo["name"]}/branches/master/protection")
request = Net::HTTP::Put.new(uri)
request.basic_auth("dummy", $token)
request["Accept"] = "application/vnd.github.luke-cage-preview+jso"
request.body = JSON.dump({
  "required_status_checks" => {
    "strict" => true,
    "contexts" => []
  },
  "enforce_admins" => true,
  "required_pull_request_reviews" => {
    "dismiss_stale_reviews" => true
  },
  "restrictions" => nil
})

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end
p(response)
end

3

现在(2023年4月)您具备了规则集的概念

规则集可在 GitHub Free 和 GitHub Free for organizations 的公共仓库中使用,也可以在 GitHub Pro、GitHub Team 和 GitHub Enterprise Cloud 的公共和私有仓库中使用。

这将允许您为组织内所有仓库定义和实施规则


1
你应该尝试使用Github API的update branch protection端点,并使用某种自动化过程来将分支保护规则应用于组织中的所有新分支。
PUT /repos/:owner/:repo/branches/:branch/protection

谢谢您的回复。我可以使用API成功地为一个仓库中的一个分支启用分支保护,但如何使用它为所有仓库和每个仓库中的一组分支实现这一目标呢?有什么建议吗? - Ashley
你可以使用 GET /orgs/:org/repos 端点获取组织中的所有存储库,然后使用 GET /repos/:owner/:repo/branches 获取该存储库的所有分支。接下来,结合 PUT /repos/:owner/:repo/branches/:branch/protection 来保护组织中所有存储库的所有分支。这样说清楚了吗? - Adil B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接