2
2
no_verify=
3
3
test_mode=
4
4
update_mode=
5
+ list_mode=
5
6
6
7
show_help () {
7
8
cat << EOF
@@ -26,6 +27,7 @@ Options:
26
27
-n Do not run pre-commit checks
27
28
-t Test mode (dry run, print commands only)
28
29
-u Update mode (only list mirrored PRs and update existing mirrored PR)
30
+ -c List all mirrored PRs and optionally close them if original PR is merged/closed
29
31
-h Show this help message
30
32
31
33
EOF
@@ -39,11 +41,12 @@ run() {
39
41
fi
40
42
}
41
43
42
- while getopts " hntu " opt; do
44
+ while getopts " hntuc " opt; do
43
45
case $opt in
44
46
n) no_verify=yes ;;
45
47
t) test_mode=yes ;;
46
48
u) update_mode=yes ;;
49
+ c) list_mode=yes ;;
47
50
h)
48
51
echo " usage: $( basename " $( readlink -f " $0 " ) " ) "
49
52
show_help
@@ -60,17 +63,42 @@ shift $((OPTIND - 1))
60
63
61
64
set -eo pipefail
62
65
66
+ UPSTREAM_REPO=${GH_UPSTREAM_REPO:- " openshift-pipelines/pipelines-as-code" }
67
+
63
68
if ! command -v gh & > /dev/null; then
64
69
echo " 🛑 Error: GitHub CLI ('gh') is not installed. Please install it to continue."
65
70
echo " 🔗 See: https://cli.github.com/"
66
71
exit 1
67
72
fi
68
73
74
+ if [[ -n $list_mode ]]; then
75
+ gh pr list --repo " $UPSTREAM_REPO " --json number,title,author,headRefName,state |
76
+ jq -r '
77
+ .[]
78
+ | select(.headRefName | startswith("test-pr-"))
79
+ | . as $pr
80
+ | ($pr.headRefName | capture("^test-pr-(?<orig_number>[^-]+)-(?<orig_author>.+)$")) as $m
81
+ | ($pr.title | sub("^\\[MIRRORED\\]\\s*"; "")) as $clean_title
82
+ | "\($pr.number): \($clean_title) [Original: #\($m.orig_number) by \($m.orig_author)] (State: \($pr.state))"
83
+ ' | while read -r line; do
84
+ pr_num=$( echo " $line " | awk -F: ' {print $1}' )
85
+ orig_num=$( echo " $line " | sed -n ' s/.*Original: #\([0-9]*\).*/\1/p' )
86
+ orig_state=$( gh pr view " $orig_num " --repo " $UPSTREAM_REPO " --json state,mergedAt -q ' if .state == "MERGED" or .mergedAt != null then "merged" else .state end' 2> /dev/null || echo " unknown" )
87
+ if [[ " $orig_state " == " merged" || " $orig_state " == " closed" ]]; then
88
+ read -n1 -r -p " ❓ Original PR #$orig_num is $orig_state . Close mirrored PR #$pr_num ? [y/N]: " ans < /dev/tty
89
+ if [[ " $ans " =~ ^[Yy]$ ]]; then
90
+ echo " 🔒 Closing mirrored PR #$pr_num ..."
91
+ run gh pr close " $pr_num " --repo " $UPSTREAM_REPO "
92
+ fi
93
+ fi
94
+ done
95
+ exit 0
96
+ fi
97
+
69
98
echo " ✅ GitHub CLI is installed. Ready to proceed!"
70
99
71
100
PR_NUMBER=${1:- }
72
101
FORK_REMOTE=${GH_FORK_REMOTE:- $2 }
73
- UPSTREAM_REPO=${GH_UPSTREAM_REPO:- " openshift-pipelines/pipelines-as-code" }
74
102
75
103
# 🛡️ Check for uncommitted changes
76
104
if ! git diff-index --quiet HEAD --; then
@@ -96,7 +124,7 @@ if [[ -z ${PR_NUMBER} ]]; then
96
124
| . as $pr
97
125
| ($pr.headRefName | capture("^test-pr-(?<orig_number>[^-]+)-(?<orig_author>.+)$")) as $m
98
126
| ($pr.title | sub("^\\[MIRRORED\\]\\s*"; "")) as $clean_title
99
- | "\($pr.number): \($clean_title)) [Original: #\($m.orig_number) by \($m.orig_author)]"
127
+ | "\($pr.number): \($clean_title) [Original: #\($m.orig_number) by \($m.orig_author)]"
100
128
' | fzf --prompt=" 🔎 Select mirrored PR to update: " )
101
129
PR_NUMBER=$( echo " $PR_SELECTION " | sed ' s/.*Original: #\([0-9]*\).*/\1/' | xargs)
102
130
echo " 🔍 Selected PR #${PR_NUMBER} to update."
0 commit comments