~niklaseklund/egerrit

ref: 2f5b35f76a37ae64b21bd6d3689950779c1762d3 egerrit/egerrit-comment.el -rw-r--r-- 15.0 KiB
2f5b35f7Niklas Eklund Add command to publish draft comments 3 months ago
                                                                                
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
;;; egerrit-comment.el --- Comment for egerrit -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Free Software Foundation, Inc.

;; This file is not part of GNU Emacs.

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This library provides the comment for `egerrit'.

;;; Code:

;;;; Requirements

(require 'egerrit-datastructures)
(require 'egerrit-diff)
(require 'egerrit-request)

(declare-function egerrit-next-conversation "egerrit")
(declare-function egerrit-previous-conversation "egerrit")
(declare-function egerrit--generate-comments-buffer "egerrit")
(declare-function egerrit--goto-to-conversation "egerrit")
(declare-function egerrit-quit-review-or-feedback "egerrit")
(declare-function egerrit--create-user "egerrit")
(declare-function egerrit--create-range "egerrit")

(defvar egerrit--current-conversation)
(defvar egerrit--current-entities)
(defvar egerrit--current-change)
(defvar egerrit--current-revision)

;;;; Variables

(defcustom egerrit-comment-display-buffer-action
  '(display-buffer-in-side-window
    (side . bottom)
    (dedicated . t))
  "The action used to display the comment popup buffer."
  :group 'egerrit
  :type 'list)

(defvar egerrit-comment-hooks nil
  "Hooks to run when opening a comment.")

;;;; Functions

(defun egerrit-send-draft-comments ()
  "Send comments as drafts."
  (let ((draft-comments
         (thread-last egerrit--current-entities
                      (seq-map #'egerrit--create-draft-comment)
                      (seq-remove #'null))))

    (seq-map (lambda (it)
               (egerrit-put-draft-comment
                egerrit--current-change
                egerrit--current-revision
                it))
             draft-comments)))

(defun egerrit-post-draft-comments (change revision)
  "Post draft comments on CHANGE's REVISION."
  (let ((url (egerrit-request-url
              (format "/changes/%s/revisions/%s/review"
                      (egerrit-change-id change)
                      (egerrit-revision-id revision)) )))
    (egerrit-request
     url
     :type "POST"
     :header '(("Content-Type" . "application/json"))
     :auth t
     :data '(("drafts" . "PUBLISH_ALL_REVISIONS")))))

(defun egerrit-put-draft-comment (change revision comment)
  "Put a draft COMMENT CHANGE's REVISION."
  (let ((url (egerrit-request-url
              (format "/changes/%s/revisions/%s/drafts"
                      (egerrit-change-id change)
                      (egerrit-revision-id revision)))))
    (egerrit-request
     url
     :type "PUT"
     :header '(("Content-Type" . "application/json"))
     :auth t
     :data comment
     :parser #'egerrit-request-parser)))

;;;; Commands

;;;###autoload
(defun egerrit-create-comment ()
  "Add or update a comment.
This function will work both for review/feedback comments."
  (interactive)
  (let* ((conversation-id (plist-get (text-properties-at (point)) 'conversation-id))
         (conversation
          (if (eq major-mode 'egerrit-diff-mode)
              (egerrit--diff-review-comment)
            (seq-find (lambda (it)
                        (string= (egerrit--conversation-id it) (symbol-name conversation-id)))
                      egerrit--current-entities)))
         (buffer (egerrit-get-buffer "egerrit-comment"))
         (window (display-buffer buffer egerrit-comment-display-buffer-action)))
    (with-selected-window window
      (erase-buffer)
      (run-hooks 'egerrit-comment-hooks)
      (egerrit-comment-mode)
      (setq egerrit--current-conversation conversation)
      (when-let ((reply
                  (alist-get "Reply" (egerrit--conversation-comments conversation) nil nil #'string=)))
        (insert (substring-no-properties reply 0 (length reply)))))
    (select-window window)))

;;;###autoload
(defun egerrit-kill-comment ()
  "Kill comment at point."
  (interactive)
  (let* ((id (plist-get (text-properties-at (point)) 'conversation-id))
         (new-location)
         (conversation-deleted nil))
    ;; Update entities by removing comment
    (when id
      (setq egerrit--current-entities
            (thread-last egerrit--current-entities
                         (seq-map (lambda (it)
                                    (if (string= (symbol-name id) (egerrit--conversation-id it))
                                        (let ((comments (egerrit--conversation-comments it)))
                                          (if (= (length comments) 1)
                                              (progn
                                                (setq conversation-deleted t)
                                                nil)
                                            (setf (egerrit--conversation-comments it) (butlast comments))
                                            it))
                                      it)))
                         (seq-remove #'null)))

      ;; Determine location
      (if (not conversation-deleted)
          (setq new-location id)
        (let ((inhibit-message t))
          (setq new-location
                (cond ((egerrit-next-conversation) (plist-get (text-properties-at (point)) 'conversation-id))
                      ((egerrit-previous-conversation) (plist-get (text-properties-at (point)) 'conversation-id))
                      (t nil)))))

      (egerrit--generate-comments-buffer egerrit--current-entities)
      (read-only-mode)

      ;; Move to new location exist
      (when new-location
        (egerrit--goto-to-conversation new-location)
        (recenter)))))

;;;###autoload
(defun egerrit-publish-comments ()
  "Publish comments."
  (interactive)
  (let ((change egerrit--current-change)
        (revision egerrit--current-revision))

    ;; Send comments as drafts
    (egerrit-publish-draft-comments)

    ;; Publish draft comments
    (run-with-timer
     2 nil
     (lambda ()
       (egerrit-post-draft-comments change revision)))
    (egerrit-quit-review-or-feedback)))

;;;###autoload
(defun egerrit-publish-draft-comments ()
  "Publish comments as drafts."
  (interactive)
  (egerrit-send-draft-comments)
  (egerrit-quit-review-or-feedback))

;;;###autoload
(defun egerrit-complete-comment ()
  "Finalize the comment and refresh the comments buffer."
  (interactive)
  (let* ((conversation egerrit--current-conversation)
         (id (egerrit--conversation-id conversation))
         (comments (egerrit--conversation-comments conversation))
         (message (buffer-substring-no-properties (point-min) (point-max)))
         (updated nil))

    ;; Update or add a reply
    (if (string= (car (seq-first (seq-reverse comments))) "Reply")
        (setf (cdr (seq-first (seq-reverse comments))) message)
      (setq comments `(,@comments ("Reply" . ,message))))
    (setf (egerrit--conversation-comments conversation) comments)

    (quit-restore-window)

    (with-current-buffer (egerrit-get-buffer "egerrit-conversations")
      ;; Update conversation
      (setq egerrit--current-entities
            (seq-map (lambda (it)
                       (if (string= id (egerrit--conversation-id it))
                           (progn
                             (setq updated t)
                             conversation)
                         it))
                     egerrit--current-entities))

      ;; Add new conversation
      (unless updated
        (push conversation egerrit--current-entities))

      ;; Refresh buffer content
      (egerrit--generate-comments-buffer egerrit--current-entities)
      (read-only-mode)
      (egerrit--goto-to-conversation (intern id))
      (when (window-live-p (get-buffer-window (current-buffer)))
        (select-window (get-buffer-window (current-buffer)))
        (recenter)))))

;;;###autoload
(defun egerrit-quit-comment ()
  "Quit the comment."
  (interactive)
  (quit-restore-window))

;;;; Support functions

(defun egerrit--create-draft-comment (conversation)
  "From a CONVERSATION create a draft comment."
  (let* ((comments (egerrit--conversation-comments conversation))
         (has-reply (string= (car (seq-first (seq-reverse comments))) "Reply"))
         (is-resolved (eq 'resolved (egerrit--conversation-status conversation)))
         (is-new-conversation (and has-reply (eq (length comments) 1))))
    ;; We want to cover these cases
    ;; 1) User initiated a new conversation
    ;; 2) User engaged in an existing conversation
    ;; 3) User changed the status of an existing conversation, automatically compose message with done
    (cond ((eq is-new-conversation t)
           (let ((range (egerrit--conversation-range conversation)))
             `(("path" . ,(egerrit--conversation-file conversation))
               ("unresolved" . "true")
               ("message" . ,(cdr (seq-first comments)))
               ("range" . (("start_line" . ,(egerrit--range-start-line range))
                           ("start_character" . ,(egerrit--range-start-character range))
                           ("end_line" . ,(egerrit--range-end-line range))
                           ("end_character" . ,(egerrit--range-end-character range)))))))
          ((eq has-reply t)
           `(("in_reply_to" . ,(egerrit--conversation-id conversation))
             ("path" . ,(egerrit--conversation-file conversation))
             ("unresolved" . ,(if is-resolved "false" "true"))
             ("message" . ,(cdr (seq-first (seq-reverse comments))))))
          ((eq is-resolved t)
           `(("in_reply_to" . ,(egerrit--conversation-id conversation))
             ("path" . ,(egerrit--conversation-file conversation))
             ("unresolved" . ,(if is-resolved "false" "true"))
             ("message" . ,"DONE")))
          (t nil))))

(defun egerrit--create-comments (json-comments)
  "Return a list of `egerrit-comment' from JSON-COMMENTS."
  (pcase-let* ((`(,file . ,comments) json-comments))
    (seq-map (lambda (comment)
               (let-alist comment
                 (egerrit--comment-create
                  :id .id
                  :patch-set .patch_set
                  :file file
                  :author (egerrit--create-user .author)
                  :line .line
                  :range (if .range
                             (egerrit--create-range .range)
                           (egerrit--range-create :start-line .line
                                                  :end-line .line))
                  :context (egerrit--comment-highlight-context comment file)
                  :updated .updated
                  :message .message
                  :in-reply-to .in_reply_to
                  :side (if .side 'parent 'revision)
                  :unresolved .unresolved)))
             comments)))

(defun egerrit--comment-highlight-context (comment file)
  "Extract and return the context of the COMMENT made on FILE."
  (let* ((context-lines (alist-get 'context_lines comment))
         (context  (string-join
                    (seq-map (lambda (it) (alist-get 'context_line it)) context-lines)
                    "\n"))
         (line-offset)
         (start))
    (unless (string-empty-p context)
      (setq line-offset (- (let-alist comment .range.start_line)
                           (let-alist (seq-first context-lines) .line_number)))
      (with-temp-buffer
        (insert context)
        (goto-char (point-min))
        (forward-line line-offset)
        (move-to-column (let-alist comment .range.start_character))
        (setq start (point))
        (goto-char (point-max))
        (forward-line (- 0 line-offset))
        (move-to-column (let-alist comment .range.end_character))
        (when-let* ((no-commit-message (not (eq '/COMMIT_MSG file)))
                    (extension (file-name-extension (symbol-name file) t))
                    (mode (thread-last auto-mode-alist
                                       (seq-find (lambda (it)
                                                   (string-match-p (car it) extension)))
                                       (cdr))))
          (funcall mode)
          ;; We ignore errors since the context is not guaranteed to be correct
          (ignore-errors
            (font-lock-ensure)))
        ;; Add text properties to the whole region
        (remove-text-properties (point-min) (point-max) '(font-lock-face fontified))
        (font-lock-append-text-property (point-min) (point-max) 'face 'egerrit-context-background-face)

        ;; Add a text property to highlight region
        (add-text-properties start (point) '(face egerrit-context-highlight-face))
        (buffer-string)))))

(defun egerrit--diff-review-comment ()
  "Return a comment.
This comment is based on highlight in diff buffer."
  (let* ((side 'revision)
         (range)
         (region (if (use-region-p)
                     `(,(region-beginning) ,(region-end))
                   `(,(save-excursion (beginning-of-line) (point))
                     ,(save-excursion (end-of-line) (point)))))
         (gerrit-commit-message-offset 7)
         (commit-message-hunk (< (point)
                                 (save-excursion (diff-beginning-of-hunk) (point))))
         (file))
    (if commit-message-hunk
        ;; Comment on a commit message
        (progn
          (setq file "/COMMIT_MSG")
          (setq side 'revision)
          (setq range (egerrit--diff-range side
                                           (egerrit--diff-commit-message-boundary)
                                           gerrit-commit-message-offset
                                           0)))
      ;; Comment on a diff hunk
      (setq side (egerrit--diff-side))
      (setq file (if (eq side 'revision)
                     (string-remove-prefix "b/" (seq-elt (diff-hunk-file-names) 0))
                   (string-remove-prefix "a/" (seq-elt (diff-hunk-file-names) 1))))
      (setq range (egerrit--diff-range side
                                       (save-excursion
                                         (diff-beginning-of-hunk)
                                         (line-number-at-pos))
                                       (egerrit--diff-hunk-line-offset side)
                                       -1)))
    (egerrit--conversation-create
     :id (secure-hash 'md5 (current-time-string))
     :file file
     :patch-set (egerrit--revision-number egerrit--current-revision)
     :side side
     :range range
     :status 'unresolved
     :comments nil
     :region region)))

;;;; Major mode

(define-minor-mode egerrit-comment-mode
  "Mode for `egerrit' comment."
  :global nil
  :lighter " Egerrit Comment"
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "C-c C-c") #'egerrit-complete-comment)
            (define-key map (kbd "C-c C-k") #'egerrit-quit-comment)
            map))

(provide 'egerrit-comment)

;;; egerrit-comment.el ends here