-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathimport-public-pr.sh
More file actions
executable file
·168 lines (139 loc) · 6.78 KB
/
Copy pathimport-public-pr.sh
File metadata and controls
executable file
·168 lines (139 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
# import-public-pr.sh — Import a PR from the public lensesio/stream-reactor mirror
# into the lensesio-dev/stream-reactor private repo, preserving all commits and
# authorship exactly, and open a PR for review.
#
# Usage:
# ./import-public-pr.sh <public-pr-number>
#
# Optional env overrides:
# PUBLIC_REPO — public GitHub repo slug (default: lensesio/stream-reactor)
# DEV_REMOTE — remote name in the current working copy whose URL is used
# as the SSH URL of the private repo (default: origin)
# BASE_BRANCH — base branch for the new PR (default: master)
#
# Process (no cherry-pick — commits are preserved verbatim):
# 1. Clone the private repo to a temp dir.
# 2. Add the contributor's fork as a second remote ("prsource").
# 3. Checkout the PR branch as public-pr-<N>.
# 4. Push public-pr-<N> to the private repo.
# 5. Delete the temp dir (EXIT trap).
# 6. Open the PR via gh.
set -euo pipefail
# ---------------------------------------------------------------------------
# Config / defaults
# ---------------------------------------------------------------------------
PR="${1:-}"
PUBLIC_REPO="${PUBLIC_REPO:-lensesio/stream-reactor}"
DEV_REMOTE="${DEV_REMOTE:-origin}"
BASE_BRANCH="${BASE_BRANCH:-master}"
IMPORT_BRANCH="public-pr-${PR}"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
die() { echo "ERROR: $*" >&2; exit 1; }
info() { echo "==> $*"; }
# ---------------------------------------------------------------------------
# Preflight
# ---------------------------------------------------------------------------
[[ -n "$PR" ]] || die "Usage: $0 <public-pr-number>"
[[ "$PR" =~ ^[0-9]+$ ]] || die "PR number must be a positive integer, got: $PR"
for cmd in git gh jq; do
command -v "$cmd" >/dev/null 2>&1 || die "'$cmd' is required but not found in PATH."
done
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || die "Must be run from inside a git work tree."
if [[ -n "$(git status --porcelain | grep -v '^??')" ]]; then
die "Working tree has staged or modified tracked files. Please commit or stash before importing."
fi
# Resolve the SSH URL of the private repo from the current working copy's remote
DEV_SSH_URL="$(git remote get-url "$DEV_REMOTE")"
DEV_REPO_SLUG="$(echo "$DEV_SSH_URL" \
| sed -E 's|^git@github\.com:||; s|^https://github\.com/||; s|\.git$||')"
info "Private repo : $DEV_REPO_SLUG ($DEV_SSH_URL)"
# ---------------------------------------------------------------------------
# Fetch PR metadata from the public repo
# ---------------------------------------------------------------------------
info "Fetching metadata for $PUBLIC_REPO #$PR ..."
PR_JSON="$(gh pr view "$PR" --repo "$PUBLIC_REPO" \
--json number,title,author,headRefName,headRepositoryOwner,headRepository,url,body,commits)"
PR_TITLE="$(echo "$PR_JSON" | jq -r '.title')"
PR_AUTHOR="$(echo "$PR_JSON" | jq -r '.author.login')"
PR_HEAD_REF="$(echo "$PR_JSON" | jq -r '.headRefName')"
PR_FORK_OWNER="$(echo "$PR_JSON" | jq -r '.headRepositoryOwner.login')"
PR_FORK_NAME="$(echo "$PR_JSON" | jq -r '.headRepository.name')"
PR_URL="$(echo "$PR_JSON" | jq -r '.url')"
PR_BODY="$(echo "$PR_JSON" | jq -r '.body')"
COMMIT_OIDS="$(echo "$PR_JSON" | jq -r '.commits[].oid')"
FORK_URL="https://github.com/${PR_FORK_OWNER}/${PR_FORK_NAME}.git"
info "Title : $PR_TITLE"
info "Author : $PR_AUTHOR"
info "Fork : $FORK_URL"
info "Fork branch : $PR_HEAD_REF"
info "Import branch: $IMPORT_BRANCH"
info "Commits : $(echo "$COMMIT_OIDS" | wc -l | tr -d ' ')"
# ---------------------------------------------------------------------------
# Guard: refuse if the import branch already exists locally or on remote
# ---------------------------------------------------------------------------
if git show-ref --verify --quiet "refs/heads/$IMPORT_BRANCH"; then
die "Local branch '$IMPORT_BRANCH' already exists. Delete it first:
git branch -D '$IMPORT_BRANCH'"
fi
if git ls-remote --exit-code "$DEV_REMOTE" "refs/heads/$IMPORT_BRANCH" >/dev/null 2>&1; then
die "Remote branch '$IMPORT_BRANCH' already exists on $DEV_REMOTE. Delete it first:
git push $DEV_REMOTE --delete '$IMPORT_BRANCH'"
fi
# ---------------------------------------------------------------------------
# Temp dir — always deleted on exit (success or failure)
# ---------------------------------------------------------------------------
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
CLONE_DIR="$TMP/clone"
# ---------------------------------------------------------------------------
# Clone the private repo into the temp dir
# ---------------------------------------------------------------------------
info "Cloning private repo into temp dir ..."
git clone --quiet "$DEV_SSH_URL" "$CLONE_DIR"
# ---------------------------------------------------------------------------
# Add the contributor's fork and fetch
# ---------------------------------------------------------------------------
info "Adding fork remote: $FORK_URL ..."
git -C "$CLONE_DIR" remote add prsource "$FORK_URL"
info "Fetching from fork ..."
git -C "$CLONE_DIR" fetch --quiet prsource
# ---------------------------------------------------------------------------
# Create the import branch from the fork's PR branch
# ---------------------------------------------------------------------------
info "Creating branch '$IMPORT_BRANCH' from prsource/$PR_HEAD_REF ..."
git -C "$CLONE_DIR" checkout -b "$IMPORT_BRANCH" "prsource/$PR_HEAD_REF" --quiet
# ---------------------------------------------------------------------------
# Push the import branch to the private repo
# (master is never checked out — structural guarantee)
# ---------------------------------------------------------------------------
info "Pushing '$IMPORT_BRANCH' to private repo ..."
git -C "$CLONE_DIR" push --quiet --set-upstream origin "$IMPORT_BRANCH"
# ---------------------------------------------------------------------------
# Temp dir is removed here by the EXIT trap
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Open the PR against the private repo
# ---------------------------------------------------------------------------
info "Opening PR against $DEV_REPO_SLUG:$BASE_BRANCH ..."
PR_IMPORT_TITLE="[import #${PR}] ${PR_TITLE}"
PR_IMPORT_BODY="$(cat <<EOF
Imports ${PR_URL}
**Original author:** @${PR_AUTHOR}
**Fork branch:** \`${PR_FORK_OWNER}:${PR_HEAD_REF}\`
**Commits:**
$(echo "$COMMIT_OIDS" | sed 's/^/- /')
---
${PR_BODY}
EOF
)"
NEW_PR_URL="$(gh pr create \
--repo "$DEV_REPO_SLUG" \
--base "$BASE_BRANCH" \
--head "$IMPORT_BRANCH" \
--title "$PR_IMPORT_TITLE" \
--body "$PR_IMPORT_BODY")"
echo ""
echo "Done! New PR: $NEW_PR_URL"