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
|
;;; rt-liberation-rest.el --- Interface to the RT REST API
;; Copyright (C) 2014, 2015 Free Software Foundation
;;
;; Authors: Yoni Rabkin <yrk@gnu.org>
;;
;; This file is a part of rt-liberation.
;;
;; 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;;; History:
;;
;; Started in May of 2014 in order to remove rt-liberation's
;; dependency on a local copy of the RT CLI.
;;; Code:
(require 'url)
(require 'url-util)
(defvar rt-liber-rest-debug-buffer-name "*rt-liber-rest debug log*"
"Buffer name of debug capture.")
(defvar rt-liber-rest-debug-p nil
"If non-nil, record traffic in a debug buffer.")
(defvar rt-liber-rest-scheme "https"
"Scheme used for transport. Is one of http or https.")
(defvar rt-liber-rest-url ""
"URL of RT installation.")
(defvar rt-liber-rest-username ""
"Username of RT account.")
(defvar rt-liber-rest-password ""
"Password of RT account.")
(defvar rt-liber-rest-verbose-p t
"If non-nil, be verbose about what's happening.")
(defvar rt-liber-rest-response-buffer nil
"Buffer for manipulating server responses.")
(defun rt-liber-rest-write-debug (str)
"Write to debug buffer."
(when (not (stringp str))
(error "argument not string"))
(when rt-liber-rest-debug-p
(with-current-buffer
(get-buffer-create rt-liber-rest-debug-buffer-name)
(goto-char (point-max))
(insert str))))
(defun rt-liber-rest-search-string (scheme url username password query)
"Return the search query string."
(let ((user (url-encode-url username))
(pass (url-encode-url password)))
(concat scheme
"://"
url
"/REST/1.0/search/ticket" "?"
"user=" user "&"
"pass=" pass "&"
"query=" (url-encode-url query) "&"
"format=i" "&"
"orderby=+Created")))
(defun rt-liber-rest-show-string (scheme url ticket-id-list username password query)
"Return the ticket show string."
(let ((user (url-encode-url username))
(pass (url-encode-url password)))
(concat scheme
"://"
url
"/REST/1.0/ticket/" ticket-id-list
"/show" "?"
"user=" user "&"
"pass=" pass "&")))
(defun rt-liber-rest-history-string (scheme url ticket-id username password)
"Return the ticket show string."
(let ((user (url-encode-url username))
(pass (url-encode-url password)))
(concat scheme
"://"
url
"/REST/1.0/ticket/" ticket-id
"/history" "?"
"format=l" "&"
"user=" user "&"
"pass=" pass)))
(defun rt-liber-rest-command-edit-string (scheme url ticket-id username password)
"Return the ticket edit string."
(let ((user (url-encode-url username))
(pass (url-encode-url password)))
(concat scheme
"://"
url
"/REST/1.0/ticket/" ticket-id
"/edit" "?"
"user=" user "&"
"pass=" pass)))
(defun rt-liber-rest-call (url)
"Perform a REST call with URL."
(let ((url-request-method "POST"))
(let ((response
(url-retrieve-synchronously url))
str)
(setq str
(decode-coding-string
(with-current-buffer response
(buffer-substring-no-properties (point-min)
(point-max)))
'utf-8))
(rt-liber-rest-write-debug
(format "outgoing rest call -->\n%s\n<-- incoming\n%s\n" url str))
str)))
(defun rt-liber-rest-query-runner (op query-string)
"Run OP on QUERY-STRING."
(when (or (not (stringp op))
(not (stringp query-string)))
(error "bad arguments"))
(cond ((string= op "ls")
(rt-liber-rest-call
(rt-liber-rest-search-string rt-liber-rest-scheme
rt-liber-rest-url
rt-liber-rest-username
rt-liber-rest-password
query-string)))
((string= op "show")
(rt-liber-rest-call
(rt-liber-rest-show-string rt-liber-rest-scheme
rt-liber-rest-url
query-string
rt-liber-rest-username
rt-liber-rest-password
query-string)))
((string= op "history")
(rt-liber-rest-call
(rt-liber-rest-history-string rt-liber-rest-scheme
rt-liber-rest-url
query-string
rt-liber-rest-username
rt-liber-rest-password)))
(t (error "unknown op [%s]" op))))
(defun rt-liber-rest-parse-http-header ()
"Parse the HTTP header from the server."
(let ((http-ok-regexp "^HTTP.*200 OK$")
(rt-ok-regexp "^rt/.*200 ok$"))
(condition-case excep
(progn
(re-search-forward http-ok-regexp (point-max))
(re-search-forward rt-ok-regexp (point-max)))
(error "bad HTTP response from server"))))
(defun rt-liber-rest-show-process (response)
"Process and return the show query response."
(when (not (stringp response))
(error "argument not a string"))
(with-temp-buffer
(save-excursion
(insert response))
(rt-liber-rest-parse-http-header)
(buffer-substring (point) (point-max))))
(defun rt-liber-rest-show-query-runner (idsublist)
"Iterate over IDSUBLIST and return the collected result."
(when (not (listp idsublist))
(error "argument not list"))
(with-temp-buffer
(let ((ticket-ids (reverse (copy-tree idsublist)))
(c 1)
(l (length idsublist)))
(while ticket-ids
(when rt-liber-rest-verbose-p
(message "retrieving ticket %d/%d" c l)
(setq c (1+ c)))
(insert
(rt-liber-rest-show-process
(rt-liber-rest-query-runner "show" (caar ticket-ids))))
(setq ticket-ids (cdr ticket-ids))
(when ticket-ids
(insert "\n--\n")))
(when rt-liber-rest-verbose-p
(message "done retrieving %d tickets" l)))
(buffer-substring (point-min) (point-max))))
(defun rt-liber-rest-handle-response (buffer)
"Handle the response provided in BUFFER."
(with-current-buffer rt-liber-rest-response-buffer
(rt-liber-rest-write-debug (buffer-string))))
(defun rt-liber-rest-edit-runner (ticket-id field value)
"Run edit comment to set FIELD to VALUE."
(message "started edit command at %s..." (current-time-string))
(message "ticket #%s, %s <- %s" ticket-id field value)
(let ((request-data
(format "content=%s: %s"
(url-hexify-string field)
(url-hexify-string value))))
(rt-liber-rest-write-debug (concat request-data "\n"))
(let ((url-request-method "POST")
(url-request-data request-data)
rt-liber-rest-response-buffer)
(setq rt-liber-rest-response-buffer
(url-retrieve-synchronously
(rt-liber-rest-command-edit-string
rt-liber-rest-scheme
rt-liber-rest-url
ticket-id
rt-liber-rest-username
rt-liber-rest-password)))
(rt-liber-rest-handle-response rt-liber-rest-response-buffer)))
(message "edit command ended at %s" (current-time-string)))
(provide 'rt-liberation-rest)
;;; rt-liberation-rest.el ends here.
|