Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rebase-stack/SKILL.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 15 additions & 4 deletions rebase-stack/scripts/preflight.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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
Expand All @@ -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,
Expand All @@ -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]
Expand Down
119 changes: 119 additions & 0 deletions rebase-stack/scripts/test_preflight.sh
Original file line number Diff line number Diff line change
@@ -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 "<null>" 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 \
"<null>"
4 changes: 4 additions & 0 deletions rebase-stack/scripts/test_push_guard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading