aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/manage-patch-branches.scm
blob: c11b33a3f62156f6a3f5854487f5e42d56f5eb8c (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
(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 (ice-9 textual-ports)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (json)
  #:use-module (prometheus)
  #:use-module (git)
  #:use-module (guix git)
  #:use-module (guix sets)
  #:use-module (guix memoization)
  #:use-module (guix build utils)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-build-coordinator utils)
  #:use-module (guix-build-coordinator utils fibers)
  #: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 database)
  #:use-module (guix-qa-frontpage git-repository)
  #:use-module (guix-qa-frontpage patchwork)
  #:use-module (guix-qa-frontpage guix-data-service)
  #:export (create-branch-for-issue

            patchwork-series->branch

            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 (invoke/capture-output . args)
  (match (pipe)
    ((input . output)
     (let ((pid
            (spawn
             (car args)
             args
             #:output output
             #:error output)))

       (close-port output)
       (let ((output-string (get-string-all input)))
         (close-port input)

         (values
          (waitpid pid)
          output-string))))))

(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 (parse-patch-name name)
  (let ((args
         (and
          (string-prefix? "[" name)
          (let ((stop (string-index name #\])))
            (substring name 1 stop))))
        (as-bug-number
         (lambda (arg)
           (and (string-prefix? "bug#" arg)
                (string->number (substring arg (string-length "bug#"))))))
        (as-v2
         (lambda (arg)
           (and (string-prefix? "v" arg)
                (string->number (substring arg 1)))))
        (as-patch-number
         (lambda (arg)
           (match (string-split arg #\/)
             (((= string->number index) (= string->number total))
              (and index total (<= index total)
                   (cons index total)))
             (else #f)))))
    (let analyze ((bug-number #f)
                  (branch "master")
                  (version 1)
                  (index 1)
                  (total 1)
                  (arguments
                   (if args
                       (string-split args #\,)
                       '())))
      (match arguments
        ((or ("") ())
         `((bug-number . ,bug-number)
           (branch . ,branch)
           (version . ,version)
           (index . ,index)
           (total . ,total)))
        (((= as-bug-number (? number? new-bug-number))
          arguments ...)
         (analyze new-bug-number branch version index total arguments))
        (((= as-v2 (? number? new-version))
          arguments ...)
         (analyze bug-number branch new-version index total arguments))
        (((= as-patch-number ((? number? new-index) . (? number? new-total)))
          arguments ...)
         (analyze bug-number branch version new-index new-total arguments))
        ((feature-branch arguments ...)
         (analyze bug-number feature-branch version index total arguments))))))

(define (patchwork-series->branch series)
  (match (assoc-ref series "patches")
    (#() "master")
    (#(first-patch rest ...)
     (let ((details
            (parse-patch-name
             (assoc-ref first-patch "name"))))
       (assq-ref details 'branch)))))

(define (create-branch-for-issue database issue-number patchwork-series)
  (define branch-name
    (simple-format #f "issue-~A" issue-number))

  (define base-tag
    (string-append "base-for-" branch-name))

  (define (get-base-commit)
    (let ((branch
           (patchwork-series->branch patchwork-series)))
      (if (string=? branch "master")
          (get-latest-processed-branch-revision "master")

          (with-bare-git-repository
           (lambda ()
             (invoke "git" "fetch" "--prune" "origin")
             (invoke-read-line "git" "show-ref" "--hash"
                               (string-append "origin/" branch)))))))

  (define (apply-patches)
    (define (push)
      (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))

    (define (clear-cache)
      (clear-sqlite-cache-entry database
                                'issue-data
                                #:args (list issue-number)
                                #:version 3)

      (clear-sqlite-cache-entry database
                                'issue-patches-overall-status
                                #:args (list issue-number)))

    (define (insert-log results)
      (define log
        (string-join
         (map
          (lambda (patch)
            (assq-ref patch 'output))
          results)
         "\n\n"))

      (insert-create-branch-for-issue-log database issue-number log))

    (system* "git" "tag" "--delete" base-tag)
    (invoke "git" "tag" base-tag)
    (let ((base-commit-hash
           (invoke-read-line "git" "show-ref" "--hash" base-tag)))

      (let loop ((patch-data
                  (vector->list
                   (assoc-ref patchwork-series "patches")))
                 (results '()))
        (if (null? patch-data)
            (begin
              (insert-log results)

              (if (string=? base-commit-hash
                            (with-repository (getcwd) repository
                              (oid->string
                               (reference-name->oid repository "HEAD"))))
                  (simple-format
                   (current-error-port)
                   "Commit hashes match, so no patches have been applied\n")
                  (begin
                    (push)
                    (clear-cache))))
            (let* ((patch (car patch-data))
                   (name (assoc-ref patch "name"))
                   (id   (assoc-ref patch "id")))

              (simple-format #t "Running git am --ignore-whitespace --empty=drop --3way \"~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))))

                (simple-format #t "applying ~A\n" patch-file)
                (let* ((code
                        output
                        (invoke/capture-output
                         "git" "am"
                         "--empty=drop"
                         ;; As seen in #66110, there's potentially
                         ;; something going wrong in Patchwork when
                         ;; handling carriage return characters that need
                         ;; to be included in the diff, but this option
                         ;; seems to work around that
                         "--ignore-whitespace"
                         "--3way" patch-file))
                       (new-results
                        `(((id . ,id)
                           (name . ,name)
                           (output . ,output))
                          ,@results)))
                  (if (zero? (status:exit-val (cdr code)))
                      (loop (cdr patch-data)
                            new-results)
                      (begin
                        (simple-format
                         #t "Failed to apply \"~A.patch\" (~A)\n" id name)
                        (insert-log new-results)
                        #f)))))))))

  (delete-create-branch-for-issue-log database issue-number)

  (if (not (assoc-ref patchwork-series "received_all"))
      (simple-format
       #t
       "issue ~A (series: ~A): all patches have not been received, skipping\n"
       issue-number
       (assoc-ref patchwork-series "id"))

      (begin
        (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)
         (get-base-commit)
         (lambda ()
           (with-exception-handler
               (lambda (exn)
                 (simple-format
                  (current-error-port)
                  "exception when creating branch for ~A: ~A\n"
                  issue-number
                  exn)

                 (simple-format
                  (current-error-port)
                  "deleting tag and branch for issue\n")
                 (system* "git" "push" "--delete" "patches"
                          (simple-format #f "base-for-issue-~A" issue-number))
                 (system* "git" "push" "--progress" "patches" "--delete"
                          (simple-format #f "issue-~A" issue-number))

                 (raise-exception exn))
             (lambda ()
               (with-throw-handler #t
                 apply-patches
                 (lambda args
                   (display (backtrace) (current-error-port))
                   (newline (current-error-port)))))
             #:unwind? #t))
         #:remove-after? #t))))

(define* (start-manage-patch-branches-thread database
                                             metrics-registry
                                             #:key series-count)
  (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 series-count-buffer 40)

  (define (perform-pass)
    (let ((issue-numbers
           (map string->number
                (issue-numbers-for-branches)))
          (all-patchwork-series
           (with-sqlite-cache
            database
            'latest-patchwork-series-by-issue
            latest-patchwork-series-by-issue
            #:args `(#:count ,(+ series-count series-count-buffer))
            #:ttl 120))
          (latest-master-revision
           (get-latest-processed-branch-revision "master")))

      ;; Several series can use the same base revision, so memoize looking up
      ;; the changes compared to master
      (define get-changes-compared-to-master
        (memoize
         (lambda (base-commit)
           (vector-length
            (assoc-ref
             (compare-package-derivations
              (compare-package-derivations-url
               `((base   . ,base-commit)
                 (target . ,latest-master-revision))
               ;; TODO: Maybe do something smarter here?
               #:systems '("i686-linux")))
             "derivation_changes")))))

      (simple-format #t "checking for branches to delete (looking at ~A branches)\n"
                     (length issue-numbers))
      (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)
                   (if (not (assq-ref all-patchwork-series issue-number))
                       (begin
                         (simple-format
                          (current-error-port)
                          "Removing ~A, issue no longer in latest-patchwork-series-by-issue\n"
                          issue-number)
                         #t)
                       #f)
                   (if (string=? "master"
                                 (patchwork-series->branch
                                  (assq-ref all-patchwork-series issue-number)))
                       (let ((base-commit
                              (assq-ref
                               (get-issue-branch-base-and-target-refs issue-number)
                               'base)))
                         (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"
                                          "invalid")
                                         (lambda (invalid)
                                           (string=? invalid "unknown commit"))))
                                   (begin
                                     (simple-format
                                      (current-error-port)
                                      "Removing ~A, base revision (~A) gone\n"
                                      issue-number
                                      base-commit)
                                     #t)
                                   (begin
                                     (simple-format
                                      (current-error-port)
                                      "warning: exception when fetching revision details: ~A\n"
                                      exn)
                                     #f)))
                           (lambda ()
                             (let ((derivation-change-count
                                    (get-changes-compared-to-master base-commit)))
                               (if (> derivation-change-count 10000)
                                   (begin
                                     (simple-format
                                      (current-error-port)
                                      "Removing ~A, ~A derivation changes between base (~A) and latest master revision (~A)\n"
                                      issue-number
                                      derivation-change-count
                                      base-commit
                                      latest-master-revision)
                                     #t)
                                   #f)))
                           #:unwind? #t))
                       ;; Don't do the following checks on changes for
                       ;; non-master branches.
                       #f))

           (with-bare-git-repository
            (lambda ()
              (let ((tag (simple-format #f "base-for-issue-~A" issue-number)))
                (with-exception-handler
                    (lambda (exn)
                      (simple-format
                       (current-error-port)
                       "ignoring exception when deleting tag ~A\n" tag))
                  (lambda ()
                    (run "git" "push" "patches" "--delete" tag))
                  #:unwind? #t))
              (run "git" "push" "patches" "--delete"
                   (simple-format #f "issue-~A" issue-number))))))
       issue-numbers))
    (simple-format #t "finished checking for branches to delete\n")

    (let* ((issue-numbers
           (map string->number
                (issue-numbers-for-branches)))
           (all-patchwork-series
            (with-sqlite-cache
             database
             'latest-patchwork-series-by-issue
             latest-patchwork-series-by-issue
             #:args `(#:count ,series-count)
             #:ttl 120)))
      (for-each
       (match-lambda
         ((issue-number . patchwork-series)
          (when (or (not
                     (member issue-number
                             issue-numbers
                             =))

                    ;; 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)))
                          (patchwork-series-date
                           (assoc-ref patchwork-series "date")))
                      (if (and branch-committer-date
                               patchwork-series-date)
                          (let* ((branch-committer-time
                                  (date->time-utc branch-committer-date))
                                 (patchwork-series-time
                                  (date->time-utc
                                   (string->date
                                    patchwork-series-date
                                    "~Y-~m-~dT~H:~M:~S")))
                                 (recreate-branch?
                                  (time<? branch-committer-time
                                          patchwork-series-time)))
                            (simple-format
                             #t
                             "considering recreating branch for issue ~A (~A, ~A, ~A)\n"
                             issue-number
                             branch-committer-time
                             patchwork-series-time
                             recreate-branch?)
                            (time<? branch-committer-time
                                    patchwork-series-time))
                          #f)))
            (simple-format #t "creating branch for issue ~A\n" issue-number)

            (with-exception-handler
                (const #t)
              (lambda ()
                (create-branch-for-issue database
                                         issue-number
                                         patchwork-series))
              #:unwind? #t))))
       all-patchwork-series)

      (simple-format (current-error-port)
                     "finished processing patch branches (first: ~A, last: ~A)\n"
                     (car (first all-patchwork-series))
                     (car (last all-patchwork-series)))))

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

  (call-with-new-thread
   (lambda ()
     (catch 'system-error
       (lambda ()
         (set-thread-name "patch branches"))
       (const #t))

     (while #t
       (with-exception-handler
           (lambda (exn)
             (simple-format
              (current-error-port)
              "exception in manage patch branches thread: ~A\n"
              exn)
             (unless (worker-thread-timeout-error? exn)
               (sleep 240)))
         (lambda ()
           (with-throw-handler #t
             (lambda ()
               (call-with-duration-metric
                metrics-registry
                "manage_patch_branches_duration_seconds"
                perform-pass
                #:buckets (list 30 60 120 240 480 960 1920 3840 (inf))))
             (lambda args
               (display (backtrace) (current-error-port))
               (newline (current-error-port))))
           (sleep 3600))
         #:unwind? #t)))))