如何挑选GitHub的API

时间:2018-12-19 21:12:11

标签: git github github-api

我无法通过GitHub API执行简单摘樱桃。一定有可能,但是我不清楚如何...

我正在为C#中的Slack开发一个聊天机器人,以帮助管理Kubernetes环境和GitHub版本,并希望通过修补程序功能对其进行扩展。在给定环境的情况下,它应该创建一个与当前版本匹配的分支,并在其中选择一个或多个提交SHA,这是请求的作者通过Slack提供的。

所有管道均已安装到位。使用POST /repos/:owner/:repo/git/refs,我可以创建与特定发行版匹配的分支。这意味着我已经准备好branch namecommit SHAtree SHA进行下一步了;挑选一个或多个将SHA提交到该分支。使用POST /repos/:owner/:repo/git/commits我可以创建一个提交,但是我不确定要使用哪个树和/或父树-这可能会导致我在调用POST /repos/:owner/:repo/merges时遇到问题,因为它返回的状态为409(合并冲突),当然不在本地。

我似乎发现的唯一真实示例是https://github.com/tibdex/github-cherry-pick。但是,它并不完全符合我的情况,而且我很难理解Git的内部工作原理。

我的情况(最新到最旧);

* commit E (current state of `master`)
* commit D
* commit C (deployed to environment)
* commit B
* commit A

在这种情况下,我想将提交E摘录到提交C的新分支中,创建一个可以释放的集合(A,B,C,E)。

* commit E (current state of `master`)
* commit D
|
| * commit E (new branch, to be deployed)
|/
* commit C (deployed to environment)
* commit B
* commit A

基本上我需要的是此bash的GitHub API版本;

git checkout -b {new-branch-name} {sha}
git cherry-pick {sha}
git push main {new-branch-name}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这是我通过伪代码在Github API上实现Cherry-pick的方式:

// here is a commit, for example, from a pull request:
listOfCommits = GET /repos/$owner/$repo/pulls/$number/commits
commit = listOfCommits.head // the first one in the list

// Here is the branch we want to cherry-pick to:
branch = GET /repos/$owner/$repo/branches/$branchName
branchSha = branch.commit.sha
branchTree = branch.commit.commit.tree.sha

// Create a temporary commit on the branch, which extends as a sibling of
// the commit we want but contains the current tree of the target branch:
parentSha = commit.parents.head // first parent -- there should only be one
tempCommit = POST /repos/$owner/$repo/git/commits { "message": "temp",
                                                    "tree": branchTree,
                                                    "parents": [parentSha] }

// Now temporarily force the branch over to that commit
PATCH /repos/$owner/$repo/git/refs/heads/$refName { sha = tempCommit.sha,
                                                    force = true }

// Merge the commit we want into this mess:
merge = POST /repos/$owner/$repo/merges { "base": branchName
                                  "head": commit.sha }

// and get that tree!
mergeTree = merge.commit.tree.sha

// Now that we know what the tree should be, create the cherry-pick commit.
// Note that branchSha is the original from up at the top.
cherry = POST /repos/$owner/$repo/git/commits { "message": "looks good!",
                                                "tree": mergeTree,
                                                "parents": [branchSha] }

// Replace the temp commit with the real commit:
PATCH /repos/$owner/$repo/git/refs/heads/$refName { sha = cherry.sha,
                                                    force = true }

// Done!

Git专家,请提出建议,但是我相信这可以实现:

git checkout -b {new-branch-name} {sha}
git cherry-pick {sha}
git push main {new-branch-name}