| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
| 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) |
| 21 | #:use-module (guix json) |
| 22 | #:use-module (json) |
| 23 | #:use-module (srfi srfi-1) |
| 24 | #:use-module (ice-9 match) |
| 25 | #:export (build? |
| 26 | build-id |
| 27 | build-derivation |
| 28 | build-system |
| 29 | build-status |
| 30 | build-timestamp |
| 31 | |
| 32 | checkout? |
| 33 | checkout-commit |
| 34 | checkout-input |
| 35 | |
| 36 | evaluation? |
| 37 | evaluation-id |
| 38 | evaluation-spec |
| 39 | evaluation-complete? |
| 40 | evaluation-checkouts |
| 41 | |
| 42 | %query-limit |
| 43 | queued-builds |
| 44 | latest-builds |
| 45 | latest-evaluations |
| 46 | evaluations-for-commit)) |
| 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 | |
| 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 | |
| 62 | (define-json-mapping <build> make-build build? |
| 63 | json->build |
| 64 | (id build-id "id") ;integer |
| 65 | (derivation build-derivation) ;string | #f |
| 66 | (system build-system) ;string |
| 67 | (status build-status "buildstatus" ) ;integer |
| 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 | '()))))) |
| 76 | |
| 77 | (define-json-mapping <checkout> make-checkout checkout? |
| 78 | json->checkout |
| 79 | (commit checkout-commit) ;string (SHA1) |
| 80 | (input checkout-input)) ;string (name) |
| 81 | |
| 82 | (define-json-mapping <evaluation> make-evaluation evaluation? |
| 83 | json->evaluation |
| 84 | (id evaluation-id) ;integer |
| 85 | (spec evaluation-spec "specification") ;string |
| 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))))) |
| 94 | |
| 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 | |
| 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))))) |
| 109 | (map json->build (vector->list queue)))) |
| 110 | |
| 111 | (define* (latest-builds url #:optional (limit %query-limit) |
| 112 | #:key |
| 113 | evaluation |
| 114 | system |
| 115 | job) |
| 116 | "Return the latest builds performed by the CI server at URL. If EVALUATION |
| 117 | is an integer, restrict to builds of EVALUATION. If SYSTEM is true (a system |
| 118 | string 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 | |
| 124 | (let ((latest (json-fetch (string-append url "/api/latestbuilds?nr=" |
| 125 | (number->string limit) |
| 126 | (option "evaluation" evaluation |
| 127 | number->string) |
| 128 | (option "system" system) |
| 129 | (option "job" job))))) |
| 130 | ;; Note: Hydra does not provide a "derivation" field for entries in |
| 131 | ;; 'latestbuilds', but Cuirass does. |
| 132 | (map json->build (vector->list latest)))) |
| 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 |
| 137 | (vector->list |
| 138 | (json->scm |
| 139 | (http-fetch (string-append url "/api/evaluations?nr=" |
| 140 | (number->string limit))))))) |
| 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 |
| 145 | as 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))) |