forked from galaxyproject/p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
404 lines (336 loc) · 14.2 KB
/
process.py
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env python
import os
import re
import yaml
from github import Github
import sqlite3
import datetime
from dateutil import parser as dtp
import parsedatetime
import argparse
import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()
logging.getLogger('github').setLevel(logging.INFO)
gh = Github(
login_or_token=os.environ.get('GITHUB_USERNAME', None) or os.environ.get('GITHUB_OAUTH_TOKEN', None),
password=os.environ.get('GITHUB_PASSWORD', None),
)
log.warn("GH API RATE LIMIT: %s/%s" % gh.rate_limiting)
UPVOTE_REGEX = '(:\+1:|^\s*\+1\s*$)'
DOWNVOTE_REGEX = '(:\-1:|^\s*\-1\s*$)'
class PullRequestFilter(object):
def __init__(self, name, conditions, actions, committer_group=None,
bot_user=None, dry_run=False, next_milestone=None, repo=None):
self.name = name
self.conditions = conditions
self.actions = actions
self.committer_group = [] if committer_group is None else committer_group
self.repo = repo
self.bot_user = bot_user
self.dry_run = dry_run
self.next_milestone = next_milestone
log.info("Registered PullRequestFilter %s", name)
def _condition_it(self):
for condition_dict in self.conditions:
for key in condition_dict:
yield (key, condition_dict[key])
def apply(self, pr):
"""Apply a given PRF to a given PR. Causes all appropriate conditions
to be evaluated for a PR, and then the appropriate actions to be
executed
"""
log.debug("\t[%s]", self.name)
try:
self.issue = self.repo.get_issue(pr.number)
except Exception, e:
log.warn("Could not access issue")
log.warn(e)
return False
for (condition_key, condition_value) in self._condition_it():
res = self.evaluate(pr, condition_key, condition_value)
log.debug("\t\t%s, %s => %s", condition_key, condition_value, res)
if not res:
return True
log.info("Matched %s", pr.number)
# If we've made it this far, we pass ALL conditions
for action in self.actions:
self.execute(pr, action)
return True
def _time_to_int(self, result, condition_value):
# Times we shoe-horn into numeric types.
# Since condition_value is a string, we have to have some special
# logic for correcting it into a time
(date_type, date_string) = condition_value.split('::', 1)
if date_type == 'relative':
# Get the current time, adjusted for strings like "168
# hours ago"
current = datetime.datetime.now()
calendar = parsedatetime.Calendar()
compare_against, parsed_as = calendar.parseDT(date_string, current)
elif date_type == 'precise':
compare_against = dtp.parse(date_string)
else:
raise Exception("Unknown date string type. Please use 'precise::2016-01-01' or 'relative::yesterday'")
# Now we update the result to be the total number of seconds
result = (result - compare_against).total_seconds()
# And condition value to zero
condition_value = 0
# As a result, all of the math in evaluate() works.
return result, condition_value
def evaluate(self, pr, condition_key, condition_value):
"""Evaluate a condition like "title_contains" or "plus__ge".
The condition_key maps to a function such as "check_title_contains" or "check_plus"
If there is a '__X' that maps to a comparator function which is
then used to evlauate the result.
"""
# Some conditions contain an aditional operation we must respect, e.g.
# __gt or __eq
if '__' in condition_key:
(condition_key, condition_op) = condition_key.split('__', 1)
else:
condition_op = None
func = getattr(self, 'check_' + condition_key)
result = func(pr, cv=condition_value)
if condition_key == 'created_at':
result, condition_value = self._time_to_int(result, condition_value)
# There are two types of conditions, text and numeric.
# Numeric conditions are only appropriate for the following types:
# 1) plus, 2) minus, 3) times which were hacked in
if condition_key in ('plus', 'minus', 'created_at'):
if condition_op == 'gt':
return int(result) > int(condition_value)
elif condition_op == 'ge':
return int(result) >= int(condition_value)
elif condition_op == 'eq':
return int(result) == int(condition_value)
elif condition_op == 'ne':
return int(result) != int(condition_value)
elif condition_op == 'lt':
return int(result) < int(condition_value)
elif condition_op == 'le':
return int(result) <= int(condition_value)
# Then there are the next set of tpyes which are mostly text types
else:
# These have generally already been evaluated by the function, we
# just return value/!value
if condition_op == 'not':
return not result
else:
return result
def check_title_contains(self, pr, cv=None):
"""condition_value in pr.title
"""
return cv in pr.title
def check_milestone(self, pr, cv=None):
"""condition_value == pr.milestone
"""
return pr.milestone == cv
def check_state(self, pr, cv=None):
"""checks if state == one of cv in (open, closed, merged)
"""
if cv == 'merged':
return pr.merged
else:
return pr.state == cv
def _find_in_comments(self, pr, regex):
"""Search for hits to a regex in a list of comments
"""
if getattr(pr, 'memo_comments', None) is None:
pr.memo_comments = list(self.issue.get_comments())
for comment in pr.memo_comments:
# log.debug('%s, "%s" => %s', regex, comment.body, re.match(regex, comment.body))
if re.findall(regex, comment.body, re.MULTILINE):
yield comment
def check_plus(self, pr, cv=None):
count = 0
for plus1_comment in self._find_in_comments(pr, UPVOTE_REGEX):
if plus1_comment.user.login in self.committer_group:
count += 1
return count
def check_has_tag(self, pr, cv=None):
"""Checks that at least one tag matches the regex provided in condition_value
"""
# Tags aren't actually listed in the PR, we have to fetch the issue for that
m = re.compile(cv)
issue = self.repo.get_issue(pr.number)
for label in issue.get_labels():
if m.match(label.name):
return True
return False
def check_minus(self, pr, cv=None):
count = 0
for minus1_comment in self._find_in_comments(pr, DOWNVOTE_REGEX):
if minus1_comment.user.login in self.committer_group:
count += 1
return count
def check_to_branch(self, pr, cv=None):
return pr.base.ref == cv
def check_created_at(self, pr, cv=None):
"""Due to condition_values with times, check_created_at simply returns pr.created_at
Other math must be done to correctly check time. See _time_to_int
"""
return pr.created_at
def execute(self, pr, action):
"""Execute an action by name.
"""
log.info("Executing action")
if self.dry_run:
return
func = getattr(self, 'execute_' + action['action'])
return func(pr, action)
def execute_comment(self, pr, action):
"""Commenting action, generates a comment on the parent PR
"""
comment_text = action['comment'].format(
author='@' + pr.user.login
#TODO
#merged_by=
).strip().replace('\n', ' ')
# Check if we've made this exact comment before, so we don't comment
# multiple times and annoy people.
for possible_bot_comment in self._find_in_comments(
pr, comment_text):
if possible_bot_comment.user.login == self.bot_user:
log.info("Comment action previously applied, not duplicating")
else:
log.info("Comment action previously applied, not duplicating. However it was applied under a different user. Strange?")
return
# Create the comment
self.issue.create_comment(
comment_text
)
def execute_assign_next_milestone(self, pr, action):
"""Assigns a pr's milestone to next_milestone
"""
# Can only update milestone through associated PR issue.
self.issue.edit(milestone=self.next_milestone)
def execute_assign_tag(self, pr, action):
"""Tags a PR
"""
tag_name = action['action_value']
self.issue.add_to_labels(tag_name)
def execute_remove_tag(self, pr, action):
"""remove a tag from PR if it matches the regex
"""
m = re.compile(action['action_value'])
for label in self.issue.get_labels():
if m.match(label.name):
self.issue.remove_from_labels(label.name)
class MergerBot(object):
def __init__(self, conf_path, dry_run=False):
self.dry_run = dry_run
with open(conf_path, 'r') as handle:
self.config = yaml.load(handle)
self.create_db(database_name=os.path.abspath(
self.config['meta']['database_path']))
self.timefmt = "%Y-%m-%dT%H:%M:%S.Z"
self.repo_owner = self.config['repository']['owner']
self.repo_name = self.config['repository']['name']
self.repo = gh.get_repo(self.repo_owner + '/' + self.repo_name)
self.pr_filters = []
self.next_milestone = [
milestone for milestone in self.repo.get_milestones() if
milestone.title == self.config['repository']['next_milestone']][0]
for rule in self.config['repository']['filters']:
prf = PullRequestFilter(
name=rule['name'],
conditions=rule['conditions'],
actions=rule['actions'],
next_milestone=self.next_milestone,
repo=self.repo,
committer_group=self.config['repository']['pr_approvers'],
bot_user=self.config['meta']['bot_user'],
dry_run=self.dry_run,
)
self.pr_filters.append(prf)
def create_db(self, database_name='cache.sqlite'):
"""Create the database if it doesn't exist"""
self.conn = sqlite3.connect(database_name)
cursor = self.conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS pr_data(
pr_id INTEGER PRIMARY KEY,
updated_at TEXT
)
"""
)
def fetch_pr_from_db(self, id):
"""select PR from database cache by PR #"""
cursor = self.conn.cursor()
cursor.execute("""SELECT * FROM pr_data WHERE pr_id == ?""", (str(id), ))
row = cursor.fetchone()
if row is None:
return row
pretty_row = (
row[0],
datetime.datetime.strptime(row[1], self.timefmt)
)
return pretty_row
def cache_pr(self, id, updated_at):
"""Store the PR in the DB cache, along with the last-updated
date"""
cursor = self.conn.cursor()
cursor.execute("""INSERT INTO pr_data VALUES (?, ?)""",
(str(id), updated_at.strftime(self.timefmt)))
self.conn.commit()
def update_pr(self, id, updated_at):
"""Update the PR date in the cache"""
if self.dry_run:
return
cursor = self.conn.cursor()
cursor.execute("""UPDATE pr_data SET updated_at = ? where pr_id = ?""",
(updated_at.strftime(self.timefmt), str(id)))
self.conn.commit()
def all_prs(self):
"""List all open PRs in the repo.
This... needs work. As it is it fetches EVERY PR, open and closed
and that's a monotonically increasing number of API requests per
run. Suboptimal.
"""
log.info("Locating closed PRs")
results = self.repo.get_pulls(state='closed')
for i, result in enumerate(results):
yield result
log.info("Locating open PRs")
results = self.repo.get_pulls(state='open')
for result in results:
yield result
def get_modified_prs(self):
"""This will contain a list of all new/updated PRs to filter
"""
changed_prs = []
# Loop across our GH results
for resource in self.all_prs():
# Fetch the PR's ID which we use as a key in our db.
cached_pr = self.fetch_pr_from_db(resource.id)
# If it's new, cache it.
if cached_pr is None:
self.cache_pr(resource.id, resource.updated_at)
changed_prs.append(resource)
else:
# compare updated_at times.
cached_pr_time = cached_pr[1]
if cached_pr_time != resource.updated_at:
log.debug('[%s] Cache says: %s last updated at %s', resource.number, cached_pr_time, resource.updated_at)
changed_prs.append(resource)
return changed_prs
def run(self):
"""Find modified PRs, apply the PR filter, and execute associated
actions"""
changed_prs = self.get_modified_prs()
log.info("Found %s PRs to examine", len(changed_prs))
for changed in changed_prs:
log.debug("Evaluating %s", changed.number)
for pr_filter in self.pr_filters:
success = pr_filter.apply(changed)
if success and not self.dry_run:
# Otherwise we'll hit it again later
self.update_pr(changed.id, changed.updated_at)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='P4 bot')
parser.add_argument('--dry-run', dest='dry_run', action='store_true')
args = parser.parse_args()
bot = MergerBot('conf.yaml', **vars(args))
bot.run()