forked from atseanpaul/review-o-matic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit-o-matic.py
executable file
·123 lines (98 loc) · 3.88 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
#!/usr/bin/env python3
import argparse
import json
import subprocess
import sys
import time
from gerrit import Gerrit
class Submitter(object):
def __init__(self, last_cid, review, verify, ready):
self.vote_review = 2 if review else None
self.vote_verify = 1 if verify else None
self.vote_cq_ready = 2 if ready else None
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=True)
last_change = self.gerrit.get_change(last_cid)
self.changes = self.gerrit.get_related_changes(last_change)
if not self.changes:
self.changes = [last_change]
else:
self.changes.reverse()
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):
for i,c in enumerate(self.changes):
sys.stdout.write('\rRunning reviewer (%d/%d)' % (i, self.num_changes()))
c = self.gerrit.get_change(c.change_id)
if c.is_merged() or not self.change_needs_action(c):
continue
self.gerrit.review(c, self.tag, '', False, self.vote_review,
self.vote_verify, None)
def submit_changes(self):
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.change_id)
if c.is_merged():
merged += 1
continue
if self.change_needs_action(c):
self.gerrit.review(c, self.tag, '', False, self.vote_review,
self.vote_verify, self.vote_cq_ready)
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.change_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('--no-review', action='store_false', dest='review',
default=True, help='Don\'t mark changes as reviewed')
parser.add_argument('--no-verify', action='store_false', dest='verify',
default=True, help='Don\'t mark changes as verified')
parser.add_argument('--no-ready', action='store_false', dest='ready',
default=True, help='Don\'t mark changes as ready')
args = parser.parse_args()
s = Submitter(args.last_cid, args.review, args.verify, args.ready)
s.review_changes()
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
while True:
sys.stdout.write('\rSleeping... ')
if s.detect_change():
break
time.sleep(60)
if __name__ == '__main__':
sys.exit(main())