-
Notifications
You must be signed in to change notification settings - Fork 30
/
gitlab-mode.el
293 lines (239 loc) · 10.1 KB
/
gitlab-mode.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
;; gitlab-mode.el --- Mode for Gitlab
;; Copyright (C) 2014, 2015, 2016 Nicolas Lamirault <[email protected]>
;; 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 2
;; 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, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;; A major mode for Gitlab
;;; Code:
;;; Code:
(require 'browse-url)
(require 'tabulated-list)
(require 'vc-git)
;; Gitlab library
(require 'gitlab-projects)
(require 'gitlab-issues)
;; Core
(defun print-current-project-id ()
"Display current project id."
(interactive)
(message (concat "Current ID is: " (tabulated-list-get-id))))
(defun print-current-issue-id ()
"Display current issue id."
(interactive)
(message (format "Current ID is: %d" (assoc-default 'id (tabulated-list-get-id)))))
(defun project-make-button (text &rest props)
"Make button with TEXT propertized with PROPS."
(let ((button-text (if (display-graphic-p)
text
(concat "[" text "]")))
(button-face (if (display-graphic-p)
'(:box (:line-width 2 :color "dark grey")
:background "light grey"
:foreground "black")
'link)))
(apply 'insert-text-button button-text
'face button-face
'follow-link t
props)))
;; Projects
(defun gitlab-project-clone-button-action (button)
"Action for BUTTON."
(interactive)
(let* ((project (gitlab-get-project (button-get button 'project-id)))
(name (assoc-default 'path project))
(repo (assoc-default 'ssh_url_to_repo project))
(target-dir (read-directory-name "Clone to directory:" (car query-replace-defaults))))
(if (file-directory-p (expand-file-name name target-dir))
(progn
(message "Target directory exists and is not empty. Trying to pull.")
(let ((default-directory (file-name-as-directory (expand-file-name name target-dir))))
(vc-git-command nil 0 nil "pull" repo)))
(progn
(make-directory name target-dir)
(vc-git-command nil 0 nil "clone" repo (file-name-as-directory (expand-file-name name target-dir)))))
(revert-buffer nil t)
(goto-char (point-min))))
(defun gitlab-goto-project ()
"Got to web page of the project."
(interactive)
(let* ((project (gitlab-get-project (tabulated-list-get-id))))
(browse-url (assoc-default 'web_url project))))
;;;###autoload
(defun gitlab-show-project-description (project)
"Doc string PROJECT."
(interactive)
(with-help-window (help-buffer)
(with-current-buffer standard-output
(let ((desc (assoc-default 'description project))
(homepage (assoc-default 'web_url project))
(id (assoc-default 'id project))
(status (number-to-string (assoc-default 'visibility_level project))))
(insert " Name: ")
(princ (assoc-default 'name project))
(princ "\n")
(insert " Path: ")
(princ (assoc-default 'path_with_namespace project))
(princ "\n\n")
(insert " Repository: ")
(princ (assoc-default 'ssh_url_to_repo project))
(insert "\n\n")
(insert " " (propertize "Status" 'font-lock-face 'bold) ": ")
(cond ((string= status "0")
(insert (propertize (capitalize "Private") 'font-lock-faces 'font-lock-builtin-face)))
((string= status "10")
(insert (propertize (capitalize "Internal") 'font-lock-faces 'font-lock-builtin-face)))
((string= status "20")
(insert (propertize (capitalize "Public") 'font-lock-faces 'font-lock-builtin-face))))
(insert " -- ")
(project-make-button
"Clone to / Pull"
'action 'gitlab-project-clone-button-action
'project-id id)
(insert "\n\n")
(insert " " (propertize "Summary" 'font-lock-face 'bold)
": " (if desc desc) "\n")
(when homepage
(insert " " (propertize "Homepage" 'font-lock-face 'bold) ": ")
(help-insert-xref-button homepage 'help-url homepage)
(insert "\n"))))))
(defun gitlab-describe-project (&optional button)
"Describe the current project.
If optional arg BUTTON is non-nil, describe its associated project."
(interactive)
(let ((project (gitlab-get-project (tabulated-list-get-id))))
(if project
(gitlab-show-project-description project)
(user-error "No project here"))))
(defun gitlab-issues-for-project (&optional project-id)
"From projects buffer, opens issues buffer for project at point."
(interactive)
(let ((project (gitlab-get-project (or project-id (tabulated-list-get-id)))))
(if project
(progn
(pop-to-buffer "*Gitlab issues*" nil)
(gitlab-issues-mode)
(setq tabulated-list-entries
(create-issues-entries (gitlab-list-project-issues (assoc-default 'id project))))
(tabulated-list-print t)
(tabulated-list-sort 1))
(user-error "No project here"))))
(defun gitlab-show-projects ()
"Show Gitlab projects."
(interactive)
(pop-to-buffer "*Gitlab projects*" nil)
(gitlab-projects-mode)
(setq tabulated-list-entries
(create-projects-entries (gitlab-list-all-projects)))
(tabulated-list-print t))
(defun create-projects-entries (projects)
"Create entries for 'tabulated-list-entries from PROJECTS."
(mapcar (lambda (p)
(let ((id (number-to-string (assoc-default 'id p)))
(owner (if (assoc-default 'owner p)
(assoc-default 'owner p)
(assoc-default 'namespace p)))
(namespace (assoc-default 'namespace p))
(description (if (eq (assoc-default 'description p) nil)
""
(assoc-default 'description p))))
(list id
(vector ;id
(assoc-default 'name p)
(assoc-default 'name owner)
(assoc-default 'name namespace)
(replace-regexp-in-string "\^M\\|\n" " " description)))))
projects))
;; Issues
(defun gitlab-goto-issue ()
"Got to web page of the issue."
(interactive)
(browse-url (assoc-default 'web_url (tabulated-list-get-id))))
(defun gitlab-close-issue ()
"Close issue at point."
(interactive)
(let ((issue (tabulated-list-get-id)))
(gitlab-edit-issue (assoc-default 'project_id issue) (assoc-default 'id issue)
nil nil nil nil nil "close")
(gitlab-issues-for-project (assoc-default 'project_id issue))))
(defun gitlab-open-issue ()
"Reopen issue at point."
(interactive)
(let ((issue (tabulated-list-get-id)))
(gitlab-edit-issue (assoc-default 'project_id issue) (assoc-default 'id issue)
nil nil nil nil nil "reopen")
(gitlab-issues-for-project (assoc-default 'project_id issue))))
(defun create-issues-entries (issues)
"Create entries for 'tabulated-list-entries from ISSUES."
(mapcar (lambda (i)
(let ((id (number-to-string (assoc-default 'id i)))
(author (assoc-default 'author i)))
(list i
(vector ;id
(assoc-default 'state i)
(format "%s" (assoc-default 'project_id i))
(assoc-default 'name author)
(assoc-default 'title i)))))
issues))
;;;###autoload
(defun gitlab-show-issues ()
"Show Gitlab issues."
(interactive)
(pop-to-buffer "*Gitlab issues*" nil)
(gitlab-issues-mode)
(setq tabulated-list-entries
(create-issues-entries (gitlab-list-all-issues)))
(tabulated-list-print t))
;; Gitlab projects mode
(defvar gitlab-projects-mode-hook nil)
(defvar gitlab-projects-mode-map
(let ((map (make-keymap)))
(define-key map (kbd "v") 'print-current-project-id)
(define-key map (kbd "w") 'gitlab-goto-project)
(define-key map (kbd "d") 'gitlab-describe-project)
map)
"Keymap for `gitlab-projects-mode' major mode.")
(define-key gitlab-projects-mode-map (kbd "i") 'gitlab-issues-for-project)
(define-derived-mode gitlab-projects-mode tabulated-list-mode "Gitlab projects"
"Major mode for browsing Gitlab projects."
:group 'gitlab
(setq tabulated-list-format [;("ID" 5 t)
("Name" 25 t)
("Owner" 25 t)
("Namespace" 25 t)
("Description" 0 nil)])
(setq tabulated-list-padding 2)
(setq tabulated-list-sort-key (cons "Name" nil))
(tabulated-list-init-header))
;; Gitlab issues mode
(defvar gitlab-issues-mode-hook nil)
(defvar gitlab-issues-mode-map
(let ((map (make-keymap)))
(define-key map (kbd "v") 'print-current-issue-id)
(define-key map (kbd "w") 'gitlab-goto-issue)
map)
"Keymap for `gitlab-issues-mode' major mode.")
(define-key gitlab-issues-mode-map (kbd "c") 'gitlab-close-issue)
(define-key gitlab-issues-mode-map (kbd "o") 'gitlab-open-issue)
(define-derived-mode gitlab-issues-mode tabulated-list-mode "Gitlab issues"
"Major mode for browsing Gitlab issues."
:group 'gitlab
(setq tabulated-list-format [;("ID" 5 t)
("State" 10 t)
("Project" 8 t)
("Author" 20 t)
("Title" 0 t)])
(setq tabulated-list-padding 2)
(setq tabulated-list-sort-key (cons "Title" nil))
(tabulated-list-init-header))
(provide 'gitlab-mode)
;;; gitlab-mode.el ends here