问题
I'm trying to use the github api (via githubot https://github.com/iangreenleaf/githubot) to GET a pull request merge commit sha from the pull request number.
I'm able to get the proper response (example here under "get a single pull request": https://developer.github.com/v3/pulls/) but the merge_commit_sha gives me this error:
fatal: bad object 304fc816f33f808080c9c87895eea2d66081d373
When I compare the 2 pages on github I'm seeing both the merge_commit_sha from the commit page but I'm seeing a different commit sha from the pull request merge page. Both parent are the same but the merge commit is different. The one returned from the api call doesn't work, but the other one let's me revert the pull request commit via
git revert -m 1 commit_sha
Here are some example screen shots
So this leads me to 2 questions: - What is the difference between those 2 commit sha's and why does only one work to revert the pull request?
- How do I obtain the merge pull request commit sha using the github api?
Thanks.
回答1:
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 pr/<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 pr/<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.
回答2:
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.
来源:https://stackoverflow.com/questions/22331524/get-pull-request-merge-commit-sha-from-pull-request-number-using-github-api