ci.scm 4.9 KB

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