Skip to content

Commit 1e99f6f

Browse files
chmouelzakisk
authored andcommitted
feat: Implement dry run functionality for mirror-pr script
* Added a dry run mode to the `mirror-pr.sh` script. * Introduced the `-t` flag, which allowed users to preview commands prior to execution. * Implemented a `run` helper function to conditionally execute or log commands. * Wrapped all critical commands with the new dry run mechanism, including `git` and `gh` operations. * Updated the script's help message to document the new test mode option. * Enhanced script safety and debuggability by offering a non-destructive verification method. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 5dafa4a commit 1e99f6f

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

β€Žhack/mirror-pr.sh

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
2-
#
32
no_verify=
3+
test_mode=
4+
45
show_help() {
56
cat <<EOF
67
πŸͺž Mirror an external contributor's pull request to a maintainer's fork for E2E tests.
@@ -20,22 +21,30 @@ show_help() {
2021
If no PR number or not fork are provided, it will prompt you to select one
2122
using fzf.
2223
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
3128
3229
EOF
3330
}
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
3541
case $opt in
3642
n) ## do not run pre-commit checks
3743
no_verify=yes
3844
;;
45+
t) ## test mode (dry run)
46+
test_mode=yes
47+
;;
3948
h)
4049
echo "usage: $(basename $(readlink -f $0))"
4150
show_help
@@ -74,7 +83,7 @@ CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
7483
resetgitbranch() {
7584
new_branch_name=$(git rev-parse --abbrev-ref HEAD)
7685
echo "↩️ Resetting to original branch ${CURRENT_BRANCH} from ${new_branch_name}"
77-
git checkout "$CURRENT_BRANCH" || true
86+
run git checkout "$CURRENT_BRANCH" || true
7887
}
7988
trap resetgitbranch EXIT
8089

@@ -128,7 +137,7 @@ echo "πŸ‘€ - Author: $PR_AUTHOR"
128137

129138
# 1️⃣ Checkout the PR locally
130139
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"
132141

133142
# 2️⃣ Push the branch to your fork
134143
NEW_BRANCH_NAME="test-pr-${PR_NUMBER}-${PR_AUTHOR}"
@@ -149,17 +158,22 @@ else
149158
exit 1
150159
fi
151160
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
161174
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
163177

164178
if [[ -n ${already_opened_pr} ]]; then
165179
exit 0
@@ -172,26 +186,35 @@ DO_NOT_MERGE_LABEL="do-not-merge" # You might need to create this label in your
172186

173187
echo "πŸ†• Creating a new mirrored pull request on ${UPSTREAM_REPO}..."
174188

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
183202

184203
# βœ… Check if the PR was created successfully
185204
if [[ -z "$CREATED_PR_URL" ]]; then
186205
echo "❗ Error: Failed to create the mirrored pull request."
187206
exit 1
188207
fi
189208

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>\
192214
A mirrored PR has been opened for end-to-end testing: [View PR](${CREATED_PR_URL})<br><br>\
193215
⏳ Follow progress there for E2E results.<br>\
194216
If you need to update the PR with new changes, please ask a maintainer to rerun \`hack/mirror-pr.sh\`."
217+
fi
195218

196219
echo "πŸŽ‰ Successfully created mirrored pull request!"
197220
echo " ${CREATED_PR_URL}"

0 commit comments

Comments
Β (0)