FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-pull-request-github/src/github/pullRequestModel.ts at main · gee4vee/vscode-pull-request-github · GitHub
gee4vee
/
vscode-pull-request-github
Public
forked from
microsoft/vscode-pull-request-github
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-pull-request-github
/
src
/
github
/
pullRequestModel.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
1674 lines (1468 loc) · 50.6 KB
Breadcrumbs
vscode-pull-request-github
/
src
/
github
/
pullRequestModel.ts
Copy path
File metadata and controls
1674 lines (1468 loc) · 50.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import
*
as
buffer
from
'buffer'
;
import
*
as
path
from
'path'
;
import
equals
from
'fast-deep-equal'
;
import
*
as
vscode
from
'vscode'
;
import
{
DiffSide
,
IComment
,
IReviewThread
,
ViewedState
}
from
'../common/comment'
;
import
{
parseDiff
}
from
'../common/diffHunk'
;
import
{
commands
,
contexts
}
from
'../common/executeCommands'
;
import
{
GitChangeType
,
InMemFileChange
,
SlimFileChange
}
from
'../common/file'
;
import
{
GitHubRef
}
from
'../common/githubRef'
;
import
Logger
from
'../common/logger'
;
import
{
Remote
}
from
'../common/remote'
;
import
{
ITelemetry
}
from
'../common/telemetry'
;
import
{
ReviewEvent
as
CommonReviewEvent
,
isReviewEvent
,
TimelineEvent
}
from
'../common/timelineEvent'
;
import
{
resolvePath
,
toPRUri
,
toReviewUri
}
from
'../common/uri'
;
import
{
formatError
}
from
'../common/utils'
;
import
{
OctokitCommon
}
from
'./common'
;
import
{
FolderRepositoryManager
}
from
'./folderRepositoryManager'
;
import
{
GitHubRepository
}
from
'./githubRepository'
;
import
{
AddCommentResponse
,
AddReactionResponse
,
AddReviewThreadResponse
,
DeleteReactionResponse
,
DeleteReviewResponse
,
EditCommentResponse
,
GetChecksResponse
,
isCheckRun
,
LatestReviewCommitResponse
,
MarkPullRequestReadyForReviewResponse
,
PendingReviewIdResponse
,
PullRequestCommentsResponse
,
PullRequestFilesResponse
,
PullRequestMergabilityResponse
,
ReactionGroup
,
ResolveReviewThreadResponse
,
StartReviewResponse
,
SubmitReviewResponse
,
TimelineEventsResponse
,
UnresolveReviewThreadResponse
,
UpdatePullRequestResponse
,
}
from
'./graphql'
;
import
{
GithubItemStateEnum
,
IAccount
,
IRawFileChange
,
ISuggestedReviewer
,
MergeMethod
,
PullRequest
,
PullRequestChecks
,
PullRequestMergeability
,
ReviewEvent
,
}
from
'./interface'
;
import
{
IssueModel
}
from
'./issueModel'
;
import
{
convertRESTPullRequestToRawPullRequest
,
convertRESTReviewEvent
,
convertRESTUserToAccount
,
getReactionGroup
,
insertNewCommitsSinceReview
,
parseGraphQLComment
,
parseGraphQLReaction
,
parseGraphQLReviewEvent
,
parseGraphQLReviewThread
,
parseGraphQLTimelineEvents
,
parseMergeability
,
restPaginate
,
}
from
'./utils'
;
interface
IPullRequestModel
{
head
:
GitHubRef
|
null
;
}
export
interface
IResolvedPullRequestModel
extends
IPullRequestModel
{
head
:
GitHubRef
;
}
export
interface
ReviewThreadChangeEvent
{
added
:
IReviewThread
[
]
;
changed
:
IReviewThread
[
]
;
removed
:
IReviewThread
[
]
;
}
export
interface
FileViewedStateChangeEvent
{
changed
:
{
fileName
:
string
;
viewed
:
ViewedState
;
}
[
]
;
}
export
type
FileViewedState
=
{
[
key
:
string
]
:
ViewedState
}
;
export
class
PullRequestModel
extends
IssueModel
<
PullRequest
>
implements
IPullRequestModel
{
static
ID
=
'PullRequestModel'
;
public
isDraft
?:
boolean
;
public
localBranchName
?:
string
;
public
mergeBase
?:
string
;
public
suggestedReviewers
?:
ISuggestedReviewer
[
]
;
public
hasChangesSinceLastReview
?:
boolean
;
private
_showChangesSinceReview
:
boolean
;
private
_hasPendingReview
:
boolean
=
false
;
private
_onDidChangePendingReviewState
:
vscode
.
EventEmitter
<
boolean
>
=
new
vscode
.
EventEmitter
<
boolean
>
(
)
;
public
onDidChangePendingReviewState
=
this
.
_onDidChangePendingReviewState
.
event
;
private
_reviewThreadsCache
:
IReviewThread
[
]
=
[
]
;
private
_reviewThreadsCacheInitialized
=
false
;
private
_onDidChangeReviewThreads
=
new
vscode
.
EventEmitter
<
ReviewThreadChangeEvent
>
(
)
;
public
onDidChangeReviewThreads
=
this
.
_onDidChangeReviewThreads
.
event
;
private
_fileChangeViewedState
:
FileViewedState
=
{
}
;
private
_viewedFiles
:
Set
<
string
>
=
new
Set
(
)
;
private
_unviewedFiles
:
Set
<
string
>
=
new
Set
(
)
;
private
_onDidChangeFileViewedState
=
new
vscode
.
EventEmitter
<
FileViewedStateChangeEvent
>
(
)
;
public
onDidChangeFileViewedState
=
this
.
_onDidChangeFileViewedState
.
event
;
private
_onDidChangeChangesSinceReview
=
new
vscode
.
EventEmitter
<
void
>
(
)
;
public
onDidChangeChangesSinceReview
=
this
.
_onDidChangeChangesSinceReview
.
event
;
private
_comments
:
IComment
[
]
|
undefined
;
private
_onDidChangeComments
:
vscode
.
EventEmitter
<
void
>
=
new
vscode
.
EventEmitter
(
)
;
public
readonly
onDidChangeComments
:
vscode
.
Event
<
void
>
=
this
.
_onDidChangeComments
.
event
;
// Whether the pull request is currently checked out locally
private
_isActive
:
boolean
;
public
get
isActive
(
)
:
boolean
{
return
this
.
_isActive
;
}
public
set
isActive
(
isActive
:
boolean
)
{
this
.
_isActive
=
isActive
;
if
(
!
this
.
_isActive
)
{
this
.
clearFileViewedContext
(
)
;
}
}
_telemetry
:
ITelemetry
;
constructor
(
telemetry
:
ITelemetry
,
githubRepository
:
GitHubRepository
,
remote
:
Remote
,
item
:
PullRequest
,
isActive
?:
boolean
,
)
{
super
(
githubRepository
,
remote
,
item
,
true
)
;
this
.
_telemetry
=
telemetry
;
this
.
isActive
=
!
!
isActive
;
this
.
_showChangesSinceReview
=
false
;
this
.
update
(
item
)
;
}
public
clear
(
)
{
this
.
comments
=
[
]
;
this
.
_reviewThreadsCacheInitialized
=
false
;
this
.
_reviewThreadsCache
=
[
]
;
}
public
async
initializeReviewThreadCache
(
)
:
Promise
<
void
>
{
await
this
.
getReviewThreads
(
)
;
this
.
_reviewThreadsCacheInitialized
=
true
;
}
public
get
reviewThreadsCache
(
)
:
IReviewThread
[
]
{
return
this
.
_reviewThreadsCache
;
}
public
get
reviewThreadsCacheReady
(
)
:
boolean
{
return
this
.
_reviewThreadsCacheInitialized
;
}
public
get
isMerged
(
)
:
boolean
{
return
this
.
state
===
GithubItemStateEnum
.
Merged
;
}
public
get
hasPendingReview
(
)
:
boolean
{
return
this
.
_hasPendingReview
;
}
public
set
hasPendingReview
(
hasPendingReview
:
boolean
)
{
if
(
this
.
_hasPendingReview
!==
hasPendingReview
)
{
this
.
_hasPendingReview
=
hasPendingReview
;
this
.
_onDidChangePendingReviewState
.
fire
(
this
.
_hasPendingReview
)
;
}
}
public
get
showChangesSinceReview
(
)
{
return
this
.
_showChangesSinceReview
;
}
public
set
showChangesSinceReview
(
isChangesSinceReview
:
boolean
)
{
this
.
_showChangesSinceReview
=
isChangesSinceReview
;
this
.
_onDidChangeChangesSinceReview
.
fire
(
)
;
}
get
comments
(
)
:
IComment
[
]
{
return
this
.
_comments
??
[
]
;
}
set
comments
(
comments
:
IComment
[
]
)
{
this
.
_comments
=
comments
;
this
.
_onDidChangeComments
.
fire
(
)
;
}
get
fileChangeViewedState
(
)
:
FileViewedState
{
return
this
.
_fileChangeViewedState
;
}
public
isRemoteHeadDeleted
?:
boolean
;
public
head
:
GitHubRef
|
null
;
public
isRemoteBaseDeleted
?:
boolean
;
public
base
:
GitHubRef
;
protected
updateState
(
state
:
string
)
{
if
(
state
.
toLowerCase
(
)
===
'open'
)
{
this
.
state
=
GithubItemStateEnum
.
Open
;
}
else
{
this
.
state
=
this
.
item
.
merged
?
GithubItemStateEnum
.
Merged
:
GithubItemStateEnum
.
Closed
;
}
}
update
(
item
:
PullRequest
)
:
void
{
super
.
update
(
item
)
;
this
.
isDraft
=
item
.
isDraft
;
this
.
suggestedReviewers
=
item
.
suggestedReviewers
;
if
(
item
.
isRemoteHeadDeleted
!=
null
)
{
this
.
isRemoteHeadDeleted
=
item
.
isRemoteHeadDeleted
;
}
if
(
item
.
head
)
{
this
.
head
=
new
GitHubRef
(
item
.
head
.
ref
,
item
.
head
.
label
,
item
.
head
.
sha
,
item
.
head
.
repo
.
cloneUrl
,
item
.
head
.
repo
.
owner
,
item
.
head
.
repo
.
name
)
;
}
if
(
item
.
isRemoteBaseDeleted
!=
null
)
{
this
.
isRemoteBaseDeleted
=
item
.
isRemoteBaseDeleted
;
}
if
(
item
.
base
)
{
this
.
base
=
new
GitHubRef
(
item
.
base
.
ref
,
item
.
base
!
.
label
,
item
.
base
!
.
sha
,
item
.
base
!
.
repo
.
cloneUrl
,
item
.
base
.
repo
.
owner
,
item
.
base
.
repo
.
name
)
;
}
}
/**
* Validate if the pull request has a valid HEAD.
* Use only when the method can fail silently, otherwise use `validatePullRequestModel`
*/
isResolved
(
)
:
this
is
IResolvedPullRequestModel
{
return
!
!
this
.
head
;
}
/**
* Validate if the pull request has a valid HEAD. Show a warning message to users when the pull request is invalid.
*
@param
message Human readable action execution failure message.
*/
validatePullRequestModel
(
message
?:
string
)
:
this
is
IResolvedPullRequestModel
{
if
(
!
!
this
.
head
)
{
return
true
;
}
const
reason
=
vscode
.
l10n
.
t
(
'There is no upstream branch for Pull Request #{0}. View it on GitHub for more details'
,
this
.
number
)
;
if
(
message
)
{
message
+=
`:
${
reason
}
`
;
}
else
{
message
=
reason
;
}
const
openString
=
vscode
.
l10n
.
t
(
'Open on GitHub'
)
;
vscode
.
window
.
showWarningMessage
(
message
,
openString
)
.
then
(
action
=>
{
if
(
action
&&
action
===
openString
)
{
vscode
.
commands
.
executeCommand
(
'vscode.open'
,
vscode
.
Uri
.
parse
(
this
.
html_url
)
)
;
}
}
)
;
return
false
;
}
/**
* Approve the pull request.
*
@param
message Optional approval comment text.
*/
async
approve
(
message
?:
string
)
:
Promise
<
CommonReviewEvent
>
{
const
action
:
Promise
<
CommonReviewEvent
>
=
(
await
this
.
getPendingReviewId
(
)
)
?
this
.
submitReview
(
ReviewEvent
.
Approve
,
message
)
:
this
.
createReview
(
ReviewEvent
.
Approve
,
message
)
;
return
action
.
then
(
x
=>
{
/* __GDPR__
"pr.approve" : {}
*/
this
.
_telemetry
.
sendTelemetryEvent
(
'pr.approve'
)
;
this
.
_onDidChangeComments
.
fire
(
)
;
return
x
;
}
)
;
}
/**
* Request changes on the pull request.
*
@param
message Optional comment text to leave with the review.
*/
async
requestChanges
(
message
?:
string
)
:
Promise
<
CommonReviewEvent
>
{
const
action
:
Promise
<
CommonReviewEvent
>
=
(
await
this
.
getPendingReviewId
(
)
)
?
this
.
submitReview
(
ReviewEvent
.
RequestChanges
,
message
)
:
this
.
createReview
(
ReviewEvent
.
RequestChanges
,
message
)
;
return
action
.
then
(
x
=>
{
/* __GDPR__
"pr.requestChanges" : {}
*/
this
.
_telemetry
.
sendTelemetryEvent
(
'pr.requestChanges'
)
;
this
.
_onDidChangeComments
.
fire
(
)
;
return
x
;
}
)
;
}
/**
* Close the pull request.
*/
async
close
(
)
:
Promise
<
PullRequest
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
ret
=
await
octokit
.
call
(
octokit
.
api
.
pulls
.
update
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
pull_number
:
this
.
number
,
state
:
'closed'
,
}
)
;
/* __GDPR__
"pr.close" : {}
*/
this
.
_telemetry
.
sendTelemetryEvent
(
'pr.close'
)
;
return
convertRESTPullRequestToRawPullRequest
(
ret
.
data
,
this
.
githubRepository
)
;
}
/**
* Create a new review.
*
@param
event The type of review to create, an approval, request for changes, or comment.
*
@param
message The summary comment text.
*/
private
async
createReview
(
event
:
ReviewEvent
,
message
?:
string
)
:
Promise
<
CommonReviewEvent
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
{
data
}
=
await
octokit
.
call
(
octokit
.
api
.
pulls
.
createReview
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
pull_number
:
this
.
number
,
event
:
event
,
body
:
message
,
}
)
;
return
convertRESTReviewEvent
(
data
,
this
.
githubRepository
)
;
}
/**
* Submit an existing review.
*
@param
event The type of review to create, an approval, request for changes, or comment.
*
@param
body The summary comment text.
*/
async
submitReview
(
event
?:
ReviewEvent
,
body
?:
string
)
:
Promise
<
CommonReviewEvent
>
{
let
pendingReviewId
=
await
this
.
getPendingReviewId
(
)
;
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
if
(
!
pendingReviewId
&&
(
event
===
ReviewEvent
.
Comment
)
)
{
// Create a new review so that we can comment on it.
pendingReviewId
=
await
this
.
startReview
(
)
;
}
if
(
pendingReviewId
)
{
const
{
data
}
=
await
mutate
<
SubmitReviewResponse
>
(
{
mutation
:
schema
.
SubmitReview
,
variables
:
{
id
:
pendingReviewId
,
event
:
event
||
ReviewEvent
.
Comment
,
body
,
}
,
}
)
;
this
.
hasPendingReview
=
false
;
await
this
.
updateDraftModeContext
(
)
;
const
reviewEvent
=
parseGraphQLReviewEvent
(
data
!
.
submitPullRequestReview
.
pullRequestReview
,
this
.
githubRepository
)
;
const
threadWithComment
=
this
.
_reviewThreadsCache
.
find
(
thread
=>
thread
.
comments
.
length
?
(
thread
.
comments
[
0
]
.
pullRequestReviewId
===
reviewEvent
.
id
)
:
undefined
,
)
;
if
(
threadWithComment
)
{
threadWithComment
.
comments
=
reviewEvent
.
comments
;
threadWithComment
.
viewerCanResolve
=
true
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
]
,
changed
:
[
threadWithComment
]
,
removed
:
[
]
}
)
;
}
return
reviewEvent
;
}
else
{
throw
new
Error
(
`Submitting review failed, no pending review for current pull request:
${
this
.
number
}
.`
)
;
}
}
async
updateMilestone
(
id
:
string
)
:
Promise
<
void
>
{
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
finalId
=
id
===
'null'
?
null
:
id
;
try
{
await
mutate
<
UpdatePullRequestResponse
>
(
{
mutation
:
schema
.
UpdatePullRequest
,
variables
:
{
input
:
{
pullRequestId
:
this
.
item
.
graphNodeId
,
milestoneId
:
finalId
,
}
,
}
,
}
)
;
}
catch
(
err
)
{
Logger
.
appendLine
(
err
)
;
}
}
async
updateAssignees
(
assignees
:
string
[
]
)
:
Promise
<
void
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
await
octokit
.
call
(
octokit
.
api
.
issues
.
addAssignees
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
issue_number
:
this
.
number
,
assignees
,
}
)
;
}
/**
* Query to see if there is an existing review.
*/
async
getPendingReviewId
(
)
:
Promise
<
string
|
undefined
>
{
const
{
query
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
currentUser
=
await
this
.
githubRepository
.
getAuthenticatedUser
(
)
;
try
{
const
{
data
}
=
await
query
<
PendingReviewIdResponse
>
(
{
query
:
schema
.
GetPendingReviewId
,
variables
:
{
pullRequestId
:
this
.
item
.
graphNodeId
,
author
:
currentUser
,
}
,
}
)
;
return
data
.
node
.
reviews
.
nodes
.
length
>
0
?
data
.
node
.
reviews
.
nodes
[
0
]
.
id
:
undefined
;
}
catch
(
error
)
{
return
;
}
}
async
getViewerLatestReviewCommit
(
)
:
Promise
<
{
sha
:
string
}
|
undefined
>
{
Logger
.
debug
(
`Fetch viewers latest review commit`
,
IssueModel
.
ID
)
;
const
{
query
,
remote
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
try
{
const
{
data
}
=
await
query
<
LatestReviewCommitResponse
>
(
{
query
:
schema
.
LatestReviewCommit
,
variables
:
{
owner
:
remote
.
owner
,
name
:
remote
.
repositoryName
,
number
:
this
.
number
,
}
,
}
)
;
return
data
.
repository
.
pullRequest
.
viewerLatestReview
?
{
sha
:
data
.
repository
.
pullRequest
.
viewerLatestReview
.
commit
.
oid
,
}
:
undefined
;
}
catch
(
e
)
{
return
undefined
;
}
}
/**
* Delete an existing in progress review.
*/
async
deleteReview
(
)
:
Promise
<
{
deletedReviewId
:
number
;
deletedReviewComments
:
IComment
[
]
}
>
{
const
pendingReviewId
=
await
this
.
getPendingReviewId
(
)
;
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
{
data
}
=
await
mutate
<
DeleteReviewResponse
>
(
{
mutation
:
schema
.
DeleteReview
,
variables
:
{
input
:
{
pullRequestReviewId
:
pendingReviewId
}
,
}
,
}
)
;
const
{
comments
,
databaseId
}
=
data
!
.
deletePullRequestReview
.
pullRequestReview
;
this
.
hasPendingReview
=
false
;
await
this
.
updateDraftModeContext
(
)
;
this
.
getReviewThreads
(
)
;
return
{
deletedReviewId
:
databaseId
,
deletedReviewComments
:
comments
.
nodes
.
map
(
comment
=>
parseGraphQLComment
(
comment
,
false
,
this
.
githubRepository
)
)
,
}
;
}
/**
* Start a new review.
*
@param
initialComment The comment text and position information to begin the review with
*
@param
commitId The optional commit id to start the review on. Defaults to using the current head commit.
*/
async
startReview
(
commitId
?:
string
)
:
Promise
<
string
>
{
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
{
data
}
=
await
mutate
<
StartReviewResponse
>
(
{
mutation
:
schema
.
StartReview
,
variables
:
{
input
:
{
body
:
''
,
pullRequestId
:
this
.
item
.
graphNodeId
,
commitOID
:
commitId
||
this
.
head
?.
sha
,
}
,
}
,
}
)
;
if
(
!
data
)
{
throw
new
Error
(
'Failed to start review'
)
;
}
this
.
_onDidChangeComments
.
fire
(
)
;
return
data
.
addPullRequestReview
.
pullRequestReview
.
id
;
}
/**
* Creates a new review thread, either adding it to an existing pending review, or creating
* a new review.
*
@param
body The body of the thread's first comment.
*
@param
commentPath The path to the file being commented on.
*
@param
startLine The start line on which to add the comment.
*
@param
endLine The end line on which to add the comment.
*
@param
side The side the comment should be deleted on, i.e. the original or modified file.
*
@param
suppressDraftModeUpdate If a draft mode change should event should be suppressed. In the
* case of a single comment add, the review is created and then immediately submitted, so this prevents
* a "Pending" label from flashing on the comment.
*
@returns
The new review thread object.
*/
async
createReviewThread
(
body
:
string
,
commentPath
:
string
,
startLine
:
number
,
endLine
:
number
,
side
:
DiffSide
,
suppressDraftModeUpdate
?:
boolean
,
)
:
Promise
<
IReviewThread
|
undefined
>
{
if
(
!
this
.
validatePullRequestModel
(
'Creating comment failed'
)
)
{
return
;
}
const
pendingReviewId
=
await
this
.
getPendingReviewId
(
)
;
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
{
data
}
=
await
mutate
<
AddReviewThreadResponse
>
(
{
mutation
:
schema
.
AddReviewThread
,
variables
:
{
input
:
{
path
:
commentPath
,
body
,
pullRequestId
:
this
.
graphNodeId
,
pullRequestReviewId
:
pendingReviewId
,
startLine
:
startLine
===
endLine
?
undefined
:
startLine
,
line
:
endLine
,
side
,
}
,
}
,
}
)
;
if
(
!
data
)
{
throw
new
Error
(
'Creating review thread failed.'
)
;
}
if
(
!
data
.
addPullRequestReviewThread
.
thread
)
{
throw
new
Error
(
'File has been deleted.'
)
;
}
if
(
!
suppressDraftModeUpdate
)
{
this
.
hasPendingReview
=
true
;
await
this
.
updateDraftModeContext
(
)
;
}
const
thread
=
data
.
addPullRequestReviewThread
.
thread
;
const
newThread
=
parseGraphQLReviewThread
(
thread
,
this
.
githubRepository
)
;
this
.
_reviewThreadsCache
.
push
(
newThread
)
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
newThread
]
,
changed
:
[
]
,
removed
:
[
]
}
)
;
return
newThread
;
}
/**
* Creates a new comment in reply to an existing comment
*
@param
body The text of the comment to be created
*
@param
inReplyTo The id of the comment this is in reply to
*
@param
isSingleComment Whether this is a single comment, i.e. one that
* will be immediately submitted and so should not show a pending label
*
@param
commitId The commit id the comment was made on
*
@returns
The new comment
*/
async
createCommentReply
(
body
:
string
,
inReplyTo
:
string
,
isSingleComment
:
boolean
,
commitId
?:
string
,
)
:
Promise
<
IComment
|
undefined
>
{
if
(
!
this
.
validatePullRequestModel
(
'Creating comment failed'
)
)
{
return
;
}
let
pendingReviewId
=
await
this
.
getPendingReviewId
(
)
;
if
(
!
pendingReviewId
)
{
pendingReviewId
=
await
this
.
startReview
(
commitId
)
;
}
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
{
data
}
=
await
mutate
<
AddCommentResponse
>
(
{
mutation
:
schema
.
AddComment
,
variables
:
{
input
:
{
pullRequestReviewId
:
pendingReviewId
,
body
,
inReplyTo
,
commitOID
:
commitId
||
this
.
head
?.
sha
,
}
,
}
,
}
)
;
if
(
!
data
)
{
throw
new
Error
(
'Creating comment reply failed.'
)
;
}
const
{
comment
}
=
data
.
addPullRequestReviewComment
;
const
newComment
=
parseGraphQLComment
(
comment
,
false
,
this
.
githubRepository
)
;
if
(
isSingleComment
)
{
newComment
.
isDraft
=
false
;
}
const
threadWithComment
=
this
.
_reviewThreadsCache
.
find
(
thread
=>
thread
.
comments
.
some
(
comment
=>
comment
.
graphNodeId
===
inReplyTo
)
,
)
;
if
(
threadWithComment
)
{
threadWithComment
.
comments
.
push
(
newComment
)
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
]
,
changed
:
[
threadWithComment
]
,
removed
:
[
]
}
)
;
}
return
newComment
;
}
/**
* Check whether there is an existing pending review and update the context key to control what comment actions are shown.
*/
async
validateDraftMode
(
)
:
Promise
<
boolean
>
{
const
inDraftMode
=
!
!
(
await
this
.
getPendingReviewId
(
)
)
;
if
(
inDraftMode
!==
this
.
hasPendingReview
)
{
this
.
hasPendingReview
=
inDraftMode
;
}
await
this
.
updateDraftModeContext
(
)
;
return
inDraftMode
;
}
private
async
updateDraftModeContext
(
)
{
if
(
this
.
isActive
)
{
await
vscode
.
commands
.
executeCommand
(
'setContext'
,
'reviewInDraftMode'
,
this
.
hasPendingReview
)
;
}
}
/**
* Edit an existing review comment.
*
@param
comment The comment to edit
*
@param
text The new comment text
*/
async
editReviewComment
(
comment
:
IComment
,
text
:
string
)
:
Promise
<
IComment
>
{
const
{
mutate
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
let
threadWithComment
=
this
.
_reviewThreadsCache
.
find
(
thread
=>
thread
.
comments
.
some
(
c
=>
c
.
graphNodeId
===
comment
.
graphNodeId
)
,
)
;
if
(
!
threadWithComment
)
{
return
this
.
editIssueComment
(
comment
,
text
)
;
}
const
{
data
}
=
await
mutate
<
EditCommentResponse
>
(
{
mutation
:
schema
.
EditComment
,
variables
:
{
input
:
{
pullRequestReviewCommentId
:
comment
.
graphNodeId
,
body
:
text
,
}
,
}
,
}
)
;
if
(
!
data
)
{
throw
new
Error
(
'Editing review comment failed.'
)
;
}
const
newComment
=
parseGraphQLComment
(
data
.
updatePullRequestReviewComment
.
pullRequestReviewComment
,
!
!
comment
.
isResolved
,
this
.
githubRepository
)
;
if
(
threadWithComment
)
{
const
index
=
threadWithComment
.
comments
.
findIndex
(
c
=>
c
.
graphNodeId
===
comment
.
graphNodeId
)
;
threadWithComment
.
comments
.
splice
(
index
,
1
,
newComment
)
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
]
,
changed
:
[
threadWithComment
]
,
removed
:
[
]
}
)
;
}
return
newComment
;
}
/**
* Deletes a review comment.
*
@param
commentId The comment id to delete
*/
async
deleteReviewComment
(
commentId
:
string
)
:
Promise
<
void
>
{
try
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
id
=
Number
(
commentId
)
;
const
threadIndex
=
this
.
_reviewThreadsCache
.
findIndex
(
thread
=>
thread
.
comments
.
some
(
c
=>
c
.
id
===
id
)
)
;
if
(
threadIndex
===
-
1
)
{
this
.
deleteIssueComment
(
commentId
)
;
}
else
{
await
octokit
.
call
(
octokit
.
api
.
pulls
.
deleteReviewComment
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
comment_id
:
id
,
}
)
;
if
(
threadIndex
>
-
1
)
{
const
threadWithComment
=
this
.
_reviewThreadsCache
[
threadIndex
]
;
const
index
=
threadWithComment
.
comments
.
findIndex
(
c
=>
c
.
id
===
id
)
;
threadWithComment
.
comments
.
splice
(
index
,
1
)
;
if
(
threadWithComment
.
comments
.
length
===
0
)
{
this
.
_reviewThreadsCache
.
splice
(
threadIndex
,
1
)
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
]
,
changed
:
[
]
,
removed
:
[
threadWithComment
]
}
)
;
}
else
{
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
:
[
]
,
changed
:
[
threadWithComment
]
,
removed
:
[
]
}
)
;
}
}
}
}
catch
(
e
)
{
throw
new
Error
(
formatError
(
e
)
)
;
}
}
/**
* Get existing requests to review.
*/
async
getReviewRequests
(
)
:
Promise
<
IAccount
[
]
>
{
const
githubRepository
=
this
.
githubRepository
;
const
{
remote
,
octokit
}
=
await
githubRepository
.
ensure
(
)
;
const
result
=
await
octokit
.
call
(
octokit
.
api
.
pulls
.
listRequestedReviewers
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
pull_number
:
this
.
number
,
}
)
;
return
result
.
data
.
users
.
map
(
(
user
:
any
)
=>
convertRESTUserToAccount
(
user
,
githubRepository
)
)
;
}
/**
* Add reviewers to a pull request
*
@param
reviewers A list of GitHub logins
*/
async
requestReview
(
reviewers
:
string
[
]
)
:
Promise
<
void
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
await
octokit
.
call
(
octokit
.
api
.
pulls
.
requestReviewers
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
pull_number
:
this
.
number
,
reviewers
,
}
)
;
}
/**
* Remove a review request that has not yet been completed
*
@param
reviewer A GitHub Login
*/
async
deleteReviewRequest
(
reviewer
:
string
)
:
Promise
<
void
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
await
octokit
.
call
(
octokit
.
api
.
pulls
.
removeRequestedReviewers
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
pull_number
:
this
.
number
,
reviewers
:
[
reviewer
]
,
}
)
;
}
async
deleteAssignees
(
assignee
:
string
)
:
Promise
<
void
>
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
await
octokit
.
call
(
octokit
.
api
.
issues
.
removeAssignees
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
issue_number
:
this
.
number
,
assignees
:
[
assignee
]
,
}
)
;
}
private
diffThreads
(
oldReviewThreads
:
IReviewThread
[
]
,
newReviewThreads
:
IReviewThread
[
]
)
:
void
{
const
added
:
IReviewThread
[
]
=
[
]
;
const
changed
:
IReviewThread
[
]
=
[
]
;
const
removed
:
IReviewThread
[
]
=
[
]
;
newReviewThreads
.
forEach
(
thread
=>
{
const
existingThread
=
oldReviewThreads
.
find
(
t
=>
t
.
id
===
thread
.
id
)
;
if
(
existingThread
)
{
if
(
!
equals
(
thread
,
existingThread
)
)
{
changed
.
push
(
thread
)
;
}
}
else
{
added
.
push
(
thread
)
;
}
}
)
;
oldReviewThreads
.
forEach
(
thread
=>
{
if
(
!
newReviewThreads
.
find
(
t
=>
t
.
id
===
thread
.
id
)
)
{
removed
.
push
(
thread
)
;
}
}
)
;
this
.
_onDidChangeReviewThreads
.
fire
(
{
added
,
changed
,
removed
,
}
)
;
}
async
getReviewThreads
(
)
:
Promise
<
IReviewThread
[
]
>
{
const
{
remote
,
query
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
try
{
const
{
data
}
=
await
query
<
PullRequestCommentsResponse
>
(
{
query
:
schema
.
PullRequestComments
,
variables
:
{
owner
:
remote
.
owner
,
name
:
remote
.
repositoryName
,
number
:
this
.
number
,
}
,
}
)
;
const
reviewThreads
=
data
.
repository
.
pullRequest
.
reviewThreads
.
nodes
.
map
(
node
=>
{
return
parseGraphQLReviewThread
(
node
,
this
.
githubRepository
)
;
}
)
;
const
oldReviewThreads
=
this
.
_reviewThreadsCache
;
this
.
_reviewThreadsCache
=
reviewThreads
;
this
.
diffThreads
(
oldReviewThreads
,
reviewThreads
)
;
return
reviewThreads
;
}
catch
(
e
)
{
Logger
.
appendLine
(
`Failed to get pull request review comments:
${
e
}
`
)
;
return
[
]
;
}
}
/**
* Get all review comments.
*/
async
initializeReviewComments
(
)
:
Promise
<
void
>
{
const
{
remote
,
query
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
try
{
const
{
data
}
=
await
query
<
PullRequestCommentsResponse
>
(
{
query
:
schema
.
PullRequestComments
,
variables
:
{
owner
:
remote
.
owner
,
name
:
remote
.
repositoryName
,
number
:
this
.
number
,
}
,
}
)
;
const
comments
=
data
.
repository
.
pullRequest
.
reviewThreads
.
nodes
.
map
(
node
=>
node
.
comments
.
nodes
.
map
(
comment
=>
parseGraphQLComment
(
comment
,
node
.
isResolved
,
this
.
githubRepository
)
,
remote
)
)
.
reduce
(
(
prev
,
curr
)
=>
prev
.
concat
(
curr
)
,
[
]
)
.
sort
(
(
a
:
IComment
,
b
:
IComment
)
=>
{
return
a
.
createdAt
>
b
.
createdAt
?
1
:
-
1
;
}
)
;
this
.
comments
=
comments
;
}
catch
(
e
)
{
Logger
.
appendLine
(
`Failed to get pull request review comments:
${
e
}
`
)
;
}
}
/**
* Get a list of the commits within a pull request.
*/
async
getCommits
(
)
:
Promise
<
OctokitCommon
.
PullsListCommitsResponseData
>
{
try
{
Logger
.
debug
(
`Fetch commits of PR #
${
this
.
number
}
- enter`
,
PullRequestModel
.
ID
)
;
const
{
remote
,
octokit
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
commitData
=
await
octokit
.
call
(
octokit
.
api
.
pulls
.
listCommits
,
{
pull_number
:
this
.
number
,
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
}
)
;
Logger
.
debug
(
`Fetch commits of PR #
${
this
.
number
}
- done`
,
PullRequestModel
.
ID
)
;
return
commitData
.
data
;
}
catch
(
e
)
{
vscode
.
window
.
showErrorMessage
(
`Fetching commits failed:
${
formatError
(
e
)
}
`
)
;
return
[
]
;
}
}
/**
* Get all changed files within a commit
*
@param
commit The commit
*/
async
getCommitChangedFiles
(
commit
:
OctokitCommon
.
PullsListCommitsResponseData
[
0
]
,
)
:
Promise
<
OctokitCommon
.
ReposGetCommitResponseFiles
>
{
try
{
Logger
.
debug
(
`Fetch file changes of commit
${
commit
.
sha
}
in PR #
${
this
.
number
}
- enter`
,
PullRequestModel
.
ID
,
)
;
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
fullCommit
=
await
octokit
.
call
(
octokit
.
api
.
repos
.
getCommit
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
ref
:
commit
.
sha
,
}
)
;
Logger
.
debug
(
`Fetch file changes of commit
${
commit
.
sha
}
in PR #
${
this
.
number
}
- done`
,
PullRequestModel
.
ID
,
)
;
return
fullCommit
.
data
.
files
??
[
]
;
}
catch
(
e
)
{
vscode
.
window
.
showErrorMessage
(
`Fetching commit file changes failed:
${
formatError
(
e
)
}
`
)
;
return
[
]
;
}
}
/**
* Gets file content for a file at the specified commit
*
@param
filePath The file path
*
@param
commit The commit
*/
async
getFile
(
filePath
:
string
,
commit
:
string
)
{
const
{
octokit
,
remote
}
=
await
this
.
githubRepository
.
ensure
(
)
;
const
fileContent
=
await
octokit
.
call
(
octokit
.
api
.
repos
.
getContent
,
{
owner
:
remote
.
owner
,
repo
:
remote
.
repositoryName
,
path
:
filePath
,
ref
:
commit
,
}
)
;
if
(
Array
.
isArray
(
fileContent
.
data
)
)
{
throw
new
Error
(
`Unexpected array response when getting file
${
filePath
}
`
)
;
}
const
contents
=
(
fileContent
.
data
as
any
)
.
content
??
''
;
const
buff
=
buffer
.
Buffer
.
from
(
contents
,
(
fileContent
.
data
as
any
)
.
encoding
)
;
return
buff
.
toString
(
)
;
}
/**
* Get the timeline events of a pull request, including comments, reviews, commits, merges, deletes, and assigns.
*/
async
getTimelineEvents
(
)
:
Promise
<
TimelineEvent
[
]
>
{
Logger
.
debug
(
`Fetch timeline events of PR #
${
this
.
number
}
- enter`
,
PullRequestModel
.
ID
)
;
const
{
query
,
remote
,
schema
}
=
await
this
.
githubRepository
.
ensure
(
)
;
try
{
const
[
{
data
}
,
latestReviewCommitInfo
,
currentUser
,
reviewThreads
]
=
await
Promise
.
all
(
[
query
<
TimelineEventsResponse
>
(
{
query
:
schema
.
TimelineEvents
,
variables
:
{
owner
:
remote
.
owner
,
name
:
remote
.
repositoryName
,
number
:
this
.
number
,
}
,
}
)
,
this
.
getViewerLatestReviewCommit
(
)
,
this
.
githubRepository
.
getAuthenticatedUser
(
)
,
this
.
getReviewThreads
(
)
]
)
;
const
ret
=
data
.
repository
.
pullRequest
.
timelineItems
.
nodes
;
const
events
=
parseGraphQLTimelineEvents
(
ret
,
this
.
githubRepository
)
;
this
.
addReviewTimelineEventComments
(
events
,
reviewThreads
)
;
insertNewCommitsSinceReview
(
events
,
latestReviewCommitInfo
?.
sha
,
currentUser
,
this
.
head
)
;
return
events
;
}
catch
(
e
)
{
console
.
log
(
e
)
;
return
[
]
;
}
}
private
addReviewTimelineEventComments
(
events
:
TimelineEvent
[
]
,
reviewThreads
:
IReviewThread
[
]
)
:
void
{
interface
CommentNode
extends
IComment
{
childComments
?:
CommentNode
[
]
;
}
const
reviewEvents
=
events
.
filter
(
isReviewEvent
)
;
const
reviewComments
=
reviewThreads
.
reduce
(
(
previous
,
current
)
=>
(
previous
as
IComment
[
]
)
.
concat
(
current
.
comments
)
,
[
]
)
;
const
reviewEventsById
=
reviewEvents
.
reduce
(
(
index
,
evt
)
=>
{
index
[
evt
.
id
]
=
evt
;
evt
.
comments
=
[
]
;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL