1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix ci)
21 #:use-module (guix http-client)
22 #:use-module (guix utils)
24 #:use-module (srfi srfi-1)
25 #:use-module (ice-9 match)
26 #:use-module (guix i18n)
27 #:use-module (guix diagnostics)
28 #:autoload (guix channels) (channel)
29 #:export (build-product?
32 build-product-file-size
59 evaluations-for-commit
61 channel-with-substitutes-available))
65 ;;; This module provides a client to the HTTP interface of the Hydra and
66 ;;; Cuirass continuous integration (CI) tools.
70 (define-json-mapping <build-product> make-build-product
73 (id build-product-id) ;integer
74 (type build-product-type) ;string
75 (file-size build-product-file-size) ;integer
76 (path build-product-path)) ;string
78 (define-json-mapping <build> make-build build?
80 (id build-id "id") ;integer
81 (derivation build-derivation) ;string | #f
82 (evaluation build-evaluation) ;integer
83 (system build-system) ;string
84 (status build-status "buildstatus" ) ;integer
85 (timestamp build-timestamp) ;integer
86 (products build-products "buildproducts" ;<build-product>*
88 (map json->build-product
89 ;; Before Cuirass 3db603c1, #f is always returned.
90 (if (vector? products)
91 (vector->list products)
94 (define-json-mapping <checkout> make-checkout checkout?
96 (commit checkout-commit) ;string (SHA1)
97 (channel checkout-channel)) ;string (name)
99 (define-json-mapping <evaluation> make-evaluation evaluation?
101 (id evaluation-id) ;integer
102 (spec evaluation-spec "specification") ;string
103 (complete? evaluation-complete? "status"
107 (checkouts evaluation-checkouts "checkouts" ;<checkout>*
110 (vector->list checkouts)))))
113 ;; Max number of builds requested in queries.
116 (define (json-fetch url)
117 (let* ((port (http-fetch url))
118 (json (json->scm port)))
122 (define* (queued-builds url #:optional (limit %query-limit))
123 "Return the list of queued derivations on URL."
124 (let ((queue (json-fetch (string-append url "/api/queue?nr="
125 (number->string limit)))))
126 (map json->build (vector->list queue))))
128 (define* (latest-builds url #:optional (limit %query-limit)
129 #:key evaluation system job status)
130 "Return the latest builds performed by the CI server at URL. If EVALUATION
131 is an integer, restrict to builds of EVALUATION. If SYSTEM is true (a system
132 string such as \"x86_64-linux\"), restrict to builds for SYSTEM."
133 (define* (option name value #:optional (->string identity))
135 (string-append "&" name "=" (->string value))
138 (let ((latest (json-fetch (string-append url "/api/latestbuilds?nr="
139 (number->string limit)
140 (option "evaluation" evaluation
142 (option "system" system)
144 (option "status" status
146 ;; Note: Hydra does not provide a "derivation" field for entries in
147 ;; 'latestbuilds', but Cuirass does.
148 (map json->build (vector->list latest))))
150 (define (evaluation url evaluation)
151 "Return the given EVALUATION performed by the CI server at URL."
152 (let ((evaluation (json-fetch
153 (string-append url "/api/evaluation?id="
154 (number->string evaluation)))))
155 (json->evaluation evaluation)))
157 (define* (latest-evaluations url
158 #:optional (limit %query-limit)
160 "Return the latest evaluations performed by the CI server at URL. If SPEC
161 is passed, only consider the evaluations for the given SPEC specification."
163 (format #f "&spec=~a" spec)
165 (map json->evaluation
169 (string-append url "/api/evaluations?nr="
170 (number->string limit)
173 (define* (evaluations-for-commit url commit #:optional (limit %query-limit))
174 "Return the evaluations among the latest LIMIT evaluations that have COMMIT
175 as one of their inputs."
176 (filter (lambda (evaluation)
177 (find (lambda (checkout)
178 (string=? (checkout-commit checkout) commit))
179 (evaluation-checkouts evaluation)))
180 (latest-evaluations url limit)))
182 (define (find-latest-commit-with-substitutes url)
183 "Return the latest commit with available substitutes for the Guix package
184 definitions at URL. Return false if no commit were found."
185 (let* ((job-name (string-append "guix." (%current-system)))
186 (build (match (latest-builds url 1
191 (evaluation (and build
192 (evaluation url (build-evaluation build))))
193 (commit (and evaluation
194 (match (evaluation-checkouts evaluation)
196 (checkout-commit checkout))))))
199 (define (channel-with-substitutes-available chan url)
200 "Return a channel inheriting from CHAN but which commit field is set to the
201 latest commit with available substitutes for the Guix package definitions at
202 URL. The current system is taken into account.
204 If no commit with available substitutes were found, the commit field is set to
205 false and a warning message is printed."
206 (let ((commit (find-latest-commit-with-substitutes url)))
208 (warning (G_ "could not find available substitutes at ~a~%")