aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/manage-patch-branches.scm
blob: 9df7ba0c5bb97f8e2f3a2aa8c567f4b80042f7f5 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(define-module (guix-qa-frontpage manage-patch-branches)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 match)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 threads)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (json)
  #:use-module (guix sets)
  #:use-module (guix build utils)
  #:use-module (guix-build-coordinator utils)
  #:use-module ((guix build download) #:select (http-fetch))
  #:use-module ((guix build utils) #:select (with-directory-excursion))
  #:use-module (guix-qa-frontpage mumi)
  #:use-module (guix-qa-frontpage git-repository)
  #:use-module (guix-qa-frontpage patchwork)
  #:use-module (guix-qa-frontpage guix-data-service)
  #:export (start-manage-patch-branches-thread

            get-issue-branch-base-and-target-refs))

(define (run . args)
  (simple-format (current-error-port)
                 "running: ~A\n"
                 (string-join args " "))
  (apply invoke args))

(define (issue-numbers-for-branches)
  (define rexp
    (make-regexp "\\/issue-([0-9]*)$"))

  (with-bare-git-repository
   (lambda ()
     (run "git" "fetch" "--prune" "patches")

     (let ((pipe (open-pipe* OPEN_READ
                             "git" "ls-remote" "--heads" "patches")))
       (let loop ((line (read-line pipe))
                  (branches '()))
         (if (eof-object? line)
             (begin
               (close-pipe pipe)
               (reverse branches))
             (loop (read-line pipe)
                   (match (regexp-exec rexp line)
                     (#f branches)
                     (issue-number-match
                      (cons (match:substring issue-number-match 1)
                            branches))))))))))

(define (get-issue-branch-base-and-target-refs issue)
  (define base-tag
    (string-append "base-for-issue-" (number->string issue)))

  (define target-branch
    (string-append "patches/issue-" (number->string issue)))

  (let ((base (get-commit base-tag))
        (target (get-commit target-branch)))

    (and base
         target
         `((base   . ,base)
           (target . ,target)))))

(define* (pwclient-check-create
          patch-id
          #:key
          (project "guix-patches")
          status
          context
          target-url
          description)

  (apply invoke
         `("pwclient"
           "check-create"
           "-p" ,project
           "-c" ,context
           "-s" ,status
           ,(simple-format #f "~A" patch-id)
           ,@(if description
                 `("-d" ,description)
                 '())
           ,@(if target-url
                 `("-u" ,target-url)
                 '()))))

(define (invoke-read-line prog . args)
  (let* ((pipe (apply open-pipe* OPEN_READ prog
                      args))
         (result
          (read-line pipe)))
    (close-pipe pipe)
    result))

(define (create-branch-for-issue issue-number patchwork-series)
  (define (apply-patches)
    (let ((series-data
           (call-with-values
               (lambda ()
                 (http-get (string->uri
                            (string-append
                             (%patchwork-instance) "/api/1.0"
                             "/series/" patchwork-series "/"))
                           #:streaming? #t))
             (lambda (response body)
               (json->scm body)))))

      (if (assoc-ref series-data "received_all")
          (let* ((patch-data
                  (vector->list
                   (assoc-ref series-data "patches")))
                 (branch-name
                  (simple-format #f "issue-~A" issue-number))
                 (base-tag
                  (string-append "base-for-" branch-name)))

            (simple-format #t "all patches have been received\n")

            (system* "git" "tag" "--delete" base-tag)
            (invoke "git" "tag" base-tag)

            (let ((patch-ids
                   (map
                    (lambda (patch)
                      (let ((name (assoc-ref patch "name"))
                            (id   (assoc-ref patch "id")))

                        (simple-format
                         #t "Running git am \"~A.patch\" (~A)\n"
                         id name)

                        (let ((patch-file
                               (simple-format #f "~A.patch" id)))
                          (call-with-output-file patch-file
                            (lambda (output)
                              (let ((port size (http-fetch
                                                (string->uri (assoc-ref patch "mbox")))))
                                (dump-port port output))))

                          (with-exception-handler
                              (lambda (exn)
                                (simple-format #t "exception when applying patch ~A: ~A\n"
                                               patch-file exn)
                                (raise-exception exn))
                            (lambda ()
                              (simple-format #t "applying ~A\n" patch-file)
                              (invoke "git" "am" "--empty=drop" "--3way" patch-file))
                            #:unwind? #t))
                        id))
                    patch-data)))

              (let ((base-commit-hash
                     (invoke-read-line "git" "show-ref" "--hash" base-tag))
                    (target-commit-hash
                     (invoke-read-line "git" "rev-parse" "HEAD")))

                (if (string=? base-commit-hash
                              target-commit-hash)
                    (simple-format
                     (current-error-port)
                     "Commit hashes match, so no patches have been applied\n")

                    (begin
                      (system* "git" "push" "--delete" "patches" base-tag)
                      (invoke "git" "push" "--verbose" "patches" base-tag)

                      ;; Delete the branch, to provide a clearer history
                      (system* "git" "push" "--progress" "patches" "--delete" branch-name)

                      (invoke "git" "push" "--progress" "-u" "patches" branch-name))))))

          (begin
            (simple-format #t "all patches have not been received, skipping\n")))))

  (let ((latest-master-commit
         (get-latest-processed-branch-revision "master")))

    (with-bare-git-repository
     (lambda ()
       (invoke "git" "fetch" "--prune" "origin")
       (system* "git" "worktree" "remove" "--force"
                (simple-format #f "../issue-~A" issue-number))
       (system* "git" "branch" "-D"
                (simple-format #f "issue-~A" issue-number))))

    (with-git-worktree
     (simple-format #f "issue-~A" issue-number)
     latest-master-commit
     (lambda ()
       (with-exception-handler
           (lambda (exn)
             (simple-format
              (current-error-port)
              "exception when applying patch: ~A\n"
              exn))
         apply-patches
         #:unwind? #t))
     #:remove-after? #t)))

(define (start-manage-patch-branches-thread)
  (define (dig alist . parts)
    (if (pair? alist)
        (match parts
          ((part)
           (assoc-ref alist part))
          ((part . rest)
           (if (list? alist)
               (apply dig
                      (assoc-ref alist part)
                      (cdr parts))
               #f)))
        #f))

  (define (perform-pass)
    (let* ((issue-numbers
            (issue-numbers-for-branches))
           (all-patchwork-series
            (latest-patchwork-series-by-issue))
           (series-to-create-branches-for
            (take all-patchwork-series
                  150)))
      (with-bare-git-repository
       (lambda ()
         (simple-format #t "checking for branches to delete\n")
         (for-each
          (lambda (issue-number)
            (when (or (if (not (mumi-issue-open? issue-number))
                          (begin (simple-format (current-error-port)
                                                "Removing ~A, issue closed\n"
                                                issue-number)
                                 #t)
                          #f)
                      (let* ((series
                              (assq-ref all-patchwork-series
                                        (string->number issue-number)))
                             (comparison-url
                              (and series
                                   (and=>
                                    (get-issue-branch-base-and-target-refs
                                     (string->number issue-number))
                                    patch-series-compare-url))))
                        (with-exception-handler
                            (lambda (exn)
                              (if (and
                                   (guix-data-service-error? exn)
                                   (and=>
                                    (dig
                                     (guix-data-service-error-response-body exn)
                                     "query_parameters"
                                     "base_commit"
                                     "message")
                                    (lambda (message)
                                      (string=? message "unknown commit"))))
                                  (begin
                                    (simple-format
                                     (current-error-port)
                                     "Removing ~A, base revision gone\n"
                                     issue-number)
                                    #t)
                                  #f))
                          (lambda ()
                            (if comparison-url
                                (patch-series-comparison comparison-url)
                                #f)
                            #f)
                          #:unwind? #t)))

              (run "git" "push" "patches" "--delete"
                   (string-append "base-for-issue-" issue-number))
              (run "git" "push" "patches" "--delete"
                   (string-append "issue-" issue-number))))
          issue-numbers)))

      (simple-format #t "finished checking for branches to delete\n")

      (for-each
       (match-lambda
         ((issue-number . patchwork-series)
          (when (or (not
                     (member (number->string issue-number)
                             issue-numbers
                             string=?))

                    ;; Does the branch need re-creating with a new series?
                    (let ((branch-committer-date
                           (get-git-branch-head-committer-date
                            (simple-format #f "patches/issue-~A" issue-number))))
                      (if branch-committer-date
                          (time<?
                           (date->time-utc branch-committer-date)
                           (date->time-utc
                            (string->date
                             (assoc-ref patchwork-series "date")
                             "~Y-~m-~dT~H:~M:~S")))
                          #f)))
            (simple-format #t "creating branch for issue ~A\n" issue-number)
            (with-exception-handler
                (lambda (exn)
                  (simple-format
                   (current-error-port)
                   "exception when creating branch for ~A: ~A\n"
                   issue-number
                   exn))
              (lambda ()
                (with-throw-handler #t
                  (lambda ()
                    (create-branch-for-issue issue-number
                                             (number->string
                                              (assoc-ref patchwork-series
                                                         "id"))))
                  (lambda args
                    (display (backtrace) (current-error-port))
                    (newline (current-error-port)))))))))
       series-to-create-branches-for)

      (simple-format (current-error-port)
                     "finished processing patch branches (last issue: ~A)\n"
                     (car (last series-to-create-branches-for))))
    (sleep 3600))

  (setenv "GIT_SSH_COMMAND"
          "ssh -o StrictHostKeyChecking=no")

  (call-with-new-thread
   (lambda ()
     (while #t
       (with-exception-handler
           (lambda (exn)
             (simple-format
              (current-error-port)
              "exception in manage patch branches thread: ~A\n"
              exn))
         (lambda ()
           (with-throw-handler #t
             perform-pass
             (lambda args
               (display (backtrace) (current-error-port))
               (newline (current-error-port)))))
         #:unwind? #t)))))