-
Notifications
You must be signed in to change notification settings - Fork 9
/
submit-o-matic.py
executable file
·176 lines (143 loc) · 5.6 KB
/
submit-o-matic.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
#!/usr/bin/python3
import argparse
import json
import logging
import subprocess
import sys
import time
from gerrit import Gerrit
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
class Submitter(object):
def __init__(self, last_cid, review, verify, ready, abandon, force_review,
dry_run):
self.abandon = abandon
self.vote_review = 2 if review else None
self.vote_verify = 1 if verify else None
self.vote_cq_ready = ready
self.force_review = force_review
self.dry_run = dry_run
self.tag = 'autogenerated:submit-o-matic'
self.max_in_flight = 100 # 50 for the cq, 50 for the pre-cq
self.in_flight = []
self.changes = []
self.gerrit = Gerrit('https://chromium-review.googlesource.com',
use_internal=False)
last_change = self.gerrit.get_change(last_cid)
ancestor_changes = self.gerrit.get_ancestor_changes(last_change)
for c in reversed(ancestor_changes):
if c.status == 'NEW':
self.changes.append(c)
self.changes.append(last_change)
def change_needs_action(self, change):
return change.is_merged() or \
(self.vote_review and not change.is_reviewed()) or \
(self.vote_verify and not change.is_verified()) or \
(self.vote_cq_ready and not change.is_cq_ready())
def num_changes(self):
return len(self.changes)
def num_in_flight(self):
return len(self.in_flight)
def review_changes(self):
if not self.vote_review and not self.vote_verify and not self.abandon:
return
for i,c in enumerate(self.changes):
sys.stdout.write('\rRunning reviewer (%d/%d)' % (i, self.num_changes()))
c = self.gerrit.get_change(c.number)
if self.abandon:
if not self.dry_run:
self.gerrit.abandon(c)
else:
print('DRYRUN abandon {}'.format(c))
continue
if (c.is_merged() or not self.change_needs_action(c)) and not self.force_review:
continue
if not self.dry_run:
self.gerrit.review(c, self.tag, '', False, self.vote_review,
self.vote_verify, None)
else:
print('DRYRUN review (r={}, v={}) {}'.format(self.vote_review,
self.vote_verify,
c))
def submit_changes(self):
if self.abandon:
return
self.in_flight = []
merged = 0
for i,c in enumerate(self.changes):
if self.num_in_flight() >= self.max_in_flight:
break
sys.stdout.write('\rRunning submitter (%d/%d)' % (i, self.num_changes()))
c = self.gerrit.get_change(c.id)
if c.is_merged():
merged += 1
continue
if self.change_needs_action(c):
if not self.dry_run:
self.gerrit.review(c, self.tag, '', False, self.vote_review,
self.vote_verify, self.vote_cq_ready)
else:
print('DRYRUN review (r={}, v={} cq={}) {}'.format(self.vote_review,
self.vote_verify,
self.vote_cq_ready,
c))
self.in_flight.append(c)
sys.stdout.write('\r%d Changes: \n' %
self.num_changes())
sys.stdout.write('-- %d merged\n' % merged)
sys.stdout.write('-- %d in flight\n' % self.num_in_flight())
def detect_change(self):
if self.num_in_flight() == 0: # everything is merged, so no detection needed
return True
c = self.in_flight[0]
sys.stdout.write('\rDetecting: %s' % c.url())
c = self.gerrit.get_change(c.id)
if self.change_needs_action(c):
return True
return False
def main():
parser = argparse.ArgumentParser(description='Auto review/submit gerrit cls')
parser.add_argument('--last-cid', default=None, required=True,
help='Gerrit change-id of last patch in set')
parser.add_argument('--daemon', action='store_true',
help='Run in daemon mode, continuously update changes until merged')
parser.add_argument('--review', action='store_true',
help='Mark changes as reviewed')
parser.add_argument('--verify', action='store_true',
help='Mark changes as verified')
parser.add_argument('--ready', action='store_true',
help='Mark changes as ready')
parser.add_argument('--force-review', action='store_true',
help='Force reviewer to act on all patches in the series')
parser.add_argument('--tryjob', action='store_true',
help='Mark changes as ready +1 (tryjob)')
parser.add_argument('--abandon', action='store_true', help='Abandon changes')
parser.add_argument('--dry-run', action='store_true', help='Practice makes perfect')
parser.add_argument('--max-tries', default=5, help='Max number to try submit in daemon mode', type=int)
args = parser.parse_args()
ready = None
if args.ready:
ready = 2
elif args.tryjob:
ready = 1
s = Submitter(args.last_cid, args.review, args.verify, ready, args.abandon,
args.force_review, args.dry_run)
s.review_changes()
tries = 0
while True:
s.submit_changes()
if s.num_in_flight() == 0:
sys.stdout.write('\n\nCongratulations, your changes have landed!\n\n')
return True
if not args.daemon:
break
if tries >= args.max_tries:
sys.stdout.write('\n\nMax tries exceeded!\n\n')
return False
tries += 1
while True:
sys.stdout.write('\rSleeping... ')
if s.detect_change():
break
time.sleep(60)
if __name__ == '__main__':
sys.exit(main())