aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/patchwork.scm
blob: 7f2de190ff297822a4a04defdc4d0613f2fa1997 (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
(define-module (guix-qa-frontpage patchwork)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 match)
  #: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)
  #:use-module (guix-qa-frontpage debbugs)
  #:export (%patchwork-instance

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

(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"))
          pages)
  (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 page-count)
    (let-values (((response body)
                  (retry-on-error
                   (lambda ()
                     (http-request uri
                                   #:decode-body? #f))
                   #:times 2
                   #:delay 3)))
      (append!
       (vector->list
        (json-string->scm (utf8->string body)))
       (or
        (and=> (if (and pages
                        (> page-count pages))
                   #f
                   (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))
                       (+ 1 page-count)))))))
        '()))))

  (make-request initial-uri 1))

(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
                        #:pages 10))

    (let* ((data (hash-map->list cons result))
           (mumi-data
            (call-with-delay-logging mumi-bulk-issues
                                     #:args
                                     (list
                                      (map first data))))
           (debbugs-guix-usertag-data
            (call-with-delay-logging debbugs-get-issues-with-guix-usertag))
           (usertag-lookup
            (let ((hash-table (make-hash-table)))
              (for-each
               (match-lambda
                 ((tag . issues)
                  (for-each
                   (lambda (issue)
                     (hash-set! hash-table
                                issue
                                (cons tag
                                      (or (hash-ref hash-table issue)
                                          '()))))
                   (if (pair? issues)
                       issues
                       (list issues)))))
               debbugs-guix-usertag-data)
              hash-table)))
      (sort!
       (filter-map (lambda (data mumi)
                     (let ((issue-number (car data)))
                       (if (and (assq-ref mumi 'open?)
                                (every
                                 (lambda (merged-issue-number)
                                   (if (< merged-issue-number
                                          issue-number)
                                       (not (hash-ref result merged-issue-number))
                                       #t))
                                 (assq-ref mumi 'merged-with)))
                           `(,@data
                             (mumi . ,mumi)
                             (usertags . ,(or (hash-ref usertag-lookup
                                                        (car data))
                                              '())))
                           #f)))
                   data
                   mumi-data)
       (lambda (a b)
         ;; Sort by issue number
         (> (first a)
            (first b)))))))