Skip to content

Commit af3b914

Browse files
committed
fix: improve mirror-pr.sh script
- Added check for uncommitted changes before proceeding - Implemented branch reset on script exit via trap - Excluded already mirrored PRs from fzf selection - Added force flag to gh pr checkout - Added check for existing PRs before creating new one - Simplified mirrored PR title by removing redundant prefix - Improved usage message with UPSTREAM_REPO info - Added error handling for branch reset - Used consistent quoting style for variables Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent d529a98 commit af3b914

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

hack/mirror-pr.sh

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,35 @@
1717

1818
set -eo pipefail
1919

20-
# --- Prerequisite Check ---
21-
2220
if ! command -v gh &>/dev/null; then
23-
echo "Error: GitHub CLI ('gh') is not installed. Please install it to continue."
21+
echo "Error: GitHub CLI ('gh') is not installed. Please install it to continue."
2422
echo "See: https://cli.github.com/"
2523
exit 1
2624
fi
2725

2826
echo "✅ GitHub CLI is installed."
29-
# --- Configuration and Argument Parsing ---
3027

31-
PR_NUMBER=$1
28+
PR_NUMBER=${1:-}
3229
FORK_REMOTE=${GH_FORK_REMOTE:-$2}
33-
UPSTREAM_REPO=${GH_UPSTREAM_REPO:-openshift-pipelines/pipelines-as-code}
30+
UPSTREAM_REPO=${GH_UPSTREAM_REPO:-"openshift-pipelines/pipelines-as-code"}
31+
32+
# Check if there is any changes in the current branch or bail out
33+
if ! git diff-index --quiet HEAD --; then
34+
echo "❌ Error: There are uncommitted changes in the current branch. Please commit or stash them before running this script."
35+
exit 1
36+
fi
37+
38+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
39+
resetgitbranch() {
40+
new_branch_name=$(git rev-parse --abbrev-ref HEAD)
41+
echo "🔄 Resetting to original branch ${CURRENT_BRANCH} from ${new_branch_name}"
42+
git checkout "$CURRENT_BRANCH" || true
43+
}
44+
trap resetgitbranch EXIT
3445

3546
if [[ -z ${PR_NUMBER} ]]; then
3647
PR_SELECTION=$(gh pr list --repo "$UPSTREAM_REPO" --json number,title,author --template '{{range .}}{{.number}}: {{.title}} (by {{.author.login}})
37-
{{end}}' | fzf --prompt="Select PR: ")
48+
{{end}}' | grep -v "\[MIRRORED\]" | fzf --prompt="Select PR: ")
3849
PR_NUMBER=$(echo "$PR_SELECTION" | awk -F: '{print $1}' | xargs)
3950
fi
4051

@@ -44,7 +55,8 @@ fi
4455

4556
if [[ -z "$PR_NUMBER" || -z "$FORK_REMOTE" ]]; then
4657
echo "Usage: $0 <PR_NUMBER> <YOUR_REMOTE_FORK>"
47-
echo "Example: $0 1234 openshift-pipelines/pipelines-as-code my-github-user"
58+
echo "Example: $0 1234 my-github-user"
59+
echo "UPSTREAM_REPO is ${UPSTREAM_REPO} unless you configure the env variable GH_UPSTREAM_REPO."
4860
exit 1
4961
fi
5062

@@ -67,17 +79,27 @@ echo " - Author: $PR_AUTHOR"
6779

6880
# 1. Checkout the PR locally
6981
echo "🔄 Checking out PR #${PR_NUMBER} locally..."
70-
gh pr checkout "$PR_NUMBER" --repo "$UPSTREAM_REPO"
82+
gh pr checkout --force "$PR_NUMBER" --repo "$UPSTREAM_REPO"
7183

7284
# 2. Push the branch to your fork
7385
NEW_BRANCH_NAME="test-pr-${PR_NUMBER}-${PR_AUTHOR}"
7486

7587
echo "🔄 Pushing changes to a new branch '${NEW_BRANCH_NAME}' on your fork (${FORK_REMOTE})..."
7688
# Force push in case the branch already exists from a previous test run
77-
git push "$FORK_REMOTE" "HEAD:${NEW_BRANCH_NAME}" -f
89+
git push "$FORK_REMOTE" "HEAD:${NEW_BRANCH_NAME}" --force
90+
91+
# check if we didn't already have a pull request open for this branch
92+
already_opened_pr=$(
93+
gh pr list --repo "$UPSTREAM_REPO" --head \
94+
"${FORK_REMOTE}:${NEW_BRANCH_NAME}" --json url --jq '.[0].url'
95+
)
96+
if [[ -n ${already_opened_pr} ]]; then
97+
echo "🔗 A pull request already exists for this branch: ${already_opened_pr}"
98+
exit 0
99+
fi
78100

79101
# 3. Create a new Pull Request from the fork to the upstream repo
80-
MIRRORED_PR_TITLE="[MIRRORED] DO NOT MERGE: ${PR_TITLE}"
102+
MIRRORED_PR_TITLE="[MIRRORED] ${PR_TITLE}"
81103
MIRRORED_PR_BODY="Mirrors ${PR_URL} to run E2E tests. Original author: @${PR_AUTHOR}"
82104
DO_NOT_MERGE_LABEL="do-not-merge" # You might need to create this label in your repo if it doesn't exist
83105

@@ -99,7 +121,11 @@ if [[ -z "$CREATED_PR_URL" ]]; then
99121
exit 1
100122
fi
101123

102-
gh pr comment "$PR_NUMBER" --repo "$UPSTREAM_REPO" --body "A mirrored PR has been created for E2E testing: ${CREATED_PR_URL}"
124+
gh pr comment "$PR_NUMBER" --repo "$UPSTREAM_REPO" --body \
125+
":rocket: **Mirrored PR Created for E2E Testing**<br><br>\
126+
A mirrored PR has been opened for end-to-end testing: [View PR](${CREATED_PR_URL})<br><br>\
127+
:hourglass_flowing_sand: Follow progress there for E2E results.<br>\
128+
If you need to update the PR with new changes, please ask a maintainer to rerun \`hack/mirror-pr.sh\`."
103129

104130
echo "✅ Successfully created mirrored pull request!"
105131
echo " ${CREATED_PR_URL}"

0 commit comments

Comments
 (0)