1
1
#! /usr/bin/env bash
2
- #
3
2
no_verify=
3
+ test_mode=
4
+
4
5
show_help () {
5
6
cat << EOF
6
7
πͺ Mirror an external contributor's pull request to a maintainer's fork for E2E tests.
@@ -20,22 +21,30 @@ show_help() {
20
21
If no PR number or not fork are provided, it will prompt you to select one
21
22
using fzf.
22
23
23
- EOF
24
- grep -E " [ ]*[a-zA-Z0-9-]\) ##" $0 |
25
- sed -e ' s/^[ ]*/-/' \
26
- -e ' s/-\([0-9A-Za-z]*\)[ ]*|[ ]*\([0-9A-Za-z]*\)/-\1, -\2/' \
27
- -e ' s/##//' -e ' s/)[ ]*/ - /' |
28
- awk -F" - " ' {printf "%-10s %s\n", $1, $2}'
29
-
30
- cat << EOF
24
+ Options:
25
+ -n Do not run pre-commit checks
26
+ -t Test mode (dry run, print commands only)
27
+ -h Show this help message
31
28
32
29
EOF
33
30
}
34
- while getopts " hn" opt; do
31
+
32
+ run () {
33
+ if [[ -n $test_mode ]]; then
34
+ echo " [TEST MODE] $* "
35
+ else
36
+ eval " $@ "
37
+ fi
38
+ }
39
+
40
+ while getopts " hnt" opt; do
35
41
case $opt in
36
42
n) # # do not run pre-commit checks
37
43
no_verify=yes
38
44
;;
45
+ t) # # test mode (dry run)
46
+ test_mode=yes
47
+ ;;
39
48
h)
40
49
echo " usage: $( basename $( readlink -f $0 ) ) "
41
50
show_help
@@ -74,7 +83,7 @@ CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
74
83
resetgitbranch () {
75
84
new_branch_name=$( git rev-parse --abbrev-ref HEAD)
76
85
echo " β©οΈ Resetting to original branch ${CURRENT_BRANCH} from ${new_branch_name} "
77
- git checkout " $CURRENT_BRANCH " || true
86
+ run git checkout " $CURRENT_BRANCH " || true
78
87
}
79
88
trap resetgitbranch EXIT
80
89
@@ -128,7 +137,7 @@ echo "π€ - Author: $PR_AUTHOR"
128
137
129
138
# 1οΈβ£ Checkout the PR locally
130
139
echo " π₯ Checking out PR #${PR_NUMBER} locally..."
131
- gh pr checkout --force " $PR_NUMBER " --repo " $UPSTREAM_REPO "
140
+ run gh pr checkout --force " $PR_NUMBER " --repo " $UPSTREAM_REPO "
132
141
133
142
# 2οΈβ£ Push the branch to your fork
134
143
NEW_BRANCH_NAME=" test-pr-${PR_NUMBER} -${PR_AUTHOR} "
@@ -149,17 +158,22 @@ else
149
158
exit 1
150
159
fi
151
160
echo " π Running pre-commit checks before pushing..."
152
- pre-commit run --all-files --show-diff-on-failure || {
153
- echo " β Pre-commit checks failed. Please fix the issues before pushing."
154
- echo " You can fix user errors locally and pushing to the user branch."
155
- echo " git commit --amend the commit (or add a new commit) and then run this command"
156
- gh pr view " $PR_NUMBER " --repo " $UPSTREAM_REPO " --json headRefName,headRepositoryOwner,headRepository |
157
- jq -r
' "git push --force-with-lease [email protected] :\(.headRepositoryOwner.login)/\(.headRepository.name).git HEAD:\(.headRefName)"'
158
- echo " (or use --force if you know what you are doing)"
159
- exit 1
160
- }
161
+ if [[ -n $test_mode ]]; then
162
+ echo " [TEST MODE] pre-commit run --all-files --show-diff-on-failure"
163
+ else
164
+ pre-commit run --all-files --show-diff-on-failure || {
165
+ echo " β Pre-commit checks failed. Please fix the issues before pushing."
166
+ echo " You can fix user errors locally and pushing to the user branch."
167
+ echo " git commit --amend the commit (or add a new commit) and then run this command"
168
+ gh pr view " $PR_NUMBER " --repo " $UPSTREAM_REPO " --json headRefName,headRepositoryOwner,headRepository |
169
+ jq -r
' "git push --force-with-lease [email protected] :\(.headRepositoryOwner.login)/\(.headRepository.name).git HEAD:\(.headRefName)"'
170
+ echo " (or use --force if you know what you are doing)"
171
+ exit 1
172
+ }
173
+ fi
161
174
fi
162
- git push " $FORK_REMOTE " " HEAD:${NEW_BRANCH_NAME} " --force --no-verify
175
+
176
+ run git push " $FORK_REMOTE " " HEAD:${NEW_BRANCH_NAME} " --force --no-verify
163
177
164
178
if [[ -n ${already_opened_pr} ]]; then
165
179
exit 0
@@ -172,26 +186,35 @@ DO_NOT_MERGE_LABEL="do-not-merge" # You might need to create this label in your
172
186
173
187
echo " π Creating a new mirrored pull request on ${UPSTREAM_REPO} ..."
174
188
175
- # π Create the PR as a draft to prevent accidental merges before tests run.
176
- CREATED_PR_URL=$( gh pr create \
177
- --repo " $UPSTREAM_REPO " \
178
- --title " $MIRRORED_PR_TITLE " \
179
- --body " $MIRRORED_PR_BODY " \
180
- --head " ${FORK_REMOTE} :${NEW_BRANCH_NAME} " \
181
- --label " $DO_NOT_MERGE_LABEL " \
182
- --draft)
189
+ if [[ -n $test_mode ]]; then
190
+ echo " [TEST MODE] gh pr create --repo \" $UPSTREAM_REPO \" --title \" $MIRRORED_PR_TITLE \" --body \" $MIRRORED_PR_BODY \" --head \" ${FORK_REMOTE} :${NEW_BRANCH_NAME} \" --label \" $DO_NOT_MERGE_LABEL \" --draft"
191
+ CREATED_PR_URL=" https://github.com/${UPSTREAM_REPO} /pull/FAKE"
192
+ else
193
+ # π Create the PR as a draft to prevent accidental merges before tests run.
194
+ CREATED_PR_URL=$( gh pr create \
195
+ --repo " $UPSTREAM_REPO " \
196
+ --title " $MIRRORED_PR_TITLE " \
197
+ --body " $MIRRORED_PR_BODY " \
198
+ --head " ${FORK_REMOTE} :${NEW_BRANCH_NAME} " \
199
+ --label " $DO_NOT_MERGE_LABEL " \
200
+ --draft)
201
+ fi
183
202
184
203
# β
Check if the PR was created successfully
185
204
if [[ -z " $CREATED_PR_URL " ]]; then
186
205
echo " β Error: Failed to create the mirrored pull request."
187
206
exit 1
188
207
fi
189
208
190
- gh pr comment " $PR_NUMBER " --repo " $UPSTREAM_REPO " --body \
191
- " π **Mirrored PR Created for E2E Testing**<br><br>\
209
+ if [[ -n $test_mode ]]; then
210
+ echo " [TEST MODE] gh pr comment \" $PR_NUMBER \" --repo \" $UPSTREAM_REPO \" --body \" π **Mirrored PR Created for E2E Testing**<br><br>A mirrored PR has been opened for end-to-end testing: [View PR](${CREATED_PR_URL} )<br><br>β³ Follow progress there for E2E results.<br>If you need to update the PR with new changes, please ask a maintainer to rerun \` hack/mirror-pr.sh\` .\" "
211
+ else
212
+ gh pr comment " $PR_NUMBER " --repo " $UPSTREAM_REPO " --body \
213
+ " π **Mirrored PR Created for E2E Testing**<br><br>\
192
214
A mirrored PR has been opened for end-to-end testing: [View PR](${CREATED_PR_URL} )<br><br>\
193
215
β³ Follow progress there for E2E results.<br>\
194
216
If you need to update the PR with new changes, please ask a maintainer to rerun \` hack/mirror-pr.sh\` ."
217
+ fi
195
218
196
219
echo " π Successfully created mirrored pull request!"
197
220
echo " ${CREATED_PR_URL} "
0 commit comments