-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpr-review-action.el
571 lines (513 loc) · 23.1 KB
/
pr-review-action.el
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
;;; pr-review-action.el --- Action part for pr-review -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Yikai Zhao
;; Author: Yikai Zhao <[email protected]>
;; Keywords: tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'pr-review-common)
(require 'pr-review-input)
(require 'pr-review-api)
(require 'magit-section)
(require 'magit-diff)
(require 'browse-url)
(declare-function pr-review-refresh "pr-review")
(defconst pr-review--review-actions '("COMMENT" "APPROVE" "REQUEST_CHANGES")
"Available actions for `pr-review-submit-review'.")
(defconst pr-review--merge-methods '("MERGE" "REBASE" "SQUASH")
"Available methods for `pr-review-merge'.")
(defconst pr-review--subscription-states '("IGNORED" "SUBSCRIBED" "UNSUBSCRIBED")
"Available states for `pr-review-update-subscription'.")
(defun pr-review--insert-quoted-content (body)
"Insert BODY as quoted in markdown format."
(when body
(insert (replace-regexp-in-string "^" "> " body)
"\n")))
(defun pr-review-reply-to-thread (&rest _)
"Reply to current thread."
(interactive)
(let ((section (magit-current-section))
reply-content)
(when (pr-review--review-thread-item-section-p section)
(setq reply-content (oref section body)
section (oref section parent)))
(when (pr-review--review-thread-section-p section)
(pr-review--open-input-buffer
"Reply to thread."
(apply-partially #'pr-review--insert-quoted-content reply-content)
(apply-partially #'pr-review--post-review-comment-reply
(alist-get 'id pr-review--pr-info)
(oref section top-comment-id))
'refresh-after-exit))))
(declare-function pr-review-refresh "pr-review")
(defun pr-review-resolve-thread (&rest _)
"Resolve or unresolve current thread."
(interactive)
(when-let ((section (magit-current-section)))
(when (pr-review--review-thread-item-section-p section)
(setq section (oref section parent)))
(when (pr-review--review-thread-section-p section)
(let ((resolved (oref section is-resolved))
(thread-id (oref section value)))
(when (y-or-n-p (format "Really %s this thread? "
(if resolved "unresolve" "resolve")))
(pr-review--post-resolve-review-thread
thread-id (not resolved))
(pr-review-refresh))))))
(defun pr-review-comment (&rest _)
"Post comment to this PR."
(interactive)
(let ((section (magit-current-section))
reply-content)
(when (or (pr-review--comment-section-p section)
(pr-review--review-section-p section))
(setq reply-content (oref section body)))
(pr-review--open-input-buffer
"Comment to PR."
(apply-partially #'pr-review--insert-quoted-content reply-content)
(apply-partially #'pr-review--post-comment
(alist-get 'id pr-review--pr-info))
'refresh-after-exit)))
(defun pr-review--get-diff-line-info (pt)
"Return (side . (filename . line)) for diff line at PT."
(save-excursion
(goto-char pt)
(beginning-of-line)
(let (prop)
(cond
((setq prop (get-text-property (point) 'pr-review-diff-line-left))
(cons "LEFT" prop))
((setq prop (get-text-property (point) 'pr-review-diff-line-right))
(cons "RIGHT" prop))))))
(declare-function pr-review--insert-in-diff-pending-review-thread "pr-review-render")
(defun pr-review--add-pending-review-thread-exit-callback (orig-buffer review-thread body)
"Exit callback for adding pending review thread.
ORIG-BUFFER is the original pr review buffer;
REVIEW-THREAD is the related thread;
BODY is the result text user entered."
(setf (alist-get 'body review-thread) body)
(when (buffer-live-p orig-buffer)
(with-current-buffer orig-buffer
(let ((inhibit-read-only t))
(pr-review--insert-in-diff-pending-review-thread review-thread))
(set-buffer-modified-p t)
(push review-thread pr-review--pending-review-threads))))
(defun pr-review-add-pending-review-thread ()
"Add pending review thread under current point (must be in a diff line).
When a region is active, the review thread is added for multiple lines."
(interactive)
(let* ((line-info (pr-review--get-diff-line-info
(if (use-region-p) (1- (region-end)) (point))))
(start-line-info (when (use-region-p)
(pr-review--get-diff-line-info (region-beginning))))
region-text
review-thread)
(when (equal line-info start-line-info)
(setq start-line-info nil))
(if (or (null line-info)
(and start-line-info
(not (equal (cadr line-info) (cadr start-line-info)))))
(message "Cannot add review thread at current point")
(setq review-thread `((path . ,(cadr line-info))
(line . ,(cddr line-info))
(side . ,(car line-info))))
(when start-line-info
(setq review-thread (append `((startLine . ,(cddr start-line-info))
(startSide . ,(car start-line-info)))
review-thread)))
(when (use-region-p)
(setq region-text (replace-regexp-in-string
(rx line-start (any ?+ ?- ?\s)) ""
(buffer-substring-no-properties (region-beginning) (region-end))))
(unless (string-suffix-p "\n" region-text)
(setq region-text (concat region-text "\n"))))
(pr-review--open-input-buffer
"Start review thread."
(when region-text
(lambda ()
(insert "```suggestion\n" region-text "```")
(goto-char (point-min))))
(apply-partially #'pr-review--add-pending-review-thread-exit-callback
(current-buffer)
review-thread))
t)))
(defun pr-review-edit-pending-review-thread ()
"Edit pending review thread under current point."
(interactive)
(when-let* ((review-thread (get-text-property (point) 'pr-review-pending-review-thread))
(end (next-single-property-change (point) 'pr-review-pending-review-thread))
(beg (previous-single-property-change end 'pr-review-pending-review-thread)))
(let ((inhibit-read-only t))
(delete-region beg end))
(setq-local pr-review--pending-review-threads
(delq review-thread pr-review--pending-review-threads))
(pr-review--open-input-buffer
"Edit review thread."
(lambda ()
(insert (alist-get 'body review-thread))
(goto-char (point-min)))
(apply-partially #'pr-review--add-pending-review-thread-exit-callback
(current-buffer)
review-thread))
t))
(defun pr-review-edit-or-add-pending-review-thread ()
"Edit pending review thread or add a new one, depending on the current point."
(interactive)
(or (and (not (use-region-p)) ;; if region is active, always add instead of edit
(pr-review-edit-pending-review-thread))
(pr-review-add-pending-review-thread)))
(defun pr-review--submit-review-exit-callback (orig-buffer event body)
"Exit callback for submitting reviews.
ORIG-BUFFER is the original pr review buffer;
EVENT is the review action user selected;
BODY is the result text user entered."
(when (buffer-live-p orig-buffer)
(with-current-buffer orig-buffer
(pr-review--post-review (alist-get 'id pr-review--pr-info)
(alist-get 'headRefOid pr-review--pr-info)
event
(nreverse pr-review--pending-review-threads)
body)
(setq-local pr-review--pending-review-threads nil))))
(defun pr-review-submit-review (event)
"Submit review with pending review threads, with action EVENT.
When called interactively, user will be asked to choose an event."
(interactive (list (completing-read "Select review action: "
pr-review--review-actions
nil 'require-match)))
(pr-review--open-input-buffer
(format "Submit review %s (%s threads)." event (length pr-review--pending-review-threads))
nil
(apply-partially #'pr-review--submit-review-exit-callback
(current-buffer) event)
'refresh-after-exit
'allow-empty))
(defun pr-review-merge (method)
"Merge current PR with METHOD.
Available methods is `pr-review--merge-methods'.
Will confirm before sending the request."
(interactive (list (completing-read "Select merge method: "
pr-review--merge-methods
nil 'require-match)))
(when (y-or-n-p (format "Really merge this PR with method %s? " method))
(pr-review--post-merge-pr (alist-get 'id pr-review--pr-info) method)
(pr-review-refresh)))
(defun pr-review--close-or-reopen-action ()
"Return the expected action if `pr-review-close-or-reopen' is called.
Maybe 'close or 'reopen or nil."
(pcase (alist-get 'state pr-review--pr-info)
("CLOSED" 'reopen)
("OPEN" 'close)
(_ nil)))
(defun pr-review-close-or-reopen ()
"Close or re-open PR based on current state.
Will confirm before sending the request."
(interactive)
(pcase (alist-get 'state pr-review--pr-info)
("CLOSED" (when (y-or-n-p "Really re-open this PR? ")
(pr-review--post-reopen-pr (alist-get 'id pr-review--pr-info))
(pr-review-refresh)))
("OPEN" (when (y-or-n-p "Really close this PR? ")
(pr-review--post-close-pr (alist-get 'id pr-review--pr-info))
(pr-review-refresh)))
(_
(error "Cannot close or reopen PR in current state"))))
(defun pr-review-close-or-reopen-or-merge (action)
"Close or re-open or merge based on ACTION.
Used for interactive selection one of them."
(interactive (list (let ((actions pr-review--merge-methods))
(when-let ((close-or-reopen-action (pr-review--close-or-reopen-action)))
(setq actions
(append actions
(list (upcase (symbol-name close-or-reopen-action))))))
(completing-read "Select action: "
actions nil 'require-match))))
(if (member action pr-review--merge-methods)
(pr-review-merge action)
(pr-review-close-or-reopen)))
(defun pr-review-edit-comment ()
"Edit comment under current point."
(interactive)
(when-let* ((section (magit-current-section))
(-is-comment-section (pr-review--comment-section-p section))
(updatable (oref section updatable))
(id (oref section value))
(body (oref section body)))
(pr-review--open-input-buffer
"Update comment."
(lambda () (insert body))
(apply-partially #'pr-review--update-comment id)
'refresh-after-exit)))
(defun pr-review-edit-review ()
"Edit review body under current point."
(interactive)
(when-let* ((section (magit-current-section))
(-is-review-section (pr-review--review-section-p section))
(updatable (oref section updatable))
(id (oref section value))
(body (oref section body)))
(pr-review--open-input-buffer
"Update review."
(lambda () (insert body))
(apply-partially #'pr-review--update-review id)
'refresh-after-exit)))
(defun pr-review-edit-review-comment ()
"Edit review comment under current point."
(interactive)
(when-let* ((section (magit-current-section))
(-is-review-thread-item (pr-review--review-thread-item-section-p section))
(updatable (oref section updatable))
(id (oref section value))
(body (oref section body)))
(pr-review--open-input-buffer
"Update review comment."
(lambda () (insert body))
(apply-partially #'pr-review--update-review-comment id)
'refresh-after-exit)))
(defun pr-review-edit-pr-description ()
"Edit pr description (body)."
(interactive)
(when-let* ((section (magit-current-section))
(-is-description-section (pr-review--description-section-p section))
(updatable (oref section updatable))
(body (oref section body)))
(pr-review--open-input-buffer
"Update PR description."
(lambda () (insert body))
(apply-partially #'pr-review--update-pr-body (alist-get 'id pr-review--pr-info))
'refresh-after-exit)))
(defun pr-review-edit-pr-title ()
"Edit pr title."
(interactive)
(when-let* ((section (magit-current-section))
(-is-root-section (pr-review--root-section-p section))
(updatable (oref section updatable))
(title (oref section title)))
(pr-review--open-input-buffer
"Update PR title."
(lambda () (insert title))
(apply-partially #'pr-review--update-pr-title (alist-get 'id pr-review--pr-info))
'refresh-after-exit)))
(defun pr-review--make-temp-file (head-or-base filepath content)
(make-temp-file (concat (upcase (symbol-name head-or-base)) "~")
nil
(concat "~" (file-name-nondirectory filepath))
content))
(defun pr-review-view-file (head-or-base filepath &optional line)
"View the full file content in a temporary buffer.
By default, view the file under current point (must in some diff).
When invoked with prefix, prompt for head-or-base and filepath."
(interactive
(let (head-or-base filepath line)
(when-let* ((line-info (pr-review--get-diff-line-info (point))))
(setq head-or-base (if (equal (car line-info) "LEFT") 'base 'head)
filepath (cadr line-info)
line (cddr line-info)))
(when (or current-prefix-arg (null head-or-base) (null filepath))
(let ((res (completing-read "Ref: " '("head" "base") nil t)))
(setq head-or-base (intern res)))
(setq filepath (read-from-minibuffer "File path: " filepath)))
(list head-or-base filepath line)))
(when (and head-or-base filepath)
(let* ((content (pr-review--fetch-file filepath head-or-base))
(tempfile (pr-review--make-temp-file head-or-base filepath content)))
(with-current-buffer (find-file-other-window tempfile)
(goto-char (point-min))
(when line
(forward-line (1- line)))))))
(defun pr-review-ediff-file (filepath)
"View the diff using `ediff'.
By default, view the file under current point (must in some diff).
When invoked with prefix, prompt for filepath."
(interactive
(let (filepath)
(when-let* ((line-info (pr-review--get-diff-line-info (point))))
(setq filepath (cadr line-info)))
(when (or current-prefix-arg (null filepath))
(setq filepath (completing-read "File:" (pr-review--find-all-file-names) nil 'require-match)))
(list filepath)))
(let* ((base-content (pr-review--fetch-file filepath 'base))
(head-content (pr-review--fetch-file filepath 'head)))
(ediff-files (pr-review--make-temp-file 'base filepath base-content)
(pr-review--make-temp-file 'head filepath head-content))))
(defun pr-review-open-in-default-browser ()
"Open current PR in default browser."
(interactive)
(browse-url-default-browser (alist-get 'url pr-review--pr-info)))
;; general dispatching functions, call other functions based on current context
(defun pr-review--review-thread-context-p (section)
"Check whether SECTION is a review thread (or its children)."
(or (pr-review--review-thread-section-p section)
(pr-review--review-thread-item-section-p section)))
(defun pr-review--diff-context-p (section)
"Check whether SECTION is a diff section (or its children)."
(or (pr-review--diff-section-p section)
(magit-hunk-section-p section)
(magit-file-section-p section)
(magit-module-section-p section)
(get-text-property (point) 'pr-review-pending-review-thread)))
(defun pr-review-context-comment ()
"Comment on current point.
Based on current context, may be:
reply to thread, post comment, add/edit review on diff."
(interactive)
(pcase (magit-current-section)
((pred pr-review--review-thread-context-p)
(pr-review-reply-to-thread))
((pred pr-review--diff-context-p)
(pr-review-edit-or-add-pending-review-thread))
(_
(pr-review-comment))))
(defun pr-review-context-action ()
"Action on current point.
Based on current context, may be: resolve thread, submit review."
(interactive)
(pcase (magit-current-section)
((pred pr-review--review-thread-context-p)
(pr-review-resolve-thread))
;; in diff, or has pending review threads
((or (pred pr-review--diff-context-p)
(pred (lambda (_) pr-review--pending-review-threads)))
(call-interactively #'pr-review-submit-review))
(_
(call-interactively #'pr-review-close-or-reopen-or-merge))))
(defun pr-review-context-edit ()
"Edit on current point.
Based on current context, may be:
edit description, edit review comment, edit comment, edit pending diff review."
(interactive)
(pcase (magit-current-section)
((pred pr-review--description-section-p)
(pr-review-edit-pr-description))
((pred pr-review--review-thread-item-section-p)
(pr-review-edit-review-comment))
((pred pr-review--comment-section-p)
(pr-review-edit-comment))
((pred pr-review--review-section-p)
(pr-review-edit-review))
((pred pr-review--diff-context-p)
(pr-review-edit-pending-review-thread))
((pred pr-review--root-section-p)
(pr-review-edit-pr-title))
(_
(message "No action available in current context"))))
(defun pr-review--find-all-file-sections (section)
"Recursively find all file sections in SECTION."
(if (magit-file-section-p section)
(list section)
(mapcan #'pr-review--find-all-file-sections
(oref section children))))
(defun pr-review--find-all-file-names ()
"Return all file names in current buffer."
(mapcar (lambda (section) (oref section value))
(pr-review--find-all-file-sections magit-root-section)))
(defun pr-review-goto-file (filepath)
"Goto section for FILEPATH in current buffer.
When called interactively, user can select filepath from list."
(interactive (list (completing-read
"Goto file:"
(pr-review--find-all-file-names)
nil 'require-match)))
(when-let ((section (seq-find (lambda (section) (equal (oref section value) filepath))
(pr-review--find-all-file-sections magit-root-section))))
(push-mark)
(goto-char (oref section start))
(recenter)))
(defun pr-review-request-reviews (reviewer-logins)
"Request reviewers for current PR, with a list of usernames REVIEWER-LOGINS.
This will override all existing reviewers (will clear all reviewers on empty).
When called interactively, user can select reviewers from list."
(interactive
(list
(let* ((assignable-users (pr-review--get-assignable-users))
(completion-extra-properties
(list :annotation-function
(lambda (login)
(concat " " (alist-get 'name (gethash login assignable-users)))))))
(completing-read-multiple
"Request review: "
(hash-table-keys assignable-users)
nil 'require-match
(string-join
(mapcar (lambda (reviewer) (let-alist reviewer .requestedReviewer.login))
(let-alist pr-review--pr-info .reviewRequests.nodes))
",")))))
(let* ((assignable-users (pr-review--get-assignable-users))
(ids (mapcar (lambda (login)
(let ((usr (gethash login assignable-users)))
(unless usr
(error "User %s not found" login))
(alist-get 'id usr)))
reviewer-logins)))
(pr-review--post-request-reviews (alist-get 'id pr-review--pr-info) ids)
(pr-review-refresh)))
(defun pr-review-set-labels (label-names)
"Set labels for current PR, with a list of label names LABEL-NAMES.
This will override all existing labels (will clear all labels on empty).
When called interactively, user can select labels from list."
(interactive
(list
(let* ((repo-labels (pr-review--get-repo-labels))
(completion-extra-properties
(list :annotation-function
(lambda (name)
(concat " " (alist-get 'description (gethash name repo-labels)))))))
(completing-read-multiple
"Labels: "
(hash-table-keys repo-labels)
nil 'require-match
(string-join
(mapcar (lambda (label-node) (alist-get 'name label-node))
(let-alist pr-review--pr-info .labels.nodes))
",")))))
(let* ((repo-labels (pr-review--get-repo-labels))
(label-node-ids (mapcar (lambda (name)
(let ((label (gethash name repo-labels)))
(unless label
(error "Label %s not found" name))
(alist-get 'node_id label)))
label-names))
(pr-node-id (alist-get 'id pr-review--pr-info)))
(pr-review--clear-labels pr-node-id)
(when label-node-ids
(pr-review--add-labels pr-node-id label-node-ids))
(pr-review-refresh)))
(defun pr-review-update-subscription (state)
"Update subscription to STATE for current PR.
Valid state (string): IGNORED, SUBSCRIBED, UNSUBSCRIBED."
(interactive (list (completing-read "Update subscription: "
pr-review--subscription-states
nil 'require-match)))
(when (member state pr-review--subscription-states)
(pr-review--post-subscription-update (alist-get 'id pr-review--pr-info) state)
(pr-review-refresh)))
(defun pr-review-goto-database-id (database-id)
"Goto section with DATABASE-ID, which is used as the anchor in github urls.
Return t if found, nil otherwise."
(let (pos)
(save-excursion
(goto-char (point-min))
(when-let ((match (text-property-search-forward
'magit-section database-id
(lambda (target prop-value)
(when (or (pr-review--review-section-p prop-value)
(pr-review--comment-section-p prop-value)
(pr-review--review-thread-item-section-p prop-value))
(equal (number-to-string (oref prop-value databaseId))
target))))))
(setq pos (prop-match-beginning match))))
(when pos
(goto-char pos)
t)))
(provide 'pr-review-action)
;;; pr-review-action.el ends here