-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathos-bb.el
260 lines (220 loc) · 8.65 KB
/
os-bb.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
;;; os-bb.el --- Bitbucket backend for org-sync.
;; Copyright (C) 2012 Aurelien Aptel
;;
;; Author: Aurelien Aptel <aurelien dot aptel at gmail dot com>
;; Keywords: org, bitbucket, synchronization
;; Homepage: http://orgmode.org/worg/org-contrib/gsoc2012/student-projects/org-sync
;;
;; 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.
;; This file is not part of GNU Emacs.
;; 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 package implements a backend for org-sync to synchnonize
;; issues from a bitbucket repo with an org-mode buffer. Read
;; Org-sync documentation for more information about it.
;; This backend only supports basic synchronization for now.
;; Components, versions and milestones are ignored.
;;; Code:
(eval-when-compile (require 'cl))
(require 'os)
(require 'url)
(require 'json)
(defvar url-http-end-of-headers)
(defvar url-http-response-status)
(defvar os-bb-backend
'((base-url . os-bb-base-url)
(fetch-buglist . os-bb-fetch-buglist)
(send-buglist . os-bb-send-buglist))
"Bitbucket backend.")
(defvar os-bb-auth nil
"Bitbucket login (\"user\" . \"pwd\")")
(defun os-bb-request (method url &optional data)
"Send HTTP request at URL using METHOD with DATA.
AUTH is a cons (\"user\" . \"pwd\"). Return the server
decoded response in JSON."
(message "%s %s %s" method url (prin1-to-string data))
(let* ((url-request-method method)
(url-request-data data)
(auth os-bb-auth)
(buf)
(url-request-extra-headers
(unless data
'(("Content-Type" . "application/x-www-form-urlencoded")))))
(if (consp auth)
;; dynamically bind auth related vars
(let* ((str (concat (car auth) ":" (cdr auth)))
(encoded (base64-encode-string str))
(login `(("api.bitbucket.org:443" ("Bitbucket API" . ,encoded))))
(url-basic-auth-storage 'login))
(setq buf (url-retrieve-synchronously url)))
;; nothing more to bind
(setq buf (url-retrieve-synchronously url)))
(with-current-buffer buf
(goto-char url-http-end-of-headers)
(prog1
(cons url-http-response-status (ignore-errors (json-read)))
(kill-buffer)))))
;; override
(defun os-bb-base-url (url)
"Return base URL."
(cond
;; web ui url
((string-match "^\\(?:https?://\\)?\\(?:www\\.\\)?bitbucket.org/\\([^/]+\\)/\\([^/]+\\)/?$" url)
(concat "https://api.bitbucket.org/1.0/repositories/"
(match-string 1 url) "/" (match-string 2 url)))
;; api url
((string-match "api.bitbucket.org/1.0/repositories" url)
url)))
;; From https://confluence.atlassian.com/display/BITBUCKET/Issues
;; title: The title of the new issue.
;; content: The content of the new issue.
;; component: The component associated with the issue.
;; milestone: The milestone associated with the issue.
;; version: The version associated with the issue.
;; responsible: The username of the person responsible for the issue.
;; priority: The priority of the issue. Valid priorities are:
;; - trivial
;; - minor
;; - major
;; - critical
;; - blocker
;; status: The status of the issue. Valid statuses are:
;; - new
;; - open
;; - resolved
;; - on hold
;; - invalid
;; - duplicate
;; - wontfix
;; kind: The kind of issue. Valid kinds are:
;; - bug
;; - enhancement
;; - proposal
;; - task
(defconst os-bb-priority-list
'("trivial" "minor" "major" "critical" "blocker")
"List of valid priority for a bitbucket issue.")
(defconst os-bb-status-list
'("new" "open" "resolved" "on hold" "invalid" "duplicate" "wontfix")
"List of valid status for a bitbucket issue.")
(defconst os-bb-kind-list
'("bug" "enhancement" "proposal" "task")
"List of valid kind for a bitbucket issue.")
(defun os-bb-bug-to-form (bug)
"Return BUG as an form alist."
(let* ((priority (os-get-prop :priority bug))
(title (os-get-prop :title bug))
(desc (os-get-prop :desc bug))
(assignee (os-get-prop :assignee bug))
(status (if (eq (os-get-prop :status bug) 'open) "open" "resolved"))
(kind (os-get-prop :kind bug)))
(if (and priority (not (member priority os-bb-priority-list)))
(error "Invalid priority \"%s\" at bug \"%s\"." priority title))
(if (and kind (not (member kind os-bb-kind-list)))
(error "Invalid kind \"%s\" at bug \"%s\"." kind title))
(remove-if (lambda (x)
(null (cdr x)))
`(("title" . ,title)
("status" . ,status)
("content" . ,desc)
("responsible" . ,assignee)
("priority" . ,priority)
("kind" . ,kind)))))
(defun os-bb-post-encode (args)
"Return form alist ARGS as a url-encoded string."
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args "&"))
(defun os-bb-repo-name (url)
"Return repo name at URL."
(when (string-match "api\\.bitbucket.org/1\\.0/repositories/\\([^/]+\\)/\\([^/]+\\)" url)
(match-string 2 url)))
(defun os-bb-repo-user (url)
"Return repo username at URL."
(when (string-match "api\\.bitbucket.org/1\\.0/repositories/\\([^/]+\\)/\\([^/]+\\)" url)
(match-string 1 url)))
;; override
(defun os-bb-fetch-buglist (last-update)
"Return the buglist at os-base-url."
(let* ((url (concat os-base-url "/issues"))
(res (os-bb-request "GET" url))
(code (car res))
(json (cdr res))
(title (concat "Bugs of " (os-bb-repo-name url))))
`(:title ,title
:url ,os-base-url
:bugs ,(mapcar 'os-bb-json-to-bug (cdr (assoc 'issues json))))))
(defun os-bb-json-to-bug (json)
"Return JSON as a bug."
(flet ((va (key alist) (cdr (assoc key alist)))
(v (key) (va key json)))
(let* ((id (v 'local_id))
(metadata (v 'metadata))
(kind (va 'kind metadata))
(version (va 'version metadata))
(component (va 'component metadata))
(milestone (va 'milestone metadata))
(author (va 'username (v 'reported_by)))
(assignee (va 'username (v 'responsible)))
(txtstatus (v 'status))
(status (if (or (string= txtstatus "open")
(string= txtstatus "new"))
'open
'closed))
(priority (v 'priority))
(title (v 'title))
(desc (v 'content))
(ctime (os-parse-date (v 'utc_created_on)))
(mtime (os-parse-date (v 'utc_last_updated))))
`(:id ,id
:priority ,priority
:assignee ,assignee
:status ,status
:title ,title
:desc ,desc
:date-creation ,ctime
:date-modification ,mtime
:kind ,kind
:version ,version
:component ,component
:milestone ,milestone))))
;; override
(defun os-bb-send-buglist (buglist)
"Send a BUGLIST on the bugtracker and return new bugs."
(let* ((new-url (concat os-base-url "/issues"))
(new-bugs))
(dolist (b (os-get-prop :bugs buglist))
(let* ((id (os-get-prop :id b))
(data (os-bb-post-encode (os-bb-bug-to-form b)))
(modif-url (format "%s/%d/" new-url (or id 0)))
res)
(cond
;; new bug
((null id)
(setq res (os-bb-request "POST" new-url data))
(when (/= (car res) 200)
(error "Can't create new bug \"%s\"" (os-get-prop :title b)))
(push (os-bb-json-to-bug (cdr res)) new-bugs))
;; delete bug
((os-get-prop :delete b)
(setq res (os-bb-request "DELETE" modif-url))
(when (not (member (car res) '(404 204)))
(error "Can't delete bug #%d" id)))
;; update bug
(t
(setq res (os-bb-request "PUT" modif-url data))
(when (/= (car res) 200)
(error "Can't update bug #%id" id))
(push (os-bb-json-to-bug (cdr res)) new-bugs)))))
`(:bugs ,new-bugs)))
;;; os-bb.el ends here