libgit2sharp what is correct sha to supply to GitHub API merge pull-request?

青春壹個敷衍的年華 提交于 2019-12-11 03:46:31

问题


GitHub API requires merge pull-request to be submitted as

PUT /repos/:owner/:repo/pulls/:number/merge

with request body json

{
  "commit_message": "blah",
  "sha": "{SHA that pull request head must match to allow merge}",
}

Following a commit, push, create PR, what libgit2sharp property supplies the correct sha ?

For the current branch, it appears Branch.Tip.Sha is the correct value, but I'm receiving response error :

{ "message": "Head branch was modified. Review and try the merge again.", "documentation_url": "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button" }


回答1:


Two different commits and shas come into play when it comes to Pull Request.

  • The tip of your branch (the very last commit you've pushed on your branch)

    • Syntax: GET /repos/:owner/:repo/git/refs/:ref (where :ref should be of the pull/{number}/head format)
    • Example: https://api.github.com/repos/libgit2/libgit2sharp/git/refs/pull/1123/head
  • The virtual merge commit that GitHub dynamically creates behind the scene to determine mergeability and allow CI servers to run your build/tests as if your branch was already merged (and thus, detect in advance any potential issue)

    • Syntax: GET /repos/:owner/:repo/git/refs/:ref (where :ref should be of the pull/{number}/merge format)
    • Example: https://api.github.com/repos/libgit2/libgit2sharp/git/refs/pull/1123/merge

When leveraging the GitHub API to merge an opened Pull Request, the optional sha property from the json payload is expected to match the sha of the branch tip as currently known by GitHub.

Provided your local repository is in sync with what GitHub knows about your repository, this should definitely be matching what repo.Branches["your_topic_branch"].Tip.Sha returns.

Note: In order to ensure that the GitHub known head of the PR matches your local branch tip, using LibGit2Sharp, you can retrieve GitHub PR merge/head pointed at commits, by directly fetching a special reference namespace. The following code demonstrates this

var remoteName = "origin"; // or whatever your remote is named
var remote = repo.Network.Remotes[remoteName];
var prNumber = "1123"; // or whatever your pr number is

// Build the refspec
string refSpec = string.Format("+refs/pull/{1}/*:refs/remotes/{0}/pull/{1}/*", 
                        remoteName, prNumber);

// Perform the actual fetch
repo.Network.Fetch(remote, new[] { refSpec });

Console.WriteLine(repo.Branches[string.Format("pull/{0}/merge", prNumber)].Tip.Sha);


来源:https://stackoverflow.com/questions/31488803/libgit2sharp-what-is-correct-sha-to-supply-to-github-api-merge-pull-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!