Skip to content

Commit 3db73f2

Browse files
committed
Use process_event in synchronization
1 parent 860b5d4 commit 3db73f2

File tree

4 files changed

+101
-55
lines changed

4 files changed

+101
-55
lines changed

homu/main.py

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import shlex
3434
import random
3535
from .pull_req_state import PullReqState
36+
from .pull_request_events import all as all_pull_request_events
3637

3738
global_cfg = {}
3839

@@ -1166,66 +1167,85 @@ def synchronize(repo_label, repo_cfg, logger, gh, states, repos, db, mergeable_q
11661167
states[repo_label] = {}
11671168
repos[repo_label] = Repository(repo, repo_label, db)
11681169

1170+
print("Getting pulls...")
11691171
for pull in repo.iter_pulls(state='open'):
1170-
db.execute(
1171-
'SELECT status FROM pull WHERE repo = ? AND num = ?',
1172-
[repo_label, pull.number])
1173-
row = db.fetchone()
1174-
if row:
1175-
status = row[0]
1176-
else:
1177-
status = ''
1178-
for info in utils.github_iter_statuses(repo, pull.head.sha):
1179-
if info.context == 'homu':
1180-
status = info.state
1181-
break
1172+
# db.execute(
1173+
# 'SELECT status FROM pull WHERE repo = ? AND num = ?',
1174+
# [repo_label, pull.number])
1175+
# row = db.fetchone()
1176+
# if row:
1177+
# status = row[0]
1178+
# else:
1179+
# status = ''
1180+
# for info in utils.github_iter_statuses(repo, pull.head.sha):
1181+
# if info.context == 'homu':
1182+
# status = info.state
1183+
# break
1184+
1185+
if pull.number in [60966, 60730, 60547, 59312]:
1186+
# TODO: WHY DOES THIS HAPPEN!?
1187+
print("Skipping {} because GraphQL never returns a success!".format(pull.number))
1188+
continue
1189+
1190+
print("{}/{}#{}".format(repo_cfg['owner'], repo_cfg['name'], pull.number))
1191+
access_token = global_cfg['github']['access_token']
1192+
response = all_pull_request_events(access_token, repo_cfg['owner'], repo_cfg['name'], pull.number)
1193+
status = ''
11821194

11831195
state = PullReqState(pull.number, pull.head.sha, status, db, repo_label, mergeable_que, gh, repo_cfg['owner'], repo_cfg['name'], repo_cfg.get('labels', {}), repos) # noqa
1184-
state.title = pull.title
1196+
state.cfg = repo_cfg
1197+
state.title = response.initial_title
11851198
state.body = pull.body
11861199
state.head_ref = pull.head.repo[0] + ':' + pull.head.ref
11871200
state.base_ref = pull.base.ref
1188-
state.set_mergeable(None)
1189-
state.assignee = pull.assignee.login if pull.assignee else ''
1190-
1191-
for comment in pull.iter_comments():
1192-
if comment.original_commit_id == pull.head.sha:
1193-
parse_commands(
1194-
comment.body,
1195-
comment.user.login,
1196-
repo_label,
1197-
repo_cfg,
1198-
state,
1199-
my_username,
1200-
db,
1201-
states,
1202-
sha=comment.original_commit_id,
1203-
command_src=comment.to_json()['html_url'],
1204-
# FIXME switch to `comment.html_url`
1205-
# after updating github3 to 1.3.0+
1206-
)
1207-
1208-
for comment in pull.iter_issue_comments():
1209-
parse_commands(
1210-
comment.body,
1211-
comment.user.login,
1212-
repo_label,
1213-
repo_cfg,
1214-
state,
1215-
my_username,
1216-
db,
1217-
states,
1218-
command_src=comment.to_json()['html_url'],
1219-
# FIXME switch to `comment.html_url`
1220-
# after updating github3 to 1.3.0+
1221-
)
1222-
1223-
saved_state = saved_states.get(pull.number)
1224-
if saved_state:
1225-
for key, val in saved_state.items():
1226-
setattr(state, key, val)
1227-
1228-
state.save()
1201+
if response.mergeable == 'MERGEABLE':
1202+
state.set_mergeable(True)
1203+
elif response.mergeable == 'CONFLICTING':
1204+
state.set_mergeable(False)
1205+
else:
1206+
state.set_mergeable(None)
1207+
state.assignee = ''
1208+
1209+
# for comment in pull.iter_comments():
1210+
# if comment.original_commit_id == pull.head.sha:
1211+
# parse_commands(
1212+
# comment.body,
1213+
# comment.user.login,
1214+
# repo_label,
1215+
# repo_cfg,
1216+
# state,
1217+
# my_username,
1218+
# db,
1219+
# states,
1220+
# sha=comment.original_commit_id,
1221+
# command_src=comment.to_json()['html_url'],
1222+
# # FIXME switch to `comment.html_url`
1223+
# # after updating github3 to 1.3.0+
1224+
# )
1225+
#
1226+
# for comment in pull.iter_issue_comments():
1227+
# parse_commands(
1228+
# comment.body,
1229+
# comment.user.login,
1230+
# repo_label,
1231+
# repo_cfg,
1232+
# state,
1233+
# my_username,
1234+
# db,
1235+
# states,
1236+
# command_src=comment.to_json()['html_url'],
1237+
# # FIXME switch to `comment.html_url`
1238+
# # after updating github3 to 1.3.0+
1239+
# )
1240+
#
1241+
# saved_state = saved_states.get(pull.number)
1242+
# if saved_state:
1243+
# for key, val in saved_state.items():
1244+
# setattr(state, key, val)
1245+
#
1246+
# state.save()
1247+
for event in response.events:
1248+
state.process_event(event)
12291249

12301250
states[repo_label][pull.number] = state
12311251

homu/pull_req_state.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ def process_event(self, event):
420420
# We don't care about any of these events.
421421
pass
422422

423+
elif event.event_type in [
424+
'UnassignedEvent',
425+
'MilestonedEvent',
426+
'DemilestonedEvent',
427+
'ReviewRequestedEvent',
428+
'ReviewDismissedEvent',
429+
'CommentDeletedEvent']:
430+
# TODO! Review these events to see if we care about any of them.
431+
# These events were seen as "Unknown event type: {}" when doing initial testing.
432+
pass
433+
423434
else:
424435
# Ooops, did we miss this event type? Or is it new?
425436
print("Unknown event type: {}".format(event.event_type))
@@ -451,7 +462,9 @@ def process_issue_comment(self, event, command):
451462
botname = 'bors'
452463
username = event['author']['login']
453464
# TODO: Don't hardcode repo_cfg
454-
repo_cfg = {}
465+
#repo_cfg = {}
466+
repo_cfg = self.cfg
467+
455468
_assert_reviewer_auth_verified = functools.partial(
456469
assert_authorized,
457470
username,
@@ -744,6 +757,7 @@ def process_issue_comment(self, event, command):
744757
state_changed = True
745758

746759
except AuthorizationException as e:
760+
print("{} is unauthorized".format(event['author']['login']))
747761
result.comments.append(e.comment)
748762

749763
return result

homu/pull_request_events.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ def all(access_token, owner, repo, pull):
244244

245245
r = response.json()
246246

247+
if 'errors' in r:
248+
print("GraphQL query failed:")
249+
for error in r['errors']:
250+
print(" * {}".format(error['message']))
251+
time.sleep(1)
252+
continue
253+
254+
if 'data' not in r:
255+
print("response.status_code = {}".format(response.status_code))
256+
print("r = {}".format(r))
257+
247258
pull_request = r['data']['repository']['pullRequest']
248259
page_info = pull_request['timelineItems']['pageInfo']
249260
events = pull_request['timelineItems']['nodes']

homu/tests/test_process_event.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def new_state(num=1, head_sha='abcdef', status='', title='A change'):
3434
})
3535

3636
state.title = title
37+
state.cfg = {}
3738

3839
return state
3940

0 commit comments

Comments
 (0)