26

The Github API (v3) allows you to get a listing of pull requests, and get more details on an individual pull request. What I can't seem to find is the name of the branch the pull request is coming from and the branch the pull request is suggesting the code be merged into.

Using the Github API how do you determine the branches involved in a pull request?

masukomi
  • 10,313
  • 10
  • 40
  • 49

1 Answers1

43
  1. Access a Pull Request URL. Let's use https://api.github.com/repos/github/gitignore/pulls/566 as an example.

  2. Parse the JSON object.


A Pull Request references two branches. The base branch is the merge target. Usually this is the master branch of the repository.

  • base.label is github:master, meaning it's the master branch for > github/gitignore.
  • base.ref is the branch name "master".
  • base.sha is the current SHA of that branch.

The head branch is what you're merging into the base.

  • head.label is fidelski:add-obvious-autotools-files, meaning it's the add-obvious-autotools-files branch for fidelski/gitignore.
  • head.ref is the branch name add-obvious-autotools-files.
  • head.sha is the current SHA of that branch.
Beni Cherniavsky-Paskin
  • 9,483
  • 2
  • 50
  • 58
technoweenie
  • 846
  • 1
  • 8
  • 4
  • 2
    I have found that `base.sha` can be a commit on the base branch which is newer than the `git merge-base`, yet older than the current head of that branch, by about a day, even for a PR which has not been touched in longer than that. Perhaps it gets periodically updated to the base branch head but is then cached for a while? – Jesse Glick Nov 06 '15 at 22:13
  • Yep, @JesseGlick is right, `base.sha` cannot be trusted. If you're doing anything that relies on the base branch current sha, get it direct. – Shawn Erquhart Oct 25 '17 at 22:12