Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Created November 1, 2023 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuelnk/8fe9d2084b271bd27a9a704181dfd080 to your computer and use it in GitHub Desktop.
Save emmanuelnk/8fe9d2084b271bd27a9a704181dfd080 to your computer and use it in GitHub Desktop.
Open Github PR from Terminal (SSH Authorization)
  1. Install Dependencies

    brew install jq
    brew install hub
  2. Log into hub (from any repo just do hub pr list)

    • use your github username for username
    • use a personal access token for password (token should have repo access)
  3. Add script:

    touch ~/whatever/path/openpr.sh
    chmod +x ~/whatever/path/openpr.sh
  4. Add this code to openpr.sh

    #!/bin/zsh
    
    # Check if we're in a Git repository
    git rev-parse --is-inside-work-tree >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "You are not inside a Git repository. Please navigate to a Git repository and try again."
      exit 1
    fi
    
    # Get the current branch name
    current_branch=$(git symbolic-ref --short HEAD)
    
    if [ -z "$current_branch" ]; then
      echo "You are not on a valid Git branch. Please switch to a branch and try again."
      exit 1
    fi
    
    # Get the GitHub user and repository name
    github_user_and_repo=$(git config --get remote.origin.url | sed 's|.*:\(.*\)/\(.*\)\.git|\1/\2|')
    
    # Get the pull request information
    pr_info=$(hub pr list -f "%I %H%n" | grep $current_branch)
    
    if [ -z "$pr_info" ]; then
      open "https://github.com/$github_user_and_repo/compare/$current_branch"
    else
      # Get the PR number
      pr_number=$(echo $pr_info | cut -d ' ' -f 1)
    
      # Open the pull request in the browser
      open "https://github.com/$github_user_and_repo/pull/$pr_number"
    fi
  5. Add the alias to .zshrc:

    alais='~/whatever/path/openpr.sh`
  6. Now from the root of any github repo that has a remote, you can just do:

    openpr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment