This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
frrbot.py
402 lines (324 loc) · 12.3 KB
/
frrbot.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python3
#
# Deps:
# pip3 install flask PyGithub apscheduler sqlalchemy dateparser
#
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask
from flask import Response
from flask import request
from github import Github
from github import GithubException
from hmac import HMAC
from werkzeug.exceptions import BadRequest
import dateparser
import datetime
import hmac
import json
import os
import re
import yaml
# Global data ------------------------------------------------------------------
autoclosemsg = "This issue will be automatically closed in one week unless there is further activity."
noautoclosemsg = "This issue will no longer be automatically closed."
triggerlabel = "autoclose"
pr_greeting_msg = "Thanks for your contribution to FRR!\n\n"
pr_warn_signoff_msg = "* One of your commits is missing a `Signed-off-by` line; we can't accept your contribution until all of your commits have one\n"
pr_warn_blankln_msg = "* One of your commits does not have a blank line between the summary and body; this will break `git log --oneline`\n"
pr_warn_commit_msg = (
"* One of your commits has an improperly formatted commit message\n"
)
pr_guidelines_ref_msg = "\nIf you are a new contributor to FRR, please see our [contributing guidelines](http://docs.frrouting.org/projects/dev-guide/en/latest/workflow.html#coding-practices-style).\n"
# Scheduler functions ----------------------------------------------------------
def close_issue(rn, num):
app.logger.warning("[+] Closing issue #{}".format(num))
repo = g.get_repo(rn)
issue = repo.get_issue(num)
issue.edit(state="closed")
try:
issue.remove_from_labels(triggerlabel)
except GithubException as e:
pass
def schedule_close_issue(issue, when):
"""
Schedule an issue to be automatically closed on a certain date.
:param github.Issue.Issue issue: issue to close
:param datetime.datetime when: When to close the issue
"""
reponame = issue.repository.full_name
issuenum = issue.number
issueid = "{}@@@{}".format(reponame, issuenum)
app.logger.warning(
"[-] Scheduling issue #{} for autoclose (id: {})".format(issuenum, issueid)
)
scheduler.add_job(
close_issue,
run_date=when,
args=[reponame, issuenum],
id=issueid,
replace_existing=True,
)
def cancel_close_issue(issue):
"""
Dechedule an issue to be automatically closed on a certain date.
:param github.Issue.Issue issue: issue to cancel
"""
reponame = issue.repository.full_name
issuenum = issue.id
issueid = "{}@@@{}".format(reponame, issuenum)
app.logger.warning("[-] Descheduling issue #{} for closing".format(issuenum))
scheduler.remove_job(issueid)
# Module init ------------------------------------------------------------------
print("[+] Loading config")
with open("config.yaml", "r") as conffile:
conf = yaml.safe_load(conffile)
whsec = conf["gh_webhook_secret"]
auth = conf["gh_auth_token"]
print("[+] Github auth token: {}".format(auth))
print("[+] Github webhook secret: {}".format(whsec))
# Initialize GitHub API
g = Github(auth)
my_user = g.get_user().login
print("[+] Initialized GitHub API object")
# Initialize scheduler
jobstores = {"default": SQLAlchemyJobStore(url="sqlite:///jobs.sqlite")}
scheduler = BackgroundScheduler(jobstores=jobstores)
scheduler.start()
print("[+] Initialized scheduler")
print("[+] Current jobs:")
scheduler.print_jobs()
# Initialize Flask app
app = Flask(__name__)
print("[+] Initialized Flask app")
# Webhook handlers -------------------------------------------------------------
def issue_labeled(j):
reponame = j["repository"]["full_name"]
issuenum = j["issue"]["number"]
issue = g.get_repo(reponame).get_issue(issuenum)
def label_autoclose():
closedate = datetime.datetime.now() + datetime.timedelta(weeks=1)
schedule_close_issue(issue, closedate)
issue.create_comment(autoclosemsg)
label_actions = {"autoclose": label_autoclose}
try:
labelname = j["label"]["name"]
label_actions[labelname]()
except KeyError:
pass
return Response("OK", 200)
def issue_comment_created(j):
"""
Handle an issue comment being created.
First we check if the comment contains a trigger phrase. If it does, and
the user who made the comment has admin privileges on the repository, we
then try to parse the trigger phrase and its arguments and take the
specified action. If the action fails to parse nothing is done.
If the comment doesn't contain a trigger phrase, and this issue is
scheduled for autoclose, then we'll consider the comment to be activity on
the issue and cancel the autoclose.
Trigger phrases are of the form '@<botusername> <verb> <arguments>.
Current verbs:
autoclose <time period>
Automatically close this issue in <time period>.
"""
reponame = j["repository"]["full_name"]
issuenum = j["issue"]["number"]
repo = g.get_repo(reponame)
issue = repo.get_issue(issuenum)
body = j["comment"]["body"]
sender = j["sender"]["login"]
perm = repo.get_collaborator_permission(sender)
def verb_autoclose(arg):
"""
Verb to automatically close an issue after a certain period of time.
:param tp str: trigger phrase
:param arg str: automatically close this issue in <arg>, where <arg> is
a time period in the future or a date. For instance, time period could
be "in 1 day" to close the issue in 1 day, or "May 25th" to specify the
next occurring May 15th.
"""
if not (perm == "write" or perm == "admin"):
app.logger.warning("[-] User '{}' ({}) isn't authorized to use this command".format(sender, perm))
return
closedate = dateparser.parse(arg)
if closedate is not None and closedate > datetime.datetime.now():
schedule_close_issue(issue, closedate)
issue.add_to_labels("autoclose")
issue.get_comment(j["comment"]["id"]).create_reaction("+1")
elif closedate is None:
app.logger.warning("[-] Couldn't parse '{}' as a datetime".format(arg))
verbs = {"autoclose": verb_autoclose}
had_verb = False
for verb in verbs.keys():
tp = "@{} {} ".format(my_user, verb)
if tp.lower() in body.lower():
partition = body.lower().partition(tp.lower())
app.logger.warning("[+] Trigger detected: {} {}".format(partition[1], partition[2]))
verbs[verb](partition[2])
had_verb = True
issueid = "{}@@@{}".format(reponame, issuenum)
if not had_verb and scheduler.get_job(issueid) is not None:
scheduler.remove_job(issueid)
issue.remove_from_labels(triggerlabel)
issue.create_comment(noautoclosemsg)
return Response("OK", 200)
def pull_request_opened(j):
"""
Handle a pull request being opened.
This function checks each commit's message for proper summary line
formatting, Signed-off-by, and modified directories. If it finds formatting
issues or missing Signed-off-by, it leaves a review on the PR asking for
the problem to be fixed.
Also, modified directories are extracted from commits and used to apply the
corresponding topic labels.
"""
reponame = j["repository"]["full_name"]
repo = g.get_repo(reponame)
pr = repo.get_pull(j["number"])
commits = pr.get_commits()
labels = set()
# apply labels based on commit messages
label_map = {
"babeld": "babel",
"bfdd": "bfd",
"bgpd": "bgp",
"debian": "packaging",
"doc": "documentation",
"docker": "docker",
"eigrpd": "eigrp",
"fpm": "fpm",
"isisd": "isis",
"ldpd": "ldp",
"lib": "libfrr",
"nhrpd": "nhrp",
"ospf6d": "ospfv3",
"ospfd": "ospf",
"pbrd": "pbr",
"pimd": "pim",
"pkgsrc": "packaging",
"python": "clippy",
"redhat": "packaging",
"ripd": "rip",
"ripngd": "ripng",
"sharpd": "sharp",
"snapcraft": "packaging",
"solaris": "packaging",
"staticd": "staticd",
"tests": "tests",
"tools": "tools",
"vtysh": "vtysh",
"vrrp": "vrrp",
"watchfrr": "watchfrr",
"yang": "yang",
"zebra": "zebra",
# files
"configure.ac": "build",
"makefile.am": "build",
"bootstrap.sh": "build",
}
warn_bad_msg = False
warn_signoff = False
warn_blankln = False
for commit in commits:
msg = commit.commit.message
if msg.startswith("Revert") or msg.startswith("Merge"):
continue
if len(msg.split('\n')) < 2 or len(msg.split('\n')[1]) > 0:
warn_blankln = True
match = re.match(r"^([^:\n]+):", msg)
if match:
lbls = map(lambda x: x.strip(), match.groups()[0].split(","))
lbls = map(lambda x: x.lower(), lbls)
lbls = filter(lambda x: x in label_map, lbls)
lbls = map(lambda x: label_map[x], lbls)
lbls = set(lbls)
labels = labels | lbls
else:
warn_bad_msg = True
if not "Signed-off-by:" in msg:
warn_signoff = True
if warn_bad_msg or warn_signoff or warn_blankln:
comment = pr_greeting_msg
comment += pr_warn_commit_msg if warn_bad_msg else ""
comment += pr_warn_signoff_msg if warn_signoff else ""
comment += pr_warn_blankln_msg if warn_blankln else ""
comment += pr_guidelines_ref_msg
pr.create_review(body=comment, event="REQUEST_CHANGES")
if labels:
pr.add_to_labels(*labels)
return Response("OK", 200)
# API handler map
# {
# 'event1': {
# 'action1': ev1_action1_handler,
# 'action2': ev1_action2_handler,
# ...
# }
# 'event2': {
# 'action1': ev2_action1_handler,
# 'action2': ev2_action2_handler,
# ...
# }
# }
event_handlers = {
"issues": {"labeled": issue_labeled},
"issue_comment": {"created": issue_comment_created},
"pull_request": {"opened": pull_request_opened},
}
def handle_webhook(request):
try:
evtype = request.headers["X_GITHUB_EVENT"]
except KeyError as e:
app.logger.warning("[-] No X-GitHub-Event header...")
return Response("No X-GitHub-Event header", 400)
app.logger.warning("[+] Handling webhook '{}'".format(evtype))
try:
event = event_handlers[evtype]
except KeyError as e:
app.logger.warning("[+] Unknown event '{}'".format(evtype))
return Response("OK", 200)
try:
j = request.get_json()
except BadRequest as e:
app.logger.warning("[-] Could not parse payload as JSON")
return Response("Bad JSON", 400)
try:
action = j["action"]
except KeyError as e:
app.logger.warning("[+] No action for event '{}'".format(evtype))
return Response("OK", 200)
try:
handler = event_handlers[evtype][action]
except KeyError as e:
app.logger.warning("[+] No handler for action '{}'".format(action))
return Response("OK", 200)
try:
sender = j["sender"]["login"]
reponame = j["repository"]["full_name"]
repo = g.get_repo(reponame)
if sender == my_user:
app.logger.warning("[+] Ignoring event triggered by me")
return Response("OK", 200)
except KeyError as e:
pass
app.logger.warning("[+] Handling action '{}' on event '{}'".format(action, evtype))
return handler(j)
# Flask hooks ------------------------------------------------------------------
def gh_sig_valid(req):
mydigest = "sha1=" + HMAC(bytes(whsec, "utf8"), req.get_data(), "sha1").hexdigest()
ghdigest = req.headers["X_HUB_SIGNATURE"]
comp = hmac.compare_digest(ghdigest, mydigest)
app.logger.warning("[+] Request: mine = {}, theirs = {}".format(mydigest, ghdigest))
return comp
@app.route("/", methods=["GET", "POST"])
def parse_payload():
try:
if not gh_sig_valid(request):
return Response("Unauthorized", 401)
except:
return Response("Unauthorized", 401)
if request.method == "POST":
return handle_webhook(request)
else:
return Response("OK", 200)