ci_cost_gate.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bash
  2. # The cost gate as CI runs it.
  3. #
  4. # On a pull request the gate is given the base branch's docs/stages/approved.json
  5. # (--base-approved), so the phase lifecycle is checked against history: a done
  6. # phase cannot be edited or switched back on, no phase can be deleted, and a
  7. # change of the effective ceiling is printed. One file cannot show any of that.
  8. #
  9. # The base is the tip of the pull request's base branch, fetched shallow. The
  10. # checkout of a pull request is the merge of that tip and the head, so comparing
  11. # the two is exactly what merging changes. If the base cannot be fetched or does
  12. # not contain the file, this FAILS: a check that quietly does not run is the
  13. # thing this script exists to prevent.
  14. #
  15. # On other events (a push to main, a manual run, a local run) there is no base to
  16. # compare with; the plain gate runs and the reason is printed.
  17. #
  18. # bash scripts/ci_cost_gate.sh [extra cost_check.py arguments]
  19. #
  20. # Environment: GITHUB_EVENT_NAME and GITHUB_BASE_REF (set by GitHub Actions),
  21. # PYTHON (default python), CAH_BASE_REMOTE (default origin).
  22. set -euo pipefail
  23. here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  24. python_bin="${PYTHON:-python}"
  25. remote="${CAH_BASE_REMOTE:-origin}"
  26. event="${GITHUB_EVENT_NAME:-}"
  27. gate=("$python_bin" -I "$here/cost_check.py" --root .)
  28. case "$event" in
  29. pull_request | pull_request_target)
  30. base_ref="${GITHUB_BASE_REF:-}"
  31. if [ -z "$base_ref" ]; then
  32. echo "ci-cost-gate: FAIL: $event without GITHUB_BASE_REF; cannot find the base branch" >&2
  33. exit 1
  34. fi
  35. if ! git fetch --no-tags --depth=1 "$remote" "refs/heads/${base_ref}" >&2; then
  36. echo "ci-cost-gate: FAIL: could not fetch ${remote}/${base_ref}; the lifecycle check needs the base's approved.json" >&2
  37. exit 1
  38. fi
  39. tmp="$(mktemp -d)"
  40. trap 'rm -rf "$tmp"' EXIT
  41. if ! git show "FETCH_HEAD:docs/stages/approved.json" >"$tmp/approved.json"; then
  42. echo "ci-cost-gate: FAIL: ${base_ref} has no docs/stages/approved.json to compare with" >&2
  43. exit 1
  44. fi
  45. echo "ci-cost-gate: comparing with ${base_ref} ($(git rev-parse --short FETCH_HEAD))"
  46. "${gate[@]}" --base-approved "$tmp/approved.json" "$@"
  47. ;;
  48. *)
  49. echo "ci-cost-gate: event '${event:-none}' has no base branch; lifecycle check skipped, plain gate only"
  50. "${gate[@]}" "$@"
  51. ;;
  52. esac