Skip to content

Commit a024fbe

Browse files
committed
Keep track of cursor and break up status
Break up the "status" field into multiple orthogonal state fields: * build state (whether the primary is running or has succeeded or failed) * try state (whether the most recent try is running or has succeeded or failed) * approval state (whether the pull request is approved) Previously (and still) these were mostly possible to determine by looking at `state.get_status()` and `state.try_`, but storing them separately helps make state changes more explicit. Also, keep track of the current github synchronization cursor in the pull request state, so that we can use it later.
1 parent 2526ffe commit a024fbe

File tree

3 files changed

+173
-83
lines changed

3 files changed

+173
-83
lines changed

homu/pull_req_state.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
AuthorizationException,
1616
AuthState,
1717
)
18+
from enum import Enum
1819

1920

2021
class ProcessEventResult:
@@ -40,6 +41,26 @@ def sha_or_blank(sha):
4041
return sha if re.match(r'^[0-9a-f]+$', sha) else ''
4142

4243

44+
45+
class BuildState(Enum):
46+
"""
47+
The state of a merge build or a try build
48+
"""
49+
NONE = 'none'
50+
PENDING = 'pending'
51+
SUCCESS = 'success'
52+
FAILURE = 'failure'
53+
ERROR = 'error'
54+
55+
56+
class ApprovalState(Enum):
57+
"""
58+
The approval state for a pull request.
59+
"""
60+
UNAPPROVED = 'unapproved'
61+
APPROVED = 'approved'
62+
63+
4364
class PullReqState:
4465
num = 0
4566
priority = 0
@@ -50,6 +71,9 @@ class PullReqState:
5071
base_ref = ''
5172
assignee = ''
5273
delegate = ''
74+
last_github_cursor = None
75+
build_state = BuildState.NONE
76+
try_state = BuildState.NONE
5377

5478
def __init__(self, num, head_sha, status, db, repo_label, mergeable_que,
5579
gh, owner, name, label_events, repos):
@@ -94,6 +118,13 @@ def __repr__(self):
94118
self.status,
95119
)
96120

121+
@property
122+
def approval_state(self):
123+
if self.approved_by != '':
124+
return ApprovalState.APPROVED
125+
else:
126+
return ApprovalState.UNAPPROVED
127+
97128
def sort_key(self):
98129
return [
99130
STATUS_TO_PRIORITY.get(self.get_status(), -1),
@@ -339,6 +370,8 @@ def process_event(self, event):
339370
applied as a result of this event.
340371
"""
341372

373+
self.last_github_cursor = event.cursor
374+
342375
# TODO: Don't hardcode botname!
343376
botname = 'bors'
344377
# TODO: Don't hardcode hooks!
@@ -356,13 +389,18 @@ def process_event(self, event):
356389
# TODO: Do we *always* reset the state?
357390
result.changed = result.changed or self.status != ''
358391
self.status = ''
392+
result.changed = result.changed or self.try_state != BuildState.NONE
393+
self.try_state = BuildState.NONE
394+
result.changed = result.changed or self.build_state != BuildState.NONE
395+
self.build_state = BuildState.NONE
359396

360397
elif event.event_type == 'HeadRefForcePushedEvent':
361398
result.changed = self.head_sha != event['afterCommit']['oid']
362399
self.head_sha = event['afterCommit']['oid']
363400
# New commits come in: no longer approved
364401
result.changed = result.changed or self.approved_by != ''
365402
self.approved_by = ''
403+
# TODO: Do we need to reset the state here?
366404

367405
elif event.event_type == 'BaseRefChangedEvent':
368406
# Base ref changed: no longer approved
@@ -788,33 +826,25 @@ def process_homu_state(self, event, command):
788826
result.changed = True
789827
self.try_ = False
790828
self.status = 'pending'
791-
# TODO: Something with states
792-
# result.changed = True
793-
# self.build_state = 'pending'
794-
pass
829+
self.build_state = BuildState.PENDING
795830

796831
elif state['type'] == 'BuildCompleted':
797832
result.changed = True
798833
self.try_ = False
799834
self.status = 'completed'
800-
# TODO: Something with states
801-
# result.changed = True
802-
# self.build_state = 'completed'
803-
pass
835+
self.build_state = BuildState.SUCCESS
804836

805837
elif state['type'] == 'BuildFailed':
806838
result.changed = True
807839
self.try_ = False
808840
self.status = 'failure'
809-
# TODO: Something with states
810-
# result.changed = True
811-
# self.build_state = 'failure'
812-
pass
841+
self.build_state = BuildState.FAILURE
813842

814843
elif state['type'] == 'TryBuildStarted':
815844
result.changed = True
816845
self.try_ = True
817846
self.status = 'pending'
847+
self.try_state = BuildState.PENDING
818848
# TODO: Multiple tries?
819849
# result.changed = True
820850
# self.tries.append(PullRequestTry(
@@ -827,6 +857,7 @@ def process_homu_state(self, event, command):
827857
elif state['type'] == 'TryBuildCompleted':
828858
result.changed = True
829859
self.status = 'success'
860+
self.try_state = BuildState.SUCCESS
830861
# TODO: Multiple tries?
831862
# item = next((try_
832863
# for try_ in self.tries
@@ -844,6 +875,7 @@ def process_homu_state(self, event, command):
844875
elif state['type'] == 'TryBuildFailed':
845876
result.changed = True
846877
self.status = 'failure'
878+
self.try_state = BuildState.FAILURE
847879

848880
return result
849881

homu/pull_request_events.py

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,89 +18,98 @@
1818
hasNextPage
1919
endCursor
2020
}
21-
nodes {
22-
eventType: __typename
23-
... on PullRequestCommit {
24-
commit {
25-
oid
21+
edges {
22+
cursor
23+
node {
24+
eventType: __typename
25+
... on PullRequestCommit {
26+
commit {
27+
oid
28+
}
2629
}
27-
}
28-
... on AssignedEvent {
29-
actor {
30-
login
30+
... on AssignedEvent {
31+
actor {
32+
login
33+
}
34+
user {
35+
login
36+
}
3137
}
32-
user {
33-
login
38+
... on UnassignedEvent {
39+
actor {
40+
login
41+
}
42+
user {
43+
login
44+
}
3445
}
35-
}
36-
... on UnassignedEvent {
37-
actor {
38-
login
46+
... on IssueComment {
47+
author {
48+
login
49+
}
50+
body
51+
publishedAt
3952
}
40-
user {
41-
login
53+
... on SubscribedEvent {
54+
actor {
55+
login
56+
}
4257
}
43-
}
44-
... on IssueComment {
45-
author {
46-
login
58+
... on LabeledEvent {
59+
actor {
60+
login
61+
}
62+
label {
63+
name
64+
}
4765
}
48-
body
49-
publishedAt
50-
}
51-
... on SubscribedEvent {
52-
actor {
53-
login
66+
... on UnlabeledEvent {
67+
actor {
68+
login
69+
}
70+
label {
71+
name
72+
}
5473
}
55-
}
56-
... on LabeledEvent {
57-
actor {
58-
login
74+
... on BaseRefChangedEvent {
75+
actor {
76+
login
77+
}
5978
}
60-
label {
61-
name
79+
... on HeadRefForcePushedEvent {
80+
actor {
81+
login
82+
}
83+
beforeCommit {
84+
oid
85+
}
86+
afterCommit {
87+
oid
88+
}
6289
}
63-
}
64-
... on UnlabeledEvent {
65-
actor {
66-
login
67-
}
68-
label {
69-
name
90+
... on RenamedTitleEvent {
91+
actor {
92+
login
93+
}
94+
previousTitle
95+
currentTitle
7096
}
71-
}
72-
... on BaseRefChangedEvent {
73-
actor {
74-
login
75-
}
76-
}
77-
... on HeadRefForcePushedEvent {
78-
actor {
79-
login
80-
}
81-
beforeCommit {
82-
oid
83-
}
84-
afterCommit {
85-
oid
86-
}
87-
}
88-
... on RenamedTitleEvent {
89-
actor {
90-
login
91-
}
92-
previousTitle
93-
currentTitle
94-
}
95-
... on MentionedEvent {
96-
actor {
97-
login
97+
... on MentionedEvent {
98+
actor {
99+
login
100+
}
98101
}
99102
}
100103
}
101104
}
102105
}
103106
}
107+
rateLimit {
108+
limit
109+
cost
110+
remaining
111+
resetAt
112+
}
104113
}
105114
"""
106115

@@ -126,7 +135,8 @@ def initial_title(self):
126135

127136

128137
class PullRequestEvent:
129-
def __init__(self, data):
138+
def __init__(self, cursor, data):
139+
self.cursor = cursor
130140
self.data = data
131141

132142
def __getitem__(self, key):
@@ -262,15 +272,17 @@ def all(access_token, owner, repo, pull):
262272

263273
pull_request = r['data']['repository']['pullRequest']
264274
page_info = pull_request['timelineItems']['pageInfo']
265-
events = pull_request['timelineItems']['nodes']
275+
events = pull_request['timelineItems']['edges']
266276

267277
result.title = pull_request['title']
268278
result.author = pull_request['author']['login']
269279
result.state = pull_request['state']
270280
result.head_sha = pull_request['headRefOid']
271281
result.mergeable = pull_request['mergeable']
272282

273-
result.events.extend([PullRequestEvent(e) for e in events])
283+
result.events.extend([PullRequestEvent(e['cursor'], e['node'])
284+
for e
285+
in events])
274286

275287
if not page_info['hasNextPage']:
276288
break

0 commit comments

Comments
 (0)