forked from underscorgan/community_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
octokit_utils.rb
246 lines (210 loc) · 6.64 KB
/
octokit_utils.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env ruby
require 'octokit'
class OctokitUtils
attr_accessor :client
def initialize(access_token)
Octokit.auto_paginate = true
@client = Octokit::Client.new(:access_token => "#{access_token}")
client.user.login
@pr_cache = {}
end
def list_repos(organization, options)
if not options[:repo_regex]
regex = '.*'
else
regex = options[:repo_regex]
end
repos ||= client.organization_repositories(organization).collect {|org| org[:name] if org[:name] =~ /#{regex}/}
# The collection leaves nil entries in for non-matches
repos = repos.select {|repo| repo }
return repos.sort.uniq
end
def pulls(repo, options)
@pr_cache[[repo, options]] ||= client.pulls(repo, options)
end
def fetch_pull_requests_with_bad_status(repo, options={:state=>'open', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
status = client.statuses(repo, pr.head.sha)
if status.first != nil and status.first.state != 'success'
returnVal.push (pr)
end
end
returnVal
end
def fetch_pull_requests_which_need_squashed(repo, options={:state=>'open', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
commits = client.pull_request_commits(repo, pr.number)
if commits.size > 1
returnVal.push (pr)
end
end
returnVal
end
def fetch_pull_requests(repo, options={:state=>'open', :sort=>'updated'})
pulls(repo, options)
end
def fetch_merged_pull_requests(repo, options={:state=>'closed', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
if pr.merged_at != nil
returnVal.push (pr)
end
end
returnVal
end
def fetch_uncommented_pull_requests(repo, options={:state=>'open', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
size = client.issue_comments(repo, pr.number, options).size
if size == 0
returnVal.push (pr)
end
end
returnVal
end
def fetch_unmerged_pull_requests(repo, options={:state=>'closed', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
if pr.merged_at == nil
returnVal.push (pr)
end
end
returnVal
end
def fetch_pull_requests_which_need_rebase(repo, options={:state=>'open', :sort=>'updated'})
returnVal = []
pulls(repo, options).each do |pr|
status = client.pull_request(repo, pr.number, options)
if status.mergeable == false
returnVal.push (pr)
end
end
returnVal
end
def fetch_pull_requests_with_last_owner_comment(repo, options={:state=>'open', :sort=>'updated'})
prs ||= pulls(repo, options)
return [] if prs.empty?
owner = prs.first.base.repo.owner
if owner.type == 'User'
members = { owner.login => :owner }
else
members = client.organization_members(owner.login).each_with_object({}) { |user, hash| hash[user.login] = :owner }
end
latest_comment_by_pr = client.issues_comments(repo, {:sort=> 'updated', :direction => 'desc'}).each_with_object({}) do |c, hash|
hash[c.issue_url] ||= c
end
prs = prs.select do |p|
latest_comment_by_pr[p.issue_url] && members[latest_comment_by_pr[p.issue_url].user.login] == :owner
end
prs
end
def self.sort_pulls(prs)
prs.sort do |a, b|
result = a.base.repo.name <=> b.base.repo.name
result = a.number <=> b.number if result == 0
result
end
end
def pulls_newer_than(time, options)
if not options[:pulls] and not options[:repo]
raise ArgumentError, 'One of :pulls or :repo must be specified in the options hash'
end
if not options[:pulls]
pulls=fetch_pull_requests(options[:repo])
else
pulls=options[:pulls]
end
pulls.select { |pull| pull[:updated_at] > time }
end
def pulls_older_than(time, options)
if not options[:pulls] and not options[:repo]
raise ArgumentError, 'One of :pulls or :repo must be specified in the options hash'
end
if not options[:pulls]
pulls=fetch_pull_requests(options[:repo])
else
pulls=options[:pulls]
end
pulls.select { |pull| pull[:updated_at] < time }
end
def pulls_in_range(start_time, end_time, options)
if not options[:pulls] and not options[:repo]
raise ArgumentError, 'One of :pulls or :repo must be specified in the options hash'
end
if not options[:pulls]
pulls=fetch_pull_requests(options[:repo])
else
pulls=options[:pulls]
end
pulls.select{ |pull| pull[:updated_at] < end_time and pull[:updated_at] > start_time }
end
def fetch_tags(repo, options)
if not options[:tag_regex]
regex = '.*'
else
regex = options[:tag_regex]
end
tags ||= client.tags(repo)
tags.select {|tag| tag[:name] =~ /#{regex}/}
end
def ref_from_tag(tag)
tag[:commit][:sha]
end
def date_of_ref(repo, ref)
commit ||= client.commit(repo, ref)
commit[:commit][:author][:date]
end
def commits_since_date(repo, date)
commits ||= client.commits_since(repo, date)
commits.size
end
def test_for_release(repo, options)
if not options[:commits] and not options[:time]
raise ArgumentError, 'One of :commits or :time must be specified in the options hash'
end
newest_tag = fetch_tags(repo, options).first
if newest_tag
date_of_tag = date_of_ref(repo, ref_from_tag(newest_tag)).to_dateime
if options[:time] and date_of_tag < (DateTime.now - options[:time])
if options[:commits] and (client.commits(repo, since: date_of_tag).count > options[:commits])
puts "#{repo}: A new release is needed"
else
puts "#{repo}: A new release is needed"
end
elsif not options[:time] and (client.commits(repo, since: date_of_tag).count > options[:commits])
puts "#{repo}: A new release is needed"
end
#else?
end
end
def format_pulls(pulls)
pulls.each do |pull|
repo = pull.repo.full_name
updated_at = pull.updated_at
url = pull.url
number = pull.number
puts "#{repo},#{updated_at},#{number},#{url}"
end
end
def fetch_repo_missing_labels(repo, required_labels)
returnVal = []
repo_labels = []
labels_data = client.labels(repo, {})
labels_data.each do |label|
repo_labels.push (label.name)
end
required_labels.each do |required_label|
unless repo_labels.include?(required_label[:name])
returnVal.push (required_label)
end
end
returnVal
end
def add_repo_labels(repo, required_labels)
required_labels.each do |required_label|
client.add_label(repo, required_label[:name], required_label[:color])
end
end
end