|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Resolve gh pr create target repo and block fork-only diffs against tiann/hapi. |
| 3 | +# Sourced by gh-wrapper.sh and hapi-pr-create-fork.sh. |
| 4 | +# |
| 5 | +# Incident: heavygee/hapi tooling PR #62 filed as cross-repo tiann/hapi#971 (2026-06-24) |
| 6 | +# because `gh repo view` defaults to tiann/hapi in this clone. |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +PR_TARGET_GUARD_ROOT="${PR_TARGET_GUARD_ROOT:-$(git rev-parse --show-toplevel 2>/dev/null || pwd)}" |
| 10 | + |
| 11 | +# Paths that must never ride a PR to upstream/main (fork canon / operator tooling). |
| 12 | +PR_TARGET_FORK_ONLY_PREFIXES=( |
| 13 | + 'docs/tooling/' |
| 14 | + 'docs/operator/' |
| 15 | + 'docs/plans/' |
| 16 | + 'scripts/tooling/' |
| 17 | + '.cursor/rules/' |
| 18 | + 'CLAUDE.md' |
| 19 | +) |
| 20 | + |
| 21 | +# Infra branch names — fork PRs only unless explicitly using hapi-pr-create with product diff. |
| 22 | +PR_TARGET_FORK_INFRA_BRANCH_RE='^(tooling/|docs/|driver/|garden/|chore/fork)' |
| 23 | + |
| 24 | +pr_target_extract_repo_flag() { |
| 25 | + local arg next=0 repo="" |
| 26 | + for arg in "$@"; do |
| 27 | + if [[ $next -eq 1 ]]; then |
| 28 | + repo="$arg" |
| 29 | + next=0 |
| 30 | + continue |
| 31 | + fi |
| 32 | + case "$arg" in |
| 33 | + --repo|-R) |
| 34 | + next=1 |
| 35 | + ;; |
| 36 | + esac |
| 37 | + done |
| 38 | + printf '%s' "$repo" |
| 39 | +} |
| 40 | + |
| 41 | +pr_target_default_gh_repo() { |
| 42 | + gh repo view --json nameWithOwner --jq '.nameWithOwner' 2>/dev/null || true |
| 43 | +} |
| 44 | + |
| 45 | +pr_target_resolve_repo() { |
| 46 | + local explicit |
| 47 | + explicit="$(pr_target_extract_repo_flag "$@")" |
| 48 | + if [[ -n "$explicit" ]]; then |
| 49 | + printf '%s' "$explicit" |
| 50 | + return 0 |
| 51 | + fi |
| 52 | + pr_target_default_gh_repo |
| 53 | +} |
| 54 | + |
| 55 | +pr_target_fork_only_paths_in_diff() { |
| 56 | + local range="${1:-upstream/main...HEAD}" |
| 57 | + local path prefix |
| 58 | + while IFS= read -r path; do |
| 59 | + [[ -z "$path" ]] && continue |
| 60 | + for prefix in "${PR_TARGET_FORK_ONLY_PREFIXES[@]}"; do |
| 61 | + if [[ "$path" == "$prefix"* || "$path" == "$prefix" ]]; then |
| 62 | + printf '%s\n' "$path" |
| 63 | + fi |
| 64 | + done |
| 65 | + done < <(git -C "$PR_TARGET_GUARD_ROOT" diff --name-only "$range" 2>/dev/null || true) |
| 66 | +} |
| 67 | + |
| 68 | +pr_target_is_fork_infra_branch() { |
| 69 | + local branch="${1:-$(git -C "$PR_TARGET_GUARD_ROOT" rev-parse --abbrev-ref HEAD 2>/dev/null || true)}" |
| 70 | + [[ -z "$branch" || "$branch" == "HEAD" ]] && return 1 |
| 71 | + [[ "$branch" =~ $PR_TARGET_FORK_INFRA_BRANCH_RE ]] |
| 72 | +} |
| 73 | + |
| 74 | +# Print block reason to stdout; exit 0 if block, 1 if allow. |
| 75 | +pr_target_upstream_block_reason() { |
| 76 | + local repo="$1" |
| 77 | + shift |
| 78 | + [[ "$repo" != "tiann/hapi" ]] && return 1 |
| 79 | + |
| 80 | + local branch paths |
| 81 | + branch="$(git -C "$PR_TARGET_GUARD_ROOT" rev-parse --abbrev-ref HEAD 2>/dev/null || true)" |
| 82 | + paths="$(pr_target_fork_only_paths_in_diff "upstream/main...HEAD" | sort -u)" |
| 83 | + |
| 84 | + if [[ -n "$paths" ]]; then |
| 85 | + { |
| 86 | + echo "REFUSE: gh pr create targets tiann/hapi but this branch diff includes fork-only paths:" |
| 87 | + echo "" |
| 88 | + while IFS= read -r p; do |
| 89 | + [[ -z "$p" ]] && continue |
| 90 | + echo " - $p" |
| 91 | + done <<< "$paths" |
| 92 | + echo "" |
| 93 | + cat <<'EOF' |
| 94 | +Fork tooling/docs belong on heavygee/hapi main, not upstream. |
| 95 | +
|
| 96 | +Use instead: |
| 97 | + hapi-pr-create-fork --title "..." --body-file /tmp/body.md |
| 98 | +
|
| 99 | +Or explicitly (after verification): |
| 100 | + gh pr create --repo heavygee/hapi --base main ... |
| 101 | +
|
| 102 | +Upstream product PRs only: |
| 103 | + hapi-pr-create --title "..." --body-file /tmp/body.md |
| 104 | +
|
| 105 | +Postmortem: accidental tiann/hapi#971 (2026-06-24). |
| 106 | +EOF |
| 107 | + } |
| 108 | + return 0 |
| 109 | + fi |
| 110 | + |
| 111 | + if pr_target_is_fork_infra_branch "$branch"; then |
| 112 | + cat <<EOF |
| 113 | +REFUSE: gh pr create targets tiann/hapi from infra branch '$branch'. |
| 114 | +
|
| 115 | +Branches matching tooling/*, docs/*, driver/*, garden/* are fork-side only. |
| 116 | +
|
| 117 | +Use: |
| 118 | + hapi-pr-create-fork --title "..." --body-file /tmp/body.md |
| 119 | +EOF |
| 120 | + return 0 |
| 121 | + fi |
| 122 | + |
| 123 | + return 1 |
| 124 | +} |
0 commit comments