GET pull request merge commit sha from pull request number using github api

GitHub has deprecated the merge_commit_sha attribute because it was confusing. As they describe here:

 The merge_commit_sha attribute holds the SHA of the test merge commit

Which means that GitHub creates a special branch where they merge master and your pull request branch, and merge_commit_sha points to that surrogate merge commit, however you don't have it in your local repo. You would have to fetch the special pull/<pull_request_id>/merge branch to see that commit.

Good thing is that you can do that before merging the pull request. And tools like Jenkins GitHub pull request builder take advantage of this technique. Meanwhile commit_sha is the actual merge commit, that's why you are able to revert it.

So, if it's still unclear, merge_commit_sha does give you the correct commit sha, but to use it you have to fetch pull/<pull_request_id>/merge branch first. To avoid problems of future deprecation, you could fetch the head commit of the aforementioned merge branch instead of using merge_commit_sha.

By the way, if you're building something with Hubot, you may want to check out this book (shameless plug). It includes a chapter about GitHub integration.


You can fetch the list of events for a pull requests, and then find the "merged" event:

http://developer.github.com/v3/issues/events/#events-1

The commit_id attribute of that event will hold the sha of the merge commit.