Skip to content
Kwankyu Lee edited this page Oct 31, 2023 · 9 revisions

Bash scripts useful to review PRs locally.

Dependencies:

gh-review

Create a local branch named p/12345/pr-description from a GitHub PR #12345.

Usage:gh-review 12345

#!/bin/bash

set -ex
PR_NUMBER="$1"
BRANCH_NAME=p/$PR_NUMBER/$(gh pr view "$PR_NUMBER" --json headRefName | jq -r '.headRefName')
gh pr checkout $PR_NUMBER -b "$BRANCH_NAME" 

gh-close-merged:

Delete all local branches (created by gh-review) whose connected PR was merged in GitHub.

Usage: gh-close-merged

#!/bin/bash

# Iterate over all local branches
for branch in $(git branch | cut -c 3-); do
    # Check if the branch name matches the format
    if [[ $branch =~ ^p/([0-9]+) ]]; then
        # Extract the PR number from the branch name
        branch_pr_number=${BASH_REMATCH[1]}

        # Get the state of the pull request
        pr_state=$(gh pr view $branch_pr_number --json state | jq -r '.state')

        # Check if the pull request is merged
        if [[ $pr_state == "MERGED" ]]; then
            # Delete the local branch
            if git branch -d $branch; then
                echo "Branch $branch was deleted since pull request #$branch_pr_number was merged."
            else
                echo "Failed to delete branch $branch."
            fi
            echo "---"
        fi
    fi
done

Acknowledgment: Thanks, ChatGPT!

Clone this wiki locally