From 0ebce7c776fdc5cb2a7bfa035918de06d75ae4de Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 24 Mar 2026 01:18:10 +0200 Subject: [PATCH] feat: update grl/gtk funcs --- plugins/common/git.zsh | 168 ++++++++++++++++++++++++++++++----------- 1 file changed, 124 insertions(+), 44 deletions(-) diff --git a/plugins/common/git.zsh b/plugins/common/git.zsh index a2a1d251..5b730736 100755 --- a/plugins/common/git.zsh +++ b/plugins/common/git.zsh @@ -354,6 +354,8 @@ function grename() { # Internal helper: search for an open PR in chenasraf/homebrew-tap by title # and add the "pr-pull" label if not already present. +# Returns 0 if label was added or already present, 1 if no PR found. +# Sets _gtp_pr_number to the matched PR number on success. function _gtp_label() { local search="$1" local pr_data @@ -363,25 +365,49 @@ function _gtp_label() { return 1 fi - local pr_number - pr_number=$(echo "$pr_data" | jq -r '.number') + _gtp_pr_number=$(echo "$pr_data" | jq -r '.number') if echo "$pr_data" | jq -e '.labels | index("pr-pull")' &>/dev/null; then - echo "PR #$pr_number already has the \"pr-pull\" label" + echo " ✓ PR #$_gtp_pr_number already has the \"pr-pull\" label" return 0 fi - gh pr edit "$pr_number" --repo chenasraf/homebrew-tap --add-label "pr-pull" - echo "Added \"pr-pull\" label to PR #$pr_number" + gh pr edit "$_gtp_pr_number" --repo chenasraf/homebrew-tap --add-label "pr-pull" + echo " ✓ Added \"pr-pull\" label to PR #$_gtp_pr_number" +} + +# Internal helper: wait for a homebrew-tap PR to be closed/merged. +# Returns 0 if closed, 1 if checks failed. +function _gtp_wait_closed() { + local pr_number="$1" + local pr_state + pr_state=$(gh pr view "$pr_number" --repo chenasraf/homebrew-tap --json state --jq '.state' 2>/dev/null) + + if [[ "$pr_state" == "MERGED" || "$pr_state" == "CLOSED" ]]; then + return 0 + fi + + # Check if PR checks have failed + local check_output + check_output=$(gh pr checks "$pr_number" --repo chenasraf/homebrew-tap 2>&1) + if echo "$check_output" | grep -q "fail"; then + echo " ✗ Tap PR #$pr_number checks failed:" + echo "$check_output" | sed 's/^/ /' + return 1 + fi + + return 2 # still open, not failed } # Search for an open PR in chenasraf/homebrew-tap by title and add the "pr-pull" label. +# Polls until the PR is found. function gtp() { - if [[ "$1" == "--help" || "$1" == "-h" ]]; then + if [[ "$1" == "-h" || "$1" == "--help" ]]; then echo "Usage: gtp " echo "" echo "Search for an open PR in chenasraf/homebrew-tap whose title matches" echo " and add the \"pr-pull\" label to it." + echo "Polls until a matching PR is found." return 0 fi @@ -390,21 +416,44 @@ function gtp() { return 1 fi - if ! _gtp_label "$1"; then - echo "No open PR found matching \"$1\"" - return 1 - fi + printf "Searching for homebrew-tap PR matching \"$1\" " + + local _gtp_pr_number + while true; do + if _gtp_label "$1"; then + echo "" + printf "Waiting for tap PR #$_gtp_pr_number to be merged " + local wait_rc + while true; do + _gtp_wait_closed "$_gtp_pr_number" + wait_rc=$? + if [[ $wait_rc -eq 0 ]]; then + echo "" + echo " ✓ Tap PR #$_gtp_pr_number merged — release complete!" + return 0 + elif [[ $wait_rc -eq 1 ]]; then + echo "" + return 1 + fi + printf "." + sleep 15 + done + fi + printf "." + sleep 15 + done } # Find a release PR in chenasraf/, wait for checks to pass, merge via -# rebase, then poll for a homebrew-tap PR and add the "pr-pull" label. +# rebase, then poll for a homebrew-tap PR, add the "pr-pull" label, and wait +# for it to be merged. function grl() { - if [[ "$1" == "--help" || "$1" == "-h" ]]; then + if [[ "$1" == "-h" || "$1" == "--help" ]]; then echo "Usage: grl [tap_search_term]" echo "" echo "Find an open release PR (titled \"chore release\") in chenasraf/," echo "wait for all CI checks to pass, merge it via rebase, then poll for a" - echo "matching homebrew-tap PR and add the \"pr-pull\" label." + echo "matching homebrew-tap PR, add the \"pr-pull\" label, and wait for it to close." echo "" echo "If is omitted, is used to search the tap PR." return 0 @@ -418,52 +467,83 @@ function grl() { local repo="chenasraf/$1" local tap_search="${2:-$1}" - # Find release PR + # Step 1: Find release PR + printf "Finding release PR in $repo " local pr_number - pr_number=$(gh pr list --repo "$repo" --state open --search "chore release in:title" --json number,title --jq '.[0].number') - - if [[ -z "$pr_number" ]]; then - echo "No open release PR found in $repo" - return 1 - fi - - echo "Found release PR #$pr_number in $repo" - - # Poll until all checks pass - echo "Waiting for checks to pass..." while true; do - local status - status=$(gh pr checks "$pr_number" --repo "$repo" 2>&1) - local rc=$? - - if [[ $rc -eq 0 ]]; then - echo "All checks passed!" + pr_number=$(gh pr list --repo "$repo" --state open --search "chore release in:title" --json number,title --jq '.[0].number') + if [[ -n "$pr_number" ]]; then + echo "" + echo " ✓ Found release PR #$pr_number" break fi - - if echo "$status" | grep -q "fail"; then - echo "Checks failed:" - echo "$status" - return 1 - fi - + printf "." sleep 15 done - # Merge using rebase + # Step 2: Poll until all checks pass + echo "" + printf "Waiting for checks on PR #$pr_number " + local check_output rc + while true; do + check_output=$(gh pr checks "$pr_number" --repo "$repo" 2>&1) + rc=$? + + if [[ $rc -eq 0 ]]; then + echo "" + echo " ✓ All checks passed!" + break + fi + + if echo "$check_output" | grep -q "fail"; then + echo "" + echo " ✗ Checks failed:" + echo "$check_output" | sed 's/^/ /' + return 1 + fi + + printf "." + sleep 15 + done + + # Step 3: Merge using rebase + echo "" echo "Merging PR #$pr_number via rebase..." if ! gh pr merge "$pr_number" --repo "$repo" --rebase; then - echo "Failed to merge PR #$pr_number" + echo " ✗ Failed to merge PR #$pr_number" return 1 fi - echo "Merged successfully!" + echo " ✓ Merged successfully!" - # Poll for homebrew-tap PR - echo "Waiting for homebrew-tap PR matching \"$tap_search\"..." + # Step 4: Poll for homebrew-tap PR and add label + echo "" + printf "Searching for homebrew-tap PR matching \"$tap_search\" " + + local _gtp_pr_number while true; do if _gtp_label "$tap_search"; then - return 0 + break fi + printf "." + sleep 15 + done + + # Step 5: Wait for tap PR to be merged + echo "" + printf "Waiting for tap PR #$_gtp_pr_number to be merged " + local wait_rc + while true; do + _gtp_wait_closed "$_gtp_pr_number" + wait_rc=$? + if [[ $wait_rc -eq 0 ]]; then + echo "" + echo " ✓ Tap PR #$_gtp_pr_number merged — release complete!" + return 0 + elif [[ $wait_rc -eq 1 ]]; then + echo "" + return 1 + fi + printf "." sleep 15 done }