-
Notifications
You must be signed in to change notification settings - Fork 51
/
code-review-section.el
1849 lines (1665 loc) · 81.4 KB
/
code-review-section.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
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
;;; code-review-section.el --- UI -*- 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:
;;
;; Code to build the UI.
;;
;;; Code:
(require 'emojify)
(require 'deferred)
(require 'magit-section)
(require 'magit-diff)
(require 'shr)
(require 'code-review-faces)
(require 'code-review-db)
(require 'code-review-utils)
(require 'code-review-interfaces)
(require 'code-review-github)
(require 'code-review-gitlab)
(require 'code-review-bitbucket)
(defcustom code-review-section-indent-width 1
"Indent width for nested sections."
:type 'integer
:group 'code-review)
(defcustom code-review-section-image-scaling 0.8
"Image scaling number used to resize images in buffer."
:type 'float
:group 'code-review)
(defcustom code-review-fill-column 80
"Column number to wrap comments."
:group 'code-review
:type 'integer)
(defcustom code-review-buffer-name "*Code Review*"
"Name of the code review main buffer."
:group 'code-review
:type 'string)
(defcustom code-review-commit-buffer-name "*Code Review Commit*"
"Name of the code review commit buffer."
:group 'code-review
:type 'string)
(defcustom code-review-new-buffer-window-strategy
#'switch-to-buffer-other-window
"Function used after create a new Code Review buffer."
:group 'code-review
:type 'function)
(defun code-review--propertize-keyword (str)
"Add property face to STR."
(propertize str 'face
(cond
((member str '("MERGED" "SUCCESS" "COMPLETED" "APPROVED" "REJECTED"))
'code-review-success-state-face)
((member str '("FAILURE" "TIMED_OUT" "ERROR" "CHANGES_REQUESTED" "CLOSED" "CONFLICTING"))
'code-review-error-state-face)
((member str '("RESOLVED" "OUTDATED"))
'code-review-info-state-face)
((member str '("PENDING"))
'code-review-pending-state-face)
(t
'code-review-state-face))))
;; fix unbound symbols
(defvar magit-root-section)
(defvar code-review-comment-commit-buffer?)
(defvar code-review-comment-cursor-pos)
(defvar code-review-reaction-types
`(("THUMBS_UP" . ":+1:")
("THUMBS_DOWN" . ":-1:")
("LAUGH" . ":laughing:")
("CONFUSED" . ":confused:")
("HEART" . ":heart:")
("HOORAY" . ":tada:")
("ROCKET" . ":rocket:")
("EYES" . ":eyes:"))
"All available reactions.")
(declare-function code-review-promote-comment-to-new-issue "code-review")
(declare-function code-review-utils--visit-binary-file-at-remote "code-review-utils")
(declare-function code-review-utils--visit-binary-file-at-point "code-review-utils")
(defvar code-review-section-full-refresh? nil
"Indicate if we want to perform a complete restart.
For internal usage only.")
(defvar code-review-section-grouped-comments nil
"Hold grouped comments to avoid computation on every hunk line.
For internal usage only.")
(defvar code-review-section-hold-written-comment-ids nil
"List to hold written comments ids.
For internal usage only.")
(defvar code-review-section-hold-written-comment-count nil
"List of number of lines of comments written in the buffer.
For internal usage only.")
(defvar code-review-section--display-all-comments t
"Variable to define if we should display all comments or not.
For internal usage only.")
(defvar code-review-section--display-top-level-comments t
"Variable to define if we should display top level comments or not.
For internal usage only.")
(defvar code-review-section--display-diff-comments t
"Variable to define if we should display diff comments or not.
For internal usage only.")
;; utility functions
(defun code-review--html-written-loc (body &optional indent)
"Compute how many lines the HTML BODY will have in the buffer.
INDENT is an optional."
(let ((shr-indentation (* (or indent 0) (shr-string-pixel-width "-")))
(image-scaling-factor code-review-section-image-scaling)
(shr-width code-review-fill-column)
start
dom)
(with-temp-buffer
(insert body)
(setq dom (libxml-parse-html-region (point-min) (point-max))))
(-> (with-temp-buffer
(setq start (point))
(insert " ")
(narrow-to-region start (1+ start))
(goto-char start)
(shr-insert-document dom)
;; delete the inserted " "
(delete-char 1)
(buffer-substring-no-properties (point-min) (point-max)))
(split-string "\n")
(length)
(- 1))))
(defun code-review--dom-string (dom)
"Return string of DOM."
(mapconcat (lambda (sub)
(if (stringp sub)
sub
(code-review--dom-string sub)))
(dom-children dom) ""))
(defun code-review--shr-tag-div (dom)
"Rendering div tag as DOM in shr, with special handle for suggested-changes."
(if (not (string-match-p ".*suggested-changes.*" (or (dom-attr dom 'class) "")))
(shr-tag-div dom)
(let ((tbody (dom-by-tag dom 'tbody)))
(let ((shr-current-font 'code-review-info-state-face))
(shr-insert "* Suggested change:")
(insert "\n"))
(dolist (tr (dom-non-text-children tbody))
(dolist (td (dom-non-text-children tr))
(let ((classes (split-string (or (dom-attr td 'class) ""))))
(cond
((member "blob-num" classes) t)
((member "blob-code-deletion" classes)
(let ((shr-current-font 'diff-indicator-removed))
(shr-insert "-"))
(insert (propertize (concat (code-review--dom-string td)
"\n")
'face 'diff-removed)))
((member "blob-code-addition" classes)
(let ((shr-current-font 'diff-indicator-added))
(shr-insert "+"))
(insert (propertize (concat (code-review--dom-string td)
"\n")
'face 'diff-added)))
(t
(shr-generic td)))))))))
(defun code-review--insert-html (body &optional indent)
"Insert html content BODY.
INDENT is an optional number, if provided,
INDENT count of spaces are added at the start of every line."
(let ((shr-indentation (* (or indent 0) (shr-string-pixel-width "-")))
(image-scaling-factor code-review-section-image-scaling)
(shr-external-rendering-functions '((div . code-review--shr-tag-div)))
(shr-width code-review-fill-column)
(start (point))
end
dom)
(with-temp-buffer
(insert body)
(setq dom (libxml-parse-html-region (point-min) (point-max))))
;; narrow the buffer and insert dom. otherwise there would be an extra new line at start
(save-restriction
(insert " ")
(narrow-to-region start (1+ start))
(goto-char start)
(shr-insert-document dom)
;; delete the inserted " "
(delete-char 1)
(setq end (point)))
(when (> shr-indentation 0)
;; shr-indentation does not work for images and code block
;; let's fix it: prepend space for any lines that does not starts with a space
;; (but we still need to use shr-indentation because otherwise the line will be too long)
(save-excursion
(goto-char start)
(while (< (point) end)
(unless (or (looking-at-p "\n")
(eq 'space (car-safe (get-text-property (point) 'display))))
(beginning-of-line)
(insert (propertize " " 'display `(space :width (,shr-indentation)))))
(forward-line))))))
;; headers
(defclass code-review-author-section (magit-section)
((keymap :initform 'code-review-author-section-map)
(login :initarg :login)
(url :initarg :url)))
(defvar code-review-author-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-utils--visit-author-at-point)
(define-key map [mouse-2] 'code-review-utils--visit-author-at-point)
(define-key map [follow-link] 'code-review-utils--visit-author-at-point)
map)
"Keymaps for header author section.")
(defun code-review-section-insert-author ()
"Insert the author of the PR in the buffer."
(let-alist (code-review-db--pullreq-raw-infos)
(when .author.login
(let ((obj (code-review-author-section
:login .author.login
:url .author.url)))
(magit-insert-section (code-review-author-section obj)
(insert (format "%-17s" "Author: "))
(insert (propertize (format "@%s" .author.login)
'face 'code-review-author-header-face
'mouse-face 'highlight
'help-echo "Visit author's page"
'keymap 'code-review-author-section-map))
(insert ?\n))))))
(defclass code-review-title-section (magit-section)
((keymap :initform 'code-review-title-section-map)
(title :initform nil
:type (or null string))))
(defvar code-review-title-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-set-title)
map)
"Keymaps for code-comment sections.")
(defun code-review-section-insert-header-title ()
"Insert the title header line."
(let ((pr (code-review-db-get-pullreq)))
(setq header-line-format
(propertize
(format "#%s: %s" (oref pr number) (oref pr title))
'font-lock-face
'magit-section-heading))))
(defun code-review-section-insert-title ()
"Insert the title of the header buffer."
(when-let (title (code-review-db--pullreq-title))
(magit-insert-section (code-review-title-section title)
(insert (format "%-17s" "Title: ") title)
(insert ?\n))))
(defclass code-review-state-section (magit-section)
((state :initform nil
:type (or null string))))
(defun code-review-section-insert-state ()
"Insert the state of the header buffer."
(when-let (state (code-review-db--pullreq-state))
(let ((value (if state state "none")))
(magit-insert-section (code-review-state-section value)
(insert (format "%-17s" "State: ") value)
(insert ?\n)))))
(defclass code-review-ref-section (magit-section)
((base :initarg :base
:type (or null string))
(head :initarg :head
:type (or null string))))
(defun code-review-section-insert-ref ()
"Insert the state of the header buffer."
(let* ((pr (code-review-db-get-pullreq))
(obj (code-review-ref-section
:base (oref pr base-ref-name)
:head (oref pr head-ref-name))))
(magit-insert-section (code-review-ref-section obj)
(insert (format "%-17s" "Refs: "))
(insert (oref pr base-ref-name))
(insert (propertize " ... " 'font-lock-face 'magit-dimmed))
(insert (oref pr head-ref-name))
(insert ?\n))))
(defclass code-review-milestone-section (magit-section)
((keymap :initform 'code-review-milestone-section-map)
(title :initarg :title)
(perc :initarg :perc)
(number :initarg :number
:type number)))
(defvar code-review-milestone-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-set-milestone)
map)
"Keymaps for milestone section.")
(defun code-review-section-insert-milestone ()
"Insert the milestone of the header buffer."
(let ((milestones (code-review-db--pullreq-milestones)))
(let-alist milestones
(let* ((title (when (not (string-empty-p .title)) .title))
(obj (code-review-milestone-section :title title :perc .perc)))
(magit-insert-section (code-review-milestone-section obj)
(insert (format "%-17s" "Milestone: "))
(insert (propertize (code-review-pretty-milestone obj) 'font-lock-face 'magit-dimmed))
(insert ?\n))))))
(defclass code-review-labels-section (magit-section)
((keymap :initform 'code-review-labels-section-map)
(labels :initarg :labels)))
(defvar code-review-labels-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-set-label)
map)
"Keymaps for code-comment sections.")
(defun code-review-section-insert-labels ()
"Insert the labels of the header buffer."
(let* ((infos (code-review-db--pullreq-raw-infos))
(labels (code-review--distinct-labels
(append (code-review-db--pullreq-labels)
(a-get-in infos (list 'labels 'nodes)))))
(obj (code-review-labels-section :labels labels)))
(magit-insert-section (code-review-labels-section obj)
(insert (format "%-17s" "Labels: "))
(if labels
(dolist (label labels)
(insert (a-get label 'name))
(let* ((raw-color (a-get label 'color))
(color (if (string-prefix-p "#" raw-color)
raw-color
(concat "#" raw-color)))
(background (code-review-utils--sanitize-color color))
(foreground (code-review-utils--contrast-color color))
(o (make-overlay (- (point) (length (a-get label 'name))) (point))))
(overlay-put o 'priority 2)
(overlay-put o 'evaporate t)
(overlay-put o 'font-lock-face
`((:background ,background)
(:foreground ,foreground)
forge-topic-label)))
(insert " "))
(insert (propertize "None yet" 'font-lock-face 'magit-dimmed)))
(insert ?\n))))
(defclass code-review-assignees-section (magit-section)
((keymap :initform 'code-review-assignees-section-map)
(assignees :initarg :assignees)))
(defvar code-review-assignees-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-set-assignee)
(define-key map [mouse-2] 'code-review-set-assignee)
(define-key map [follow-link] 'code-review-set-assignee)
map)
"Keymaps for code-comment sections.")
(defclass code-review-assignee-section (magit-section)
((keymap :initform 'code-review-assignee-section-map)
(name :initarg :name)
(url :initarg :url)))
(defun code-review-assignee-visit-at-remote (&rest _)
(interactive)
(with-slots (value) (magit-current-section)
(let ((url (oref value url)))
(if url
(browse-url url)
(message "Can't visit the user in remote. Missing profile URL.")))))
(defvar code-review-assignee-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-assignee-visit-at-remote)
(define-key map [mouse-2] 'code-review-assignee-visit-at-remote)
(define-key map [follow-link] 'code-review-assignee-visit-at-remote)
map)
"Keymaps for assignee section")
(defun code-review-section-insert-assignee ()
"Insert the assignee of the header buffer."
(let* ((infos (code-review-db--pullreq-assignees))
(assignee-names (-map
(lambda (a)
(let ((name
(if (a-get a 'name)
(format "%s (@%s)"
(a-get a 'name)
(a-get a 'login))
(format "@%s" (a-get a 'login)))))
`((name . ,name)
(url . ,(a-get a 'url)))))
infos)))
(magit-insert-section (code-review-assignees-section)
(insert (format "%-17s" "Assignees: "))
(if (not assignee-names)
(insert (propertize "No one — Assign yourself"
'font-lock-face 'code-review-dimmed
'mouse-face 'highlight
'help-echo "Set new assignee"
'keymap 'code-review-assignees-section-map))
(progn
(insert (propertize "Set new assignee"
'font-lock-face 'code-review-dimmed
'mouse-face 'highlight
'help-echo "Set new assignee"
'keymap 'code-review-assignees-section-map))
(insert ?\n)
(dolist (assignee assignee-names)
(let-alist assignee
(let ((assignee-obj (code-review-assignee-section
:name .name
:url .url)))
(magit-insert-section (code-review-assignee-section assignee-obj)
(insert (propertize .name
'face 'code-review-author-header-face
'mouse-face 'highlight
'help-echo "Visit author's page"
'keymap 'code-review-assignee-section-map))))))
(insert ?\n)))
(insert ?\n))))
(defclass code-review-project-section (magit-section)
((name :initarg :name)))
(defun code-review-section-insert-project ()
"Insert the project of the header buffer."
(let-alist (code-review-db--pullreq-raw-infos)
(let* ((project-names (-map
(lambda (p)
(a-get-in p (list 'project 'name)))
.projectCards.nodes))
(projects (if project-names
(string-join project-names ", ")
(propertize "None yet" 'font-lock-face 'magit-dimmed))))
(magit-insert-section (code-review-project-section projects)
(insert (format "%-17s" "Projects: ") projects)
(insert ?\n)))))
(defclass code-review-is-draft-section (magit-section)
((draft? :initform nil
:type (or null string))))
(defun code-review-section-insert-is-draft ()
"Insert the isDraft value of the header buffer."
(let-alist (code-review-db--pullreq-raw-infos)
(let* ((draft? (if .isDraft "true" "false")))
(magit-insert-section (code-review-is-draft-section draft?)
(insert (format "%-17s" "Draft: ") draft?)
(insert ?\n)))))
(defclass code-review-suggested-reviewers-section (magit-section)
((keymap :initform 'code-review-suggested-reviewers-section-map)))
(defvar code-review-suggested-reviewers-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-request-review-at-point)
(define-key map [mouse-2] 'code-review-request-review-at-point)
(define-key map [follow-link] 'code-review-request-review-at-point)
map)
"Keymaps for suggested reviewers section.")
(defun code-review-section-insert-suggested-reviewers ()
"Insert the suggested reviewers."
(let ((infos (code-review-db--pullreq-raw-infos)))
(let-alist infos
(let* ((reviewers-group (code-review-utils--fmt-reviewers infos))
(reviewers (->> .suggestedReviewers
(-map
(lambda (r)
(a-get-in r (list 'reviewer 'login))))
(-filter
(lambda (r)
(let* ((res nil))
(maphash
(lambda (_status users)
(setq res (append res
(-map
(lambda (it)
(a-get it 'login))
users))))
reviewers-group)
(and (not (equal r nil))
(not (-contains-p res r))))))))
(suggested-reviewers (if (not reviewers)
(propertize "No suggestions" 'font-lock-face 'magit-dimmed)
reviewers)))
(magit-insert-section (code-review-suggested-reviewers-section suggested-reviewers)
(insert "Suggested-Reviewers:")
(if (not reviewers)
(insert " " suggested-reviewers)
(dolist (sr suggested-reviewers)
(insert ?\n)
(insert (propertize "Request Review"
'face 'code-review-request-review-face
'mouse-face 'highlight
'help-echo "Request review from reviewe"
'keymap 'code-review-suggested-reviewers-section-map))
(insert " - ")
(insert (propertize (concat "@" sr) 'face 'code-review-author-face))))
(insert ?\n))))))
(defclass code-review-reviewers-section (magit-section)
(()))
(defclass code-review-reviewer-section (magit-section)
((keymap :initform 'code-review-reviewer-section-map)
(login :initarg :login)
(url :initarg :url)))
(defvar code-review-reviewer-section-map
(let ((map (make-sparse-keymap)))
(define-key map [mouse-2] 'code-review-reviewer-visit-at-remote)
(define-key map [follow-link] 'code-review-reviewer-visit-at-remote)
map)
"Keymaps for reviewer section.")
(defun code-review-reviewer-visit-at-remote (&rest _)
(interactive)
(with-slots (value) (magit-current-section)
(let ((url (oref value url)))
(if url
(browse-url url)
(message "Can't visit the user in remote. Missing profile URL.")))))
(defun code-review-section-insert-reviewers ()
"Insert the reviewers section."
(let* ((infos (code-review-db--pullreq-raw-infos))
(groups (code-review-utils--fmt-reviewers infos)))
(magit-insert-section (code-review-reviewers-section)
(insert "Reviewers:\n")
(maphash (lambda (status users-objs)
(dolist (user-o users-objs)
(let-alist user-o
(let ((obj (code-review-reviewer-section
:login .login
:url .url)))
(magit-insert-section (code-review-reviewer-section obj)
(insert (code-review--propertize-keyword status))
(insert " - ")
(insert (propertize (concat "@" .login)
'face 'code-review-author-face
'mouse-face 'highlight
'help-echo "Visit user profile"
'keymap 'code-review-reviewer-section-map))
(when .code-owner?
(insert " as CODE OWNER"))
(when .at
(insert " " (propertize (code-review-utils--format-timestamp .at) 'face 'code-review-timestamp-face))))
(insert ?\n)))))
groups))))
;; headers hook definition
(defun code-review-section-insert-headers ()
"Insert all the headers."
(magit-insert-headers 'code-review-headers-hook))
;; commits
(defclass code-review-commits-header-section (magit-section)
(()))
(defclass code-review-commit-section (magit-section)
((keymap :initform 'code-review-commit-section-map)
(sha :initarg :sha)
(msg :initarg :msg)))
(defvar code-review-commit-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-commit-at-point)
map)
"Keymaps for commit section.")
(defclass code-review-commit-check-detail-section (magit-section)
((keymap :initform 'code-review-commit-check-detail-section-map)
(details :initarg :details)
(check :initarg :check)))
(defvar code-review-commit-check-detail-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-commit-goto-check-at-remote)
(define-key map [mouse-2] 'code-review-commit-goto-check-at-remote)
(define-key map [follow-link] 'code-review-commit-goto-check-at-remote)
map)
"Keymaps for commit check section.")
(defun code-review-section-insert-commits ()
"Insert commits from PULL-REQUEST."
(let ((pr (code-review-db-get-pullreq)))
(let-alist (oref pr raw-infos)
(magit-insert-section (code-review-commits-header-section)
(insert (propertize "Commits:" 'font-lock-face 'magit-section-heading))
(magit-insert-heading)
(dolist (c .commits.nodes)
(let-alist c
(let* ((sha (a-get-in c (list 'commit 'abbreviatedOid)))
(msg (a-get-in c (list 'commit 'message)))
(obj (code-review-commit-section :sha sha :msg msg)))
(magit-insert-section commit-section (code-review-commit-section obj)
(if (and (code-review-github-repo-p pr) .commit.statusCheckRollup.contexts.nodes)
(progn
(insert (format "%s%s %s "
(propertize (format "%-6s " (oref obj sha)) 'font-lock-face 'magit-hash)
(car (split-string (oref obj msg) "\n"))
(if (string-equal .commit.statusCheckRollup.state "SUCCESS")
":white_check_mark:"
":x:")))
(insert
(propertize "Expand for Details:" 'font-lock-face 'code-review-checker-detail-face))
(oset commit-section hidden t)
(magit-insert-heading)
(when (> (length (split-string (oref obj msg) "\n")) 1)
(insert (oref obj msg))
(insert "\n"))
(dolist (check .commit.statusCheckRollup.contexts.nodes)
(let-alist check
(let ((obj (code-review-commit-check-detail-section :check check :details .detailsUrl)))
(magit-insert-section (code-review-commit-check-detail-section obj)
(if (string-equal .conclusion "SUCCESS")
(progn
(insert (propertize (format "%-7s %s / %s" "" .checkSuite.workflowRun.workflow.name .name)
'font-lock-face 'code-review-checker-name-face))
(insert " - ")
(insert (propertize (format "%s " (format "Successful in %s."
(code-review-utils--elapsed-time .completedAt .startedAt)))
'font-lock-face 'magit-dimmed))
(insert (propertize ":white_check_mark: Details"
'font-lock-face 'code-review-checker-detail-face
'mouse-face 'highlight
'help-echo "Visit the page for details"
'keymap 'code-review-commit-check-detail-section-map)))
(progn
(insert (propertize (format "%-7s %s / %s" "" .checkSuite.workflowRun.workflow.name .title)
'font-lock-face 'code-review-checker-name-face))
(insert " - ")
(insert (propertize (format "%s " .summary)
'font-lock-face 'magit-dimmed))
(insert (propertize ":x: Details"
'font-lock-face 'code-review-checker-detail-face
'mouse-face 'highlight
'help-echo "Visit the page for details"
'keymap 'code-review-commit-check-detail-section-map))))))
(insert "\n"))))
(progn
(insert (propertize (format "%-6s " (oref obj sha)) 'font-lock-face 'magit-hash))
(insert (oref obj msg))
(insert ?\n)))))))
(insert ?\n)))))
;; description
(defclass code-review-description-section (magit-section)
((keymap :initform 'code-review-description-section-map)
(id :initarg :id)
(msg :initarg :msg)
(reactions :initarg :reactions)))
(defvar code-review-description-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-r") 'code-review-description-reaction-at-point)
map)
"Keymaps for description section.")
(defun code-review-section-insert-pr-description ()
"Insert PULL-REQUEST description."
(when-let (infos (code-review-db--pullreq-raw-infos))
(let-alist infos
(let* ((is-html? (when .bodyHTML t))
(is-empty? (and (string-empty-p .bodyHTML)
(string-empty-p .bodyText)))
(description-cleaned (if is-empty?
"No description provided."
(or .bodyHTML .bodyText)))
(reaction-objs (-map
(lambda (r)
(code-review-reaction-section
:id (a-get r 'id)
:content (a-get r 'content)))
.reactions.nodes))
(obj (code-review-description-section :msg description-cleaned
:id .databaseId
:reactions reaction-objs)))
(magit-insert-section (code-review-description-section obj)
(insert (propertize "Description" 'font-lock-face 'magit-section-heading))
(magit-insert-heading)
(insert ?\n)
(magit-insert-section (code-review-description-section obj)
(if is-empty?
(insert (propertize description-cleaned 'font-lock-face 'magit-dimmed))
(if is-html?
(code-review--insert-html description-cleaned (* 2 code-review-section-indent-width))
(insert description-cleaned)))
(insert ?\n)
(when .reactions.nodes
(code-review-comment-insert-reactions
reaction-objs
"pr-description"
.databaseId))
(insert ?\n)))))))
;; feedback
(defclass code-review-feedback-section (magit-section)
((keymap :initform 'code-review-feedback-section-map)
(msg :initarg :msg)))
(defvar code-review-feedback-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-set-feedback)
(define-key map (kbd "C-c C-k") 'code-review-delete-feedback)
map)
"Keymaps for feedback section.")
(defun code-review-section-insert-feedback-heading ()
"Insert feedback heading."
(let* ((feedback (code-review-db--pullreq-feedback))
(obj (code-review-feedback-section :msg feedback)))
(magit-insert-section (code-review-feedback-section obj)
(insert (propertize "Your Review Feedback" 'font-lock-face 'magit-section-heading))
(magit-insert-heading)
(magit-insert-section (code-review-feedback-section obj)
(if feedback
(insert feedback)
(insert (propertize "Leave a comment here." 'font-lock-face 'magit-dimmed))))
(insert ?\n)
(insert ?\n))))
;;; general comments - top level comments
(defclass code-review-comment-header-section (magit-section)
(()))
(defclass code-review-comment-section (magit-section)
((keymap :initform 'code-review-comment-section-map)
(author :initarg :author
:type string)
(msg :initarg :msg
:type string)
(id :initarg :id)
(reactions :initarg :reactions)
(typename :initarg :typename)
(face :initform 'magit-log-author)))
(defvar code-review-comment-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-r") 'code-review-conversation-reaction-at-point)
(define-key map (kbd "C-c C-n") 'code-review-promote-comment-at-point-to-new-issue)
map)
"Keymaps for comment section.")
(cl-defmethod code-review--insert-conversation-section ((github code-review-github-repo))
"Function to insert conversation section for GITHUB PRs."
(let-alist (oref github raw-infos)
(let ((list-of-comments (->> .reviews.nodes
(-filter
(lambda (n)
(not (string-empty-p (a-get n 'bodyHTML)))))
(append .comments.nodes)
(--sort
(< (time-to-seconds (date-to-time (a-get it 'createdAt)))
(time-to-seconds (date-to-time (a-get other 'createdAt))))))))
(when (not list-of-comments)
(insert (propertize "No conversation found." 'font-lock-face 'magit-dimmed))
(insert ?\n))
(dolist (c list-of-comments)
(let* ((reactions (a-get-in c (list 'reactions 'nodes)))
(reaction-objs (when reactions
(-map
(lambda (r)
(code-review-reaction-section
:id (a-get r 'id)
:content (a-get r 'content)))
reactions)))
(obj (code-review-comment-section
:author (a-get-in c (list 'author 'login))
:msg (a-get c 'bodyHTML)
:id (a-get c 'databaseId)
:typename (a-get c 'typename)
:reactions reaction-objs)))
(magit-insert-section (code-review-comment-section obj)
(insert (concat
(propertize (format "@%s" (oref obj author)) 'font-lock-face (oref obj face))
" - "
(propertize (code-review-utils--format-timestamp (a-get c 'createdAt)) 'face 'code-review-timestamp-face)))
(magit-insert-heading)
(insert ?\n)
(code-review-insert-comment-lines obj)
(when reactions
(code-review-comment-insert-reactions
reaction-objs
"comment"
(a-get c 'databaseId)))
(insert ?\n))))
(insert ?\n))))
(cl-defmethod code-review--insert-conversation-section ((gitlab code-review-gitlab-repo))
"Function to insert conversation section for GITLAB PRs."
(let-alist (oref gitlab raw-infos)
(let ((thread-groups (-group-by
(lambda (it)
(a-get-in it (list 'discussion 'id)))
.comments.nodes)))
(dolist (g (a-keys thread-groups))
(magit-insert-section (code-review-comment-thread-section)
(insert (propertize "New Thread" 'font-lock-face 'code-review-thread-face))
(magit-insert-heading)
(let ((thread-comments (alist-get g thread-groups nil nil 'equal)))
(dolist (c thread-comments)
(let* ((obj (code-review-comment-section
:author (a-get-in c (list 'author 'login))
:msg (a-get c 'bodyHTML)
:id (a-get c 'databaseId))))
(magit-insert-section (code-review-comment-section obj)
(insert (concat
(propertize (format "@%s" (oref obj author)) 'font-lock-face (oref obj face))
" - "
(propertize (code-review-utils--format-timestamp (a-get c 'createdAt)) 'face 'code-review-timestamp-face)))
(magit-insert-heading)
(code-review-insert-comment-lines obj)
(insert ?\n))))))))))
(cl-defmethod code-review--insert-conversation-section ((bitbucket code-review-bitbucket-repo))
(let-alist (oref bitbucket raw-infos)
(let ((list-of-comments (--sort
(< (time-to-seconds (date-to-time (a-get it 'createdAt)))
(time-to-seconds (date-to-time (a-get other 'createdAt))))
.comments.nodes)))
(dolist (c list-of-comments)
(let* ((obj (code-review-comment-section
:author (a-get-in c (list 'author 'login))
:msg (a-get c 'bodyHTML)
:id (a-get c 'databaseId)
:typename (a-get c 'typename))))
(magit-insert-section (code-review-comment-section obj)
(insert (concat
(propertize (format "@%s" (oref obj author)) 'font-lock-face (oref obj face))
" - "
(propertize (code-review-utils--format-timestamp (a-get c 'createdAt)) 'face 'code-review-timestamp-face)))
(magit-insert-heading)
(insert ?\n)
(code-review-insert-comment-lines obj)
(insert ?\n)))))))
(defun code-review-section-insert-top-level-comments ()
"Insert general comments for the PULL-REQUEST in the buffer."
(when-let (pr (code-review-db-get-pullreq))
(magit-insert-section (code-review-comment-header-section)
(insert (propertize "Conversation" 'font-lock-face 'magit-section-heading))
(magit-insert-heading)
(when code-review-section--display-top-level-comments
(code-review--insert-conversation-section pr)))))
;; files report
(defclass code-review-files-report-section (magit-section)
(()))
;; -
(defclass code-review-base-comment-section (magit-section)
((state :initarg :state
:type string)
(author :initarg :author
:type string)
(msg :initarg :msg
:type string)
(position :initarg :position
:type number)
(reactions :initarg :reactions
:type (or null
(satisfies
(lambda (it)
(-all-p #'code-review-reaction-section-p it)))))
(path :initarg :path
:type string)
(diffHunk :initarg :diffHunk
:type (or null string))
(id :initarg :id
:documentation "ID that identifies the comment in the Forge.")
(internalId :initarg :internalId)
(amount-loc :initform nil)
(outdated? :initform nil
:type boolean)
(reply? :initform nil
:type boolean)
(local? :initform nil
:type boolean)
(createdAt :initarg :createdAt)
(updatedAt :initarg :updatedAt)))
(defclass code-review-reaction-section ()
((id :initarg :id)
(content :initarg :content)))
(defclass code-review-reactions-section (magit-section)
((context-name :initarg :context-name)
(comment-id :initarg :comment-id)
(reactions :initarg :reactions
:type (satisfies
(lambda (it)
(-all-p #'code-review-reaction-section-p it))))))
(defclass code-review-code-comment-section (code-review-base-comment-section)
((keymap :initform 'code-review-code-comment-section-map)
(diffHunk :initarg :diffHunk)
(id :initarg :id
:documentation "ID that identifies the comment in the Forge.")
(amount-loc :initform nil)
(outdated? :initform nil
:type boolean)
(reply? :initform nil
:type boolean)
(local? :initform nil
:type boolean)))
(defclass code-review-local-comment-section (code-review-base-comment-section)
((keymap :initform 'code-review-local-comment-section-map)
(local? :initform t)
(reply? :initform nil)
(edit? :initform nil)
(send? :initarg :send?)
(outdated? :initform nil)
(heading-face :initform 'code-review-recent-comment-heading)
(body-face :initform nil)
(diffHunk :initform nil)
(line-type :initarg :line-type)))
(defclass code-review-reply-comment-section (code-review-base-comment-section)
((keymap :initform 'code-review-reply-comment-section-map)
(reply? :initform t)
(local? :initform t)
(edit? :initform nil)
(outdated? :initform nil)
(heading-face :initform 'code-review-recent-comment-heading)
(body-face :initform nil)))
(defclass code-review-outdated-comment-section (code-review-base-comment-section)
((keymap :initform 'code-review-outdated-comment-section-map)
(local? :initform t)
(outdated? :initform t)))
(defclass code-review-check-section (magit-section)
((details :initarg :details)))
(defclass code-review-binary-file-section (magit-section)
((keymap :initform 'code-review-binary-file-section-map)))
(defvar code-review-code-comment-section-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'code-review-comment-add-or-edit)
(define-key map (kbd "C-c C-r") 'code-review-code-comment-reaction-at-point)
(define-key map (kbd "C-c C-n") 'code-review-promote-comment-at-point-to-new-issue)
map)
"Keymaps for code-comment sections.")