|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +BRANCH="chore/update-vcr-cassettes" |
| 5 | +COMMIT_MSG="chore: update VCR cassettes" |
| 6 | +MAX_RETRIES=2 |
| 7 | +RETRY_DELAY=60 |
| 8 | + |
| 9 | +# ── Preflight checks ──────────────────────────────────────────────── |
| 10 | +command -v gh >/dev/null 2>&1 || { echo "Error: gh CLI is required"; exit 1; } |
| 11 | +command -v uv >/dev/null 2>&1 || { echo "Error: uv is required"; exit 1; } |
| 12 | + |
| 13 | +cd "$(git rev-parse --show-toplevel)" |
| 14 | + |
| 15 | +if [ -n "$(git status --porcelain)" ]; then |
| 16 | + echo "Error: working tree is dirty — commit or stash changes first" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +# ── Branch setup ───────────────────────────────────────────────────── |
| 21 | +BASE_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@' || echo "main") |
| 22 | +git checkout "$BASE_BRANCH" |
| 23 | +git pull --ff-only |
| 24 | +git checkout -B "$BRANCH" |
| 25 | + |
| 26 | +# ── Record cassettes (with retries) ───────────────────────────────── |
| 27 | +echo "Recording cassettes..." |
| 28 | +attempt=0 |
| 29 | +until make update-cassettes; do |
| 30 | + attempt=$((attempt + 1)) |
| 31 | + if [ "$attempt" -gt "$MAX_RETRIES" ]; then |
| 32 | + echo "Error: make update-cassettes failed after $((MAX_RETRIES + 1)) attempts" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + echo "Attempt $attempt failed, retrying in ${RETRY_DELAY}s..." |
| 36 | + sleep "$RETRY_DELAY" |
| 37 | +done |
| 38 | + |
| 39 | +# ── Commit & push ─────────────────────────────────────────────────── |
| 40 | +if git diff --quiet tests/cassettes/; then |
| 41 | + echo "No cassette changes detected — nothing to do." |
| 42 | + git checkout "$BASE_BRANCH" |
| 43 | + git branch -D "$BRANCH" |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +git add tests/cassettes/ |
| 48 | +git commit -m "$COMMIT_MSG" |
| 49 | +git push -u origin "$BRANCH" --force-with-lease |
| 50 | + |
| 51 | +# ── Open PR ────────────────────────────────────────────────────────── |
| 52 | +EXISTING_PR=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number // empty') |
| 53 | + |
| 54 | +if [ -n "$EXISTING_PR" ]; then |
| 55 | + echo "PR #$EXISTING_PR already exists — updated with force push." |
| 56 | + gh pr view "$EXISTING_PR" --web |
| 57 | +else |
| 58 | + gh pr create \ |
| 59 | + --title "$COMMIT_MSG" \ |
| 60 | + --body "Automated re-recording of all VCR cassettes against live APIs." \ |
| 61 | + --head "$BRANCH" \ |
| 62 | + --base "$BASE_BRANCH" |
| 63 | +fi |
0 commit comments