17
17
18
18
set -eo pipefail
19
19
20
- # --- Prerequisite Check ---
21
-
22
20
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."
24
22
echo " See: https://cli.github.com/"
25
23
exit 1
26
24
fi
27
25
28
26
echo " ✅ GitHub CLI is installed."
29
- # --- Configuration and Argument Parsing ---
30
27
31
- PR_NUMBER=$1
28
+ PR_NUMBER=${1 :- }
32
29
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
34
45
35
46
if [[ -z ${PR_NUMBER} ]]; then
36
47
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: " )
38
49
PR_NUMBER=$( echo " $PR_SELECTION " | awk -F: ' {print $1}' | xargs)
39
50
fi
40
51
44
55
45
56
if [[ -z " $PR_NUMBER " || -z " $FORK_REMOTE " ]]; then
46
57
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."
48
60
exit 1
49
61
fi
50
62
@@ -67,17 +79,27 @@ echo " - Author: $PR_AUTHOR"
67
79
68
80
# 1. Checkout the PR locally
69
81
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 "
71
83
72
84
# 2. Push the branch to your fork
73
85
NEW_BRANCH_NAME=" test-pr-${PR_NUMBER} -${PR_AUTHOR} "
74
86
75
87
echo " 🔄 Pushing changes to a new branch '${NEW_BRANCH_NAME} ' on your fork (${FORK_REMOTE} )..."
76
88
# 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
78
100
79
101
# 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} "
81
103
MIRRORED_PR_BODY=" Mirrors ${PR_URL} to run E2E tests. Original author: @${PR_AUTHOR} "
82
104
DO_NOT_MERGE_LABEL=" do-not-merge" # You might need to create this label in your repo if it doesn't exist
83
105
@@ -99,7 +121,11 @@ if [[ -z "$CREATED_PR_URL" ]]; then
99
121
exit 1
100
122
fi
101
123
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\` ."
103
129
104
130
echo " ✅ Successfully created mirrored pull request!"
105
131
echo " ${CREATED_PR_URL} "
0 commit comments