-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchPullRequest.py
732 lines (679 loc) · 30.4 KB
/
FetchPullRequest.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
## @file
# Fetch the git commits required to process a pull request
#
# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
'''
FetchPullRequest
'''
from __future__ import print_function
import os
import sys
import git
import email
import textwrap
import datetime
EMAIL_ARCHIVE_ADDRESS = os.environ['EMAIL_ARCHIVE_ADDRESS']
class Progress(git.remote.RemoteProgress):
def __init__(self):
git.remote.RemoteProgress.__init__(self)
self.PreviousLine = ''
def update(self, op_code, cur_count, max_count=None, message=''):
Line = '\r' + self._cur_line
if len(self.PreviousLine) > len(Line):
Line = Line + ' ' * (len(self.PreviousLine) - len(Line))
sys.stdout.write(Line)
self.PreviousLine = Line
def FetchPullRequest (HubPullRequest, Depth = 200):
#
# Fetch the base.ref branch and current PR branch from the base repository
# of the pull request
#
RepositoryPath = os.path.normpath (os.path.join ('Repository', HubPullRequest.base.repo.full_name))
if os.path.exists (RepositoryPath):
try:
print ('pr[%d]' % (HubPullRequest.number), 'mount', RepositoryPath)
GitRepo = git.Repo(RepositoryPath)
Origin = GitRepo.remotes['origin']
except:
try:
print ('pr[%d]' % (HubPullRequest.number), 'init', RepositoryPath)
GitRepo = git.Repo.init (RepositoryPath, bare=True)
Origin = GitRepo.create_remote ('origin', HubPullRequest.base.repo.html_url)
except:
print ('pr[%d]' % (HubPullRequest.number), 'init', RepositoryPath, 'FAILED')
return None, None
else:
try:
print ('pr[%d]' % (HubPullRequest.number), 'init', RepositoryPath)
os.makedirs (RepositoryPath)
GitRepo = git.Repo.init (RepositoryPath, bare=True)
Origin = GitRepo.create_remote ('origin', HubPullRequest.base.repo.html_url)
except:
print ('pr[%d]' % (HubPullRequest.number), 'init', RepositoryPath, 'FAILED')
return None, None
#
# Shallow fetch base.ref branch from origin
#
try:
print ('pr[%d]' % (HubPullRequest.number), 'fetch', HubPullRequest.base.ref, 'from', RepositoryPath)
Origin.fetch(HubPullRequest.base.ref, progress=Progress(), depth = Depth)
except:
print ('pr[%d]' % (HubPullRequest.number), 'fetch', HubPullRequest.base.ref, 'from', RepositoryPath, 'FAILED')
return None, None
#
# Fetch the current pull request branch from origin
#
try:
print ('pr[%d]' % (HubPullRequest.number), 'fetch pull request', HubPullRequest.number, 'from', RepositoryPath)
Origin.fetch('+refs/pull/%d/*:refs/remotes/origin/pr/%d/*' % (HubPullRequest.number, HubPullRequest.number), progress=Progress())
print ('pr[%d]' % (HubPullRequest.number), 'fetch', RepositoryPath, 'done')
except:
print ('pr[%d]' % (HubPullRequest.number), 'fetch pull request', HubPullRequest.number, 'from', RepositoryPath, 'NOT FOUND')
return None, None
#
# Retrieve the latest version of Maintainers.txt from origin/base.ref
#
try:
Maintainers = GitRepo.git.show('origin/%s:Maintainers.txt' % (HubPullRequest.base.ref))
except:
print ('Maintainers.txt does not exist in origin/%s' % (HubPullRequest.base.ref))
Maintainers = ''
return GitRepo, Maintainers
def ParseCcLines(Body):
AddressList = []
for Line in Body.splitlines():
if Line.lower().startswith ('cc:'):
Address = Line[3:].strip()
if ',' in Address:
continue
if '<' not in Address or '>' not in Address:
continue
if '@' not in Address.rsplit('<',1)[1].split('>',1)[0]:
continue
AddressList.append(Address)
return AddressList
def GetLineEnding (Line):
if Line.endswith('\r\n'):
return '\r\n'
elif Line.endswith('\r'):
return '\r'
else:
return '\n'
def MetaDataBlockText(HubPullRequest, Commit, AddressList, LineEnding):
Text = ''
#
# Add link to pull request
#
Text = Text + '#' * 4 + LineEnding
Text = Text + '# PR(%s): %s%s' % (HubPullRequest.state, HubPullRequest.html_url, LineEnding)
#
# Add base SHA value
#
Text = Text + '# Base SHA: ' + HubPullRequest.base.sha + LineEnding
#
# Add submitter
#
Text = Text + '# Submitter: [' + HubPullRequest.user.login + ']' + LineEnding
#
# Add link to commit
#
if Commit is not None:
Text = Text + '# Commit: ' + Commit.html_url + LineEnding
#
# Add list of assigned reviewers
#
for Address in AddressList:
Text = Text + '# ' + Address + LineEnding
Text = Text + '#' * 4 + LineEnding
Text = Text + LineEnding
return Text
def QuoteText (Text, Prefix, Depth):
if Depth <= 0:
return Text
Text = Text.splitlines(keepends=True)
return (Prefix * Depth) + (Prefix * Depth).join(Text)
def WrapParagraph (Paragraph, LineEnding):
WrappedParagraph = textwrap.wrap(
Paragraph,
replace_whitespace=False,
drop_whitespace=False,
break_long_words=False,
break_on_hyphens=False
)
Length = len(WrappedParagraph[0]) - len(WrappedParagraph[0].lstrip(' '))
WrappedParagraph = [X.lstrip(' ') for X in WrappedParagraph]
if len(WrappedParagraph) > 1 and WrappedParagraph[-1].rstrip() == '':
WrappedParagraph[-2] = WrappedParagraph[-2] + WrappedParagraph[-1]
WrappedParagraph = WrappedParagraph[:-1]
return QuoteText (LineEnding.join(WrappedParagraph), ' ', Length)
def WrapText (Text, LineEnding):
Result = ''
for Paragraph in Text.splitlines(keepends=True):
Result = Result + WrapParagraph (Paragraph, LineEnding)
return Result
def CommentAsEmailText(Comment, LineEnding, Prefix, Depth):
#
# Wrap long lines in comment body, but never split HTML links that may
# contain hyphens.
#
WrappedBody = []
if Comment.body is not None:
for Paragraph in Comment.body.splitlines(keepends=True):
PrefixDepth = 0
if Prefix:
while Paragraph.startswith (Prefix):
Paragraph = Paragraph[len(Prefix):]
PrefixDepth = PrefixDepth + 1
WrappedParagraph = QuoteText (
WrapParagraph(Paragraph, LineEnding),
Prefix,
PrefixDepth
)
WrappedBody.append (WrappedParagraph)
if hasattr(Comment, 'state'):
String = 'On %s @%s started a review with state %s:%s%s' % (
str(Comment.submitted_at),
Comment.user.login,
Comment.state,
LineEnding,
''.join(WrappedBody)
)
else:
String = 'On %s @%s wrote:%s%s' % (
str(Comment.created_at),
Comment.user.login,
LineEnding,
''.join(WrappedBody)
)
if String[-1] not in ['\n','\r']:
String = String + LineEnding
try:
for Reaction in Comment.get_reactions():
String = String + '@%s reacted with %s%s' % (
Reaction.user.login,
Reaction.content,
LineEnding
)
except:
pass
return '-' * 20 + LineEnding + QuoteText (String, Prefix, Depth)
def QuoteCommentList (Comments, Before = '', After = '', LineEnding = '\n', Prefix = '> '):
#
# Sort comments from oldest comment to newest comment
#
Comments = sorted (Comments, key=lambda Comment: Comment.created_at)
Body = ''
if Before:
if Before[-1] not in ['\n','\r']:
Before = Before + LineEnding
Body = Body + QuoteText (Before, Prefix, len(Comments))
Depth = len(Comments)
for Comment in Comments:
Depth = Depth - 1
Body = Body + CommentAsEmailText(Comment, LineEnding, Prefix, Depth)
if After:
if After[-1] not in ['\n','\r']:
After = After + LineEnding
Body = Body + '-' * 20 + LineEnding
Body = Body + QuoteText (After, Prefix, len(Comments))
else:
Body = Body + '-' * 20 + LineEnding
return Body
def FormatPatch (
event,
GitRepo,
HubRepo,
HubPullRequest,
Commit,
AddressList,
PatchSeriesVersion,
PatchNumber,
CommentUser = None,
CommentType = None,
CommentId = None,
CommentPosition = None,
CommentPath = None,
Prefix = '',
CommentInReplyToId = None,
LargePatchLines = 500
):
#
# Default range is a single commit
#
CommitRange = Commit.sha + '~1..' + Commit.sha
#
# Format the Subject:
# [<repo name>][PATCH v<patch series version> <patch number>/<number of patches>]
# Format the Messsage-ID:
# <webhook-<repo name>-pr<pull>-v<patch series version>-p<patch number>@tianocore.org>
#
ToAddress = '<%s>' % (EMAIL_ARCHIVE_ADDRESS)
if CommentId:
FromAddress = '%s via TianoCore Webhook <[email protected]>' % (CommentUser)
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-c%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, PatchNumber, CommentId)
if CommentInReplyToId:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%d-c%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, PatchNumber, CommentInReplyToId)
else:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, PatchNumber)
else:
FromAddress = '%s via TianoCore Webhook <[email protected]>' % (HubPullRequest.user.login)
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0)
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, PatchNumber)
Email = GitRepo.git.format_patch (
'--stdout',
'--no-numbered',
'--to=' + ToAddress,
'--from=' + FromAddress,
'--add-header=' + HeaderInReplyToId,
'--add-header=' + HeaderMessageId,
'--subject-prefix=%s][PATCH v%d %0*d/%d' % (HubRepo.name, PatchSeriesVersion, len(str(HubPullRequest.commits)), PatchNumber, HubPullRequest.commits),
CommitRange
)
#
# Remove first line from format-patch that is not part of email and parse
# the line ending style used by git format-patch
#
Email = Email.splitlines(keepends=True)
LineEnding = GetLineEnding (Email[0])
Email = ''.join(Email[1:])
#
# Parse the email message and parse the message body. Split the message
# body at the '\n---\n' marker which seperates the commit message from the
# patch diffs.
#
Message = email.message_from_string(Email)
Pattern = '\n---\n'
Body = Message.get_payload().split (Pattern, 1)
Body[0] = Body[0] + Pattern
#
# Parse the Cc: lines from the commit message
#
CcAddressList = ParseCcLines (Body[0])
#
# Add text with links to pull request and the commit along with the list
# of maintainers/reviewers for this specific commit after the '\n---\n'
# marker so this meta data is not part of the commit message or the patch
# diffs.
#
Body[0] = Body[0] + MetaDataBlockText(
HubPullRequest,
Commit,
AddressList,
LineEnding
)
if CommentId:
#
# Get the comments that apply based on the event type
#
AllComments = []
if event == 'commit_comment':
AllComments = Commit.get_comments()
if event == 'pull_request_review_comment':
AllComments = HubPullRequest.get_review_comments()
#
# Only keep the comments that match the CommentPath and CommentPosition
# from this event
#
Comments = []
for Comment in AllComments:
if Comment.path == CommentPath and Comment.position == CommentPosition:
Comments.append(Comment)
if CommentPath == None:
#
# This is a comment against the description of the commit. Discard
# the patch diffs and append the review comments quoting the commit
# message and all previous comments.
#
Body = QuoteCommentList (
Comments,
Before = Body[0],
LineEnding = LineEnding,
Prefix = Prefix
)
else:
#
# Find the portion of the patch diffs that contain changes to the
# file specified by CommentPath
#
Start = '\ndiff --git a/' + CommentPath + ' b/' + CommentPath + '\n'
End = '\ndiff --git a/'
PatchLines = len(Body[1].splitlines())
try:
BeforeBody = Body[1].split(Start,1)[0] + '\n'
try:
AfterBody = Body[1].split(Start,1)[1].split(End,1)[1]
Body[1] = Start.lstrip() + Body[1].split(Start,1)[1].split(End,1)[0]
except:
AfterBody = ''
Body[1] = Start.lstrip() + Body[1].split(Start,1)[1]
except:
Body[1] = Body[1] + 'ERROR: %s Comment to file %s position %d not found.\n' % (Commit.sha, CommentPath, CommentPosition)
#
# Find the first line of the patch diff for file CommentPath that
# starts with '@@ '.
#
Body[1] = Body[1].splitlines(keepends=True)
for LineNumber in range(0, len(Body[1])):
if Body[1][LineNumber].startswith('@@ '):
break
#
# Insert comments into patch at CommentPosition + 1 lines after '@@ '
#
LineNumber = LineNumber + CommentPosition + 1
if PatchLines > LargePatchLines:
Body = QuoteCommentList (
Comments,
Before = Body[0] + BeforeBody + ''.join(Body[1][:LineNumber]),
After = ''.join(Body[1][LineNumber:]) + AfterBody,
LineEnding = LineEnding,
Prefix = Prefix
)
else:
Body = QuoteCommentList (
Comments,
Before = Body[0] + ''.join(Body[1][:LineNumber]),
After = ''.join(Body[1][LineNumber:]),
LineEnding = LineEnding,
Prefix = Prefix
)
else:
Body = Body[0] + Body[1]
if CcAddressList:
Message.add_header('Cc', ','.join(CcAddressList))
Message.set_payload(Body)
return Message.as_string()
def FormatPatchSummary (
event,
GitRepo,
HubRepo,
HubPullRequest,
AddressList,
PatchSeriesVersion,
CommitRange = None,
CommentUser = None,
CommentId = None,
CommentPosition = None,
CommentPath = None,
Prefix = '',
CommentInReplyToId = None,
UpdateDeltaTime = 0,
Review = None,
ReviewId = None,
ReviewComments = [],
DeleteId = None,
ParentReviewId = None,
LargePatchLines = 500
):
#
# Default range is the entire pull request
#
if CommitRange is None:
CommitRange = HubPullRequest.base.sha + '..' + HubPullRequest.head.sha
#
# Format the Subject:
# [<repo name>][PATCH v<patch series version> <patch number>/<number of patches>]
# Format the Messsage-ID:
# <webhook-<repo name>-pr<pull>-v<patch series version>-p<patch number>@tianocore.org>
#
ToAddress = '<%s>' % (EMAIL_ARCHIVE_ADDRESS)
if ReviewId:
FromAddress = '%s via TianoCore Webhook <[email protected]>' % (CommentUser)
if DeleteId:
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-r%d-d%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, ReviewId, DeleteId)
elif UpdateDeltaTime != 0:
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-r%d-t%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, ReviewId, UpdateDeltaTime)
else:
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-r%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, ReviewId)
if ParentReviewId:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%d-r%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, ParentReviewId)
elif DeleteId or UpdateDeltaTime != 0:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%d-r%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, ReviewId)
else:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0)
elif CommentId:
FromAddress = '%s via TianoCore Webhook <[email protected]>' % (CommentUser)
if UpdateDeltaTime != 0:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%d-c%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, CommentId)
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-c%d-t%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, CommentId, UpdateDeltaTime)
else:
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-c%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, CommentId)
if CommentInReplyToId:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%d-c%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, CommentInReplyToId)
else:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0)
else:
FromAddress = '%s via TianoCore Webhook <[email protected]>' % (HubPullRequest.user.login)
if UpdateDeltaTime != 0:
HeaderInReplyToId = 'In-Reply-To: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0)
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%d-t%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0, UpdateDeltaTime)
else:
HeaderInReplyToId = None
HeaderMessageId = 'Message-ID: <webhook-%s-pull%d-v%d-p%[email protected]>' % (HubRepo.name, HubPullRequest.number, PatchSeriesVersion, 0)
if HeaderInReplyToId:
Email = GitRepo.git.format_patch (
'--stdout',
'--cover-letter',
'--to=' + ToAddress,
'--from=' + FromAddress,
'--add-header=' + HeaderInReplyToId,
'--add-header=' + HeaderMessageId,
'--subject-prefix=%s][PATCH v%d' % (HubRepo.name, PatchSeriesVersion),
CommitRange
)
else:
Email = GitRepo.git.format_patch (
'--stdout',
'--cover-letter',
'--to=' + ToAddress,
'--from=' + FromAddress,
'--add-header=' + HeaderMessageId,
'--subject-prefix=%s][PATCH v%d' % (HubRepo.name, PatchSeriesVersion),
CommitRange
)
#
# Remove first line from format-patch that is not part of email and parse
# the line ending style used by git format-patch
#
Email = Email.splitlines(keepends=True)
LineEnding = GetLineEnding (Email[0])
Email = ''.join(Email[1:])
#
# Parse the email message and parse the message body discarding the file
# diffs leaving only the *** BLURB HERE *** and file change summary.
# Split the message body at *** BLURB HERE *** so the description of the
# pull request can inserted and leave the option to discard the file change
# summary.
#
Message = email.message_from_string(Email)
Pattern = '\n-- \n'
Body = Message.get_payload().split (Pattern, 1)[0] + Pattern
Body = Body.split ('*** BLURB HERE ***', 1)
#
# Add text with link to pull request and list of maintainers/reviewers
#
Body[0] = Body[0] + MetaDataBlockText(
HubPullRequest,
None,
AddressList,
LineEnding
)
#
# Add the body from the pull request
#
CcAddressList = []
if HubPullRequest.body is not None:
CcAddressList = ParseCcLines (HubPullRequest.body)
Body[0] = Body[0] + WrapText (HubPullRequest.body, LineEnding)
#
# If this is a comment against the description of the pull request
# then discard the file change summary and append the review comments
# quoting the decription of the pull request and all previous comments.
# Otherwise, this is a Patch #0 email that includes the file change summary.
#
if CommentId or Review:
if event in ['pull_request_review_comment', 'pull_request_review'] and ReviewComments:
if Review:
#
# Add description of review to email
#
Body[0] = QuoteText(Body[0], '> ', 1)
Body[0] = Body[0] + '-' * 20 + LineEnding
if Review.body:
String = 'On %s @%s started a review with state %s:' % (
str(Review.submitted_at),
Review.user.login,
Review.state
)
Body[0] = Body[0] + String + LineEnding
Body[0] = Body[0] + WrapText (Review.body, LineEnding) + LineEnding
else:
String = 'On %s @%s added a single review comment:' % (
str(Review.submitted_at),
Review.user.login
)
Body[0] = Body[0] + String + LineEnding
Body[0] = Body[0] + '-' * 20 + LineEnding
else:
Body[0] = QuoteText(Body[0], '> ', 1)
Body[0] = Body[0] + '-' * 20 + LineEnding
if ReviewId and DeleteId:
Body[0] = Body[0] + 'Review associated with CommentId %s was deleted' % (CommentId) + LineEnding
elif CommentId:
Body[0] = Body[0] + 'Review associated with CommentId %s not found' % (CommentId) + LineEnding
elif Review:
Body[0] = Body[0] + 'Review associated with ReviewId %s not found' % (Review.id) + LineEnding
else:
Body[0] = Body[0] + 'Review associated with this pull request was not found' + LineEnding
Body[0] = Body[0] + '-' * 20 + LineEnding
#
# Get the pull request review comments only keeping the comments
# that match the CommentPath and CommentPosition from this event
#
CommentDict = {}
for Comment in ReviewComments:
if Comment.path not in CommentDict:
CommentDict[Comment.path] = {}
if Comment.position not in CommentDict[Comment.path]:
CommentDict[Comment.path][Comment.position] = []
CommentDict[Comment.path][Comment.position].append(Comment)
#
# If this is a pull request review comment then discard the file
# change summary and insert the review comments at the specified
# position in the pull request diff quoting the decription of the
# pull request, the diff, and all previous comments.
#
#
# Generate a quoted file diff for the commit range
#
Diff = '\n-- \n' + GitRepo.git.diff (CommitRange)
Diff = QuoteText (''.join(Diff), '> ', 1)
Diff = Diff.splitlines(keepends=True)
#
# If diff is > LargePatchLines, then only keep diff lines associated
# with the files mentioned in the comments
#
if len(Diff) > LargePatchLines:
NewDiff = []
for CommentPath in CommentDict:
Start = '> diff --git a/' + CommentPath + ' b/' + CommentPath + '\n'
End = '> diff --git a/'
Match = False
for Line in Diff:
if Match and Line.startswith (End):
Match = False
if Line.startswith (Start):
Match = True
if Match:
NewDiff.append(Line)
Diff = NewDiff
#
# Build dictionary of conversations at line nunbers in Diff
#
Conversations = {}
for CommentPath in CommentDict:
#
# Find the line in Diff that contains the diffs against file
# specified by CommentPath
#
Start = '> diff --git a/' + CommentPath + ' b/' + CommentPath + '\n'
FileFound = False
DiffFound = False
for StartLineNumber in range (0, len(Diff)):
if Diff[StartLineNumber].startswith(Start):
FileFound = True
if FileFound:
if Diff[StartLineNumber].startswith('> @@ '):
DiffFound = True
break
if not DiffFound:
Diff.append('ERROR: Pull request %d review comment to file %s not found.\n' % (HubPullRequest.number, CommentPath))
continue
for CommentPosition in CommentDict[CommentPath]:
CommentBody = QuoteCommentList (
CommentDict[CommentPath][CommentPosition],
LineEnding = LineEnding,
Prefix = ''
)
Conversations[StartLineNumber + CommentPosition + 1] = CommentBody
#
# Insert conversations into the diff patch
#
DiffBody = ''
PreviousLineNumber = 0
LineNumbers = list(Conversations.keys())
LineNumbers.sort()
LineNumber = 0
for LineNumber in LineNumbers:
DiffBody = DiffBody + ''.join(Diff[PreviousLineNumber:LineNumber]) + Conversations[LineNumber]
PreviousLineNumber = LineNumber
DiffBody = DiffBody + ''.join(Diff[LineNumber:])
#
# Append diff patch with coversations to the email body
#
Body = Body[0] + '\n-- \n' + DiffBody
else:
#
# If this is a comment against the description of the pull request
# then discard the file change summary and append the review comments
# quoting the decription of the pull request and all previous comments.
#
IssueComments = []
for Comment in HubPullRequest.get_issue_comments():
IssueComments.append(Comment)
Reviews = HubPullRequest.get_reviews()
Comments = HubPullRequest.get_review_comments()
for Review in Reviews:
Match = False
for Comment in Comments:
if 'pull_request_review_id' in Comment.raw_data:
if Comment.raw_data['pull_request_review_id'] == Review.id:
Match = True
break
if not Match:
Review.created_at = Review.submitted_at
IssueComments.append(Review)
Body = QuoteCommentList (
IssueComments,
Before = Body[0],
LineEnding = LineEnding,
Prefix = Prefix
)
else:
#
# This is a Patch #0 email that includes the file change summary.
#
Body = Body[0] + Body[1]
#
# Update incorrect From: line in git format-patch cover letter
# Replace *** SUBJECT HERE *** with the pull request title
# Add Re: to Subject if comment is being processed
#
Message.replace_header('From', FromAddress)
if HubPullRequest.title is not None:
Message.replace_header('Subject', Message['Subject'].replace ('*** SUBJECT HERE ***', HubPullRequest.title))
if CcAddressList:
Message.add_header('Cc', ','.join(CcAddressList))
Message.set_payload(Body)
return Message.as_string()