summaryrefslogtreecommitdiff
path: root/mumi/client.scm
blob: ae3a0a94e1e0f2158cd57c37919a5a2cdcacfa5d (about) (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;;; mumi -- Mediocre, uh, mail interface
;;; Copyright © 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of mumi.
;;;
;;; mumi 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.
;;;
;;; mumi 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 mumi.  If not, see <http://www.gnu.org/licenses/>.

(define-module (mumi client)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-43)
  #:use-module (term ansi-color)
  #:use-module (web uri)
  #:use-module (kolam http)
  #:use-module (mumi config)
  #:use-module (mumi web view utils)
  #:export (search
            print-current-issue
            set-current-issue!
            clear-current-issue!))

(define (git-top-level)
  "Return the top-level directory of the current git repository."
  (let loop ((curdir (getcwd)))
    (cond
     ((file-exists? (string-append curdir "/.git"))
      curdir)
     ((string=? curdir "/")
      (error "No git top level found"))
     (else
      (loop (dirname curdir))))))

(define (client-config-directory)
  "Return client configuration directory."
  (string-append (git-top-level) "/.mumi"))

(define (client-config key)
  "Return client configuration value corresponding to KEY."
  (or (assq-ref (call-with-input-file (string-append (client-config-directory)
                                                     "/config")
                  read)
                key)
      (case key
        ((mumi-scheme) 'https)
        (else (format (current-error-port)
                      "Key '~a not configured for mumi client.~%"
                      key)))))

(define (graphql-endpoint)
  "Return GraphQL endpoint."
  (uri->string
   (build-uri (client-config 'mumi-scheme)
              #:host (client-config 'mumi-host)
              #:path "/graphql")))

(define (iso8601->date str)
  "Convert ISO-8601 date/time+zone string to date object."
  (string->date str "~Y-~m-~dT~H:~M:~S~z"))

(define (list-issue issue)
  "List issue described by ISSUE association list."
  (display (colorize-string
            (string-append "#"
                           (number->string (assoc-ref issue "number")))
            'YELLOW))
  (display " ")
  (unless (assoc-ref issue "open")
    (display (colorize-string "✓" 'BOLD 'GREEN))
    (display " "))
  (display (colorize-string
            (assoc-ref issue "title")
            'MAGENTA 'UNDERLINE))
  (newline)
  (display (string-append
            "opened "
            (colorize-string (time->string
                              (iso8601->date (assoc-ref issue "date")))
                             'CYAN)
            " by "
            (colorize-string
             (let ((submitter (assoc-ref issue "submitter")))
               (if (eq? (assoc-ref submitter "name") 'null)
                   (assoc-ref submitter "address")
                   (assoc-ref submitter "name")))
             'CYAN)))
  (newline))

(define (search query)
  "Search for issues with QUERY and list results."
  (vector-for-each (lambda (_ issue)
                     (list-issue issue))
                   (assoc-ref
                    (graphql-http-get (graphql-endpoint)
                                      `(document
                                        (query (#(issues #:search ,query)
                                                number
                                                title
                                                open
                                                date
                                                (submitter name address)))))
                    "issues")))

(define (current-issue-file)
  "Return path to current issue number file."
  (string-append (client-config-directory) "/current-issue"))

(define (current-issue-number)
  "Return current issue number."
  (let ((issue-file (current-issue-file)))
    (and (file-exists? issue-file)
         (call-with-input-file issue-file
           read))))

(define (print-current-issue)
  "Print current issue."
  (let ((issue-number (current-issue-number)))
    (if issue-number
        (list-issue
         (assoc-ref
          (graphql-http-get (graphql-endpoint)
                            `(document
                              (query (#(issue #:number ,issue-number)
                                      number
                                      title
                                      open
                                      date
                                      (submitter name address)))))
          "issue"))
        (begin
          (format (current-error-port) "No current issue!~%")
          (exit #f)))))

(define (set-current-issue! issue-number)
  "Set current issue number."
  ;; TODO: Write file atomically.
  (call-with-output-file (current-issue-file)
    (cut write issue-number <>)))

(define (clear-current-issue!)
  "Clear current issue."
  (let ((issue-file (current-issue-file)))
    (when (file-exists? issue-file)
      (delete-file issue-file))))