Skip to content

Commit 8dbb5f5

Browse files
chmouelzakisk
authored andcommitted
feat: Allow listing and closing mirrored PRs by original state
- Added a new `-c` command line option to `hack/mirror-pr.sh`. - Implemented functionality to list all mirrored pull requests in the repository. - Fetched the state of the original pull request for each mirrored PR. - Allowed interactive closing of mirrored PRs if their original counterparts were merged or closed. - Corrected a formatting issue in the `fzf` interactive selection. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent ff0bf5d commit 8dbb5f5

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

hack/mirror-pr.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
no_verify=
33
test_mode=
44
update_mode=
5+
list_mode=
56

67
show_help() {
78
cat <<EOF
@@ -26,6 +27,7 @@ Options:
2627
-n Do not run pre-commit checks
2728
-t Test mode (dry run, print commands only)
2829
-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
2931
-h Show this help message
3032
3133
EOF
@@ -39,11 +41,12 @@ run() {
3941
fi
4042
}
4143

42-
while getopts "hntu" opt; do
44+
while getopts "hntuc" opt; do
4345
case $opt in
4446
n) no_verify=yes ;;
4547
t) test_mode=yes ;;
4648
u) update_mode=yes ;;
49+
c) list_mode=yes ;;
4750
h)
4851
echo "usage: $(basename "$(readlink -f "$0")")"
4952
show_help
@@ -60,17 +63,42 @@ shift $((OPTIND - 1))
6063

6164
set -eo pipefail
6265

66+
UPSTREAM_REPO=${GH_UPSTREAM_REPO:-"openshift-pipelines/pipelines-as-code"}
67+
6368
if ! command -v gh &>/dev/null; then
6469
echo "🛑 Error: GitHub CLI ('gh') is not installed. Please install it to continue."
6570
echo "🔗 See: https://cli.github.com/"
6671
exit 1
6772
fi
6873

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+
6998
echo "✅ GitHub CLI is installed. Ready to proceed!"
7099

71100
PR_NUMBER=${1:-}
72101
FORK_REMOTE=${GH_FORK_REMOTE:-$2}
73-
UPSTREAM_REPO=${GH_UPSTREAM_REPO:-"openshift-pipelines/pipelines-as-code"}
74102

75103
# 🛡️ Check for uncommitted changes
76104
if ! git diff-index --quiet HEAD --; then
@@ -96,7 +124,7 @@ if [[ -z ${PR_NUMBER} ]]; then
96124
| . as $pr
97125
| ($pr.headRefName | capture("^test-pr-(?<orig_number>[^-]+)-(?<orig_author>.+)$")) as $m
98126
| ($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)]"
100128
' | fzf --prompt="🔎 Select mirrored PR to update: ")
101129
PR_NUMBER=$(echo "$PR_SELECTION" | sed 's/.*Original: #\([0-9]*\).*/\1/' | xargs)
102130
echo "🔍 Selected PR #${PR_NUMBER} to update."

0 commit comments

Comments
 (0)