Skip to content

Commit ff0bf5d

Browse files
chmouelzakisk
authored andcommitted
feat: Enable updating existing mirrored pull requests
- Added a new `-u` option to the `mirror-pr.sh` script. - Allowed users to select an existing mirrored pull request for direct updates. - Refactored command execution to remove `eval` for improved security. - Updated command line argument parsing for better maintainability. - Corrected help message wording and applied consistent quoting. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 1e99f6f commit ff0bf5d

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

hack/mirror-pr.sh

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
no_verify=
33
test_mode=
4+
update_mode=
45

56
show_help() {
67
cat <<EOF
@@ -18,12 +19,13 @@ show_help() {
1819
💡 Example:
1920
./mirror-pr.sh 1234 my-github-user
2021
21-
If no PR number or not fork are provided, it will prompt you to select one
22+
If no PR number or fork are provided, it will prompt you to select one
2223
using fzf.
2324
2425
Options:
2526
-n Do not run pre-commit checks
2627
-t Test mode (dry run, print commands only)
28+
-u Update mode (only list mirrored PRs and update existing mirrored PR)
2729
-h Show this help message
2830
2931
EOF
@@ -33,20 +35,17 @@ run() {
3335
if [[ -n $test_mode ]]; then
3436
echo "[TEST MODE] $*"
3537
else
36-
eval "$@"
38+
"$@"
3739
fi
3840
}
3941

40-
while getopts "hnt" opt; do
42+
while getopts "hntu" opt; do
4143
case $opt in
42-
n) ## do not run pre-commit checks
43-
no_verify=yes
44-
;;
45-
t) ## test mode (dry run)
46-
test_mode=yes
47-
;;
44+
n) no_verify=yes ;;
45+
t) test_mode=yes ;;
46+
u) update_mode=yes ;;
4847
h)
49-
echo "usage: $(basename $(readlink -f $0))"
48+
echo "usage: $(basename "$(readlink -f "$0")")"
5049
show_help
5150
exit 0
5251
;;
@@ -89,7 +88,19 @@ trap resetgitbranch EXIT
8988

9089
# 🎯 Select PR number if not provided
9190
if [[ -z ${PR_NUMBER} ]]; then
92-
if [[ ${CURRENT_BRANCH} =~ test-pr-([0-9]+)-([a-zA-Z0-9_-]+) ]]; then
91+
if [[ -n $update_mode ]]; then
92+
PR_SELECTION=$(gh pr list --repo "$UPSTREAM_REPO" --json number,title,author,headRefName |
93+
jq -r '
94+
.[]
95+
| select(.headRefName | startswith("test-pr-"))
96+
| . as $pr
97+
| ($pr.headRefName | capture("^test-pr-(?<orig_number>[^-]+)-(?<orig_author>.+)$")) as $m
98+
| ($pr.title | sub("^\\[MIRRORED\\]\\s*"; "")) as $clean_title
99+
| "\($pr.number): \($clean_title)) [Original: #\($m.orig_number) by \($m.orig_author)]"
100+
' | fzf --prompt="🔎 Select mirrored PR to update: ")
101+
PR_NUMBER=$(echo "$PR_SELECTION" | sed 's/.*Original: #\([0-9]*\).*/\1/' | xargs)
102+
echo "🔍 Selected PR #${PR_NUMBER} to update."
103+
elif [[ ${CURRENT_BRANCH} =~ test-pr-([0-9]+)-([a-zA-Z0-9_-]+) ]]; then
93104
PR_NUMBER="${BASH_REMATCH[1]}"
94105
else
95106
PR_SELECTION=$(gh pr list --repo "$UPSTREAM_REPO" --json number,title,author --template '{{range .}}{{.number}}: {{.title}} (by {{.author.login}})
@@ -100,7 +111,7 @@ fi
100111

101112
# 🔍 Check if a mirrored PR already exists
102113
already_opened_pr=$(
103-
gh pr list --repo $UPSTREAM_REPO \
114+
gh pr list --repo "$UPSTREAM_REPO" \
104115
--json number,headRepositoryOwner,headRepository,headRefName |
105116
jq -r --arg pn "$PR_NUMBER" \
106117
'.[] | select(.headRefName | test("^test-pr-\($pn)-.*")) | "[email protected]:\(.headRepositoryOwner.login)/\(.headRepository.name).git"'

0 commit comments

Comments
 (0)