forked from elsatch/org-sync
-
Notifications
You must be signed in to change notification settings - Fork 6
/
os-github.el
279 lines (238 loc) · 10.3 KB
/
os-github.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
;;; os-github.el --- Github backend for Org-sync.
;; Copyright (C) 2012 Aurelien Aptel
;;
;; Author: Aurelien Aptel <aurelien dot aptel at gmail dot com>
;; Keywords: org, github, 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 github tracker with an org-mode buffer. Read
;; Org-sync documentation for more information about it.
;; This backend supports basic bug syncing along with tag creation.
;; If you add or change the tags of an issue to something that doesn't
;; exists, it will be created.
;;; Code:
(eval-when-compile (require 'cl))
(require 'url)
(require 'os)
(require 'json)
(defvar org-sync-github-backend
'((base-url . org-sync-github-base-url)
(fetch-buglist . org-sync-github-fetch-buglist)
(send-buglist . org-sync-github-send-buglist))
"Github backend.")
(defvar url-http-end-of-headers)
(defvar org-sync-github-auth nil
"Github login (\"user\" . \"pwd\")")
(defun org-sync-github-fetch-labels ()
"Return list of labels at `org-sync-base-url'."
(let* ((url (concat org-sync-base-url "/labels"))
(json (org-sync-github-fetch-json url)))
(mapcar (lambda (x)
(cdr (assoc 'name x)))
json)))
(defun org-sync-github-random-color ()
"Return a random hex color code 6 characters string without #."
(random t)
(format "%02X%02X%02X" (random 256) (random 256) (random 256)))
(defun org-sync-github-color-p (color)
"Return non-nil if COLOR is a valid color code."
(and (stringp color) (string-match "^[0-9a-fA-F]\\{6\\}$" color)))
(defun org-sync-github-create-label (label &optional color)
"Create new COLOR LABEL at `org-sync-base-url' and return it.
LABEL must be a string. COLOR must be a 6 characters string
containing a hex color code without the #. Take a random color
when not given."
(let* ((url (concat org-sync-base-url "/labels"))
(json (json-encode `((name . ,label)
(color . ,(if (org-sync-github-color-p color)
color
(org-sync-github-random-color)))))))
(org-sync-github-request "POST" url json)))
(defun org-sync-github-handle-tags (bug existing-tags)
"Create any label in BUG that is not in EXISTING-TAGS.
Append new tags in EXISTING-TAGS by side effects."
(let* ((tags (org-sync-get-prop :tags bug)))
(dolist (tag tags)
(when (org-sync-append! tag existing-tags)
(org-sync-github-create-label tag)))))
(defun org-sync-github-time-to-string (time)
"Return TIME as a full ISO 8601 date string, but without timezone adjustments (which github doesn't support"
(format-time-string "%Y-%m-%dT%TZ" time t))
;; override
(defun org-sync-github-fetch-buglist (last-update)
"Return the buglist at `org-sync-base-url'."
(let* ((since (when last-update
(format "&since=%s" (org-sync-github-time-to-string last-update))))
(url (concat org-sync-base-url "/issues?per_page=100" since))
(json (vconcat (org-sync-github-fetch-json url)
(org-sync-github-fetch-json (concat url "&state=closed"))))
(title (concat "Bugs of " (org-sync-github-repo-name url))))
`(:title ,title
:url ,org-sync-base-url
:bugs ,(mapcar 'org-sync-github-json-to-bug json)
:since ,last-update)))
;; override
(defun org-sync-github-base-url (url)
"Return base url."
(when (string-match "github.com/\\(?:repos/\\)?\\([^/]+\\)/\\([^/]+\\)" url)
(let ((user (match-string 1 url))
(repo (match-string 2 url)))
(concat "https://api.github.com/repos/" user "/" repo ""))))
;; override
(defun org-sync-github-send-buglist (buglist)
"Send a BUGLIST on the bugtracker and return new bugs."
(let* ((new-url (concat org-sync-base-url "/issues"))
(existing-tags (org-sync-github-fetch-labels))
(newbugs))
(dolist (b (org-sync-get-prop :bugs buglist))
(let* ((sync (org-sync-get-prop :sync b))
(id (org-sync-get-prop :id b))
(data (org-sync-github-bug-to-json b))
(modif-url (format "%s/%d" new-url (or id 0)))
(result
(cond
;; new bug
((null id)
(org-sync-github-handle-tags b existing-tags)
(push (org-sync-github-json-to-bug
(org-sync-github-request "POST" new-url data)) newbugs))
;; update bug
(t
(org-sync-github-handle-tags b existing-tags)
(org-sync-github-request "PATCH" modif-url data))))
(err (cdr (assoc 'message result))))
(when (stringp err)
(error "Github: %s" err))))
`(:bugs ,newbugs)))
(defun org-sync-github-fetch-json (url)
"Return a parsed JSON object of all the pages of URL."
(let* ((ret (org-sync-github-fetch-json-page url))
(data (car ret))
(url (cdr ret))
(json data))
(while url
(setq ret (org-sync-github-fetch-json-page url))
(setq data (car ret))
(setq url (cdr ret))
(setq json (vconcat json data)))
json))
(defun org-sync-github-url-retrieve-synchronously (url)
"Retrieve the specified url using authentication data from
`org-sync-github-auth'. AUTH is a cons (\"user\" . \"pwd\")."
(let ((auth org-sync-github-auth))
(if (consp auth)
;; dynamically bind auth related vars
(let* ((str (concat (car auth) ":" (cdr auth)))
(encoded (base64-encode-string str))
(login `(("api.github.com:443" ("Github API" . ,encoded))))
(url-basic-auth-storage 'login))
(url-retrieve-synchronously url))
;; nothing more to bind
(url-retrieve-synchronously url))))
(defun org-sync-github-fetch-json-page (url)
"Return a cons (JSON object from URL . next page url)."
(let ((download-buffer (org-sync-github-url-retrieve-synchronously url))
page-next
header-end
(auth org-sync-github-auth)
ret)
(if (consp auth)
(let* ((str (concat (car auth) ":" (cdr auth)))
(encoded (base64-encode-string str))
(login `(("api.github.com:443" ("Github API" . ,encoded))))
(url-basic-auth-storage 'login))
(setq download-buffer (url-retrieve-synchronously url)))
(setq download-buffer (url-retrieve-synchronously url)))
(with-current-buffer download-buffer
;; get HTTP header end position
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(forward-char)
(setq header-end (point))
(set-buffer-multibyte t)
;; get next page url
(goto-char (point-min))
(when (re-search-forward
"<\\(https://api.github.com.+?page=[0-9]+.*?\\)>; rel=\"next\""
header-end t)
(setq page-next (match-string 1)))
(goto-char header-end)
(setq ret (cons (json-read) page-next))
(kill-buffer)
ret)))
(defun org-sync-github-request (method url &optional data)
"Send HTTP request at URL using METHOD with DATA.
Return the server decoded JSON response."
(message "%s %s %s" method url (prin1-to-string data))
(let* ((url-request-method method)
(url-request-data data)
(buf (org-sync-github-url-retrieve-synchronously url)))
(with-current-buffer buf
(set-buffer-multibyte t)
(goto-char url-http-end-of-headers)
(prog1 (json-read) (kill-buffer)))))
(defun org-sync-github-repo-name (url)
"Return the name of the repo at URL."
(if (string-match "github.com/repos/[^/]+/\\([^/]+\\)" url)
(match-string 1 url)
"<project name>"))
;; XXX: we need an actual markdown parser here...
(defun org-sync-github-filter-desc (desc)
"Return a filtered description of a GitHub description."
(setq desc (replace-regexp-in-string "\r\n" "\n" desc))
(setq desc (replace-regexp-in-string "\\([^ \t\n]\\)[ \t\n]*\\'"
"\\1\n" desc)))
(defun org-sync-github-json-to-bug (data)
"Return DATA (in json) converted to a bug."
(flet ((va (key alist) (cdr (assoc key alist)))
(v (key) (va key data)))
(let* ((id (v 'number))
(stat (if (string= (v 'state) "open") 'open 'closed))
(title (v 'title))
(desc (org-sync-github-filter-desc (v 'body)))
(author (va 'login (v 'user)))
(assignee (va 'login (v 'assignee)))
(milestone-alist (v 'milestone))
(milestone (va 'title milestone-alist))
(ctime (org-sync-parse-date (v 'created_at)))
(dtime (org-sync-parse-date (va 'due_on milestone-alist)))
(mtime (org-sync-parse-date (v 'updated_at)))
(tags (mapcar (lambda (e)
(va 'name e)) (v 'labels))))
`(:id ,id
:author ,author
:assignee ,assignee
:status ,stat
:title ,title
:desc ,desc
:milestone ,milestone
:tags ,tags
:date-deadline ,dtime
:date-creation ,ctime
:date-modification ,mtime))))
(defun org-sync-github-bug-to-json (bug)
"Return BUG as JSON."
(let ((state (org-sync-get-prop :status bug)))
(unless (member state '(open closed))
(error "Github: unsupported state \"%s\"" (symbol-name state)))
(json-encode
`((title . ,(org-sync-get-prop :title bug))
(body . ,(org-sync-get-prop :desc bug))
(assignee . ,(org-sync-get-prop :assignee bug))
(state . ,(symbol-name (org-sync-get-prop :status bug)))
(labels . [ ,@(org-sync-get-prop :tags bug) ])))))
(provide 'os-github)
;;; os-github.el ends here