all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 09f83eefd09830e37a305069b7c7a5b8eb878f88 9814 bytes (raw)
name: mumi/client.scm 	 # note: path name is non-authoritative(*)

  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
 
;;; 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 (rnrs io ports)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-43)
  #:use-module (srfi srfi-71)
  #:use-module (srfi srfi-171)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (term ansi-color)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (web uri)
  #:use-module (email email)
  #: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!
            send-email))

(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))))

(define* (issue-number-of-message message-id #:optional (retries 15))
  "Return issue number that MESSAGE-ID belongs to. Retry RETRIES number
of times with an interval of 60 seconds between retries."
  ;; TODO: Re-implement this using our GraphQL endpoint once it
  ;; supports retrieving the issue from a message ID. Later,
  ;; re-implement this using a GraphQL subscription when kolam
  ;; supports it.
  (define (poll-issue-number-of-message message-id)
    (let ((response _ (http-get (build-uri (client-config 'mumi-scheme)
                                           #:host (client-config 'mumi-host)
                                           #:path (string-append "/msgid/" message-id)))))
      (and (>= (response-code response) 300)
           (< (response-code response) 400)
           (match (split-and-decode-uri-path
                   (uri-path (response-location response)))
             (("issue" issue-number)
              (string->number issue-number))))))

  (let loop ((i retries))
    (if (zero? i)
        (begin
          (format (current-error-port)
                  "Mail not acknowledged by issue tracker. Giving up.~%")
          (exit #f))
        (or (poll-issue-number-of-message message-id)
            (begin
              (let ((retry-interval 60))
                (format (current-error-port)
                        "Trial ~a/~a: Server has not yet received our email. Will retry in ~a seconds.~%"
                        (1+ i) retries retry-interval)
                (sleep retry-interval))
              (loop (1- retries)))))))

(define (call-with-input-pipe command proc)
  "Call PROC with input pipe to COMMAND. COMMAND is a list of program
arguments."
  (match command
    ((prog args ...)
     (let ((port #f))
       (dynamic-wind
         (lambda ()
           (set! port (apply open-pipe* OPEN_READ prog args)))
         (cut proc port)
         (cut close-pipe port))))))

(define (git-send-email to patch)
  "Send email using git send-email and return the message ID of the sent
email."
  (let ((command (list "git" "send-email"
                       (string-append "--to=" to)
                       patch)))
    (display (string-join command))
    (newline)
    (call-with-input-pipe command
      (lambda (port)
        ;; FIXME: This messes up the order of stdout and stderr.
        (let ((message-id
               ;; Read till you get the Message ID.
               (port-transduce (tlog (lambda (_ line)
                                       (display line)
                                       (newline)))
                               (rany (lambda (line)
                                       (and (string-prefix-ci? "Message-ID:" line)
                                            (assq-ref
                                             (parse-email-headers
                                              (string-append line "\n"))
                                             'message-id))))
                               get-line
                               port)))
          ;; Pass through the rest.
          (display (get-string-all port))
          message-id)))))

(define (send-email patches)
  "Send PATCHES via email."
  (match patches
    ((first-patch other-patches ...)
     ;; If an issue is current, send patches to that issue's email
     ;; address. Else, send first patch to the patch email address and
     ;; get an issue number. Then, send the remaining patches to that
     ;; issue's email address.
     (for-each (cute git-send-email
                     (string-append (number->string
                                     (or (current-issue-number)
                                         (issue-number-of-message
                                          (git-send-email (client-config 'patch-email-address)
                                                          first-patch))))
                                    "@"
                                    (client-config 'debbugs-host))
                     <>)
               (if (current-issue-number)
                   patches
                   other-patches)))))

debug log:

solving 09f83ee ...
found 09f83ee in https://yhetil.org/guix/20230221003336.5374-1-arunisaac@systemreboot.net/
found ae3a0a9 in https://yhetil.org/guix/20230221003326.5353-1-arunisaac@systemreboot.net/ ||
	https://yhetil.org/guix/20230308153658.19929-3-arunisaac@systemreboot.net/
found e4a0123 in https://yhetil.org/guix/20230221003318.5334-1-arunisaac@systemreboot.net/ ||
	https://yhetil.org/guix/20230220014139.27804-1-arunisaac@systemreboot.net/ ||
	https://yhetil.org/guix/20230308153658.19929-2-arunisaac@systemreboot.net/

applying [1/3] https://yhetil.org/guix/20230221003318.5334-1-arunisaac@systemreboot.net/
diff --git a/mumi/client.scm b/mumi/client.scm
new file mode 100644
index 0000000..e4a0123

Checking patch mumi/client.scm...
Applied patch mumi/client.scm cleanly.

skipping https://yhetil.org/guix/20230220014139.27804-1-arunisaac@systemreboot.net/ for e4a0123
skipping https://yhetil.org/guix/20230308153658.19929-2-arunisaac@systemreboot.net/ for e4a0123
index at:
100644 e4a0123933b45a1dc347ee1cb5d608984033dc6d	mumi/client.scm

applying [2/3] https://yhetil.org/guix/20230221003326.5353-1-arunisaac@systemreboot.net/
diff --git a/mumi/client.scm b/mumi/client.scm
index e4a0123..ae3a0a9 100644

Checking patch mumi/client.scm...
Applied patch mumi/client.scm cleanly.

skipping https://yhetil.org/guix/20230308153658.19929-3-arunisaac@systemreboot.net/ for ae3a0a9
index at:
100644 ae3a0a94e1e0f2158cd57c37919a5a2cdcacfa5d	mumi/client.scm

applying [3/3] https://yhetil.org/guix/20230221003336.5374-1-arunisaac@systemreboot.net/
diff --git a/mumi/client.scm b/mumi/client.scm
index ae3a0a9..09f83ee 100644

Checking patch mumi/client.scm...
Applied patch mumi/client.scm cleanly.

index at:
100644 09f83eefd09830e37a305069b7c7a5b8eb878f88	mumi/client.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.