Hub: 'hub browse pr' (or similar) to get the existing pull request url?

Created on 6 Jun 2017  路  5Comments  路  Source: github/hub

I use git browse a lot. If there's an existing PR for this branch, github will show it in a button, e.g.:
image

Is there was a way to have browse or similar to go directly to the PR url, if exists?
If not, does this sound like a good idea for a feature?

Most helpful comment

Coming soon to a hub near you 馃槃
image

All 5 comments

This would be welcome! See #1451 #1107 #897 #867. Closing due to duplicate.

There is already a hub pr subcommand in master branch. Maybe there could be a hub pr open command for this? I don't want to extend the hub browse command further because I want to deprecate it.

Coming soon to a hub near you 馃槃
image

any updates on the hub pr show mentioned above?

@eibrahim No, but you can list open PRs for the current branch using hub api and a shell script like the following:

existing-pr() {
  local branch
  if branch="$(git rev-parse --symbolic-full-name @{upstream} 2>/dev/null)"; then
    branch="${branch#refs/remotes/}"
    branch="${branch#*/}"
  else
    branch="$(git rev-parse --symbolic-full-name HEAD)"
    branch="${branch#refs/heads/}"
  fi

  hub api -t graphql -fbranch="$branch" -fquery='
    query($branch: String!) {
      repository(owner: "{owner}", name: "{repo}") {
        pullRequests(headRefName: $branch, states: OPEN, first: 10) {
          edges {
            node {
              url
              repository {
                owner {
                  login
                }
              }
              headRepository {
                owner {
                  login
                }
              }
            }
          }
        }
      }
    }
  ' | group_edges 3 | while read -r url base_owner head_owner; do
    [ "$base_owner" != "$head_owner" ] || printf '%s\n' "$url"
  done | head -1
}

group_edges() {
  grep -F '[' | cut -f2 | xargs -L${1?}
}

existing-pr

It might look complicated, but in reality it's straightforward: it detects the current branch (taking into account upstream configuration), then lists the URLs to PRs that have the current branch as the "head" branch, _excluding_ PRs that come from forks.

Looks like hub pr show is in!
Many thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DennisSchiefer picture DennisSchiefer  路  4Comments

stsewd picture stsewd  路  4Comments

Kristinita picture Kristinita  路  4Comments

xxmyjk picture xxmyjk  路  4Comments

cschwendiman picture cschwendiman  路  4Comments