ci.scm 6.3 KB

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