gitgithubgithub-api

Git - Is it possible to get pull requests associated with a local branch?


I want to find the latest pull request associated with a local branch so that I can dynamically build a url to that pull request. My end goal is to build a url like this from a bash script:

https://github.com/organization/repo/pull/871

This questions is about how to get pull requests associated with a specific branch. I can take it from there.

I found this, but it returns all pull requests for a project. I'd like to filter this response so it returns only pull requests associated with the currently checked out branch locally:

https://gist.github.com/karlhorky/88b3c8c258796cd3eb97615da36e07be

Any ideas are much appreciated!


Solution

  • Assume you have the name of the repo owner as name, and the remote-tracking name of the local branch as branch, you can make a request to the GitHub API:

    GET https://api.github.com/repos/$name/$repo/pulls
    

    Then you go through each object in the returned JSON, check if item["head"]["label"] == "${name}:${branch}". If yes, then you can take item["html_url"] as the result - you don't even have to build it yourself!