-
Notifications
You must be signed in to change notification settings - Fork 9
/
bulk_merger.rb
100 lines (77 loc) · 2.63 KB
/
bulk_merger.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require "octokit"
class BulkMerger
def self.approve_unreviewed_pull_requests!(list: nil)
puts "Searching for PRs containing '#{query_string}'"
unreviewed_pull_requests = find_govuk_pull_requests("review:none #{query_string}")
if unreviewed_pull_requests.size == 0
puts "No unreviewed PRs found!"
return
end
puts "Found #{unreviewed_pull_requests.size} unreviewed PRs:\n\n"
unreviewed_pull_requests.each do |pr|
puts "- '#{pr.title}' (#{pr.html_url}) "
end
return if list
puts "\nHave you reviewed the changes, and you want to approve all these PRs? [y/N]\n"
if STDIN.gets.chomp == "y"
puts "OK! 👍 Approving away..."
else
puts "👋"
exit 1
end
unreviewed_pull_requests.each do |pr|
print "Reviewing PR '#{pr.title}' (#{pr.html_url}) "
repo = pr.repository_url.gsub("https://api.github.com/repos/", "")
client.create_pull_request_review(repo, pr.number, event: "APPROVE")
puts "✅"
end
end
def self.merge_approved_pull_requests!
unmerged_pull_requests = find_govuk_pull_requests("review:approved #{query_string}")
if unmerged_pull_requests.size == 0
puts "No unmerged PRs found!"
return
end
puts "Found #{unmerged_pull_requests.size} reviewed but unmerged PRs:\n\n"
unmerged_pull_requests.each do |pr|
puts "- '#{pr.title}' (#{pr.html_url}) "
end
puts "\nHave you reviewed the changes, and you want to MERGE all these PRs? [y/N]\n"
if STDIN.gets.chomp == "y"
puts "OK! 👍 Merging away..."
else
puts "👋"
exit 1
end
unmerged_pull_requests.each do |pr|
print "Merging PR '#{pr.title}' (#{pr.html_url}) "
repo = pr.repository_url.gsub("https://api.github.com/repos/", "")
begin
client.merge_pull_request(repo, pr.number)
puts "✅"
rescue Octokit::MethodNotAllowed => e
puts "❌ Failed to merge: #{e.message.inspect}"
end
end
end
def self.search_pull_requests(query)
client.search_issues("#{query} archived:false is:pr user:alphagov state:open in:title").items
end
def self.govuk_repos
@govuk_repos ||= client.search_repos("org:alphagov topic:govuk")
.items
.reject!(&:archived)
.map { |repo| repo.full_name }
end
def self.find_govuk_pull_requests(query)
search_pull_requests(query).select do |pr|
govuk_repos.any? { |repo| pr.repository_url.include?(repo) }
end
end
def self.client
@client ||= Octokit::Client.new(access_token: ENV.fetch("GITHUB_TOKEN"), auto_paginate: true)
end
def self.query_string
ENV.fetch("QUERY_STRING")
end
end