-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
933 lines (831 loc) · 38.5 KB
/
Server.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
## @file
# Assign reviewers to commits in a GitHub pull request based on assignments
# documented in Maintainers.txt and generate email archive of all review
# activities.
#
# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
'''
TianoCore GitHub Webhook
'''
from __future__ import print_function
import os
import sys
import argparse
import hmac
import datetime
from json import dumps
from flask import Flask, request, abort
from github import Github
from GetMaintainers import GetMaintainers
from GetMaintainers import ParseMaintainerAddresses
from SendEmails import SendEmails
from FetchPullRequest import FetchPullRequest
from FetchPullRequest import FormatPatch
from FetchPullRequest import FormatPatchSummary
#
# Globals for help information
#
__prog__ = 'TianoCoreGitHubWebHookServer'
__copyright__ = 'Copyright (c) 2020, Intel Corporation. All rights reserved.'
__description__ = 'Assign reviewers to commits in a GitHub pull request based on assignments documented in Maintainers.txt and generate email archive of all review activities.\n'
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
GITHUB_WEBHOOK_SECRET = os.environ['GITHUB_WEBHOOK_SECRET']
GITHUB_WEBHOOK_ROUTE = os.environ['GITHUB_WEBHOOK_ROUTE']
GITHUB_WEBHOOK_PORT_NUMBER = int(os.environ['GITHUB_WEBHOOK_PORT_NUMBER'])
GITHUB_REPO_WHITE_LIST = os.environ['GITHUB_REPO_WHITE_LIST']
REVIEW_REQUEST = '[CodeReview] Review-request @'
REVIEWED_BY = '[CodeReview] Reviewed-by'
SERIES_REVIEWED_BY = '[CodeReview] Series-reviewed-by'
ACKED_BY = '[CodeReview] Acked-by'
TESTED_BY = '[CodeReview] Acked-by'
def UpdatePullRequestCommitReviewers (Commit, GitHubIdList):
#
# Retrieve all review comments for this commit
#
Body = []
for Comment in Commit.get_comments():
if Comment.body is not None:
Body = Body + [Line.strip() for Line in Comment.body.splitlines()]
#
# Determine if any reviewers need to be added to this commit
#
AddReviewers = []
for Reviewer in GitHubIdList:
if REVIEW_REQUEST + Reviewer not in Body:
AddReviewers.append(REVIEW_REQUEST + Reviewer + '\n')
if AddReviewers != []:
for Reviewer in AddReviewers:
print (' ' + ' '.join(AddReviewers))
#
# NOTE: This triggers a recursion into this webhook that needs to be
# ignored
#
Commit.create_comment (''.join(AddReviewers))
#
# Return True if reviewers were added to this commit
#
return AddReviewers != []
def UpdatePullRequestReviewers (Hub, HubRepo, HubPullRequest, PullRequestGitHubIdList):
#
# Get list of reviewers already requested for the pull request
#
RequestedReviewers = HubPullRequest.get_review_requests()[0]
#
# Determine if any reviewers need to be removed
#
RemoveReviewerList = []
for Reviewer in RequestedReviewers:
if Reviewer.login not in PullRequestGitHubIdList:
print ('pr[%d]' % (HubPullRequest.number), 'Remove Reviewer : @' + Reviewer.login)
RemoveReviewerList.append(Reviewer.login)
#
# Determine if any reviewers need to be added
#
AddReviewerList = []
Collaborators = HubRepo.get_collaborators()
for Login in PullRequestGitHubIdList:
Reviewer = Hub.get_user(Login)
if Reviewer == HubPullRequest.user:
print ('pr[%d]' % (HubPullRequest.number), 'Reviewer is Author : @' + Reviewer.login)
elif Reviewer not in RequestedReviewers:
if Reviewer in Collaborators:
print ('pr[%d]' % (HubPullRequest.number), 'Add Reviewer : @' + Reviewer.login)
AddReviewerList.append (Reviewer.login)
else:
print ('pr[%d]' % (HubPullRequest.number), 'Reviewer is not a collaborator : @' + Reviewer.login)
else:
print ('pr[%d]' % (HubPullRequest.number), 'Already Assigned : @' + Reviewer.login)
#
# Update review requests
#
if RemoveReviewerList != []:
#
# NOTE: This may trigger recursion into this webhook
#
HubPullRequest.delete_review_request (RemoveReviewerList)
if AddReviewerList != []:
#
# NOTE: This may trigger recursion into this webhook
#
HubPullRequest.create_review_request (AddReviewerList)
def GetReviewComments(Comment, ReviewComments, CommentIdDict):
if Comment in ReviewComments:
return
ReviewComments.append(Comment)
#
# Add peer comments
#
if Comment.pull_request_review_id:
for PeerComment in CommentIdDict.values():
if PeerComment.pull_request_review_id == Comment.pull_request_review_id:
GetReviewComments (PeerComment, ReviewComments, CommentIdDict)
#
# Add child comments
#
for ChildComment in CommentIdDict.values():
if ChildComment.in_reply_to_id == Comment.id:
GetReviewComments (ChildComment, ReviewComments, CommentIdDict)
#
# Add parent comment
#
if Comment.in_reply_to_id and Comment.in_reply_to_id in CommentIdDict:
ParentComment = CommentIdDict[Comment.in_reply_to_id]
GetReviewComments (ParentComment, ReviewComments, CommentIdDict)
def GetReviewCommentsFromReview(Review, CommentId, CommentInReplyToId, CommentIdDict):
ReviewComments = []
for Comment in CommentIdDict.values():
if Review:
if Comment.pull_request_review_id == Review.id:
GetReviewComments (Comment, ReviewComments, CommentIdDict)
if CommentId:
if Comment.in_reply_to_id == CommentId:
GetReviewComments (Comment, ReviewComments, CommentIdDict)
if CommentInReplyToId:
if Comment.id == CommentInReplyToId:
GetReviewComments (Comment, ReviewComments, CommentIdDict)
return ReviewComments
application = Flask(__name__)
@application.route(GITHUB_WEBHOOK_ROUTE, methods=['GET', 'POST'])
def index():
"""
Main WSGI application entry.
"""
if args.Verbose:
print (request.headers)
# Only POST is implemented
if request.method != 'POST':
print (501, "only POST is supported")
abort(501, "only POST is supported")
# Enforce secret, so not just anybody can trigger these hooks
secret = GITHUB_WEBHOOK_SECRET
if secret:
# Only SHA1 is supported
header_signature = request.headers.get('X-Hub-Signature')
if header_signature is None:
print (403, "No header signature found")
abort(403, "No header signature found")
sha_name, signature = header_signature.split('=')
if sha_name != 'sha1':
print(501, "Only SHA1 is supported")
abort(501, "Only SHA1 is supported")
# HMAC requires the key to be bytes, but data is string
mac = hmac.new(bytes(secret, 'utf-8'), msg=request.data, digestmod='sha1')
# Python does not have hmac.compare_digest prior to 2.7.7
if sys.hexversion >= 0x020707F0:
if not hmac.compare_digest(str(mac.hexdigest()), str(signature)):
print(403, "hmac compare digest failed")
abort(403)
else:
# What compare_digest provides is protection against timing
# attacks; we can live without this protection for a web-based
# application
if not str(mac.hexdigest()) == str(signature):
print(403, "hmac compare digest failed")
abort(403)
# Implement ping
event = request.headers.get('X-GitHub-Event', 'ping')
if event == 'ping':
print ('ping request. respond with pong')
return dumps({'msg': 'pong'})
# Implement meta
if event == 'meta':
return dumps({'msg': 'meta'})
# Gather data
try:
payload = request.get_json()
except Exception:
print(400, "Request parsing failed")
abort(400, "Request parsing failed")
#
# Skip push and create events
#
if event in ['push', 'create']:
print ('skip event', event)
return dumps({'status': 'skipped'})
#
# Skip payload that does not provide an action
#
if 'action' not in payload:
print ('skip payload that does not provide an action. event =', event)
return dumps({'status': 'skipped'})
#
# Skip payload that does not provide a repository
#
if 'repository' not in payload:
print ('skip payload that does not provide a repository. event=', event)
return dumps({'status': 'skipped'})
#
# Skip payload that does not provide a repository full name
#
if 'full_name' not in payload['repository']:
print ('skip payload that does not provide a repository full name. event=', event)
return dumps({'status': 'skipped'})
#
# Skip requests that are not in GITHUB_REPO_WHITE_LIST
#
if payload['repository']['full_name'] not in GITHUB_REPO_WHITE_LIST:
print ('skip event for different repo')
return dumps({'status': 'skipped'})
print ('----> Process Event <----', event, payload['action'])
############################################################################
# Process issue comment events
# These are comments against the entire pull request
# Quote Patch #0 Body and add comment below below with commenters GitHubID
############################################################################
if event == 'issue_comment':
action = payload['action']
if action not in ['created', 'edited', 'deleted']:
print ('skip issue_comment event with action other than created or edited')
return dumps({'status': 'skipped'})
if 'pull_request' not in payload['issue']:
print ('skip issue_comment event without an associated pull request')
return dumps({'status': 'skipped'})
#
# Use GitHub API to get Pull Request
#
try:
HubRepo = Hub.get_repo (payload['repository']['full_name'])
HubPullRequest = HubRepo.get_pull(payload['issue']['number'])
except:
#
# Skip requests if the PyGitHub objects can not be retrieved
#
print ('skip issue_comment event for which the PyGitHub objects can not be retrieved')
return dumps({'status': 'skipped'})
#
# Skip pull request that is not open
#
if HubPullRequest.state != 'open':
print ('Skip issue_comment event against a pull request that is not open')
return dumps({'status': 'skipped'})
#
# Skip pull request with a base repo that is different than the expected repo
#
if HubPullRequest.base.repo.full_name != HubRepo.full_name:
print ('Skip issue_comment event against a different repo', HubPullRequest.base.repo.full_name)
return dumps({'status': 'skipped'})
#
# Skip pull requests with a base branch that is not the default branch
#
if HubPullRequest.base.ref != HubRepo.default_branch:
print ('Skip issue_comment event against non-default base branch', HubPullRequest.base.ref)
return dumps({'status': 'skipped'})
#
# Fetch the git commits for the pull request and return a git repo
# object and the contents of Maintainers.txt
#
GitRepo, Maintainers = FetchPullRequest (HubPullRequest)
if GitRepo is None or Maintainers is None:
print ('Skip issue_comment event that can not be fetched')
return dumps({'status': 'skipped'})
#
# Count head_ref_force_pushed and reopened events to determine the
# version of the patch series.
#
PatchSeriesVersion = 1;
Events = HubPullRequest.get_issue_events()
for Event in Events:
if Event.event in ['head_ref_force_pushed', 'reopened']:
PatchSeriesVersion = PatchSeriesVersion + 1;
PullRequestAddressList = []
for Commit in HubPullRequest.get_commits():
#
# Get list of files modifies by commit from GIT repository
#
CommitFiles = GitRepo.commit(Commit.sha).stats.files
#
# Get maintainers and reviewers for all files in this commit
#
Addresses = GetMaintainers (Maintainers, CommitFiles)
AddressList, GitHubIdList, EmailList = ParseMaintainerAddresses(Addresses)
PullRequestAddressList = list(set(PullRequestAddressList + AddressList))
#
# Generate the summary email patch #0 with body of email prefixed with >.
#
UpdateDeltaTime = 0
if action == 'edited':
#
# The delta time is the number of seconds from the time the comment
# was created to the time the comment was edited
#
UpdatedAt = datetime.datetime.strptime(payload['comment']['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
CreatedAt = datetime.datetime.strptime(payload['comment']['created_at'], "%Y-%m-%dT%H:%M:%SZ")
UpdateDeltaTime = (UpdatedAt - CreatedAt).seconds
if action == 'deleted':
UpdateDeltaTime = -1
Summary = FormatPatchSummary (
event,
GitRepo,
HubRepo,
HubPullRequest,
PullRequestAddressList,
PatchSeriesVersion,
CommitRange = HubPullRequest.base.sha + '..' + HubPullRequest.head.sha,
CommentUser = payload['comment']['user']['login'],
CommentId = payload['comment']['id'],
CommentPosition = None,
CommentPath = None,
Prefix = '> ',
UpdateDeltaTime = UpdateDeltaTime
)
#
# Send any generated emails
#
SendEmails (HubPullRequest, [Summary], args.EmailServer)
print ('----> Process Event Done <----', event, payload['action'])
return dumps({'msg': 'issue_comment created or edited'})
############################################################################
# Process commit comment events
# These are comments against a specific commit
# Quote Patch #n commit message and add comment below below with commenters GitHubID
############################################################################
if event == 'commit_comment':
action = payload['action']
if action not in ['created', 'edited']:
print ('skip commit_comment event with action other than created or edited')
return dumps({'status': 'skipped'})
#
# Skip REVIEW_REQUEST comments made by the webhook itself. This same
# information is always present in the patch emails, so filtering these
# comments prevent double emails when a pull request is opened or
# synchronized.
#
Body = payload['comment']['body'].splitlines()
for Line in payload['comment']['body'].splitlines():
if Line.startswith (REVIEW_REQUEST):
print ('skip commit_comment event with review request body from this webhook')
return dumps({'status': 'skipped'})
#
# Search for issues/pull requests that contain the comment's commit_id
#
CommitId = payload['comment']['commit_id']
CommentId = payload['comment']['id']
CommentPosition = payload['comment']['position']
CommentPath = payload['comment']['path']
EmailContents = []
for Issue in Hub.search_issues('SHA:' + CommitId):
#
# Skip Issue for a different repository
#
if Issue.repository.full_name != payload['repository']['full_name']:
print ('Skip commit_comment event against a different repo', HubPullRequest.base.repo.full_name)
continue
#
# Use GitHub API to get Pull Request
#
try:
HubRepo = Issue.repository
HubPullRequest = Issue.as_pull_request()
except:
print ('skip commit_comment event for which the PyGitHub objects can not be retrieved')
continue
#
# Skip pull request that is not open
#
if HubPullRequest.state != 'open':
print ('Skip commit_comment event against a pull request that is not open')
return dumps({'status': 'skipped'})
#
# Skip commit_comment with a base repo that is different than the expected repo
#
if HubPullRequest.base.repo.full_name != HubRepo.full_name:
print ('Skip commit_comment event against a different repo', HubPullRequest.base.repo.full_name)
continue
#
# Skip commit_comment with a base branch that is not the default branch
#
if HubPullRequest.base.ref != HubRepo.default_branch:
print ('Skip commit_comment event against non-default base branch', HubPullRequest.base.ref)
continue
#
# Fetch the git commits for the pull request and return a git repo
# object and the contents of Maintainers.txt
#
GitRepo, Maintainers = FetchPullRequest (HubPullRequest)
if GitRepo is None or Maintainers is None:
print ('Skip commit_comment event that can not be fetched')
continue
#
# Count head_ref_force_pushed and reopened events to determine the
# version of the patch series.
#
PatchSeriesVersion = 1;
Events = HubPullRequest.get_issue_events()
for Event in Events:
if Event.event in ['head_ref_force_pushed', 'reopened']:
PatchSeriesVersion = PatchSeriesVersion + 1;
#
# Determine the patch number of the commit with the comment
#
PatchNumber = 0
for Commit in HubPullRequest.get_commits():
PatchNumber = PatchNumber + 1
if Commit.sha == CommitId:
break
#
# Get commit from GIT repository
#
CommitFiles = GitRepo.commit(Commit.sha).stats.files
#
# Get maintainers and reviewers for all files in this commit
#
Addresses = GetMaintainers (Maintainers, CommitFiles)
AddressList, GitHubIdList, EmailList = ParseMaintainerAddresses(Addresses)
Email = FormatPatch (
event,
GitRepo,
HubRepo,
HubPullRequest,
Commit,
AddressList,
PatchSeriesVersion,
PatchNumber,
CommentUser = payload['comment']['user']['login'],
CommentId = CommentId,
CommentPosition = CommentPosition,
CommentPath = CommentPath,
Prefix = '> '
)
EmailContents.append (Email)
if EmailContents == []:
print ('skip commit_comment that is not for any supported repo')
return dumps({'status': 'skipped'})
#
# Send any generated emails
#
SendEmails (HubPullRequest, EmailContents, args.EmailServer)
print ('----> Process Event Done <----', event, payload['action'])
return dumps({'msg': 'commit_comment created or edited'})
############################################################################
# Process pull_request_review_comment and pull_request_review events
# Quote Patch #0 commit message and patch diff of file comment is against
############################################################################
if event in ['pull_request_review_comment', 'pull_request_review']:
action = payload['action']
Review = None
ReviewComments = []
DeleteId = None
ParentReviewId = None
UpdateDeltaTime = 0
if event in ['pull_request_review_comment']:
if action not in ['edited', 'deleted']:
print ('skip pull_request_review_comment event with action other than edited or deleted')
return dumps({'status': 'skipped'})
#
# Skip REVIEW_REQUEST comments made by the webhook itself. This same
# information is always present in the patch emails, so filtering these
# comments prevent double emails when a pull request is opened or
# synchronized.
#
Body = payload['comment']['body'].splitlines()
for Line in payload['comment']['body'].splitlines():
if Line.startswith (REVIEW_REQUEST):
print ('skip pull_request_review_comment event with review request body from this webhook')
return dumps({'status': 'skipped'})
if event in ['pull_request_review']:
if action not in ['submitted', 'edited']:
print ('skip pull_request_review event with action other than submitted or edited')
return dumps({'status': 'skipped'})
if action == 'edited' and payload['changes'] == {}:
print ('skip pull_request_review event edited action that has no changes')
return dumps({'status': 'skipped'})
EmailContents = []
#
# Use GitHub API to get Pull Request
#
try:
HubRepo = Hub.get_repo (payload['repository']['full_name'])
HubPullRequest = HubRepo.get_pull(payload['pull_request']['number'])
except:
print ('skip pull_request_review_comment event for which the PyGitHub objects can not be retrieved')
return dumps({'status': 'skipped'})
#
# Skip pull request that is not open
#
if HubPullRequest.state != 'open':
print ('Skip pull_request_review_comment event against a pull request that is not open')
return dumps({'status': 'skipped'})
#
# Skip pull_request_review_comment with a base repo that is different than the expected repo
#
if HubPullRequest.base.repo.full_name != HubRepo.full_name:
print ('Skip pull_request_review_comment event against a different repo', HubPullRequest.base.repo.full_name)
return dumps({'status': 'skipped'})
#
# Skip pull_request_review_comment with a base branch that is not the default branch
#
if HubPullRequest.base.ref != HubRepo.default_branch:
print ('Skip pull_request_review_comment event against non-default base branch', HubPullRequest.base.ref)
return dumps({'status': 'skipped'})
#
# Build dictionary of review comments
#
CommentIdDict = {}
for Comment in HubPullRequest.get_review_comments():
Comment.pull_request_review_id = None
if 'pull_request_review_id' in Comment.raw_data:
Comment.pull_request_review_id = Comment.raw_data['pull_request_review_id']
CommentIdDict[Comment.id] = Comment
#
# Determine if review has a parent review, is being deleted, or has
# an update time.
#
if event in ['pull_request_review']:
CommitId = payload['review']['commit_id']
CommentUser = payload['review']['user']['login'],
CommentId = None
CommentPosition = None
CommentPath = None
CommentInReplyToId = None
ReviewId = payload['review']['id']
try:
Review = HubPullRequest.get_review(ReviewId)
except:
Review = None
ReviewComments = GetReviewCommentsFromReview(Review, CommentId, CommentInReplyToId, CommentIdDict)
if payload['action'] == 'submitted':
UpdateDeltaTime = 0
for ReviewComment in ReviewComments:
if ReviewComment.pull_request_review_id == ReviewId:
if ReviewComment.in_reply_to_id and ReviewComment.in_reply_to_id in CommentIdDict:
ParentReviewId = CommentIdDict[ReviewComment.in_reply_to_id].pull_request_review_id
if ParentReviewId and ParentReviewId != ReviewId:
break
if payload['action'] == 'edited' and Review:
UpdatedAt = datetime.datetime.strptime(payload['pull_request']['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
UpdateDeltaTime = (UpdatedAt - Review.submitted_at).seconds
if event in ['pull_request_review_comment']:
CommitId = payload['comment']['commit_id']
CommentId = payload['comment']['id']
CommentUser = payload['comment']['user']['login'],
CommentPosition = payload['comment']['position']
CommentPath = payload['comment']['path']
CommentInReplyToId = None
ReviewId = None
if 'in_reply_to_id' in payload['comment']:
CommentInReplyToId = payload['comment']['in_reply_to_id']
if 'pull_request_review_id' in payload['comment']:
ReviewId = payload['comment']['pull_request_review_id']
try:
Review = HubPullRequest.get_review(ReviewId)
except:
Review = None
ReviewComments = GetReviewCommentsFromReview(Review, CommentId, CommentInReplyToId, CommentIdDict)
if payload['action'] == 'deleted':
UpdateDeltaTime = 0
DeleteId = payload['comment']['id']
if payload['action'] == 'edited' and Review:
UpdatedAt = datetime.datetime.strptime(payload['comment']['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
UpdateDeltaTime = (UpdatedAt - Review.submitted_at).seconds
#
# Fetch the git commits for the pull request and return a git repo
# object and the contents of Maintainers.txt
#
GitRepo, Maintainers = FetchPullRequest (HubPullRequest)
if GitRepo is None or Maintainers is None:
print ('Skip pull_request_review_comment event that can not be fetched')
return dumps({'status': 'skipped'})
#
# Count head_ref_force_pushed and reopened events to determine the
# version of the patch series.
#
PatchSeriesVersion = 1;
Events = HubPullRequest.get_issue_events()
for Event in Events:
if Event.event in ['head_ref_force_pushed', 'reopened']:
PatchSeriesVersion = PatchSeriesVersion + 1;
#
# All pull request review comments are against patch #0
#
PatchNumber = 0
#
# Build dictionary of files in range of commits from the pull request
# base sha up to the commit id of the pull request review comment.
#
CommitFiles = {}
for Commit in HubPullRequest.get_commits():
CommitFiles.update (GitRepo.commit(Commit.sha).stats.files)
#
# Get maintainers and reviewers for all files in this commit
#
Addresses = GetMaintainers (Maintainers, CommitFiles)
AddressList, GitHubIdList, EmailList = ParseMaintainerAddresses(Addresses)
#
# Generate the summary email patch #0 with body of email prefixed with >.
#
Email = FormatPatchSummary (
event,
GitRepo,
HubRepo,
HubPullRequest,
AddressList,
PatchSeriesVersion,
CommitRange = HubPullRequest.base.sha + '..' + HubPullRequest.head.sha,
CommentUser = CommentUser,
CommentId = CommentId,
CommentPosition = CommentPosition,
CommentPath = CommentPath,
Prefix = '> ',
CommentInReplyToId = CommentInReplyToId,
UpdateDeltaTime = UpdateDeltaTime,
Review = Review,
ReviewId = ReviewId,
ReviewComments = ReviewComments,
DeleteId = DeleteId,
ParentReviewId = ParentReviewId
)
EmailContents.append (Email)
#
# Send any generated emails
#
SendEmails (HubPullRequest, EmailContents, args.EmailServer)
print ('----> Process Event Done <----', event, payload['action'])
return dumps({'msg': event + ' created or edited or deleted'})
############################################################################
# Process pull request events
############################################################################
if event == 'pull_request':
action = payload['action']
if action not in ['opened', 'synchronize', 'edited', 'closed', 'reopened']:
print ('skip pull_request event with action other than opened or synchronized')
return dumps({'status': 'skipped'})
#
# Use GitHub API to get Pull Request
#
try:
HubRepo = Hub.get_repo (payload['repository']['full_name'])
HubPullRequest = HubRepo.get_pull(payload['pull_request']['number'])
except:
#
# Skip requests if the PyGitHub objects can not be retrieved
#
print ('skip pull_request event for which the PyGitHub objects can not be retrieved')
return dumps({'status': 'skipped'})
#
# Skip pull request that is not open unless this is the event that is
# closing the pull request
#
if action != 'closed':
if HubPullRequest.state != 'open':
print ('Skip pull_request event against a pull request that is not open')
return dumps({'status': 'skipped'})
#
# Skip pull request with a base repo that is different than the expected repo
#
if HubPullRequest.base.repo.full_name != HubRepo.full_name:
print ('Skip PR event against a different repo', HubPullRequest.base.repo.full_name)
return dumps({'status': 'skipped'})
#
# Skip pull requests with a base branch that is not the default branch
#
if HubPullRequest.base.ref != HubRepo.default_branch:
print ('Skip PR event against non-default base branch', HubPullRequest.base.ref)
return dumps({'status': 'skipped'})
#
# Fetch the git commits for the pull request and return a git repo
# object and the contents of Maintainers.txt
#
GitRepo, Maintainers = FetchPullRequest (HubPullRequest)
if GitRepo is None or Maintainers is None:
print ('Skip pull_request_review event that can not be fetched')
return dumps({'status': 'skipped'})
NewPatchSeries = False
PatchSeriesVersion = 1;
if action in ['opened', 'reopened']:
#
# New pull request was created
#
NewPatchSeries = True
if action in ['synchronize', 'edited', 'closed', 'reopened']:
#
# Existing pull request was updated.
# Commits were added to an existing pull request or an existing pull
# request was forced push. Get events to determine what happened
#
Events = HubPullRequest.get_issue_events()
for Event in Events:
#
# Count head_ref_force_pushed and reopened events to determine
# the version of the patch series.
#
if Event.event in ['head_ref_force_pushed', 'reopened']:
PatchSeriesVersion = PatchSeriesVersion + 1;
if Event.event in ['head_ref_force_pushed']:
#
# If the head_ref_force_pushed event occurred at the exact
# same date/time (or within 2 seconds) that the pull request
# was updated, then this was a forced push and the entire
# patch series should be emailed again.
#
if abs(Event.created_at - HubPullRequest.updated_at).seconds <= 2:
NewPatchSeries = True
PullRequestAddressList = []
PullRequestGitHubIdList = []
PullRequestEmailList = []
EmailContents = []
PatchNumber = 0
for Commit in HubPullRequest.get_commits():
PatchNumber = PatchNumber + 1
#
# Get list of files modified by commit from GIT repository
#
CommitFiles = GitRepo.commit(Commit.sha).stats.files
#
# Get maintainers and reviewers for all files in this commit
#
Addresses = GetMaintainers (Maintainers, CommitFiles)
AddressList, GitHubIdList, EmailList = ParseMaintainerAddresses(Addresses)
PullRequestAddressList = list(set(PullRequestAddressList + AddressList))
PullRequestGitHubIdList = list(set(PullRequestGitHubIdList + GitHubIdList))
PullRequestEmailList = list(set(PullRequestEmailList + EmailList))
if action in ['opened', 'synchronize', 'reopened']:
print ('pr[%d]' % (HubPullRequest.number), Commit.sha, ' @' + ' @'.join(PullRequestGitHubIdList))
#
# Update the list of required reviewers for this commit
#
ReviewersUpdated = UpdatePullRequestCommitReviewers (Commit, GitHubIdList)
#
# Generate email contents for all commits in a pull request if this is
# a new pull request or a forced push was done to an existing pull request.
# Generate email contents for patches that add new reviewers. This
# occurs when when new commits are added to an existing pull request.
#
if NewPatchSeries or ReviewersUpdated:
Email = FormatPatch (
event,
GitRepo,
HubRepo,
HubPullRequest,
Commit,
AddressList,
PatchSeriesVersion,
PatchNumber
)
EmailContents.append (Email)
if action in ['opened', 'synchronize', 'reopened']:
#
# Update the list of required reviewers for the pull request
#
UpdatePullRequestReviewers (Hub, HubRepo, HubPullRequest, PullRequestGitHubIdList)
#
# If this is a new pull request or a forced push on a pull request or an
# edit of the pulle request title or description, then generate the
# summary email patch #0 and add to be beginning of the list of emails
# to send.
#
if NewPatchSeries or action in ['edited', 'closed']:
UpdateDeltaTime = 0
if action in ['edited', 'closed']:
UpdateDeltaTime = (HubPullRequest.updated_at - HubPullRequest.created_at).seconds
Summary = FormatPatchSummary (
event,
GitRepo,
HubRepo,
HubPullRequest,
PullRequestAddressList,
PatchSeriesVersion,
UpdateDeltaTime = UpdateDeltaTime
)
EmailContents.insert (0, Summary)
#
# Send any generated emails
#
SendEmails (HubPullRequest, EmailContents, args.EmailServer)
print ('----> Process Event Done <----', event, payload['action'])
return dumps({'msg': 'pull_request opened or synchronize'})
print ('skip unsupported event')
return dumps({'status': 'skipped'})
if __name__ == '__main__':
#
# Create command line argument parser object
#
parser = argparse.ArgumentParser (prog = __prog__,
description = __description__ + __copyright__,
conflict_handler = 'resolve')
parser.add_argument ("-e", "--email-server", dest = 'EmailServer', choices = ['Off', 'SMTP', 'SendGrid'], default = 'Off',
help = "Email server type used to send emails.")
parser.add_argument ("-v", "--verbose", dest = 'Verbose', action = "store_true",
help = "Increase output messages")
parser.add_argument ("-q", "--quiet", dest = 'Quiet', action = "store_true",
help = "Reduce output messages")
parser.add_argument ("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range (0, 10), default = 0,
help = "Set debug level")
#
# Parse command line arguments
#
args = parser.parse_args ()
#
# Create GitHub object authenticated using GitHub Token for the webhook
#
try:
Hub = Github (GITHUB_TOKEN)
except:
print ('can not access GitHub APIs')
sys.exit(1)
try:
application.run(debug=False, host='localhost', port=GITHUB_WEBHOOK_PORT_NUMBER, threaded=False)
except:
print ('can not create listener for GitHub HTTP requests')
sys.exit(1)