~niklaseklund/egerrit

ref: 8f9f0dc298c2a4252dfbb584dde026e209b076fb egerrit/egerrit-info.el -rw-r--r-- 7.7 KiB
8f9f0dc2Niklas Eklund Update author-open-job 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
;;; 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)

;;;; Functions

(defun egerrit--info-str (change revision)
  "Generate an info string about CHANGE's REVISION."
  (let ((info-heading "Change info")
        (requirements-heading "Submit requirements")
        (relation-heading "Relation chain")
        (has-merge-conflicts
         (not (egerrit-get-mergeable change)))
        (related-changes
         (egerrit-get-related-changes change revision))
        (content))

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

    (setq content
          `(,(format "%s\n\n" info-heading)
            ,(when has-merge-conflicts
               (let ((str "Merge Conflict"))
                 (put-text-property 0 (length str) 'face 'egerrit-inv-error-face str)
                 (format "%s\n" 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)
            ,(format "\n%s\n\n" requirements-heading)
            ,(format "%s" (egerrit--change-requirements-str change))
            ,(when related-changes
               (format "\n%s\n\n%s\n" relation-heading (string-join related-changes "\n")))))

    (string-join (seq-remove #'null content) "")))

(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)))
    (seq-map (lambda (change)
               (let-alist change .commit.subject))
             (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