forked from underscorgan/community_management
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsetup_branch_protection.rb
executable file
·69 lines (62 loc) · 2.65 KB
/
setup_branch_protection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
# frozen_string_literal: true
# https://docs.github.com/en/rest/reference/repos#update-branch-protection
# https://octokit.github.io/octokit.rb/Octokit/Client/Repositories.html#protect_branch-instance_method
require_relative 'octokit_utils'
require_relative 'options'
options = parse_options
util = OctokitUtils.new(options[:oauth])
client = util.client
# puts client.branch_protection('puppetlabs/puppetlabs-acl', 'main', accept: 'application/vnd.github.luke-cage-preview+json').inspect
# protect_rule = {
# url: 'https://api.github.com/repos/puppetlabs/puppetlabs-acl/branches/main/protection',
# required_status_checks:
# {
# url: 'https://api.github.com/repos/puppetlabs/puppetlabs-acl/branches/main/protection/required_status_checks',
# strict: false,
# contexts: ['Spec Tests (Puppet: ~> 6.0, Ruby Ver: 2.5)', 'license/cla'],
# contexts_url: 'https://api.github.com/repos/puppetlabs/puppetlabs-acl/branches/main/protection/required_status_checks/contexts'
# },
# required_pull_request_reviews: {
# url: 'https://api.github.com/repos/puppetlabs/puppetlabs-acl/branches/main/protection/required_pull_request_reviews',
# dismiss_stale_reviews: true,
# require_code_owner_reviews: false,
# required_approving_review_count: 1
# },
# enforce_admins: {
# url: 'https://api.github.com/repos/puppetlabs/puppetlabs-acl/branches/main/protection/enforce_admins',
# enabled: false
# },
# required_linear_history: { enabled: false },
# allow_force_pushes: { enabled: false },
# allow_deletions: { enabled: false },
# required_conversation_resolution: { enabled: false }
# }
parsed = load_url(options)
parsed.each do |_k, v|
repo_name = (v['github']).to_s
begin
client.unprotect_branch(repo_name, 'main', accept: 'application/vnd.github.luke-cage-preview+json')
rescue StandardError => e
puts e
end
begin
client.protect_branch(repo_name, 'main', {
accept: 'application/vnd.github.luke-cage-preview+json',
required_status_checks: {
strict: false,
contexts: ['license/cla']
},
enforce_admins: false,
required_pull_request_reviews: {
dismiss_stale_reviews: true,
require_code_owner_reviews: false,
required_approving_review_count: 1
}
})
puts "updated #{repo_name}"
rescue StandardError => e
puts "failed to update #{repo_name}"
puts e
end
end