~niklaseklund/egerrit

ref: 8f9f0dc298c2a4252dfbb584dde026e209b076fb egerrit/egerrit-job.el -rw-r--r-- 10.7 KiB
8f9f0dc2Niklas Eklund Update author-open-job 5 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
;;; egerrit-job.el --- Job 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 job for `egerrit'.

;;; Code:

;;;; Requirements

(require 'ansi-color)
(require 'comint)
(require 'egerrit-datastructures)
(require 'egerrit-core)
(require 'egerrit-git)
(require 'egerrit-request)
(require 'subr-x)

(declare-function egerrit-url "egerrit")
(declare-function egerrit-download-change "egerrit")
(declare-function egerrit-yank-url "egerrit")
(declare-function egerrit-get-detailed-change "egerrit")
(declare-function egerrit-select-revision "egerrit")

(defvar egerrit--current-dashboard)
(defvar egerrit--current-change)
(defvar egerrit--current-revision)
(defvar egerrit--current-git-branch)
(defvar egerrit--mode)
(defvar egerrit--switch-branch)

;;;; Variables

(defcustom egerrit-job-config
  `((:name "Name" :function egerrit--job-name :length 70)
    (:name "Instance" :function egerrit--job-instance-str :length 3)
    (:name "Status" :function egerrit--job-status-str :length 30)
    (:name "Time" :function egerrit--duration-str :length 30)
    (:name "Date" :function egerrit--updated-str :length 30))
  "This alist describes the job layout.
It describes what
to show and which functions to call to get the right data."
  :type '(repeat symbol)
  :group 'egerrit)

(defcustom egerrit-job-regexp nil
  "Function that return a regexp to capture jobs."
  :type 'function
  :group 'egerrit)

(defcustom egerrit-job-function #'egerrit--add-change-jobs
  "A function that takes a change and updates it with CI jobs."
  :type 'function
  :group 'egerrit)

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

;;;;; Private

(defvar egerrit--current-job nil
  "Variable that contain an `egerrit-job'.")
(defvar egerrit--current-jobs nil
  "Variable that contain a list of `egerrit-job'.")

;;;; Functions

(defun egerrit-job-content (job)
  "Return content of JOB."
  (let* ((url (egerrit-url job))
         (egerrit--request-curl-options '("--silent" "--insecure")))
    (egerrit-request
     url
     :type "GET"
     :header '(("Content-Type" . "application/json"))
     :parser (lambda () (buffer-string)))))

(defun egerrit-select-job (jobs)
  "Select a job from list of JOBS."
  (let* ((candidates
          (thread-last jobs
                       (egerrit--sort-jobs)
                       (seq-map
                        (lambda (it)
                          (egerrit--completing-read-annotator it)))))
         (metadata `(metadata
                     (category . egerrit-job)
                     (cycle-sort-function . identity)
                     (display-sort-function . identity)))
         (collection (lambda (string predicate action)
                       (if (eq action 'metadata)
                           metadata
                         (complete-with-action action candidates string predicate))))
         (cand (completing-read "Select job: " collection nil t)))
    (cdr (assoc cand candidates))))

;;;; Commands

;;;###autoload
(defun egerrit-quit-job ()
  "Quit job."
  (interactive)
  (egerrit-switch-to-previous-git-branch)
  (delete-other-windows)
  (switch-to-buffer egerrit--current-dashboard))

;;;###autoload
(defun egerrit-switch-to-other-job ()
  "Switch to another job from the current change and revision."
  (interactive)
  (let* ((egerrit--switch-branch nil)
         (change egerrit--current-change)
         (revision egerrit--current-revision)
         (jobs egerrit--current-jobs)
         (job (egerrit-select-job jobs))
         (buffer (egerrit-get-buffer "egerrit-job"))
         (git-branch egerrit--current-git-branch))
    (with-current-buffer buffer
      (egerrit--open-job job buffer)
      (egerrit--update-git-branch `(,buffer) git-branch)
      (setq egerrit--current-jobs jobs)
      (setq egerrit--current-change change)
      (setq egerrit--current-revision revision))))

;;;###autoload
(defun egerrit-author-open-job (change)
  "Open one of CHANGE's jobs."
  (interactive
   (list (egerrit-get-detailed-change
          (tabulated-list-get-id))))
  (let ((egerrit--switch-branch nil)
        (egerrit--mode 'author))
    (egerrit-open-job change)))

;;;###autoload
(defun egerrit-open-job (change &optional revision)
  "Open one of CHANGE's jobs.

Optionally provide a REVISION to select job from."
  (interactive)
  (let ((change (or change
                    (egerrit-get-detailed-change
                     (tabulated-list-get-id))))
        (mode egerrit--mode))
    (if (and (eq egerrit--mode 'reviewer)
             (not (egerrit--clean-git-repository change)))
        (message "The git repository can't be dirty when engaging in a review")
      (when-let* ((egerrit--current-change change)
                  (project-root (egerrit-get-project-root egerrit--current-change))
                  (default-directory project-root)
                  (revision
                   (or revision
                       (if current-prefix-arg
                           (egerrit-select-revision egerrit--current-change)
                         (seq-first (seq-reverse (egerrit--change-revisions egerrit--current-change))))))
                  (buffer (egerrit-get-buffer "egerrit-job"))
                  (jobs (egerrit--revision-jobs revision))
                  (job (egerrit-select-job jobs)))
        (with-current-buffer buffer
          (egerrit--open-job job buffer)
          (egerrit--switch-branch change revision)
          (setq egerrit--current-jobs jobs)
          (setq egerrit--current-change change)
          (setq egerrit--current-revision revision)
          (setq egerrit--current-dashboard (egerrit-get-buffer "egerrit-dashboard"))
          (setq egerrit--mode mode)
          (setq-local default-directory project-root))))))

;;;; Support functions

(defun egerrit--add-change-jobs (change)
  "Add jobs to CHANGE's revisions."
  (let* ((revisions (egerrit--change-revisions change))
         (messages (egerrit--change-messages change))
         (regexp (funcall egerrit-job-regexp change))
         (jobs (thread-last messages
                            (seq-map (lambda (it) (egerrit--create-jobs it regexp)))
                            (seq-remove #'null)
                            (egerrit--add-instance))))
    (setf (egerrit--change-revisions change)
          (thread-last revisions
                       (seq-map (lambda (revision)
                                  (setf
                                   (egerrit--revision-jobs revision)
                                   (seq-filter (lambda (job)
                                                 (equal (egerrit--revision-number revision)
                                                        (egerrit--job-revision job)))
                                               jobs))
                                  revision))))
    change))

(defun egerrit--add-instance (jobs)
  "Add instance to all JOBS."
  ;; Jobs are grouped by the message they are extracted from
  (thread-last jobs
               ;; Group all messages by the revision they belong to
               (seq-group-by (lambda (it)
                               (egerrit--job-revision (car it))))
               ;; Iterate through all the revisions
               (seq-map (lambda (it)
                          ;; For each message add the instance number
                          ;; which corresponds to the index
                          (seq-map-indexed (lambda (elt index)
                                             (seq-map (lambda (job)
                                                        (setf (egerrit--job-instance job) index)
                                                        job)
                                                      elt))
                                           (cdr it))))
               ;; Flatten the list and remove the groupings
               (flatten-list)))

(defun egerrit--create-jobs (message regexp)
  "Return a list of `egerrit-job' objects matching REGEXP in MESSAGE."
  (let ((revision (egerrit--message-number message))
        (lines (split-string (egerrit--message-message message) "\n" t)))
    (thread-last lines
                 (seq-map (lambda (line)
                            (when (string-match regexp line)
                              (egerrit--job-create
                               :name (match-string 1 line)
                               :url (match-string 2 line)
                               :status (downcase (match-string 3 line))
                               :time (match-string 4 line)
                               :revision revision
                               :date (egerrit--compensate-time-with-timezone (egerrit--message-date message))))))
                 (seq-remove #'null))))

(defun egerrit--sort-jobs (jobs)
  "Sort JOBS."
  (thread-last (car jobs)
               (seq-sort-by #'egerrit--job-duration #'>)
               (seq-sort-by #'egerrit--job-priority #'>)
               (seq-sort-by #'egerrit--job-instance-priority #'>)))

(defun egerrit--open-job (job buffer)
  "Open a JOB in BUFFER."
  (with-current-buffer buffer
    (let ((inhibit-read-only t))
      (erase-buffer)
      (insert (egerrit-job-content job))
      (ansi-color-apply-on-region (point-min) (point-max))
      (comint-carriage-motion (point-min) (point-max)))
    (egerrit-job-mode)
    (setq-local next-error-find-buffer-function
                (lambda (_avoid-current
                         _extra-test-inclusive
                         _extra-test-exclusive)
                  buffer))
    (setq egerrit--current-job job)
    (run-hooks 'egerrit-job-hooks)
    (compilation-minor-mode)
    (setq-local font-lock-defaults '(compilation-mode-font-lock-keywords t))
    (read-only-mode)
    (font-lock-mode)
    (pop-to-buffer-same-window buffer)))

;;;; Major mode

(define-derived-mode egerrit-job-mode fundamental-mode "Egerrit Job"
  "Mode for `egerrit' job.")

(let ((map egerrit-job-mode-map))
  (define-key map (kbd "C-c C-y") #'egerrit-yank-url)
  (define-key map (kbd "C-c C-q") #'egerrit-quit-job)
  (define-key map (kbd "C-c C-j") #'egerrit-switch-to-other-job))

(provide 'egerrit-job)

;;; egerrit-job.el ends here