diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5d301ca1..7cda8491 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -14,6 +14,16 @@ permissions: contents: read jobs: + rebase-stack-tests: + name: Rebase Stack Tests + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - run: bash rebase-stack/scripts/test_preflight.sh + - run: bash rebase-stack/scripts/test_push_guard.sh + installer-tests: name: Installer Tests runs-on: ubuntu-24.04 diff --git a/rebase-stack/SKILL.md b/rebase-stack/SKILL.md index 0993d9c1..80d67aa5 100644 --- a/rebase-stack/SKILL.md +++ b/rebase-stack/SKILL.md @@ -1,6 +1,6 @@ --- name: rebase-stack -version: 1.0.0 +version: 1.1.0 description: >- Rebases a gh-stack-tracked chain of stacked branches onto its updated base, guides conflict resolution, validates the stack, and pushes every branch diff --git a/rebase-stack/scripts/preflight.sh b/rebase-stack/scripts/preflight.sh index dc7ba541..fc7fc2ff 100755 --- a/rebase-stack/scripts/preflight.sh +++ b/rebase-stack/scripts/preflight.sh @@ -263,9 +263,19 @@ if ! repo_json=$(gh repo view "$push_url" \ The fork-aware /push path needs the repository owner and parent repository. Check authentication and access to the repository, then retry." 4 fi -if ! jq -e 'has("isFork") and has("nameWithOwner") and has("owner") - and (.isFork != true or ((.owner.login // "") != "" - and (.parent.nameWithOwner // "") != ""))' \ +if ! parent_repo=$(jq -r ' + .parent.nameWithOwner // + (if ((.parent.owner.login // "") != "" and (.parent.name // "") != "") + then (.parent.owner.login + "/" + .parent.name) + else "" end) +' <<<"$repo_json"); then + die "Could not normalize repository topology for '${push_url}'. +The fork-aware /push path needs the repository owner and parent repository." 4 +fi +if ! jq -e --arg parentRepo "$parent_repo" ' + has("isFork") and has("nameWithOwner") and has("owner") + and (.isFork != true + or ((.owner.login // "") != "" and $parentRepo != ""))' \ >/dev/null 2>&1 <<<"$repo_json"; then die "'gh repo view' returned incomplete repository topology for '${push_url}'. The fork-aware /push path needs the repository owner and parent repository." 4 @@ -281,6 +291,7 @@ jq -n \ --arg baseRemoteSource "$base_remote_source" \ --arg pushRemote "$push_remote" \ --arg pushRemoteSource "$push_remote_source" \ + --arg parentRepo "$parent_repo" \ --argjson rebaseInProgress "$rebase_in_progress" \ '{ trunk: $stack.trunk, @@ -293,7 +304,7 @@ jq -n \ isFork: ($repo.isFork // null), repo: ($repo.nameWithOwner // null), forkOwner: ($repo.owner.login // null), - parentRepo: ($repo.parent.nameWithOwner // null), + parentRepo: (if $parentRepo == "" then null else $parentRepo end), branches: [$stack.branches[] | select(.isMerged != true) | {name, needsRebase, pr: (.pr.number // null), prState: (.pr.state // null)}], mergedBranches: [$stack.branches[] | select(.isMerged == true) | .name] diff --git a/rebase-stack/scripts/test_preflight.sh b/rebase-stack/scripts/test_preflight.sh new file mode 100755 index 00000000..97fd5cf1 --- /dev/null +++ b/rebase-stack/scripts/test_preflight.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash +# Regression tests for rebase-stack preflight repository topology handling. +# +# Uses a disposable git repository and a mocked gh CLI. Never contacts GitHub +# or modifies a real stack. +# +# Usage: +# bash rebase-stack/scripts/test_preflight.sh + +set -euo pipefail + +SCRIPTS_DIR="$(cd "$(dirname "$0")" && pwd)" +TEST_DIR=$(mktemp -d "${TMPDIR:-/tmp}/rebase-stack-preflight-test.XXXXXX") + +cleanup() { + rm -rf "$TEST_DIR" +} +trap cleanup EXIT + +pass() { + printf 'PASS: %s\n' "$1" +} + +git init --quiet --initial-branch=main --bare "$TEST_DIR/remote.git" +git init --quiet --initial-branch=main "$TEST_DIR/repo" +( + cd "$TEST_DIR/repo" + git config user.email "preflight-test@example.com" + git config user.name "Preflight Test" + git remote add origin "$TEST_DIR/remote.git" + git checkout -b story-1 >/dev/null 2>&1 + git commit --allow-empty -m "fixture" >/dev/null +) + +cd "$TEST_DIR/repo" + +mkdir "$TEST_DIR/bin" +cat > "$TEST_DIR/bin/gh" <<'STUB' +#!/usr/bin/env bash +if [ "${1:-}" = "stack" ] && [ "${2:-}" = "--version" ]; then + printf '1.0.0\n' + exit 0 +fi +if [ "${1:-}" = "stack" ] && [ "${2:-}" = "view" ]; then + printf '%s' '{"trunk":"main","currentBranch":"story-1","branches":[{"name":"story-1","needsRebase":false},{"name":"story-2","needsRebase":false}]}' + exit 0 +fi +if [ "${1:-}" = "repo" ] && [ "${2:-}" = "view" ]; then + cat "$REBASE_STACK_TEST_REPO_JSON" + exit 0 +fi +exit 1 +STUB +chmod +x "$TEST_DIR/bin/gh" + +run_case() { + local label="$1" + local repo_json="$2" + local expected_rc="$3" + local expected_parent="$4" + local rc actual_parent + + printf '%s' "$repo_json" > "$TEST_DIR/repo.json" + + if REBASE_STACK_TEST_REPO_JSON="$TEST_DIR/repo.json" \ + PATH="$TEST_DIR/bin:$PATH" \ + bash "$SCRIPTS_DIR/preflight.sh" \ + > "$TEST_DIR/stdout" 2> "$TEST_DIR/stderr"; then + rc=0 + else + rc=$? + fi + + if [ "$rc" -ne "$expected_rc" ]; then + printf 'FAIL: %s exited %s, expected %s\n' "$label" "$rc" "$expected_rc" >&2 + cat "$TEST_DIR/stderr" >&2 + return 1 + fi + + if [ "$expected_rc" -eq 0 ]; then + actual_parent=$(jq -r 'if .parentRepo == null then "" else .parentRepo end' \ + "$TEST_DIR/stdout") + if [ "$actual_parent" != "$expected_parent" ]; then + printf 'FAIL: %s emitted parentRepo=%s, expected %s\n' \ + "$label" "$actual_parent" "$expected_parent" >&2 + return 1 + fi + elif ! grep -q 'incomplete repository topology' "$TEST_DIR/stderr"; then + printf 'FAIL: %s did not report incomplete repository topology\n' "$label" >&2 + cat "$TEST_DIR/stderr" >&2 + return 1 + fi + + pass "$label" +} + +run_case \ + "legacy fork parent shape" \ + '{"isFork":true,"nameWithOwner":"myuser/repo","owner":{"login":"myuser"},"parent":{"nameWithOwner":"upstream/repo"}}' \ + 0 \ + "upstream/repo" + +run_case \ + "current fork parent shape" \ + '{"isFork":true,"nameWithOwner":"myuser/repo","owner":{"login":"myuser"},"parent":{"name":"repo","owner":{"login":"upstream"}}}' \ + 0 \ + "upstream/repo" + +run_case \ + "incomplete fork parent shape" \ + '{"isFork":true,"nameWithOwner":"myuser/repo","owner":{"login":"myuser"},"parent":{}}' \ + 4 \ + "" + +run_case \ + "direct repository" \ + '{"isFork":false,"nameWithOwner":"owner/repo","owner":{"login":"owner"}}' \ + 0 \ + "" diff --git a/rebase-stack/scripts/test_push_guard.sh b/rebase-stack/scripts/test_push_guard.sh index 3bdecfa2..d43db3d8 100644 --- a/rebase-stack/scripts/test_push_guard.sh +++ b/rebase-stack/scripts/test_push_guard.sh @@ -16,6 +16,10 @@ set -uo pipefail SCRIPTS_DIR="$(cd "$(dirname "$0")" && pwd)" TEST_DIR=$(mktemp -d "${TMPDIR:-/tmp}/rebase-stack-test.XXXXXX") +export GIT_AUTHOR_NAME="Rebase Stack Tests" +export GIT_AUTHOR_EMAIL="rebase-stack-tests@example.com" +export GIT_COMMITTER_NAME="Rebase Stack Tests" +export GIT_COMMITTER_EMAIL="rebase-stack-tests@example.com" # File-based counters survive subshells. PASS_FILE="$TEST_DIR/.pass-count"