|
| 1 | +/* jscpd:ignore-start */ |
| 2 | +// Mirror of packages/lib/src/core/templates-entrypoint/post-push-pr.ts. |
| 3 | +// The lib template test asserts rendered output equality to prevent drift. |
| 4 | +const postPushPrEnsureTemplate = String |
| 5 | + .raw`# CHANGE: ensure an open GitHub PR exists for the pushed branch before PR-bound post-push tools run. |
| 6 | +# WHY: issue #375 requires every successful git push to leave the branch with an open PR; plan sync and session backup both target PR discussion. |
| 7 | +# REF: issue-375 |
| 8 | +docker_git_github_repo_from_remote_url() { |
| 9 | + local remote_url="$1" |
| 10 | + local repo_path="" |
| 11 | + local owner="" |
| 12 | + local repo="" |
| 13 | +
|
| 14 | + case "$remote_url" in |
| 15 | + https://github.com/*) |
| 16 | + repo_path="${"${"}remote_url#https://github.com/}" |
| 17 | + ;; |
| 18 | + http://github.com/*) |
| 19 | + repo_path="${"${"}remote_url#http://github.com/}" |
| 20 | + ;; |
| 21 | + https://*@github.com/*) |
| 22 | + repo_path="${"${"}remote_url#https://*@github.com/}" |
| 23 | + ;; |
| 24 | + http://*@github.com/*) |
| 25 | + repo_path="${"${"}remote_url#http://*@github.com/}" |
| 26 | + ;; |
| 27 | + ssh://git@github.com/*) |
| 28 | + repo_path="${"${"}remote_url#ssh://git@github.com/}" |
| 29 | + ;; |
| 30 | + git@github.com:*) |
| 31 | + repo_path="${"${"}remote_url#git@github.com:}" |
| 32 | + ;; |
| 33 | + *) |
| 34 | + return 1 |
| 35 | + ;; |
| 36 | + esac |
| 37 | +
|
| 38 | + repo_path="${"${"}repo_path%%\?*}" |
| 39 | + repo_path="${"${"}repo_path%%#*}" |
| 40 | + repo_path="${"${"}repo_path%/}" |
| 41 | + repo_path="${"${"}repo_path%.git}" |
| 42 | + owner="${"${"}repo_path%%/*}" |
| 43 | + repo="${"${"}repo_path#*/}" |
| 44 | + repo="${"${"}repo%%/*}" |
| 45 | + repo="${"${"}repo%.git}" |
| 46 | +
|
| 47 | + if [[ -z "$owner" || -z "$repo" || "$owner" == "$repo_path" ]]; then |
| 48 | + return 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + printf "%s/%s\n" "$owner" "$repo" |
| 52 | +} |
| 53 | +
|
| 54 | +docker_git_github_repo_from_remote() { |
| 55 | + local remote="$1" |
| 56 | + local remote_url="" |
| 57 | +
|
| 58 | + remote_url="$(git remote get-url "$remote" 2>/dev/null || true)" |
| 59 | + if [[ -z "$remote_url" ]]; then |
| 60 | + return 1 |
| 61 | + fi |
| 62 | +
|
| 63 | + docker_git_github_repo_from_remote_url "$remote_url" |
| 64 | +} |
| 65 | +
|
| 66 | +docker_git_ensure_open_pr() { |
| 67 | + local branch="" |
| 68 | + local base_repo="" |
| 69 | + local head_repo="" |
| 70 | + local head_owner="" |
| 71 | + local head_arg="" |
| 72 | + local base_branch="" |
| 73 | + local pr_url="" |
| 74 | +
|
| 75 | + if ! command -v gh >/dev/null 2>&1; then |
| 76 | + echo "[post-push-pr] Error: gh CLI not found" >&2 |
| 77 | + return 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" |
| 81 | + if [[ -z "$branch" || "$branch" == "HEAD" ]]; then |
| 82 | + echo "[post-push-pr] Error: cannot create PR from detached HEAD" >&2 |
| 83 | + return 1 |
| 84 | + fi |
| 85 | +
|
| 86 | + if ! base_repo="$(docker_git_github_repo_from_remote upstream)"; then |
| 87 | + if ! base_repo="$(docker_git_github_repo_from_remote origin)"; then |
| 88 | + echo "[post-push-pr] Skipped: no GitHub remote found" |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + fi |
| 92 | +
|
| 93 | + if ! head_repo="$(docker_git_github_repo_from_remote origin)"; then |
| 94 | + head_repo="$base_repo" |
| 95 | + fi |
| 96 | +
|
| 97 | + base_branch="$(gh repo view "$base_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null || true)" |
| 98 | + if [[ -z "$base_branch" ]]; then |
| 99 | + echo "[post-push-pr] Error: failed to resolve default branch for $base_repo" >&2 |
| 100 | + return 1 |
| 101 | + fi |
| 102 | +
|
| 103 | + if [[ "$head_repo" == "$base_repo" ]]; then |
| 104 | + head_arg="$branch" |
| 105 | + else |
| 106 | + head_owner="${"${"}head_repo%%/*}" |
| 107 | + head_arg="${"${"}head_owner}:${"${"}branch}" |
| 108 | + fi |
| 109 | +
|
| 110 | + if ! pr_url="$(gh pr list --repo "$base_repo" --state open --head "$head_arg" --json url --jq '.[0].url // ""' 2>/dev/null)"; then |
| 111 | + echo "[post-push-pr] Error: failed to list open PRs for $head_arg in $base_repo" >&2 |
| 112 | + return 1 |
| 113 | + fi |
| 114 | + if [[ -z "$pr_url" && "$head_arg" != "$branch" ]]; then |
| 115 | + if ! pr_url="$(gh pr list --repo "$base_repo" --state open --head "$branch" --json url --jq '.[0].url // ""' 2>/dev/null)"; then |
| 116 | + echo "[post-push-pr] Error: failed to list open PRs for $branch in $base_repo" >&2 |
| 117 | + return 1 |
| 118 | + fi |
| 119 | + fi |
| 120 | +
|
| 121 | + if [[ -n "$pr_url" ]]; then |
| 122 | + echo "[post-push-pr] Open PR: $pr_url" |
| 123 | + return 0 |
| 124 | + fi |
| 125 | +
|
| 126 | + echo "[post-push-pr] Creating PR for $head_arg into $base_repo:$base_branch" |
| 127 | + gh pr create --repo "$base_repo" --base "$base_branch" --head "$head_arg" --fill |
| 128 | +} |
| 129 | +
|
| 130 | +docker_git_ensure_open_pr` |
| 131 | + |
| 132 | +export const renderPostPushPrEnsure = (): string => postPushPrEnsureTemplate |
| 133 | +/* jscpd:ignore-end */ |
0 commit comments