aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.lisp
blob: 98fe7546e6c18201795dc5b547e5eafb1a09d360 (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
(in-package :wajir)

(defun main ()
  ;; Query page of issues
  ;; [Check not in database?] <- no
  ;; Start watching issue
  ;; Send email to ^maildir^program^ containing message with issue metadata
  ;; Continue to next page

  (let ((config (make-instance 'config
                                :login "name@example.com"
                                :token "atlassian-token"
                                :endpoint "example.atlassian.net"
                                :jql "project = \"FAKE\" AND watcher != currentUser() AND key > \"FAKE-100\" ORDER BY created DESC")))

    (run config)))

(defun run (config)
  (let ((basic-auth-token (cl-base64:string-to-base64-string
                            (format nil
                                    "~A:~A"
                                    (login config)
                                    (token config)))))

    (loop
      (let* ((response (fetch-issues
                         (endpoint config)
                         (jql config)
                         :basic-auth-token basic-auth-token))

             (start-at (gethash "startAt" response))
             (max-results (gethash "maxResults" response))
             (total (gethash "total" response)))

        (format t "start-at: ~A max-results: ~A total: ~A~%"
                start-at
                max-results
                total)

        (loop for issue
              across (gethash "issues" response)
              do (watch-issue issue))

        ;; Stop looping if we're on the last page of results.
        (when (> start-at
                 (- total max-results))
          (return))))))

(defun fetch-issues (endpoint jql &key basic-auth-token)
  (jzon:parse
    (dex:post (format nil "https://~A/rest/api/3/search" endpoint)
              :content
              (jzon:stringify
                `((:jql . ,jql)
                  (fields . ("id" "key" "self"))))
              :headers `((:content-type . "application/json")
                         (:authorization
                          . ,(format nil "Basic ~A" basic-auth-token))))))

(defun watch-issue (issue)
  (format t "Watching issue ~A~%" (gethash "key" issue)))