pre-push 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Pre-push hook: validates v* tag pushes before they reach the remote.
  4. #
  5. # Checks:
  6. # 1. package.json version matches the tag
  7. # 2. CHANGELOG.md has a "## [{version}] - {date}" entry
  8. # 3. CI passed upstream on GitHub for the tagged commit
  9. #
  10. # All failures block the push and write to stderr.
  11. while read -r local_ref local_sha remote_ref remote_sha; do
  12. # Only validate v* tag pushes
  13. if [[ "$local_ref" != refs/tags/v* ]]; then
  14. continue
  15. fi
  16. # Skip tag deletions
  17. if [[ "$local_sha" == "0000000000000000000000000000000000000000" ]]; then
  18. continue
  19. fi
  20. TAG="${local_ref#refs/tags/}"
  21. VERSION="${TAG#v}"
  22. echo >&2 "Validating release $TAG..."
  23. # --- 1. package.json version must match the tag ---
  24. PKG_VERSION=$(jq -r .version package.json)
  25. if [[ "$PKG_VERSION" != "$VERSION" ]]; then
  26. echo >&2 "ABORT: package.json version is $PKG_VERSION but tag is $TAG"
  27. echo >&2 "Run: jq --arg v '$VERSION' '.version = \$v' package.json > tmp && mv tmp package.json"
  28. exit 1
  29. fi
  30. echo >&2 " package.json: $PKG_VERSION ✓"
  31. # --- 2. CHANGELOG.md must have an entry for this version ---
  32. if [[ ! -f CHANGELOG.md ]]; then
  33. echo >&2 "ABORT: CHANGELOG.md not found"
  34. exit 1
  35. fi
  36. if ! grep -q "^## \[$VERSION\] - " CHANGELOG.md; then
  37. echo >&2 "ABORT: CHANGELOG.md has no entry for [$VERSION]"
  38. echo >&2 "Expected: ## [$VERSION] - $(date +%Y-%m-%d)"
  39. exit 1
  40. fi
  41. echo >&2 " CHANGELOG.md: [$VERSION] ✓"
  42. # --- 3. CI must have passed on GitHub for this commit ---
  43. # Resolve annotated tag to its underlying commit
  44. COMMIT=$(git rev-list -n 1 "$TAG" 2>/dev/null || git rev-parse HEAD)
  45. if ! command -v gh &>/dev/null; then
  46. echo >&2 " CI: skipped (no gh CLI)"
  47. continue
  48. fi
  49. CHECK_JSON=$(gh api "repos/{owner}/{repo}/commits/$COMMIT/check-runs" 2>/dev/null || echo "")
  50. if [[ -z "$CHECK_JSON" ]]; then
  51. echo >&2 " CI: skipped (GitHub API unreachable)"
  52. continue
  53. fi
  54. TOTAL=$(echo "$CHECK_JSON" | jq -r '.total_count // 0' 2>/dev/null || echo "0")
  55. if [[ "$TOTAL" -eq 0 ]] 2>/dev/null; then
  56. echo >&2 " CI: no runs found (push commit to main first and wait for CI)"
  57. else
  58. FAILED=$(echo "$CHECK_JSON" | jq '[.check_runs // [] | .[] | select(.conclusion == "failure")] | length' 2>/dev/null || echo "0")
  59. PENDING=$(echo "$CHECK_JSON" | jq '[.check_runs // [] | .[] | select(.status != "completed")] | length' 2>/dev/null || echo "0")
  60. if [[ "$FAILED" -gt 0 ]] 2>/dev/null; then
  61. echo >&2 "ABORT: CI failed for $COMMIT"
  62. echo >&2 "https://github.com/tobi/qmd/commit/$COMMIT"
  63. exit 1
  64. fi
  65. if [[ "$PENDING" -gt 0 ]] 2>/dev/null; then
  66. echo >&2 "ABORT: CI still running ($PENDING pending)"
  67. echo >&2 "Wait for CI to finish, then push again."
  68. exit 1
  69. fi
  70. echo >&2 " CI: passed ✓"
  71. fi
  72. echo >&2 "All checks passed for $TAG ✓"
  73. done
  74. exit 0