1
1
#! /usr/bin/env bash
2
2
no_verify=
3
3
test_mode=
4
+ update_mode=
4
5
5
6
show_help () {
6
7
cat << EOF
@@ -18,12 +19,13 @@ show_help() {
18
19
💡 Example:
19
20
./mirror-pr.sh 1234 my-github-user
20
21
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
22
23
using fzf.
23
24
24
25
Options:
25
26
-n Do not run pre-commit checks
26
27
-t Test mode (dry run, print commands only)
28
+ -u Update mode (only list mirrored PRs and update existing mirrored PR)
27
29
-h Show this help message
28
30
29
31
EOF
@@ -33,20 +35,17 @@ run() {
33
35
if [[ -n $test_mode ]]; then
34
36
echo " [TEST MODE] $* "
35
37
else
36
- eval " $@ "
38
+ " $@ "
37
39
fi
38
40
}
39
41
40
- while getopts " hnt " opt; do
42
+ while getopts " hntu " opt; do
41
43
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 ;;
48
47
h)
49
- echo " usage: $( basename $( readlink -f $0 ) ) "
48
+ echo " usage: $( basename " $( readlink -f " $0 " ) " ) "
50
49
show_help
51
50
exit 0
52
51
;;
@@ -89,7 +88,19 @@ trap resetgitbranch EXIT
89
88
90
89
# 🎯 Select PR number if not provided
91
90
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
93
104
PR_NUMBER=" ${BASH_REMATCH[1]} "
94
105
else
95
106
PR_SELECTION=$( gh pr list --repo " $UPSTREAM_REPO " --json number,title,author --template ' {{range .}}{{.number}}: {{.title}} (by {{.author.login}})
100
111
101
112
# 🔍 Check if a mirrored PR already exists
102
113
already_opened_pr=$(
103
- gh pr list --repo $UPSTREAM_REPO \
114
+ gh pr list --repo " $UPSTREAM_REPO " \
104
115
--json number,headRepositoryOwner,headRepository,headRefName |
105
116
jq -r --arg pn " $PR_NUMBER " \
106
117
' .[] | select(.headRefName | test("^test-pr-\($pn)-.*")) | "[email protected] :\(.headRepositoryOwner.login)/\(.headRepository.name).git"'
0 commit comments