blob: 5931c360d2464b5804a1d87a61f69b49c17e3876 (
plain)
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
|
;;; Copyright (c) 2022 Teddy Wing
;;;
;;; This file is part of Wajir.
;;;
;;; Wajir 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.
;;;
;;; Wajir 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 Wajir. If not, see <https://www.gnu.org/licenses/>.
(in-package :wajir)
(defun main ()
;; Query page of issues
;; Start watching issue
;; Send email to program containing message with issue metadata
;; Continue to next page
;; Disable interactive debugger.
(setf *debugger-hook* #'debug-ignore)
(handler-case
(interrupt:with-user-abort
(handler-bind ((error #'(lambda (e)
(exit-with-error e sysexits:+unavailable+))))
(let ((config (parse-options)))
(run config))))
;; Control-c
(interrupt:user-abort ()
(opts:exit 130))))
(defun debug-ignore (condition hook)
(declare (ignore hook))
(princ condition)
(abort))
(defun run (config)
(let ((basic-auth-token (cl-base64:string-to-base64-string
(format nil
"~A:~A"
(login config)
(token config)))))
(loop
with start-at = 0
with max-results = 0
do
(progn
(let* ((response (fetch-issues
(endpoint config)
(jql config)
start-at
:basic-auth-token basic-auth-token))
(total (gethash "total" response)))
(when (verbose config)
(format t "start-at: ~A max-results: ~A total: ~A~%"
start-at
max-results
total))
;; Watch each issue.
(loop for issue
across (gethash "issues" response)
do (watch-issue
config
issue
:basic-auth-token basic-auth-token))
;; Stop looping if we're on the last page of results.
(when (> start-at
(- total max-results))
(return))
;; Step `start-at` for next page.
(setf max-results (gethash "maxResults" response))
(setf start-at (+ (gethash "startAt" response)
max-results)))))))
(defun fetch-issues (endpoint jql start-at &key basic-auth-token)
(jzon:parse
(dex:post (format nil "https://~A/rest/api/2/search" endpoint)
:content
(jzon:stringify
`((:jql . ,jql)
(:fields . #("id"
"key"
"project"
"summary"
"description"
"issuetype"
"creator"
"reporter"))
(:|startAt| . ,start-at)))
:headers `((:content-type . "application/json")
(:authorization
. ,(format nil "Basic ~A" basic-auth-token))))))
(defun watch-issue (config issue &key basic-auth-token)
;; 1. Watch issue in Jira
;; 2. Send email
(when (verbose config)
(format t "Watching issue ~A~%" (gethash "key" issue)))
(add-watcher
(endpoint config)
issue
:basic-auth-token basic-auth-token)
(if (sendmail config)
(deliver-email config issue)))
(defun add-watcher (endpoint issue &key basic-auth-token)
"Add the authenticated user as a watcher to issue."
(dex:post
(format nil
"https://~A/rest/api/2/issue/~A/watchers"
endpoint
(gethash "id" issue))
:headers `((:authorization . ,(format nil "Basic ~A" basic-auth-token)))))
|