aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/guix-data-service.scm
blob: 9bf799776eaf2e120d019e4c00597187d9fc81fd (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
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
(define-module (guix-qa-frontpage guix-data-service)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 match)
  #:use-module (ice-9 exceptions)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (rnrs bytevectors)
  #:use-module (zlib)
  #:use-module (json)
  #:use-module ((guix-build-coordinator utils fibers) #:select (retry-on-error))
  #:use-module (guix-qa-frontpage utils)
  #:use-module (guix-qa-frontpage patchwork)
  #:use-module (guix-qa-frontpage manage-patch-branches)
  #:export (&guix-data-service-error
            guix-data-service-error?
            guix-data-service-error-response-body
            guix-data-service-error-response-code

            guix-data-service-error->sexp

            guix-data-service-request

            package-derivations-url

            compare-package-derivations-url
            compare-package-cross-derivations-url
            compare-package-derivations

            revision-comparison-url
            revision-comparison

            list-branches-url
            list-branches

            get-latest-processed-branch-revision

            branch-revisions-url
            branch-revisions

            revision-details
            revision-details-url

            revision-system-tests-url
            revision-system-tests

            package-substitute-availability-url
            package-substitute-availability

            package-reproducibility-url))

(define-exception-type &guix-data-service-error &error
  make-guix-data-service-error
  guix-data-service-error?
  (response-body guix-data-service-error-response-body)
  (response-code guix-data-service-error-response-code))

(define (guix-data-service-error->sexp exn)
  `((exception . guix-data-service-invalid-parameters)
    (invalid_query_parameters
     .
     ,(filter-map
       (match-lambda
         ((param . val)
          (and=>
           (assoc-ref val "invalid_value")
           (lambda (value)
             (let ((message
                    (assoc-ref val "message")))
               (cons
                param
                `((value . ,value)
                  (error
                   ;; Convert the HTML error messages
                   ;; to something easier to handle
                   . ,(cond
                       ((string-contains message
                                         "failed to process revision")
                        'failed-to-process-revision)
                       ((string-contains message
                                         "yet to process revision")
                        'yet-to-process-revision)
                       ((string=? message "unknown commit")
                        'unknown-commit)
                       (else
                        'unknown-error))))))))))
       (assoc-ref
        (guix-data-service-error-response-body exn)
        "query_parameters")))))

;; Returns the port as well as the raw socket
(define* (open-socket-for-uri* uri
                               #:key (verify-certificate? #t))
  (define tls-wrap
    (@@ (web client) tls-wrap))

  (define https?
    (eq? 'https (uri-scheme uri)))

  (define plain-uri
    (if https?
        (build-uri
         'http
         #:userinfo (uri-userinfo uri)
         #:host (uri-host uri)
         #:port (or (uri-port uri) 443)
         #:path (uri-path uri)
         #:query (uri-query uri)
         #:fragment (uri-fragment uri))
        uri))

  (let ((s (open-socket-for-uri plain-uri)))
    (values
     (if https?
         (tls-wrap s (uri-host uri)
                   #:verify-certificate? verify-certificate?)
         s)
     s)))

(define* (guix-data-service-request url #:key (retry-times 0) (retry-delay 5))
  (define (make-request)
    (let ((port
           socket
           (open-socket-for-uri* (string->uri url))))

      ;; This can't be done earlier as tls-wrap/guile-gnutls doesn't support
      ;; handshake on a non blocking socket
      (let ((flags (fcntl socket F_GETFL)))
        (fcntl socket F_SETFL (logior O_NONBLOCK flags)))

      (let ((response
             body
             (http-get (string->uri url)
                       #:headers
                       '((accept-encoding . ((1 . "gzip"))))
                       #:streaming? #t
                       #:port port)))
        (if (eq? (response-code response)
                 404)
            #f
            (let ((json-body
                   (match (response-content-encoding response)
                     (('gzip)
                      ;; Stop fibers from triggering dynamic-wind in (zlib)
                      (call-with-blocked-asyncs
                       (lambda ()
                         (call-with-zlib-input-port
                          body
                          json->scm
                          #:format 'gzip))))
                     (_
                      (json->scm body)))))
              (if (or (> (response-code response)
                         400)
                      (assoc-ref json-body "error"))
                  (raise-exception
                   (make-guix-data-service-error json-body
                                                 (response-code response)))
                  (values json-body
                          response)))))))

  (if (= 0 retry-times)
      (make-request)
      (retry-on-error
       (lambda ()
         (with-fibers-port-timeouts
          make-request
          #:timeout 120))
       #:times retry-times
       #:delay retry-delay
       #:ignore (lambda (exn)
                  (and (guix-data-service-error? exn)
                       (< (guix-data-service-error-response-code exn)
                          500))))))

(define* (package-derivations-url commit
                                  #:key system target
                                  no-build-from-build-server)
  (string-append
   "https://data.qa.guix.gnu.org/revision/"
   commit
   "/package-derivations.json?"
   "system=" system
   "&target=" target
   "&field=" "(no-additional-fields)"
   "&all_results=" "on"
   (if no-build-from-build-server
       (string-append
        "&no_build_from_build_server=" no-build-from-build-server)
       "")))

(define* (compare-package-derivations-url base-and-target-refs #:key systems)
  (string-append
   "https://data.qa.guix.gnu.org/compare/package-derivations.json?"
   "base_commit=" (assq-ref base-and-target-refs 'base)
   "&target_commit=" (assq-ref base-and-target-refs 'target)
   (string-join
    (map (lambda (system)
           (simple-format #f "&system=~A" system))
         (or systems '()))
    "")
   "&target=none"
   "&field=builds&limit_results=&all_results=on"))

(define* (compare-package-cross-derivations-url base-and-target-refs #:key systems)
  (string-append
   "https://data.qa.guix.gnu.org/compare/package-derivations.json?"
   "base_commit=" (assq-ref base-and-target-refs 'base)
   "&target_commit=" (assq-ref base-and-target-refs 'target)
   (string-join
    (map (lambda (system)
           (simple-format #f "&system=~A" system))
         (or systems '()))
    "")
   "&field=builds&limit_results=&all_results=on"))

(define (compare-package-derivations url)
  (let ((json-body
         (guix-data-service-request url)))
    (if json-body
        json-body
        #f)))

(define* (revision-comparison-url base-and-target-refs #:key (json? #t))
  (string-append
   "https://data.qa.guix.gnu.org/compare"
   (if json? ".json" "")
   "?"
   "base_commit=" (assq-ref base-and-target-refs 'base)
   "&target_commit=" (assq-ref base-and-target-refs 'target)))

(define (revision-comparison url)
  (guix-data-service-request url))

(define (list-branches-url repository-id)
  (simple-format #f "https://data.qa.guix.gnu.org/repository/~A.json"
                 repository-id))

(define (list-branches url)
  (let ((json-body
         (guix-data-service-request url)))
    (vector->list
     (assoc-ref json-body "branches"))))

(define (get-latest-processed-branch-revision branch)
  (let ((json-body
         (guix-data-service-request
          (string-append
           "https://data.qa.guix.gnu.org"
           "/repository/2"
           "/branch/" branch
           "/latest-processed-revision.json"))))
    (assoc-ref
     (assoc-ref json-body "revision")
     "commit")))

(define (branch-revisions-url repository-id branch-name)
  (simple-format
   #f
   "https://data.qa.guix.gnu.org/repository/~A/branch/~A.json"
   repository-id
   branch-name))

(define (branch-revisions url)
  (let ((json-body
         (guix-data-service-request url)))
    (vector->list
     (assoc-ref json-body "revisions"))))

(define* (revision-details-url commit)
  (simple-format
   #f
   "https://data.qa.guix.gnu.org/revision/~A.json"
   commit))

(define (revision-details url)
  (guix-data-service-request url))

(define* (revision-system-tests-url commit #:key (system "x86_64-linux"))
  (simple-format
   #f
   "https://data.qa.guix.gnu.org/revision/~A/system-tests.json?system=~A"
   commit
   system))

(define (revision-system-tests url)
  (let ((json-body
         (guix-data-service-request url)))
    (vector->list
     (assoc-ref json-body "system_tests"))))

(define* (package-substitute-availability-url commit)
  (simple-format
   #f
   "https://data.qa.guix.gnu.org/revision/~A/package-substitute-availability.json"
   commit))

(define (package-substitute-availability url)
  (let ((json-body
         (guix-data-service-request url)))
    (if json-body
        (assoc-ref json-body "substitute_servers")
        #f)))

(define* (package-reproducibility-url commit)
  (simple-format
   #f
   "https://data.qa.guix.gnu.org/revision/~A/package-reproducibility.json"
   commit))