/
watch_newcomers.py
255 lines (217 loc) · 8.84 KB
/
watch_newcomers.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
#!/usr/bin/python3
"""
watch_newcomers.py
Welcomes newcomers and adds them to a group!
MIT license
:author: Srishti Sethi <ssethi@wikimedia.org>
"""
import os
import configparser
import queue
import json
import logging
import threading
import time
import re
import paramiko
import requests
from requests.auth import HTTPBasicAuth
from pygerrit2.rest import GerritRestAPI
from twilio.rest import TwilioRestClient
QUEUE = queue.Queue()
# Logging
logging.basicConfig(level=logging.DEBUG,
filename='gerrit-newcomer-bot.log',
filemode='a+',
format='%(asctime)-15s %(levelname)-8s %(message)s')
# Load configuration
CONFIG = configparser.ConfigParser()
DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(DIR, 'gerrit.conf')
CONFIG.read(CONFIG_PATH)
GERRIT_SSH = dict()
GERRIT_SSH.update(CONFIG.items('Gerrit SSH'))
GERRIT_SSH['port'] = int(GERRIT_SSH['port'])
GERRIT_SSH['timeout'] = int(GERRIT_SSH['timeout'])
MISC = dict()
MISC.update(CONFIG.items('Misc'))
TWILIO = dict()
TWILIO.update(CONFIG.items('Twilio'))
# Paramiko client
SSH_CLIENT = paramiko.SSHClient()
HOSTKEY_PATH = os.path.join(DIR, 'ssh-host-key')
SSH_CLIENT.load_host_keys(HOSTKEY_PATH)
SSH_CLIENT.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SSH_CLIENT.connect(**GERRIT_SSH)
# Rest client
REST_AUTH = HTTPBasicAuth(MISC['auth_username'], MISC['auth_password'])
REST_CLIENT = GerritRestAPI(url=MISC['base_url'], auth=REST_AUTH)
# Twilio client
TWILIO_CLIENT = TwilioRestClient(TWILIO['account_sid'], TWILIO['auth_token'])
class WatchPatchsets(threading.Thread):
"""This class watches gerrit stream event patchset-created
"""
def run(self):
while True:
try:
cmd_patchset_created = 'gerrit stream-events -s patchset-created'
_, stdout, _ = SSH_CLIENT.exec_command(cmd_patchset_created)
for line in stdout:
QUEUE.put(json.loads(line))
except BaseException as err:
e = 'Error occured while watching event: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body='Error watching event')
finally:
SSH_CLIENT.close()
time.sleep(5)
class WelcomeNewcomersAndGroupThem():
"""This class check for number of patches of the submitter and
categorizes them as:
* First time contributor - if 1 patch in Gerrit
* New contributor - more than 1 patch in Gerrit
* Rising contributor - more than 5 patches in Gerrit
It then adds a reviewer "First-time-contributor" and welcome message
to a patch if the submitter is a first time contributor
It also adds all first time and new contributors to a group 'Newcomer'
"""
def __init__(self):
self.new_contributor = False
self.first_time_contributor = False
self.rising_contributor = False
def identify(self, submitter):
""" Identifies if a submitter is a first time, new or rising contributor
"""
try:
patches_by_owner = REST_CLIENT.get("/changes/?q=owner:" + submitter)
num_patches = len(patches_by_owner)
if num_patches == 1:
self.first_time_contributor = True
elif num_patches > 0 and num_patches <= 5:
self.new_contributor = True
elif num_patches > 5:
self.rising_contributor = True
except BaseException as err:
e = 'Error occured while identifying patch owner: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def is_first_time_contributor(self):
""" Returns first_time_contributor as boolean
"""
return self.first_time_contributor
def is_new_contributor(self):
""" Returns new_contributor as boolean
"""
return self.new_contributor
def is_rising_contributor(self):
""" Returns rising_contributor as boolean
"""
return self.rising_contributor
def add_reviewer_and_comment(self, change_id, cur_rev):
""" Adds a reviewer "First-time-contributor" and welcome message
to a patch
"""
try:
if not self.is_reviewer_added_already(change_id):
query = "/changes/" + str(change_id) + "/revisions/" + str(cur_rev) + "/review"
REST_CLIENT.post(query, json={
"message": self.fetch_welcome_message()
})
except BaseException as err:
e = 'Error occured while adding reviewer and welcome comment: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def add_to_group(self, username):
""" Adds newcomer to a group
"""
try:
query_add_member = "/groups/" + MISC['newcomer_group'] + \
"/members/" + username
REST_CLIENT.put(query_add_member)
except BaseException as err:
e = 'Error occured while adding newcomer to group: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def remove_from_group(self, username):
""" Removes newcomer from a group
"""
try:
if self.is_rising_contributor_in_group(username):
query_del_member = "/groups/" + MISC['newcomer_group'] + \
"/members/" + username
REST_CLIENT.delete(query_del_member)
except BaseException as err:
e = 'Error occured while removing newcomer from group: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def is_rising_contributor_in_group(self, username):
""" Check if rising contributor is member of newcomer group
"""
try:
query_ls_members = "/groups/" + MISC['newcomer_group'] + \
"/members/"
group_members = REST_CLIENT.get(query_ls_members)
num_of_members = len(group_members)
for i in range(num_of_members):
if username == group_members[i]['username']:
return True
return False
except BaseException as err:
e = 'Error listing members of newcomer group: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def is_reviewer_added_already(self, change_id):
""" Check if newcomer bot is already added as a reviewer to a patch
"""
try:
query_change_details = "/changes/" + str(change_id) + "/detail"
change_details = REST_CLIENT.get(query_change_details)
reviewers = change_details["removable_reviewers"]
num_of_reviewers = len(reviewers)
for i in range(num_of_reviewers):
if MISC['auth_username'] == reviewers[i]['username']:
return True
return False
except BaseException as err:
e = 'Error occured while querying change details: %s', err
logging.debug(e)
TWILIO_CLIENT.messages.create(from_=TWILIO['from_num'], to=TWILIO['to_num'], body=e)
def fetch_welcome_message(self):
""" Fetch welcome message from a remote wiki page
"""
# build the API request url
url = "https://www.mediawiki.org/w/index.php?title=" + \
MISC['welcome_message_page'] + "&action=raw"
response = requests.get(url)
content = response.text
# remove tags
content = re.compile(r'<.*?>').sub('', content)
return content
def main(event):
""" Invokes functions of class 'WelcomeNewcomersAndGroupThem'
"""
logging.info('Patch details: %s', event)
username = event['patchSet']['author']['username']
change_id = event['change']['id']
revision = event['patchSet']['revision']
newcomer = WelcomeNewcomersAndGroupThem()
newcomer.identify(username)
logging.info('Patch has been uploaded by user: %s', username)
if newcomer.is_first_time_contributor():
logging.info('Patch owner is a first time contributor')
newcomer.add_reviewer_and_comment(change_id, revision)
newcomer.add_to_group(username)
if newcomer.is_new_contributor():
logging.info('Patch owner is a new contributor')
newcomer.add_to_group(username)
if newcomer.is_rising_contributor():
logging.info('Patch owner is a rising contributor')
newcomer.remove_from_group(username)
STREAM = WatchPatchsets()
STREAM.daemon = True
STREAM.start()
while True:
EVENT = QUEUE.get()
if EVENT:
main(EVENT)
STREAM.join()