aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/patchwork.scm
blob: cd5bc269ebeaa7284ad89550ae4edc2a38ad4761 (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
(define-module (guix-qa-frontpage patchwork)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (ice-9 regex)
  #:use-module (rnrs bytevectors)
  #:use-module (json)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (guix-build-coordinator utils)
  #:use-module (guix-qa-frontpage mumi)
  #:export (%patchwork-instance

            patchwork-patches
            latest-patchwork-series-by-issue

            patchwork-patch-checks))

(define %patchwork-instance
  (make-parameter "https://patches.guix-patches.cbaines.net"))

(define %link-regex
  (make-regexp "<(.*?)>;?(.*)"))
(define %link-param-regex
  (make-regexp "\\s*(.*)=(.*)"))

(define (parse-link link)
  (define (strip-quotes s)
    (if (and (string-suffix? "\"" s)
             (string-prefix? "\"" s))
        (string-drop-right
         (string-drop s 1)
         1)
        s))

  (let ((link-match (regexp-exec %link-regex link)))
    `((uri    . ,(string->uri (match:substring link-match 1)))
      ,@(map
         (lambda (param)
           (let ((param-match
                  (regexp-exec %link-param-regex param)))
             (cons (string->symbol
                    (match:substring param-match 1))
                   (strip-quotes
                    (match:substring param-match 2)))))
         (string-split (match:substring link-match 2)
                       #\;)))))

(define* (patchwork-patches
          #:key patchwork
          (archived? #f)
          (order "-id")
          (states '("1" "2" "7" "11")))
  (define (set-uri-scheme uri scheme)
    (string->uri
     (simple-format
      #f
      "~A:~A"
      scheme
      (string-join
       (drop (string-split (uri->string uri) #\:) 1)
       ":"))))

  (define initial-uri
    (string->uri
     (string-append (or patchwork
                        (%patchwork-instance))
                    "/api/patches/?"
                    "per_page=500&"
                    "order=" order "&"
                    "archived=" (if archived? "true" "false") "&"
                    (string-join
                     (map (lambda (state)
                            (string-append "state=" state))
                          states)
                     "&"))))

  (define (make-request uri)
    (let-values (((response body)
                  (http-request uri
                                #:decode-body? #f)))
      (append!
       (vector->list
        (json-string->scm (utf8->string body)))
       (or
        (and=> (assq-ref (response-headers response)
                         'link)
               (lambda (link-header)
                 (and=>
                  (find (lambda (link)
                          (let ((link-details (parse-link link)))
                            (string=?
                             (assq-ref link-details 'rel)
                             "next")))
                        (string-split link-header #\,))
                  (lambda (next-link)
                    (let ((link-details (parse-link next-link)))
                      (make-request
                       ;; The link headers don't use https, so to
                       ;; avoid the redirect, modify the URI
                       (set-uri-scheme
                        (assq-ref link-details 'uri)
                        (uri-scheme uri))))))))
        '()))))

  (retry-on-error (lambda () (make-request initial-uri))
                  #:times 10
                  #:delay 5))

(define* (latest-patchwork-series-by-issue
          #:key patchwork)
  (define (patch->issue-number patch)
    (string->number
     (match:substring
      (string-match "\\[bug#([0-9]*).*\\]"
                    (assoc-ref patch "name"))
      1)))

  (let ((result
         (make-hash-table 2048)))

    (for-each
     (lambda (patch)
       (let ((issue-number
              (patch->issue-number patch))
             (patch-series
              (assoc-ref patch "series")))

         ;; Some patches are missing series when patchwork has trouble
         ;; processing them
         (when (not (eq? (vector-length patch-series) 0))
           (or (and=>
                (hash-ref result issue-number)
                (lambda (series)
                  (let ((patch-series-number
                         (assoc-ref (vector-ref patch-series 0)
                                    "id")))
                    (when (eq? (assoc-ref series "id")
                               patch-series-number)
                      (hash-set!
                       result
                       issue-number
                       `(,@(alist-delete "patches" series)
                         ("patches" . (,@(assoc-ref series "patches")
                                       ,patch))))))))
               (hash-set!
                result
                issue-number
                `(,@(vector-ref patch-series 0)
                  ("patches" . (,patch))))))))
     (patchwork-patches #:patchwork patchwork))

    (let* ((data (hash-map->list cons result))
           (mumi-data (mumi-bulk-issues
                       (map first data))))
      (sort!
       (filter-map (lambda (data mumi)
                     (if (assq-ref mumi 'open?)
                         `(,@data
                           (mumi . ,mumi))
                         #f))
                   data
                   mumi-data)
       (lambda (a b)
         (> (first a)
            (first b)))))))

(define (patchwork-patch-checks checks-url)
  ;; Patchwork uses http URIs, so convert here to avoid the redirect
  (define https-uri
    (string->uri
     (string-append
      "https:"
      (string-join
       (drop (string-split checks-url #\:) 1)
       ":"))))

  (define (make-request)
    (let-values (((response body)
                  (http-request https-uri
                                #:decode-body? #f)))
      (vector->list
       (json-string->scm (utf8->string body)))))

  (retry-on-error make-request
                  #:times 10
                  #:delay 5))