-
Notifications
You must be signed in to change notification settings - Fork 5
/
elmine.el
416 lines (356 loc) · 15.7 KB
/
elmine.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
;;; elmine.el --- Redmine API access via elisp.
;; Copyright (c) 2012 Arthur Leonard Andersen
;;
;; Author: Arthur Andersen <[email protected]>
;; URL: http://github.com/leoc/elmine
;; Version: 0.3.1
;; Keywords: tools
;; Package-Requires: ((s "1.10.0"))
;;
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; `elmine` provides simple means of accessing the redmine Rest API
;; programmatically. This means that you do not have interactive
;; functions but functions that give and take list representations of
;; JSON objects of the redmine API.
;;; Code:
(require 'json)
(require 's)
(defun plist-merge (base new)
"Merges two plists. The keys of the second one will overwrite the old ones."
(let ((key (car new))
(val (cadr new))
(new (cddr new)))
(while (and key val)
(setq base (plist-put base key val))
(setq key (car new))
(setq val (cadr new))
(setq new (cddr new)))
base))
(defvar elmine/host nil
"The default host of the redmine.")
(defvar elmine/api-key nil
"The default API key for the redmine")
(defun elmine/get (plist key &rest keys)
"Execute `plist-get` recursively for `plist`.
Example:
(setq plist '(:a 3
:b (:c 12
:d (:e 31))))
(elmine/get plist \"a\")
;; => 3
(elmine/get plist :b)
;; => (:c 12 :d (:e 31))
(elmine/get plist :b :c)
;; => 12
(elmine/get plist :b :d :e)
;; => 31
(elmine/get plist :b :a)
;; => nil
(elmine/get plist :a :c)
;; => nil"
(save-match-data
(let ((ret (plist-get plist key)))
(while (and keys ret)
(if (listp ret)
(progn
(setq ret (elmine/get ret (car keys)))
(setq keys (cdr keys)))
(setq ret nil)))
ret)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HTTP functions using Emacs URL package ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun elmine/make-key (string)
(make-symbol (format ":%s" (s-dashed-words string))))
(defun elmine/ensure-string (object)
"Return a string representation of OBJECT."
(cond ((stringp object) object)
((keywordp object) (substring (format "%s" object) 1 nil))
((symbolp object) (symbol-name object))
((numberp object) (number-to-string object))
(t (pp-to-string object))))
(defun elmine/api-build-query-string (plist)
"Builds a query string from a given plist."
(if plist
(let (query-pairs)
(while plist
(let ((key (url-hexify-string (elmine/ensure-string (car plist))))
(val (url-hexify-string (elmine/ensure-string (cadr plist)))))
(setq query-pairs (cons (format "%s=%s" key val) query-pairs))
(setq plist (cddr plist))))
(concat "?" (s-join "&" query-pairs)))
""))
(defun elmine/api-build-url (path params)
"Creates a URL from a relative PATH, a plist of query PARAMS and
the dynamically bound `redmine-api-key` and `redmine-host` variables."
(let ((host (s-chop-suffix "/" redmine-host))
(query-str (elmine/api-build-query-string params)))
(concat host path query-str)))
(defun elmine/api-raw (method path data params)
"Perform a raw HTTP request with given METHOD, a relative PATH and a
plist of PARAMS for the query."
(let* ((redmine-host (if (boundp 'redmine-host)
redmine-host
elmine/host))
(redmine-api-key (if (boundp 'redmine-api-key)
redmine-api-key
elmine/api-key))
(url (elmine/api-build-url path params))
(url-request-method method)
(url-request-extra-headers
`(("Content-Type" . "application/json")
("X-Redmine-API-Key" . ,redmine-api-key)))
(url-request-data data)
header-end status header body)
(save-excursion
(switch-to-buffer (url-retrieve-synchronously url))
(beginning-of-buffer)
(setq header-end (save-excursion
(if (re-search-forward "^$" nil t)
(progn
(forward-char)
(point))
(point-max))))
(when (re-search-forward "^HTTP/\\(1\\.0\\|1\\.1\\) \\([0-9]+\\) \\([A-Za-z ]+\\)$" nil t)
(setq status (plist-put status :code (string-to-number (match-string 2))))
(setq status (plist-put status :text (match-string 3))))
(while (re-search-forward "^\\([^:]+\\): \\(.*\\)" header-end t)
(setq header (cons (match-string 1) (cons (match-string 2) header))))
(unless (eq header-end (point-max))
(setq body (url-unhex-string
(buffer-substring header-end (point-max)))))
(kill-buffer))
`(:status ,status
:header ,header
:body ,body)))
(defun elmine/api-get (element path &rest params)
"Perform an HTTP GET request and return a PLIST with the request information.
It returns the "
(let* ((params (if (listp (car params)) (car params) params))
(response (elmine/api-raw "GET" path nil params))
(object (elmine/api-decode (plist-get response :body)))
)
(if element
(plist-get object element)
object)))
(defun elmine/api-post (element object path &rest params)
"Does an http POST request and returns response status as symbol."
(let* ((params (if (listp (car params)) (car params) params))
(data (elmine/api-encode `(,element ,object)))
(response (elmine/api-raw "POST" path data params))
(object (elmine/api-decode (plist-get response :body))))
object))
(defun elmine/api-put (element object path &rest params)
"Does an http PUT request and returns the response status as symbol.
Either :ok or :unprocessible."
(let* ((params (if (listp (car params)) (car params) params))
(data (elmine/api-encode `(,element ,object)))
(response (elmine/api-raw "PUT" path data params))
(object (elmine/api-decode (plist-get response :body)))
(status (elmine/get response :status :code)))
(cond ((eq status 200) t)
((eq status 404)
(signal 'no-such-resource `(:response ,response))))))
(defun elmine/api-delete (path &rest params)
"Does an http DELETE request and returns the body of the response."
(let* ((params (if (listp (car params)) (car params) params))
(response (elmine/api-raw "DELETE" path nil params))
(status (elmine/get response :status :code)))
(cond ((eq status 200) t)
((eq status 404)
(signal 'no-such-resource `(:response ,response))))))
(defun elmine/api-get-all (element path &rest filters)
"Return list of ELEMENT items retrieved from PATH limited by FILTERS.
Limiting items by count can be done using `limit' in FILTERS:
- If `limit' is t, return all items.
- If `limit' is number, return items up to that count.
- Otherwise return up to 25 items (redmine api default)."
(let* ((initial-limit (plist-get filters :limit))
(initial-limit (when (or
(eq t initial-limit)
(numberp initial-limit))
initial-limit))
(limit (if (eq t initial-limit) 100 initial-limit))
(response-object (apply #'elmine/api-get nil path (plist-put filters :limit limit)))
(offset (elmine/get response-object :offset))
(limit (elmine/get response-object :limit))
(total-count (elmine/get response-object :total_count))
(issue-list (elmine/get response-object element)))
(if (and offset
limit
(< (+ offset limit) total-count)
(or (eq t initial-limit)
(and initial-limit (< (+ offset limit) initial-limit))))
(let* ((offset (+ offset limit))
(limit (if (eq t initial-limit)
t
(- initial-limit offset))))
(append issue-list (apply #'elmine/api-get-all element path
(plist-merge
filters
`(:offset ,offset :limit ,limit)))))
issue-list)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Simple JSON decode/encode functions ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun elmine/api-decode (json-string)
"Parses a JSON string and returns an object. Per default JSON objects are
going to be hashtables and JSON arrays are going to be lists."
(if (null json-string)
nil
(let ((json-object-type 'plist)
(json-array-type 'list))
(condition-case err
(json-read-from-string json-string)
(json-readtable-error
(message "%s: Could not parse json-string into an object. See %s"
(error-message-string err) json-string))))))
(defun elmine/api-encode (object)
"Return a JSON representation from the given object."
(let ((json-object-type 'plist)
(json-array-type 'list))
(condition-case err
(encode-coding-string (json-encode object) 'utf-8)
(json-readtable-error
(message "%s: Could not encode object into JSON string. See %s"
(error-message-string err) object)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; API functions to retrieve data from redmine ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun elmine/get-issues (&rest filters)
"Get a list of issues."
(apply #'elmine/api-get-all :issues "/issues.json" filters))
(defun elmine/get-issue (id &rest params)
"Get a specific issue via id."
(elmine/api-get :issue (format "/issues/%s.json" id) params))
(defun elmine/create-issue (&rest params)
"Create an issue.
You can create an issue with giving each of its parameters or simply passing
an issue object to this function."
(let ((object (if (listp (car params)) (car params) params)))
(elmine/api-post :issue object "/issues.json")))
(defun elmine/update-issue (object)
"Update an issue. The object passed to this function gets updated."
(let ((id (plist-get object :id)))
(elmine/api-put :issue object (format "/issues/%s.json" id))))
(defun elmine/delete-issue (id)
"Deletes an issue with a specific id."
(elmine/api-delete (format "/issues/%s.json" id)))
(defun elmine/get-issue-time-entries (issue-id &rest filters)
"Gets all time entries for a specific issue."
(apply #'elmine/api-get-all :time_entries
(format "/issues/%s/time_entries.json" issue-id) filters))
(defun elmine/get-issue-relations (issue-id)
"Get all relations for a specific issue."
(apply #'elmine/api-get-all :relations
(format "/issues/%s/relations.json" issue-id) nil))
(defun elmine/get-projects (&rest filters)
"Get a list with projects."
(apply #'elmine/api-get-all :projects "/projects.json" filters))
(defun elmine/get-project (project)
"Get a specific project."
(elmine/api-get :project (format "/projects/%s.json" project)))
(defun elmine/create-project (&rest params)
"Create a new project."
(let ((object (if (listp (car params)) (car params) params)))
(elmine/api-post :project object "/projects.json")))
(defun elmine/update-project (&rest params)
"Update a given project."
(let* ((object (if (listp (car params)) (car params) params))
(identifier (plist-get object :identifier)))
(elmine/api-put :project object
(format "/projects/%s.json" identifier))))
(defun elmine/delete-project (project)
"Deletes a project."
(elmine/api-delete (format "/projects/%s.json" project)))
(defun elmine/get-project-categories (project &rest filters)
"Get all categories for a project."
(apply #'elmine/api-get-all :issue_categories
(format "/projects/%s/issue_categories.json" project) filters))
(defun elmine/get-project-issues (project &rest filters)
"Get a list of issues for a specific project."
(apply #'elmine/api-get-all :issues
(format "/projects/%s/issues.json" project) filters))
(defun elmine/get-project-versions (project &rest filters)
"Get a list of versions for a specific project."
(apply #'elmine/api-get-all :versions
(format "/projects/%s/versions.json" project) filters))
(defun elmine/get-project-memberships (project &rest filters)
"Get PROJECT memberships limited by FILTERS."
(apply #'elmine/api-get-all :memberships
(format "/projects/%s/memberships.json" project) filters))
(defun elmine/get-version (id)
"Get a specific version."
(elmine/api-get :version (format "/versions/%s.json" id)))
(defun elmine/create-version (&rest params)
"Create a new version."
(let* ((object (if (listp (car params)) (car params) params))
(project (plist-get object :project_id)))
(elmine/api-post :version object
(format "/projects/%s/versions.json" project))))
(defun elmine/update-version (&rest params)
"Update a given version."
(let* ((object (if (listp (car params)) (car params) params))
(id (plist-get object :id)))
(elmine/api-put :version object
(format "/versions/%s.json" id))))
(defun elmine/get-issue-statuses ()
"Get a list of available issue statuses."
(elmine/api-get-all :issue_statuses "/issue_statuses.json"))
(defun elmine/get-issue-priorities (&rest params)
"Get a list of issue priorities."
(apply #'elmine/api-get-all :issue_priorities
"/enumerations/issue_priorities.json" params))
(defun elmine/get-trackers ()
"Get a list of tracker names and their IDs."
(elmine/api-get-all :trackers "/trackers.json"))
(defun elmine/get-issue-priorities ()
"Get a list of issue priorities and their IDs."
(elmine/api-get-all :issue_priorities "/enumerations/issue_priorities.json"))
(defun elmine/get-time-entries (&rest filters)
"Get a list of time entries."
(apply #'elmine/api-get-all :time_entries "/time_entries.json" filters))
(defun elmine/get-time-entry (id)
"Get a specific time entry."
(elmine/api-get :time_entry (format "/time_entries/%s.json" id)))
(defun elmine/get-time-entry-activities (&rest params)
"Get a list of time entry activities."
(apply #'elmine/api-get-all :time_entry_activities
"/enumerations/time_entry_activities.json" params))
(defun elmine/create-time-entry (&rest params)
"Create a new time entry"
(let* ((object (if (listp (car params)) (car params) params)))
(elmine/api-post :time_entry object "/time_entries.json")))
(defun elmine/update-time-entry (&rest params)
"Update a given time entry."
(let* ((object (if (listp (car params)) (car params) params))
(id (plist-get object :id)))
(elmine/api-put :time_entry object (format "/time_entries/%s.json" id))))
(defun elmine/delete-time-entry (id)
"Delete a specific time entry."
(elmine/api-delete (format "/time_entries/%s.json" id)))
(defun elmine/get-users (&rest filters)
"Get a list of users limited by FILTERS."
(apply #'elmine/api-get-all :users "/users.json" filters))
(defun elmine/get-user (user &rest params)
"Get USER. PARAMS can be used to retrieve additional details.
If USER is `current', get user whose credentials are used."
(elmine/api-get :user (format "/users/%s.json" user) params))
(provide 'elmine)
;;; elmine.el ends here