-
Notifications
You must be signed in to change notification settings - Fork 65
/
jira.clj
370 lines (298 loc) · 11.8 KB
/
jira.clj
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
(ns yetibot.api.jira
(:require
[taoensso.timbre :refer [info warn error]]
[schema.core :as sch]
[yetibot.core.schema :refer [non-empty-str]]
[clojure.string :as s]
[clj-http.client :as client]
[clojure.core.memoize :as memo]
[yetibot.core.config :refer [get-config]]
[yetibot.core.util.http :refer [get-json fetch]]
[clj-time [format :refer [formatter formatters show-formatters parse unparse]]]))
(def jira-schema
{:domain non-empty-str
:user non-empty-str
:password non-empty-str
:projects [{:key non-empty-str
(sch/optional-key :default) {:version {:id non-empty-str}}}]
(sch/optional-key :default) {:issue {:type {:id non-empty-str}}
:project {:key non-empty-str}}
(sch/optional-key :max) {:results non-empty-str}
(sch/optional-key :subtask) {:issue {:type {:id non-empty-str}}}})
;; config
(def ^:dynamic *jira-project*
"Settings for the current channel, bound by yetibot.commands.jira"
nil)
(defn config [] (:value (get-config jira-schema [:jira])))
(defn configured? [] (config))
(defn projects [] (:projects (config)))
(defn project-for-key [k] (first (filter #(= (:key %) k) (projects))))
(defn project-keys [] (map :key (projects)))
(defn project-keys-str [] (s/join "," (project-keys)))
(defn default-version-id [project-key] (-> (project-for-key project-key)
:default :version :id))
(defn default-project-key [] (or *jira-project*
(-> (config) :default :project :key)
(first (project-keys))))
(defn default-project [] (project-for-key (default-project-key)))
(defn max-results []
(if-let [mr (-> (config) :max :results)]
(read-string mr)
10))
(defn sub-task-issue-type-id [] (-> (config) :subtask :issue :type :id ))
(defn default-issue-type-id [] (-> (config) :default :issue :type :id))
(defn base-uri [] (str "https://" (:domain (config))))
(defn api-uri [] (str (base-uri) "/rest/api/latest"))
(def auth (map (config) [:user :password]))
;; formatters
;; move to yetibot.core util if anyone else needs date parsing and formatting:
(def date-time-format (:date-hour-minute formatters))
(defn parse-and-format-date-string [date-string]
(unparse date-time-format (parse date-string)))
(def client-opts {:as :json
:basic-auth auth
:throw-entire-message? true
:insecure? true})
(def error-handling-opts {:coerce :always
:throw-exceptions false})
(defn endpoint [& fmt-with-args]
(str (api-uri) (apply format fmt-with-args)))
;; helpers
(defn GET [& fmt-with-args] (client/get (apply endpoint fmt-with-args) client-opts))
;; formatters
(defn url-from-key [k]
(str (base-uri) "/browse/" k))
(defn format-issue [issue-data]
(let [fs (:fields issue-data)]
[(:summary fs)
(str "Assignee: " (-> fs :assignee :displayName))
(str "Status: " (-> fs :status :name))
(url-from-key (:key issue-data))]))
(defn format-issue-short [issue-data]
(let [fs (:fields issue-data)]
(format "[%s] [%s] %s %s"
(or (-> fs :assignee :name) "unassigned")
(-> fs :status :name)
(:summary fs)
(url-from-key (:key issue-data)))))
(defn format-comment [c]
(str "📞 "
(-> c :author :name) " "
(parse-and-format-date-string (:created c))
": " (:body c)))
(defn format-worklog-item [w]
(str "🚧 " (-> w :author :name) " " (:timeSpent w) ": " (:comment w)
" [" (parse-and-format-date-string (:started w)) "]"))
(defn format-worklog-items [issue-data]
(when-let [worklog (-> issue-data :fields :worklog :worklogs)]
(map format-worklog-item worklog)))
(defn format-subtasks [issue-data]
;; TODO
nil)
(defn format-attachment-item [a]
(str "📎 "
(-> a :author :name) " "
(parse-and-format-date-string (:created a))
": " (:content a)))
(defn format-attachments [issue-data]
(when-let [attachments (-> issue-data :fields :attachment)]
(map format-attachment-item attachments)))
(defn format-issue-long
"Show the full details for an issue"
[issue-data]
(let [fs (:fields issue-data)]
(flatten
(keep identity
[(str (:key issue-data) " ↪︎ " (-> fs :status :name) " ↪︎ " (:summary fs))
(:description fs)
(s/join
" "
[(str "👷 " (-> fs :assignee :name))
(str "👮 " (-> fs :reporter :name))])
(s/join
" "
[(str "❗️ Priority: " (-> fs :priority :name))
(str " ✅ Fix version: " (s/join ", " (map :name (:fixVersions fs))))])
(str "🕐 Created: " (parse-and-format-date-string (:created fs))
" 🕗 Updated: " (parse-and-format-date-string (:updated fs)))
(map format-comment (-> fs :comment :comments))
(format-worklog-items issue-data)
(format-subtasks issue-data)
(format-attachments issue-data)
(str "👉 " (url-from-key (:key issue-data)))]))))
;; issues
(defn issue-create-meta [] (GET "/issue/createmeta"))
(defn get-transitions [i]
(client/get (endpoint "/issue/%s/transitions?transitionId" i)
client-opts))
(def ^:private find-resolve (partial filter #(= "Resolve Issue" (:name %))))
(defn- transition-issue [i transition-id comment]
(let [params {:update {:comment [{:add {:body comment}}]}
:fields {:resolution {:name "Fixed"}}
:transition transition-id}]
(client/post
(endpoint "/issue/%s/transitions?transitionId" i)
(merge client-opts
{:form-params params :content-type :json}))))
(defn resolve-issue
"Transition an issue to the resolved state. If it is unable to make that
transition nil will be returned"
[i comment]
(let [ts (:body (get-transitions i))
resolve-t (find-resolve (:transitions ts))]
(when-let [t (first resolve-t)]
(transition-issue i (:id t) comment))))
(defn post-comment [issue-key body]
(let [uri (endpoint "/issue/%s/comment" issue-key)]
(client/post uri
(merge client-opts
{:content-type :json
:form-params {:body body}}))))
(defn get-issue
"Fetch json for a given JIRA"
[i]
(let [uri (endpoint "/issue/%s" i)
opts (merge client-opts {:query-params {"fields" "*navigable,comment,worklog,attachment"}})]
(try
(:body (client/get uri opts))
(catch Exception _ nil))))
(def fetch-and-format-issue-short (comp format-issue-short get-issue))
(defn find-project [pk]
(try
(:body (client/get (endpoint "/project/%s" pk) client-opts))
(catch Exception _
nil)))
(defn priorities []
(:body (client/get (endpoint "/priority") client-opts)))
(defn find-priority-by-key [k]
(let [kp (re-pattern (str "(?i)" k))]
(first (filter #(re-find kp (:name %)) (priorities)))))
(defn issue-types []
(:body (client/get (endpoint "/issuetype") client-opts)))
(defn update-issue
[issue-key {:keys [fix-version summary component-ids assignee priority-key desc timetracking]}]
(let [pri-id (if priority-key (:id (find-priority-by-key priority-key)))
params {:fields
(merge
{}
(when fix-version {:fixVersions [{:name fix-version}]})
(when summary {:summary summary})
(when assignee {:assignee assignee})
(when component-ids {:components (map #(hash-map :id %) component-ids)})
(when desc {:description desc})
(when timetracking {:timetracking timetracking})
(when pri-id {:priority {:id pri-id}}))}]
(info "update issue" (pr-str params))
(client/put
(endpoint "/issue/%s" issue-key)
(merge client-opts
{:coerce :always
:throw-exceptions false
:form-params params
:content-type :json}))))
(defn create-issue
"This thing is a beast; thanks JIRA."
[{:keys [summary component-ids assignee priority-key desc project-key
fix-version timetracking issue-type-id parent]
:or {desc "" assignee "-1"
issue-type-id (if parent (sub-task-issue-type-id) (default-issue-type-id))
project-key (default-project-key)}}]
(if-let [prj (find-project project-key)]
(if-let [priority (if priority-key
(find-priority-by-key priority-key)
(first (priorities)))]
(let [pri-id (:id priority)
prj-id (:id prj)
fix-version-map (if fix-version
{:name fix-version}
(when-let [dvi (default-version-id project-key)]
{:id dvi}))
params {:fields
(merge {:assignee {:name assignee}
:project {:id prj-id}
:summary summary
:components (map #(hash-map :id %) component-ids)
:description desc
:issuetype {:id issue-type-id}
:priority {:id pri-id}}
(when fix-version-map :fixVersions [fix-version-map])
(when timetracking {:timetracking timetracking})
(when parent {:parent {:id parent}}))}]
(info "create issue" (pr-str params))
(client/post
(endpoint "/issue")
(merge client-opts
{:coerce :always
:throw-exceptions false
:form-params params
:content-type :json})))
(warn "Could not find a priority for key " priority-key))
(warn "Could not find project" project-key)))
(defn delete-issue [issue-key]
(client/delete
(endpoint "/issue/%s" issue-key)
(merge client-opts {:coerce :always
:content-type :json
:throw-exceptions false})))
(defn assign-issue
[issue-key assignee]
(client/put
(endpoint "/issue/%s/assignee" issue-key)
(merge client-opts
; error-handling-opts
{:content-type :json
:form-params {:name assignee}})))
;; versions
(defn versions
([] (versions (default-project-key)))
([project-key]
(client/get
(endpoint "/project/%s/versions" project-key)
client-opts)))
;; components
(defn components [project-key]
(client/get
(endpoint "/project/%s/components" project-key)
client-opts))
(def all-components
(memo/ttl #(map components (project-keys))
:ttl/threshold 3600000))
(defn find-component-like
"Match components across all projects"
[pattern-str]
(let [re (re-pattern (str "(?i)" pattern-str))]
(filter #(re-find re (:name %)) (mapcat :body (all-components)))))
:id "28241"
;; users
(defn get-users []
(:body
(client/get
(endpoint "/user/assignable/multiProjectSearch")
(merge client-opts
{:query-params
{"projectKeys" (project-keys-str)}}))))
;; search
(defn- projects-jql [] (str "(project in (" (project-keys-str) "))"))
(defn search [jql]
(info "JQL search" jql)
(client/get
(endpoint "/search")
(merge client-opts
{:coerce :always
:throw-exceptions false
:query-params
{:jql jql
:startAt 0
:maxResults (max-results)
:fields ["summary" "status" "assignee"]}})))
(defn search-in-projects [jql]
(search (str (projects-jql) " AND (" jql ")")))
(defn search-by-query [query]
(search-in-projects
(str
"(summary ~ \"" query "\" OR description ~ \"" query
"\" OR comment ~ \"" query "\")")))
(defn recent [] (search (projects-jql)))
;; prime cache
;; todo: move into a start fn
;; (future (all-components))