~niklaseklund/egerrit

ref: d608c5f60f5409ef4e7a63670593a8790816006a egerrit/egerrit-info.el -rw-r--r-- 8.3 KiB
d608c5f6Niklas Eklund Introduce a config for info buffer 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
;;; egerrit-info.el --- Info 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 info for `egerrit'.

;;; Code:

;;;; Requirements

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

(declare-function egerrit--subject-str "egerrit")
(declare-function egerrit--owner-str "egerrit")
(declare-function egerrit--change-reviewers "egerrit")
(declare-function egerrit-get-detailed-change "egerrit")
(declare-function egerrit-select-revision "egerrit")

(defvar egerrit--current-change)

;;;; Variables

(defcustom egerrit-info-config
  '(egerrit--change-info-section
    egerrit--requirement-section
    egerrit--relation-section)
  "This list describes section functions for the info buffer."
  :type '(repeat symbol)
  :group 'egerrit)

;;;; Functions

(defun egerrit--change-info-section (change _revision)
  "Return change info section for CHANGE."
  (let ((heading "Change info")
        (has-merge-conflicts
         (not (egerrit-get-mergeable change)))
        (merge-conflict-str "Merge Conflict"))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)
    (put-text-property 0 (length merge-conflict-str) 'face 'egerrit-inv-error-face merge-conflict-str)

    `(,(format "%s\n\n" heading)
      ,(when has-merge-conflicts
         (format "%s\n" merge-conflict-str))
      ,(egerrit--info-entry-str "Subject" (egerrit--subject-str change))
      ,(egerrit--info-entry-str "Owner" (egerrit--owner-str change))
      ,(egerrit--info-entry-str "Reviewers"
                                (string-join (seq-map
                                              (lambda (it)
                                                (alist-get 'name it))
                                              (alist-get 'REVIEWER (egerrit--change-reviewers change)))
                                             ", ")
                                'egerrit-unknown-face)
      ,(egerrit--info-entry-str "CC"
                                (string-join (seq-map
                                              (lambda (it)
                                                (alist-get 'name it))
                                              (alist-get 'CC (egerrit--change-reviewers change)))
                                             ", ")
                                'egerrit-unknown-face))))

(defun egerrit--requirement-section (change _revision)
  "Return requirement section for CHANGE."
  (let ((heading "Submit requirements"))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)

    `(,(format "\n%s\n\n" heading)
      ,(format "%s\n" (egerrit--change-requirements-str change)))))

(defun egerrit--relation-section (change revision)
  "Return the relation section for CHANGE's REVISION."
  (let ((heading "Relation chain")
        (related-changes
         (egerrit-get-related-changes change revision)))

    (put-text-property 0 (length heading) 'face 'egerrit-heading-face heading)

    (when related-changes
      `(,(format "\n%s\n\n%s\n" heading (string-join related-changes "\n"))))))

(defun egerrit--info-str (change revision)
  "Generate an info string about CHANGE's REVISION."
  (string-join
   (thread-last egerrit-info-config
                (seq-map (lambda (it) (funcall it change revision)))
                (seq-remove #'null)
                (flatten-list))
   ""))

(defun egerrit-get-related-changes (change revision)
  "Get a list of related change(s) for CHANGE's REVISION."
  (let* ((url (egerrit-request-url
               (format "/changes/%s/revisions/%s/related"
                       (egerrit-change-id change)
                       (egerrit-revision-id revision))))
         (related-changes
          (egerrit-request
           url
           :type "GET"
           :header '(("Content-Type" . "application/json"))
           :parser #'egerrit-request-parser
           :auth t)))
    ;; TODO(Niklas Eklund, 20220620): Consider adding more information to related changes?
    ;; Author, change number?
    (seq-map (lambda (change)
               (let-alist change (format "%s (%s)".commit.subject .status)))
             (alist-get 'changes related-changes))))

(defun egerrit-get-mergeable (change)
  "Get a response from Gerrit if CHANGE is mergeable."
  (let* ((url (egerrit-request-url
               (format "/changes/%s/revisions/%s/mergeable"
                       (egerrit-change-id change)
                       (egerrit--change-current-revision change))))
         (mergeable
          (egerrit-request
           url
           :type "GET"
           :header '(("Content-Type" . "application/json"))
           :parser #'egerrit-request-parser
           :auth t)))
    (alist-get 'mergeable mergeable)))

;;;; Commands

;;;###autoload
(defun egerrit-open-info ()
  "Open info buffer about change."
  (interactive)
  (let ((change (pcase major-mode
                  ('egerrit-dashboard-mode (tabulated-list-get-id))
                  ('egerrit-diff-mode egerrit--current-change)
                  ('egerrit-conversation-mode egerrit--current-change)
                  ('egerrit-job-mode egerrit--current-change)
                  (_ nil))))
    (when (egerrit-change-p change)
      (let* ((change (egerrit-get-detailed-change change))
             (revision
              (if current-prefix-arg
                  (egerrit-select-revision change)
                (seq-first (seq-reverse (egerrit--change-revisions change)))))
             (buffer (egerrit-get-buffer "egerrit-info")))

        (with-current-buffer buffer
          (let ((inhibit-read-only t))
            (erase-buffer)
            (insert (egerrit--info-str change revision)))
          (goto-char (point-min))
          (read-only-mode)
          (pop-to-buffer (current-buffer)))))))

;;;; Support functions

(defun egerrit--info-entry-str (key value &optional value-face)
  "Return a string of KEY VALUE.

Optionally add VALUE-FACE to value."
  (let ((key-str (format "%s: " key)))
    (put-text-property 0 (length key-str) 'face 'egerrit-unknown-face key-str)
    (when value-face
      (put-text-property 0 (length value) 'face value-face value))
    (format "%s%s\n" key-str value)))

(defun egerrit--info-review-str (labels)
  "Return a review string from LABELS."
  (thread-last labels
               (seq-map (lambda (it)
                          (let-alist it
                            (unless (= (or .value 0) 0)
                              (let ((name-str .name)
                                    (value-str (format "(%s)" .value)))
                                (put-text-property 0 (length value-str)
                                                   'face (if (> .value 0) 'egerrit-inv-success-face 'egerrit-inv-error-face)
                                                   value-str)
                                (format "%s %s" name-str value-str))))))
               (seq-remove #'null)))

(defun egerrit--change-requirements-str (change)
  "Return a requirement string for CHANGE."
  (let ((code-reviews
         (egerrit--info-review-str
          (let-alist (egerrit--change-labels change)  .Code-Review.all)))
        (secondary-reviews (egerrit--info-review-str
                            (let-alist (egerrit--change-labels change)  .Secondary-Review.all)))
        (verified
         (egerrit--info-review-str
          (let-alist (egerrit--change-labels change)  .Verified.all))))
    (concat
     (egerrit--info-entry-str "Code-Review" (string-join code-reviews ", "))
     (egerrit--info-entry-str "Secondary-Review" (string-join secondary-reviews ", "))
     (egerrit--info-entry-str "Verified" (string-join verified ", ")))))

(provide 'egerrit-info)

;;; egerrit-info.el ends here