forked from underscorgan/community_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_requests.rb
executable file
·144 lines (121 loc) · 4.91 KB
/
pull_requests.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'optparse'
require_relative 'octokit_utils'
options = {}
options[:oauth] = ENV['GITHUB_COMMUNITY_TOKEN'] if ENV['GITHUB_COMMUNITY_TOKEN']
parser = OptionParser.new do |opts|
opts.banner = 'Usage: pull_requests.rb [options]'
opts.on('-a', '--after DAYS', 'Pull requests that were last updated after DAYS days ago.') { |v| options[:after] = v.to_i }
opts.on('-b', '--before DAYS', 'Pull requests that were last updated before DAYS days ago.') { |v| options[:before] = v.to_i }
opts.on('-c', '--count', 'Only print the count of pull requests.') { options[:count] = true }
opts.on('-e', '--show-empty', 'List repos with no pull requests') { options[:empty] = true }
opts.on('-n', '--namespace NAME', 'GitHub namespace. Required.') { |v| options[:namespace] = v }
opts.on('-r', '--repo-regex REGEX', 'Repository regex') { |v| options[:repo_regex] = v }
opts.on('-s', '--sort', 'Sort output based on number of pull requests') { options[:sort] = true }
opts.on('-t', '--oauth-token TOKEN', 'OAuth token. Required.') { |v| options[:oauth] = v }
opts.on('-v', '--verbose', 'More output') { options[:verbose] = true }
# default filters
opts.on('--puppetlabs', 'Select Puppet Labs\' modules') {
options[:namespace] = 'puppetlabs'
options[:repo_regex] = '^puppetlabs-'
}
opts.on('--puppetlabs-supported', 'Select only Puppet Labs\' supported modules') {
options[:namespace] = 'puppetlabs'
options[:repo_regex] = '^puppetlabs-(acl|apache|apt|aws|catalog_preview|concat|docker_platform|f5|firewall|haproxy|inifile|java|java_ks|mysql|netscaler|ntp|postgresql|powershell|reboot|registry|sqlserver|stdlib|tomcat|vcsrepo)'
}
opts.on('--community', 'Select community modules') {
options[:namespace] = 'puppet-community'
options[:repo_regex] = '^puppet-'
}
opts.on('--no-response', 'Select PRs which had no response in the last 30 days') {
options[:before] = 30
}
opts.on('--needs-closing', 'Select PRs where the last response is from an owner, but no further activity for the last 30 days') {
options[:before] = 30
options[:last_comment] = :owner
}
opts.on('--bad-status', 'Select PRs where the status is bad') {
options[:bad_status] = 1
}
opts.on('--needs-squashed', 'Select PRs that need squashed') {
options[:needs_squashed] = 1
}
opts.on('--needs-rebase', 'Select PRs where they need a rebase') {
options[:needs_rebase] = 1
}
opts.on('--no-comments', 'Select PRs where there are no comments') {
options[:no_comments] = 1
}
end
parser.parse!
missing = []
missing << '-n' if options[:namespace].nil?
missing << '-t' if options[:oauth].nil?
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts parser
exit
end
if options[:before] and options[:after]
puts "Only one of -a and -b can be specified"
exit
end
options[:repo_regex] = '.*' if options[:repo_regex].nil?
util = OctokitUtils.new(options[:oauth])
repos = util.list_repos(options[:namespace], options)
repo_data = []
repos.each do |repo|
begin
if options[:last_comment] == :owner
pulls = util.fetch_pull_requests_with_last_owner_comment("#{options[:namespace]}/#{repo}")
elsif options[:needs_rebase]
pulls = util.fetch_pull_requests_which_need_rebase("#{options[:namespace]}/#{repo}")
elsif options[:bad_status]
pulls = util.fetch_pull_requests_with_bad_status("#{options[:namespace]}/#{repo}")
elsif options[:needs_squashed]
pulls = util.fetch_pull_requests_which_need_squashed("#{options[:namespace]}/#{repo}")
elsif options[:no_comments]
pulls = util.fetch_uncommented_pull_requests("#{options[:namespace]}/#{repo}")
else
pulls = util.fetch_pull_requests("#{options[:namespace]}/#{repo}")
end
if options[:before]
opts = { :pulls => pulls }
start_time = (DateTime.now - options[:before]).to_time
pulls = util.pulls_older_than(start_time, opts)
elsif options[:after]
opts = { :pulls => pulls }
end_time = (DateTime.now - options[:after]).to_time
pulls = util.pulls_newer_than(end_time, opts)
end
if not options[:empty] and pulls.empty?
next
end
if options[:count]
repo_data << { 'repo' => "#{options[:namespace]}/#{repo}", 'pulls' => nil, 'pull_count' => pulls.length }
else
repo_data << { 'repo' => "#{options[:namespace]}/#{repo}", 'pulls' => pulls, 'pull_count' => pulls.length }
end
rescue
puts "Unable to fetch pull requests for #{options[:namespace]}/#{repo}" if options[:verbose]
end
end
if options[:sort]
repo_data.sort_by! { |x| -x['pull_count'] }
end
repo_data.each do |entry|
puts "=== #{entry['repo']} ==="
case entry['pull_count']
when 0
puts ' no open pull requests'
when 1
puts ' 1 open pull request'
else
puts " #{entry['pull_count']} open pull requests"
end
unless options[:count]
entry['pulls'].each do |pull|
puts " #{pull[:html_url]} - #{pull[:title]}"
end
end
end