fix(git-tools): pr-create API fallback resolves base from forge default branch (E4)
ci/woodpecker/pr/ci Pipeline failed
ci/woodpecker/pr/ci Pipeline failed
The API-fallback payload hardcoded "base": "main", mistargeting every fallback PR on repos whose trunk is not main. With zero fleet tea logins (measured under T53) every pr-create takes the fallback, so stack repos (default branch next) got main-targeted PRs. gitea_pr_create_api now resolves the base when -B is omitted: gitea_default_branch() queries the provider repo API (GET /api/v1/repos/<owner>/<repo> -> default_branch) using the same host/repo/token resolution as the POST itself. Explicit -B still wins and skips the lookup entirely. A failed or empty resolution fails loud (-B remedy named) instead of guessing a base; the "main" literal is gone. Hermetic suite test-pr-create-fallback-default-base.sh (PATH-stubbed curl, sandbox HOME, git.example.test fixture): no -B -> payload base equals the stubbed forge default (next) with the repo GET performed; explicit -B wins with zero repo GETs; lookup failure -> loud refusal, no POST; the literal main never appears as a fallback. Fixture stubs in test-gitea-login-resolution.sh and test-issue-create-interactive-auth.sh gain repo-root answers (assertions unchanged) because the fallback now makes a call those stubs predated. Sweep: 32/33 git-tools suites green; test-issue-close-fail-closed.sh fails identically at baseline origin/next (pre-existing, untouched).
This commit is contained in:
@@ -19,6 +19,24 @@ ISSUE=""
|
||||
|
||||
# get_remote_host, get_gitea_token, get_repo_info, and get_gitea_repo_args are provided by detect-platform.sh
|
||||
|
||||
gitea_default_branch() {
|
||||
# Forge default branch for the current repo (T51-P2 WP5a / spec E4): the
|
||||
# API fallback must not guess a base. Empty output or any lookup failure
|
||||
# returns nonzero so the caller fails loud instead of mistargeting a PR.
|
||||
local host repo token url branch
|
||||
host=$(get_remote_host) || return 1
|
||||
repo=$(get_repo_info) || return 1
|
||||
token=$(get_gitea_token "$host") || return 1
|
||||
url="https://${host}/api/v1/repos/${repo}"
|
||||
branch=$(curl -fsS \
|
||||
-H "User-Agent: curl/8" \
|
||||
-H "Authorization: token ${token}" \
|
||||
"$url" \
|
||||
| python3 -c 'import json,sys; print(json.load(sys.stdin).get("default_branch",""))' 2>/dev/null) || return 1
|
||||
[[ -n "$branch" ]] || return 1
|
||||
printf '%s' "$branch"
|
||||
}
|
||||
|
||||
gitea_pr_create_api() {
|
||||
local host repo token url payload
|
||||
host=$(get_remote_host) || {
|
||||
@@ -38,14 +56,28 @@ gitea_pr_create_api() {
|
||||
echo "Warning: API fallback applies title/body/head/base only; labels/milestone/draft require authenticated tea setup." >&2
|
||||
fi
|
||||
|
||||
payload=$(TITLE="$TITLE" BODY="$BODY" HEAD_BRANCH="$HEAD_BRANCH" BASE_BRANCH="$BASE_BRANCH" python3 - <<'PY'
|
||||
# Base resolution (spec E4): an explicit -B always wins; with none, the
|
||||
# forge default branch is resolved from the provider API -- never the
|
||||
# historical "main" literal, which mistargeted every fallback PR on
|
||||
# repos whose trunk is not main (e.g. mosaicstack/stack -> next).
|
||||
local api_base=""
|
||||
if [[ -n "$BASE_BRANCH" ]]; then
|
||||
api_base="$BASE_BRANCH"
|
||||
else
|
||||
api_base=$(gitea_default_branch) || {
|
||||
echo "Error: could not resolve the forge default branch for the API-fallback base; pass -B <branch> explicitly" >&2
|
||||
return 1
|
||||
}
|
||||
fi
|
||||
|
||||
payload=$(TITLE="$TITLE" BODY="$BODY" HEAD_BRANCH="$HEAD_BRANCH" API_BASE="$api_base" python3 - <<'PY'
|
||||
import json
|
||||
import os
|
||||
|
||||
payload = {
|
||||
"title": os.environ["TITLE"],
|
||||
"head": os.environ["HEAD_BRANCH"],
|
||||
"base": os.environ["BASE_BRANCH"] or "main",
|
||||
"base": os.environ["API_BASE"],
|
||||
}
|
||||
body = os.environ.get("BODY", "")
|
||||
if body:
|
||||
|
||||
@@ -100,6 +100,15 @@ case "$url" in
|
||||
*/commits/*/status)
|
||||
printf '{"state":"success","statuses":[{"context":"ci/mock","status":"success"}]}'
|
||||
;;
|
||||
# Repo roots: the pr-create API fallback resolves its base from the forge
|
||||
# default_branch (T51-P2 WP5a). Exact-suffix matches so the /pulls POST
|
||||
# endpoint (no trailing path) still falls through to the catch-all.
|
||||
*/api/v1/repos/USC/uconnect)
|
||||
printf '{"default_branch":"main"}'
|
||||
;;
|
||||
*/api/v1/repos/mosaicstack/stack)
|
||||
printf '{"default_branch":"next"}'
|
||||
;;
|
||||
*)
|
||||
printf '{}'
|
||||
;;
|
||||
|
||||
@@ -67,7 +67,18 @@ cat > "$BIN_DIR/curl" <<'SH'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
printf 'curl %s\n' "$*" >> "$MOSAIC_TEST_LOG"
|
||||
printf '%s\n' '{"number":703}'
|
||||
# Repo roots: the pr-create API fallback resolves its base from the forge
|
||||
# default_branch (T51-P2 WP5a). Exact-suffix so every other endpoint keeps
|
||||
# the historical answer below.
|
||||
url="${*: -1}"
|
||||
case "$url" in
|
||||
*/api/v1/repos/mosaicstack/stack)
|
||||
printf '%s\n' '{"default_branch":"next"}'
|
||||
;;
|
||||
*)
|
||||
printf '%s\n' '{"number":703}'
|
||||
;;
|
||||
esac
|
||||
SH
|
||||
chmod +x "$BIN_DIR/tea" "$BIN_DIR/curl"
|
||||
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env bash
|
||||
# test-pr-create-fallback-default-base.sh — hermetic test for the API-fallback
|
||||
# base resolution in pr-create.sh (T51-P2 WP5a / spec E4).
|
||||
#
|
||||
# The API-fallback payload historically hardcoded "base": "main", mistargeting
|
||||
# every fallback PR on repos whose trunk is not main (e.g. mosaicstack/stack,
|
||||
# default branch "next"). The fix: an explicit -B always wins; with none, the
|
||||
# base is resolved from the provider API default_branch, and a failed
|
||||
# resolution fails loud instead of guessing.
|
||||
#
|
||||
# Hermetic by construction: every curl invocation is a PATH-first stub; the
|
||||
# fixture repo's remote is git.example.test (never dialed); HOME is a sandbox
|
||||
# with no tea config (so the wrapper takes the API fallback path); GITEA_TOKEN
|
||||
# comes from the environment. No real forge is contacted.
|
||||
|
||||
# shellcheck disable=SC2317
|
||||
set -uo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
WORK_DIR="${MOSAIC_TEST_WORK_DIR:-$PWD/.mosaic-test-work/pr-create-fallback-base}"
|
||||
|
||||
PASS=0 FAIL=0 FAILED_CASES=""
|
||||
|
||||
ok() { PASS=$((PASS + 1)); }
|
||||
bad() { FAIL=$((FAIL + 1)); FAILED_CASES="$FAILED_CASES $1"; printf 'FAIL: %s\n' "$1" >&2; }
|
||||
|
||||
assert_rc() { local d="$1" e="$2" a="$3"; [ "$e" = "$a" ] && ok || bad "$d (expected rc=$e got rc=$a)"; }
|
||||
assert_eq() { local d="$1" e="$2" a="$3"; [ "$e" = "$a" ] && ok || bad "$d (expected [$e] got [$a])"; }
|
||||
assert_contains() { local d="$1" h="$2" n="$3"; case "$h" in *"$n"*) ok ;; *) bad "$d (missing [$n])" ;; esac; }
|
||||
|
||||
json_field() { # $1 payload file, $2 field
|
||||
python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get(sys.argv[2], ""))' "$1" "$2"
|
||||
}
|
||||
|
||||
rm -rf "$WORK_DIR"
|
||||
|
||||
# ---- fixture -----------------------------------------------------------------
|
||||
ROOT="$WORK_DIR/fixture"
|
||||
TOOLS="$ROOT/tools/git"
|
||||
mkdir -p "$TOOLS" "$ROOT/repo" "$ROOT/home" "$ROOT/stub"
|
||||
cp "$SCRIPT_DIR/pr-create.sh" "$TOOLS/pr-create.sh"
|
||||
cp "$SCRIPT_DIR/detect-platform.sh" "$TOOLS/detect-platform.sh"
|
||||
|
||||
git -C "$ROOT/repo" init -q -b fix/e4
|
||||
git -C "$ROOT/repo" -c user.name=fixture -c user.email=fixture@test commit -q --allow-empty -m base
|
||||
git -C "$ROOT/repo" remote add origin https://git.example.test/acme/widgets.git
|
||||
|
||||
# curl stub: GET repo -> default_branch JSON (or failure mode); POST pulls ->
|
||||
# capture payload, answer with a minimal PR JSON. Every call is logged.
|
||||
cat > "$ROOT/stub/curl" <<STUB
|
||||
#!/usr/bin/env bash
|
||||
set -u
|
||||
mode="\${CURL_STUB_GET_MODE:-ok}"
|
||||
printf '%s\n' "\$*" >> "$ROOT/curl-calls.log"
|
||||
url="\${!#}"
|
||||
if [[ "\$url" == */api/v1/repos/acme/widgets ]]; then
|
||||
# repo GET (default-branch resolution)
|
||||
if [[ "\$mode" != "ok" ]]; then
|
||||
echo "curl stub: simulated repo lookup failure" >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s\n' '{"id":1,"default_branch":"next","full_name":"acme/widgets"}'
|
||||
exit 0
|
||||
fi
|
||||
if [[ "\$url" == */api/v1/repos/acme/widgets/pulls ]]; then
|
||||
# PR POST: capture the payload, emit a PR-shaped answer
|
||||
while [[ \$# -gt 0 ]]; do
|
||||
case "\$1" in
|
||||
-d) printf '%s' "\$2" > "$ROOT/payload.json"; shift 2 ;;
|
||||
*) shift ;;
|
||||
esac
|
||||
done
|
||||
printf '%s\n' '{"number":42,"html_url":"https://git.example.test/acme/widgets/pulls/42"}'
|
||||
exit 0
|
||||
fi
|
||||
echo "curl stub: unexpected URL \$url" >&2
|
||||
exit 1
|
||||
STUB
|
||||
chmod +x "$ROOT/stub/curl"
|
||||
|
||||
run_pr_create() { # args... -> sets RC/OUT/ERR
|
||||
RC=0
|
||||
OUT=$(cd "$ROOT/repo" && env -i \
|
||||
PATH="$ROOT/stub:/usr/bin:/bin" \
|
||||
HOME="$ROOT/home" \
|
||||
GITEA_TOKEN=stub-token \
|
||||
bash "$TOOLS/pr-create.sh" "$@" 2>"$ROOT/err.txt")
|
||||
RC=$?
|
||||
ERR="$(cat "$ROOT/err.txt")"
|
||||
}
|
||||
|
||||
calls_matching() { grep -c -- "$1" "$ROOT/curl-calls.log" 2>/dev/null || true; }
|
||||
|
||||
echo "== (1) no -B: fallback base resolves to the forge default branch, not main =="
|
||||
: > "$ROOT/curl-calls.log"; rm -f "$ROOT/payload.json"
|
||||
run_pr_create -t "fix thing"
|
||||
assert_rc "rc" 0 "$RC"
|
||||
assert_contains "API fallback path taken (tea login unresolvable in fixture)" "$ERR" "trying Gitea API fallback"
|
||||
assert_eq "repo GET performed" 1 "$(calls_matching '/api/v1/repos/acme/widgets$')"
|
||||
assert_eq "POST performed" 1 "$(calls_matching '/pulls$')"
|
||||
assert_eq "payload base is forge default (next)" "next" "$(json_field "$ROOT/payload.json" base)"
|
||||
assert_eq "payload head" "fix/e4" "$(json_field "$ROOT/payload.json" head)"
|
||||
assert_eq "payload title" "fix thing" "$(json_field "$ROOT/payload.json" title)"
|
||||
|
||||
echo "== (2) explicit -B wins; the default branch is not consulted =="
|
||||
: > "$ROOT/curl-calls.log"; rm -f "$ROOT/payload.json"
|
||||
run_pr_create -t "fix thing" -B release/1.x
|
||||
assert_rc "rc" 0 "$RC"
|
||||
assert_eq "repo GET not consulted for explicit base" 0 "$(calls_matching '/api/v1/repos/acme/widgets$')"
|
||||
assert_eq "payload base is the explicit -B" "release/1.x" "$(json_field "$ROOT/payload.json" base)"
|
||||
|
||||
echo "== (3) default-branch lookup failure: loud refusal, no POST =="
|
||||
: > "$ROOT/curl-calls.log"; rm -f "$ROOT/payload.json"
|
||||
RC=0
|
||||
OUT=$(cd "$ROOT/repo" && env -i \
|
||||
PATH="$ROOT/stub:/usr/bin:/bin" \
|
||||
HOME="$ROOT/home" \
|
||||
GITEA_TOKEN=stub-token \
|
||||
CURL_STUB_GET_MODE=fail \
|
||||
bash "$TOOLS/pr-create.sh" -t "fix thing" 2>"$ROOT/err.txt")
|
||||
RC=$?
|
||||
ERR="$(cat "$ROOT/err.txt")"
|
||||
assert_rc "nonzero rc on unresolvable base" 1 "$RC"
|
||||
assert_contains "loud error names -B" "$ERR" "could not resolve the forge default branch"
|
||||
assert_contains "error names the remedy" "$ERR" "pass -B <branch> explicitly"
|
||||
assert_eq "no POST issued" 0 "$(calls_matching '/pulls$')"
|
||||
|
||||
echo "== (4) payload never contains the literal fallback main =="
|
||||
: > "$ROOT/curl-calls.log"; rm -f "$ROOT/payload.json"
|
||||
run_pr_create -t "fix thing"
|
||||
assert_rc "rc" 0 "$RC"
|
||||
assert_eq "base field is next, never main" "next" "$(json_field "$ROOT/payload.json" base)"
|
||||
|
||||
echo
|
||||
echo "pass=$PASS fail=$FAIL"
|
||||
if [ "$FAIL" -gt 0 ]; then
|
||||
echo "FAILED CASES:$FAILED_CASES"
|
||||
exit 1
|
||||
fi
|
||||
echo "ALL GREEN"
|
||||
Reference in New Issue
Block a user