简体   繁体   English

使用 github api 从拉取请求号获取拉取请求合并提交 sha

[英]GET pull request merge commit sha from pull request number using github api

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.我正在尝试使用 github api(通过 githubot https://github.com/iangreenleaf/githubot )从拉取请求编号中获取拉取请求合并提交 sha。

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:我能够得到正确的响应(“获取单个拉取请求”下的示例: https : //developer.github.com/v3/pulls/ )但 merge_commit_sha 给了我这个错误:

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.当我比较 github 上的 2 个页面时,我从提交页面看到了 merge_commit_sha,但我从拉取请求合并页面看到了不同的提交 sha。 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从 api 调用返回的一个不起作用,但另一个让我通过以下方式还原拉取请求提交

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?所以这让我想到了 2 个问题: - 这 2 个提交 sha 之间有什么区别,为什么只有一个可以恢复拉取请求?

  • How do I obtain the merge pull request commit sha using the github api?如何使用 github api 获取合并拉取请求提交 sha?

Thanks.谢谢。

GitHub has deprecated the merge_commit_sha attribute because it was confusing. GitHub 已弃用merge_commit_sha属性,因为它令人困惑。 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.这意味着 GitHub 创建一个特殊的分支,在其中合并 master 和您的拉取请求分支,并且merge_commit_sha指向该代理合并提交,但是您的本地存储库中没有它。 You would have to fetch the special pull/<pull_request_id>/merge branch to see that commit.您必须获取特殊的pull/<pull_request_id>/merge分支才能查看该提交。

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.Jenkins GitHub pull request builder这样的工具利用了这种技术。 Meanwhile commit_sha is the actual merge commit, that's why you are able to revert it.同时commit_sha是实际的合并提交,这就是您能够还原它的原因。

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.因此,如果仍然不清楚, merge_commit_sha确实为您提供了正确的提交 sha,但要使用它,您必须先获取pull/<pull_request_id>/merge分支。 To avoid problems of future deprecation, you could fetch the head commit of the aforementioned merge branch instead of using merge_commit_sha .为了避免将来弃用的问题,您可以获取上述合并分支的头部提交,而不是使用merge_commit_sha

By the way, if you're building something with Hubot, you may want to check out this book (shameless plug).顺便说一句,如果你正在用 Hubot 构建一些东西,你可能想看看这本书(无耻的插件)。 It includes a chapter about GitHub integration.它包括一个关于 GitHub 集成的章节。

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 http://developer.github.com/v3/issues/events/#events-1

The commit_id attribute of that event will hold the sha of the merge commit.该事件的commit_id属性将保存合并提交的 sha。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM