Commit | Line | Data |
---|---|---|
b3517f3f | 1 | ;;; GNU Guix --- Functional package management for GNU |
2c33901f | 2 | ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
53e61956 | 3 | ;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org> |
b3517f3f LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
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. | |
11 | ;;; | |
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. | |
16 | ;;; | |
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/>. | |
19 | ||
20 | (define-module (guix ci) | |
21 | #:use-module (guix http-client) | |
a85a74ce | 22 | #:use-module (json) |
a3b72a8f | 23 | #:use-module (srfi srfi-1) |
a85a74ce | 24 | #:use-module (ice-9 match) |
53e61956 MO |
25 | #:export (build-product? |
26 | build-product-id | |
27 | build-product-type | |
28 | build-product-file-size | |
29 | build-product-path | |
30 | ||
31 | build? | |
b3517f3f LC |
32 | build-id |
33 | build-derivation | |
34 | build-system | |
35 | build-status | |
36 | build-timestamp | |
53e61956 | 37 | build-products |
b3517f3f | 38 | |
a3b72a8f LC |
39 | checkout? |
40 | checkout-commit | |
41 | checkout-input | |
42 | ||
43 | evaluation? | |
44 | evaluation-id | |
45 | evaluation-spec | |
46 | evaluation-complete? | |
47 | evaluation-checkouts | |
48 | ||
b3517f3f LC |
49 | %query-limit |
50 | queued-builds | |
a3b72a8f LC |
51 | latest-builds |
52 | latest-evaluations | |
a85a74ce | 53 | evaluations-for-commit)) |
b3517f3f LC |
54 | |
55 | ;;; Commentary: | |
56 | ;;; | |
57 | ;;; This module provides a client to the HTTP interface of the Hydra and | |
58 | ;;; Cuirass continuous integration (CI) tools. | |
59 | ;;; | |
60 | ;;; Code: | |
61 | ||
4e05bbb0 MO |
62 | (define-json-mapping <build-product> make-build-product |
63 | build-product? | |
64 | json->build-product | |
53e61956 MO |
65 | (id build-product-id) ;integer |
66 | (type build-product-type) ;string | |
67 | (file-size build-product-file-size) ;integer | |
68 | (path build-product-path)) ;string | |
4e05bbb0 | 69 | |
a85a74ce LC |
70 | (define-json-mapping <build> make-build build? |
71 | json->build | |
72 | (id build-id "id") ;integer | |
b3517f3f LC |
73 | (derivation build-derivation) ;string | #f |
74 | (system build-system) ;string | |
a85a74ce | 75 | (status build-status "buildstatus" ) ;integer |
4e05bbb0 MO |
76 | (timestamp build-timestamp) ;integer |
77 | (products build-products "buildproducts" ;<build-product>* | |
78 | (lambda (products) | |
79 | (map json->build-product | |
80 | ;; Before Cuirass 3db603c1, #f is always returned. | |
bb76f50b | 81 | (if (vector? products) |
4e05bbb0 MO |
82 | (vector->list products) |
83 | '()))))) | |
b3517f3f | 84 | |
a85a74ce LC |
85 | (define-json-mapping <checkout> make-checkout checkout? |
86 | json->checkout | |
a3b72a8f LC |
87 | (commit checkout-commit) ;string (SHA1) |
88 | (input checkout-input)) ;string (name) | |
89 | ||
a85a74ce LC |
90 | (define-json-mapping <evaluation> make-evaluation evaluation? |
91 | json->evaluation | |
a3b72a8f | 92 | (id evaluation-id) ;integer |
2c33901f | 93 | (spec evaluation-spec "specification") ;string |
a85a74ce LC |
94 | (complete? evaluation-complete? "in-progress" |
95 | (match-lambda | |
96 | (0 #t) | |
97 | (_ #f))) ;Boolean | |
98 | (checkouts evaluation-checkouts "checkouts" ;<checkout>* | |
99 | (lambda (checkouts) | |
100 | (map json->checkout | |
101 | (vector->list checkouts))))) | |
a3b72a8f | 102 | |
b3517f3f LC |
103 | (define %query-limit |
104 | ;; Max number of builds requested in queries. | |
105 | 1000) | |
106 | ||
107 | (define (json-fetch url) | |
108 | (let* ((port (http-fetch url)) | |
109 | (json (json->scm port))) | |
110 | (close-port port) | |
111 | json)) | |
112 | ||
b3517f3f LC |
113 | (define* (queued-builds url #:optional (limit %query-limit)) |
114 | "Return the list of queued derivations on URL." | |
115 | (let ((queue (json-fetch (string-append url "/api/queue?nr=" | |
116 | (number->string limit))))) | |
a85a74ce | 117 | (map json->build (vector->list queue)))) |
b3517f3f | 118 | |
a3b72a8f | 119 | (define* (latest-builds url #:optional (limit %query-limit) |
9e989d9e | 120 | #:key evaluation system job status) |
a3b72a8f LC |
121 | "Return the latest builds performed by the CI server at URL. If EVALUATION |
122 | is an integer, restrict to builds of EVALUATION. If SYSTEM is true (a system | |
123 | string such as \"x86_64-linux\"), restrict to builds for SYSTEM." | |
124 | (define* (option name value #:optional (->string identity)) | |
125 | (if value | |
126 | (string-append "&" name "=" (->string value)) | |
127 | "")) | |
128 | ||
b3517f3f | 129 | (let ((latest (json-fetch (string-append url "/api/latestbuilds?nr=" |
a3b72a8f LC |
130 | (number->string limit) |
131 | (option "evaluation" evaluation | |
132 | number->string) | |
ef6f9f16 | 133 | (option "system" system) |
9e989d9e MO |
134 | (option "job" job) |
135 | (option "status" status | |
136 | number->string))))) | |
b3517f3f LC |
137 | ;; Note: Hydra does not provide a "derivation" field for entries in |
138 | ;; 'latestbuilds', but Cuirass does. | |
a85a74ce | 139 | (map json->build (vector->list latest)))) |
a3b72a8f LC |
140 | |
141 | (define* (latest-evaluations url #:optional (limit %query-limit)) | |
142 | "Return the latest evaluations performed by the CI server at URL." | |
143 | (map json->evaluation | |
a85a74ce LC |
144 | (vector->list |
145 | (json->scm | |
146 | (http-fetch (string-append url "/api/evaluations?nr=" | |
147 | (number->string limit))))))) | |
a3b72a8f LC |
148 | |
149 | ||
150 | (define* (evaluations-for-commit url commit #:optional (limit %query-limit)) | |
151 | "Return the evaluations among the latest LIMIT evaluations that have COMMIT | |
152 | as one of their inputs." | |
153 | (filter (lambda (evaluation) | |
154 | (find (lambda (checkout) | |
155 | (string=? (checkout-commit checkout) commit)) | |
156 | (evaluation-checkouts evaluation))) | |
157 | (latest-evaluations url limit))) |