news: Add entry for script execution via "guix repl".
[jackhill/guix/guix.git] / guix / ci.scm
CommitLineData
b3517f3f 1;;; GNU Guix --- Functional package management for GNU
2c33901f 2;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
b3517f3f
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix ci)
20 #:use-module (guix http-client)
a85a74ce
LC
21 #:use-module (guix json)
22 #:use-module (json)
a3b72a8f 23 #:use-module (srfi srfi-1)
a85a74ce 24 #:use-module (ice-9 match)
b3517f3f
LC
25 #:export (build?
26 build-id
27 build-derivation
28 build-system
29 build-status
30 build-timestamp
31
a3b72a8f
LC
32 checkout?
33 checkout-commit
34 checkout-input
35
36 evaluation?
37 evaluation-id
38 evaluation-spec
39 evaluation-complete?
40 evaluation-checkouts
41
b3517f3f
LC
42 %query-limit
43 queued-builds
a3b72a8f
LC
44 latest-builds
45 latest-evaluations
a85a74ce 46 evaluations-for-commit))
b3517f3f
LC
47
48;;; Commentary:
49;;;
50;;; This module provides a client to the HTTP interface of the Hydra and
51;;; Cuirass continuous integration (CI) tools.
52;;;
53;;; Code:
54
4e05bbb0
MO
55(define-json-mapping <build-product> make-build-product
56 build-product?
57 json->build-product
58 (type build-product-type)
59 (file-size build-product-file-size)
60 (path build-product-path))
61
a85a74ce
LC
62(define-json-mapping <build> make-build build?
63 json->build
64 (id build-id "id") ;integer
b3517f3f
LC
65 (derivation build-derivation) ;string | #f
66 (system build-system) ;string
a85a74ce 67 (status build-status "buildstatus" ) ;integer
4e05bbb0
MO
68 (timestamp build-timestamp) ;integer
69 (products build-products "buildproducts" ;<build-product>*
70 (lambda (products)
71 (map json->build-product
72 ;; Before Cuirass 3db603c1, #f is always returned.
73 (if products
74 (vector->list products)
75 '())))))
b3517f3f 76
a85a74ce
LC
77(define-json-mapping <checkout> make-checkout checkout?
78 json->checkout
a3b72a8f
LC
79 (commit checkout-commit) ;string (SHA1)
80 (input checkout-input)) ;string (name)
81
a85a74ce
LC
82(define-json-mapping <evaluation> make-evaluation evaluation?
83 json->evaluation
a3b72a8f 84 (id evaluation-id) ;integer
2c33901f 85 (spec evaluation-spec "specification") ;string
a85a74ce
LC
86 (complete? evaluation-complete? "in-progress"
87 (match-lambda
88 (0 #t)
89 (_ #f))) ;Boolean
90 (checkouts evaluation-checkouts "checkouts" ;<checkout>*
91 (lambda (checkouts)
92 (map json->checkout
93 (vector->list checkouts)))))
a3b72a8f 94
b3517f3f
LC
95(define %query-limit
96 ;; Max number of builds requested in queries.
97 1000)
98
99(define (json-fetch url)
100 (let* ((port (http-fetch url))
101 (json (json->scm port)))
102 (close-port port)
103 json))
104
b3517f3f
LC
105(define* (queued-builds url #:optional (limit %query-limit))
106 "Return the list of queued derivations on URL."
107 (let ((queue (json-fetch (string-append url "/api/queue?nr="
108 (number->string limit)))))
a85a74ce 109 (map json->build (vector->list queue))))
b3517f3f 110
a3b72a8f 111(define* (latest-builds url #:optional (limit %query-limit)
ef6f9f16
MO
112 #:key
113 evaluation
114 system
115 job)
a3b72a8f
LC
116 "Return the latest builds performed by the CI server at URL. If EVALUATION
117is an integer, restrict to builds of EVALUATION. If SYSTEM is true (a system
118string such as \"x86_64-linux\"), restrict to builds for SYSTEM."
119 (define* (option name value #:optional (->string identity))
120 (if value
121 (string-append "&" name "=" (->string value))
122 ""))
123
b3517f3f 124 (let ((latest (json-fetch (string-append url "/api/latestbuilds?nr="
a3b72a8f
LC
125 (number->string limit)
126 (option "evaluation" evaluation
127 number->string)
ef6f9f16
MO
128 (option "system" system)
129 (option "job" job)))))
b3517f3f
LC
130 ;; Note: Hydra does not provide a "derivation" field for entries in
131 ;; 'latestbuilds', but Cuirass does.
a85a74ce 132 (map json->build (vector->list latest))))
a3b72a8f
LC
133
134(define* (latest-evaluations url #:optional (limit %query-limit))
135 "Return the latest evaluations performed by the CI server at URL."
136 (map json->evaluation
a85a74ce
LC
137 (vector->list
138 (json->scm
139 (http-fetch (string-append url "/api/evaluations?nr="
140 (number->string limit)))))))
a3b72a8f
LC
141
142
143(define* (evaluations-for-commit url commit #:optional (limit %query-limit))
144 "Return the evaluations among the latest LIMIT evaluations that have COMMIT
145as one of their inputs."
146 (filter (lambda (evaluation)
147 (find (lambda (checkout)
148 (string=? (checkout-commit checkout) commit))
149 (evaluation-checkouts evaluation)))
150 (latest-evaluations url limit)))