-
Notifications
You must be signed in to change notification settings - Fork 51
/
code-review-comment.el
471 lines (415 loc) · 18.3 KB
/
code-review-comment.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
;;; code-review-comment.el --- Manage comments -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 Wanderson Ferreira
;;
;; Author: Wanderson Ferreira <https://github.com/wandersoncferreira>
;; Maintainer: Wanderson Ferreira <[email protected]>
;; Version: 0.0.7
;; Homepage: https://github.com/wandersoncferreira/code-review
;;
;; This file is not part of GNU Emacs.
;;
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; This library implements support functions to manage code review comments.
;;
;;; Code:
(require 'code-review-section)
(require 'code-review-utils)
(require 'markdown-mode)
(defcustom code-review-comment-buffer-name "*Code Review Comment*"
"Name of comment buffer."
:group 'code-review
:type 'string)
(defcustom code-review-comment-buffer-msg ";;; Type C-c C-c to include your comment locally\n;;; After that check the transient menu (press r) to see possible actions"
"Helper text on top of comment buffer."
:group 'code-review
:type 'string)
(defcustom code-review-comment-feedback-msg ";;; Leave a comment here."
"Default text to feedback slot."
:group 'code-review
:type 'string)
(defcustom code-review-comment-single-comment-msg ";;; Equivalent to add a simple comment to the PR without a review."
"Default text to single comment section e.g. conversation."
:group 'code-review
:type 'string)
(defcustom code-review-comment-suggestion-msg ";;; Type C-c C-c to include your comment locally\n;;; Type C-c C-k to abort"
"Default text to suggestion code in comment buffer."
:group 'code-review
:type 'string)
;;; internal vars
(defvar code-review-comment-cursor-pos nil
"Variable to hold the cursor position where the comment will be added.
For internal usage only.")
(defvar code-review-comment-feedback? nil
"Differentiate between a regular comment from the main feedback comment.
For internal usage only.")
(defvar code-review-comment-title? nil
"Are you writing a title?.
For internal usage only.")
(defvar code-review-promote-comment-to-issue? nil
"Are you promoting a comment to a new issue?.
For internal usage only.")
(defvar code-review-comment-description? nil
"Are you writing a description?.
For internal usage only.")
(defvar code-review-comment-single-comment? nil
"Include a single new comment to the PR without a Review.
For internal usage only.")
(defvar code-review-comment-uncommitted nil
"Hold uncommitted comment.
For internal usage only.")
(defvar code-review-comment-commit-buffer? nil
"Toggle if we are at the commit review buffer.
For internal usage only.")
(defvar code-review-comment-send? nil
"Toggle if we should send the local comment immediately.
For internal usage only.")
(defvar code-review-comment-suggestion? nil
"Are you writing a code suggestion?.
For internal usage only.")
;; remove free variable warnings
(defvar code-review-comment-buffer-name)
(defvar code-review-commit-buffer-name)
(defvar code-review-buffer-name)
;;; general functons
(defun code-review-comment-reset-global-vars ()
"Reset all stateful vars."
(setq code-review-comment-cursor-pos nil
code-review-comment-feedback? nil
code-review-comment-title? nil
code-review-promote-comment-to-issue? nil
code-review-comment-description? nil
code-review-comment-uncommitted nil
code-review-comment-commit-buffer? nil
code-review-comment-single-comment? nil
code-review-comment-send? nil
code-review-comment-suggestion? nil))
;;; Comment C_UD
(defun code-review-comment-add (&optional msg)
"Add comment.
Optionally define a MSG."
(let ((buffer (get-buffer-create code-review-comment-buffer-name)))
(with-current-buffer buffer
(erase-buffer)
(insert (if msg msg code-review-comment-buffer-msg))
(insert ?\n)
(switch-to-buffer-other-window buffer)
(code-review-comment-mode))))
(defun code-review-comment-edit ()
"Edit comment."
(with-slots (value) (magit-current-section)
(let ((buffer (get-buffer-create code-review-comment-buffer-name)))
(with-current-buffer buffer
(erase-buffer)
(insert (oref value msg))
(insert ?\n)
(goto-char (point-min)))
(switch-to-buffer-other-window buffer)
(code-review-comment-mode))))
;;; handlers ADD or EDIT
(cl-defmethod code-review-comment-handler-add-or-edit ((obj code-review-code-comment-section))
"Reply to comment OBJ."
(let ((reply-comment (code-review-reply-comment-section
:state (oref obj state)
:author (oref obj author)
:path (oref obj path)
:position (oref obj position)
:diffHunk (oref obj diffHunk)
:id (oref obj id))))
(setq code-review-comment-uncommitted reply-comment)
(code-review-comment-add)))
(cl-defmethod code-review-comment-handler-add-or-edit ((obj code-review-local-comment-section))
"Edit local comment in OBJ."
(oset obj edit? t)
(setq code-review-comment-uncommitted obj)
(code-review-comment-edit))
(cl-defmethod code-review-comment-handler-add-or-edit ((obj code-review-title-section))
"Change title in OBJ."
(setq code-review-comment-uncommitted obj
code-review-comment-title? t)
(code-review-comment-add))
(defclass code-review-comment-promote-to-issue ()
((reference-link :initarg :reference-link)
(author :initarg :author)
(title :initarg :title)
(body :initarg :body)
(buffer-text :initform nil)))
(cl-defmethod code-review-comment-handler-add-or-edit ((obj code-review-comment-promote-to-issue))
"Edit msg and title before promoting OBJ comment to new issue."
(setq code-review-comment-uncommitted obj
code-review-promote-comment-to-issue? t)
(code-review-comment-add
(format "<!-- Do not remove the Title and Body placeholders. -->
<!-- You can add multi-line segments in the body section. -->\n\nTitle: %s\n\nBody:\n> %s"
(oref obj title)
(oref obj body))))
(cl-defmethod code-review-comment-handler-add-or-edit (obj)
"Add a comment in the OBJ."
;;; only hunks allowed here
(with-slots (type) (magit-current-section)
(if (not (equal type 'hunk))
(message "You can't add text over unspecified region.")
(let* ((current-line (line-number-at-pos))
(line (save-excursion
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(line-type (cond
((string-prefix-p "-" line)
"REMOVED")
((string-prefix-p "+" line)
"ADDED")
(t
"UNCHANGED")))
(suggestion
(format "%s\n\n```suggestion\n%s\n```\n"
code-review-comment-suggestion-msg
(substring line 1)))
(amount-loc nil))
(save-excursion
(while (and (not (looking-at
"Comment by\\|Reviewed by\\|Reply by\\|modified\\|new file\\|deleted"))
(not (equal (point) (point-min))))
(forward-line -1))
(let ((section (magit-current-section)))
(if (not section)
(setq amount-loc 0)
(with-slots (type value) section
(if (equal type 'file)
(setq amount-loc 0)
(setq amount-loc (or (oref value amount-loc) 0)))))))
(let* ((diff-pos (+ 1 (- current-line
amount-loc
(a-get obj 'head-pos))))
(local-comment (code-review-local-comment-section
:state "LOCAL COMMENT"
:author (code-review-utils--git-get-user)
:path (a-get obj 'path)
:position diff-pos
:line-type line-type
:send? code-review-comment-send?)))
(setq code-review-comment-uncommitted local-comment)
(if code-review-comment-suggestion?
(progn
(code-review-comment-add suggestion)
(with-current-buffer (get-buffer code-review-comment-buffer-name)
(forward-line -2)))
(code-review-comment-add)))))))
;;;###autoload
(defun code-review-comment-add-or-edit (&optional suggestion-code?)
"Add or edit comment depending on context.
Inform if a SUGGESTION-CODE? is being proposed."
(interactive)
(let ((section (magit-current-section)))
(with-current-buffer (get-buffer code-review-buffer-name)
(setq code-review-comment-cursor-pos (point)
code-review-comment-suggestion? suggestion-code?)
(with-slots (value) section
(if (code-review-reactions-section-p section)
(code-review-reactions-reaction-at-point)
(code-review-comment-handler-add-or-edit value))))))
;;; handlers COMMIT
(cl-defmethod code-review-comment-handler-commit ((obj code-review-reply-comment-section) default-buff-msg)
"Commit the reply OBJ and clean the DEFAULT-BUFF-MSG from the text if any."
(let* ((reply-pos (- (+ (oref obj position)
(length (split-string (oref obj msg) "\n")))
2))
(clean-msg (code-review-utils--comment-clean-msg
(oref obj msg)
default-buff-msg))
(raw-comment `((author (login . ,(oref obj author)))
(state . ,(oref obj state))
(comments (nodes ((internal-id . ,(uuidgen-4))
(bodyText . ,clean-msg)
(path . ,(oref obj path))
(position . ,reply-pos)
(databaseId . ,(oref obj id))
(diffHunk . ,(oref obj diffHunk))
(outdated)
(local?)
(reply? . t)))))))
(code-review-db--pullreq-raw-comments-update raw-comment)
(code-review--build-buffer)
(setq code-review-comment-uncommitted nil)))
(cl-defmethod code-review-comment-handler-commit ((obj code-review-local-comment-section) default-buff-msg)
"Commit the local comment OBJ and clean the DEFAULT-BUFF-MSG from the text if any."
(let* ((buff-name (if code-review-comment-commit-buffer?
code-review-commit-buffer-name
code-review-buffer-name))
(clean-msg (code-review-utils--comment-clean-msg
(oref obj msg)
default-buff-msg))
(raw-comment `((author (login . ,(oref obj author)))
(state . ,(oref obj state))
(comments (nodes ((internal-id . ,(uuidgen-4))
(bodyText . ,clean-msg)
(path . ,(oref obj path))
(position . ,(oref obj position))
(databaseId)
(diffHunk)
(outdated)
(reply?)
(line-type . ,(oref obj line-type))
(local? . t)))))))
(when (oref obj edit?)
;;; delete old comment from raw
(code-review-db-delete-raw-comment (oref obj internalId)))
(if (oref obj send?)
(progn
(oset obj msg clean-msg)
(code-review-new-code-comment
(code-review-db-get-pullreq)
obj
(lambda (&rest _)
(let ((code-review-section-full-refresh? t))
(code-review--build-buffer buff-name)))))
(progn
(code-review-db--pullreq-raw-comments-update raw-comment)
(code-review--build-buffer buff-name)))))
(cl-defmethod code-review-comment-handler-commit ((obj code-review-comment-promote-to-issue) _default-buff-msg)
"Commit the promotion of comment OBJ to new issue and clean the DEFAULT-BUFF-MSG from the text if any."
(save-match-data
(let ((text (oref obj buffer-text))
(regex (rx "Title:"
(group-n 1 (* any) "\n")
(one-or-more "\n")
"Body:"
(group-n 2 (zero-or-more anything))))
(title)
(body))
(and (string-match regex text)
(setq title (match-string 1 text))
(setq body (match-string 2 text)))
(let* ((pr (code-review-db-get-pullreq))
(body (concat (string-trim body)
"\n\n"
(format "_Originally posted by @%s in %s_"
(oref obj author)
(oref obj reference-link)))))
(setq code-review-promote-comment-to-issue? nil)
(code-review-new-issue
pr body title
(lambda (&rest _) (message "New issue created.")))))))
;;;###autoload
(defun code-review-comment-commit ()
"Commit comment."
(interactive)
(unwind-protect
(let* ((buffer (get-buffer code-review-comment-buffer-name))
(comment-text (string-trim
(with-current-buffer buffer
(save-excursion
(buffer-substring-no-properties (point-min) (point-max))))))
(pr (code-review-db-get-pullreq)))
(kill-buffer (current-buffer))
(if (= 1 (length (window-list (window-frame (selected-window)))))
(delete-frame (window-frame (selected-window)))
(delete-window (selected-window)))
(cond
(code-review-comment-description?
(oset pr raw-infos (-> (oref pr raw-infos)
(a-assoc 'bodyText comment-text)
(a-assoc 'bodyHTML nil)))
(code-review-send-description
pr
(lambda ()
(code-review-db-update pr)
(code-review--build-buffer)
(code-review-comment-reset-global-vars))))
(code-review-comment-title?
(oset pr title comment-text)
(code-review-send-title
pr
(lambda ()
(code-review-db-update pr)
(code-review--build-buffer)
(code-review-comment-reset-global-vars))))
(code-review-comment-feedback?
(let ((msg
(code-review-utils--comment-clean-msg
comment-text
code-review-comment-feedback-msg)))
(code-review-db--pullreq-feedback-update msg)
(code-review--build-buffer)
(code-review-comment-reset-global-vars)))
(code-review-promote-comment-to-issue?
(progn
(oset code-review-comment-uncommitted buffer-text comment-text)
(code-review-comment-handler-commit
code-review-comment-uncommitted
code-review-comment-buffer-msg)
(code-review-comment-reset-global-vars)))
(code-review-comment-send?
(progn
(oset code-review-comment-uncommitted msg comment-text)
(code-review-comment-handler-commit
code-review-comment-uncommitted
code-review-comment-single-comment-msg)
(code-review-comment-reset-global-vars)))
(code-review-comment-single-comment?
(let ((msg
(code-review-utils--comment-clean-msg
comment-text
code-review-comment-single-comment-msg))
(callback (lambda (&rest _)
(let ((code-review-section-full-refresh? t))
(code-review--build-buffer)
(code-review-comment-reset-global-vars)))))
(code-review-new-issue-comment pr msg callback)))
(t
(progn
(oset code-review-comment-uncommitted msg comment-text)
(code-review-comment-handler-commit
code-review-comment-uncommitted
(if code-review-comment-suggestion?
code-review-comment-suggestion-msg
code-review-comment-buffer-msg))
(code-review-comment-reset-global-vars)))))))
;;; ----
;;;###autoload
(defun code-review-input-mention-user-at-point ()
"Insert @USERNAME at current point to mention an user."
(interactive)
(let* ((pr (code-review-db-get-pullreq))
(user (completing-read
"Mention user: "
(-map
(lambda (it)
(a-get it 'login))
(code-review-get-assignable-users pr))
nil 'require-match)))
(insert "@" user " ")))
;;;###autoload
(defun code-review-comment-quit ()
"Quit the comment window."
(interactive)
(kill-buffer (current-buffer))
(if (= 1 (length (window-list (window-frame (selected-window)))))
(delete-frame (window-frame (selected-window)))
(delete-window (selected-window)))
(with-current-buffer (get-buffer code-review-buffer-name)
(goto-char code-review-comment-cursor-pos)
(code-review-comment-reset-global-vars)))
(defvar code-review-comment-mode-map
(let ((map (copy-keymap markdown-mode-map)))
(define-key map (kbd "C-c C-c") 'code-review-comment-commit)
(define-key map (kbd "C-c C-k") 'code-review-comment-quit)
(define-key map (kbd "C-c @") 'code-review-input-mention-user-at-point)
(set-keymap-parent map markdown-mode-map)
map))
(define-derived-mode code-review-comment-mode markdown-mode "Code Review Comment"
"Code Review Comment.")
(provide 'code-review-comment)
;;; code-review-comment.el ends here