我想检查功能分支是否已使用github api / Octokit合并到我的主分支中。我还没能找到关于这个主题的任何文档或帖子。这可能吗?有谁知道任何解决这个主题的帖子?
答案 0 :(得分:4)
这相当于git log master..my-cool-branch
使用Octokit - 您想知道您的分支上是否有任何不在master
的提交:
var head = "my-cool-branch";
var baseBranch = "master";
var compareBaseToHead = await client.Repository.Commit.Compare(owner, repo, baseBranch, head);
if (compareBaseToHead.TotalCommits == 0)
{
Console.WriteLine("Branch {0} has been merged into {1}", head, baseBranch);
}
else
{
Console.WriteLine("Branch {0} has NOT been merged into {1}", head, baseBranch);
}
答案 1 :(得分:2)
我实际上刚刚完成了一个包含这种检查的脚本。我的脚本的总体目标是查看我们组织中的所有存储库并清理已合并的分支。该脚本以groovy编写并使用kohsuke's github-api java client。 java客户端缺少一个我打开PR的功能。在PR合并并发布新版本之前,您必须省略分支保护检查或执行我所做的操作(自行提取代码并使用添加的功能构建代码)。
检查分支是否已合并且没有新的合并提交的逻辑在cleanupBranches
方法中。基本上,我循环遍历给定仓库的所有CLOSED拉动请求(PR)。如果拉取请求被合并,那么我将PR的头部分支与repo的默认分支进行比较(并非我们所有的分支都命名为master
)。如果PR的头部分支有0个提交尚未合并到默认分支,则可以删除分支。
我将整个脚本仅用于上下文。您会注意到我确实更改了一些配置属性默认值。
@GrabResolver(name='binrepo', root='https://binrepo.myCompany.com')
@Grab(group='org.kohsuke', module='github-api', version='SNAPSHOT')
import org.kohsuke.github.*
// store some things here so they are available everywhere
class Config {
static Boolean doDelete = Boolean.valueOf(System.getProperty('branch.cleanup.doDelete', 'false'))
static String orgName = System.getProperty('branch.cleanup.gitOrgName', 'myOrg')
static String gitApiUrl = System.getProperty('branch.cleanup.gitApiUrl', 'https://git.myCompany.com/api/v3')
static String apiKey
}
def executeOnShell(String command, boolean log = false) {
File workingDir = new File(System.properties.'user.dir')
def process = new ProcessBuilder(addShellPrefix(command))
.directory(workingDir)
.redirectErrorStream(true)
.start()
if (log) {
process.inputStream.eachLine { println it }
}
process.waitFor();
}
def addShellPrefix(String command) {
def commandArray = new String[3]
commandArray[0] = "sh"
commandArray[1] = "-c"
commandArray[2] = command
return commandArray
}
def allRepos(GHOrganization org, String...repoPrefixes) {
println "Fetching all repositories under the ${Config.orgName} org that match prefix(es) ${repoPrefixes}"
return org.getRepositories().entrySet().findAll{ entry ->
if (repoPrefixes) {
return repoPrefixes.any{ prefix -> entry.key.startsWith(prefix) }
} else {
return true
}
}
}
def cleanupBranches(repo) {
def defaultBranchName = repo.getDefaultBranch()
def defaultBranch = repo.getBranch(defaultBranchName)
def deletedBranchNames = []
def branchesByName = repo.getBranches().entrySet().findAll{ !it.key.equals(defaultBranchName) && !it.value.isProtected() }.collectEntries{[it.key, it.value]}
def pullRequests = repo.queryPullRequests().base(defaultBranchName).state(GHIssueState.CLOSED).list().withPageSize(100).each{ pr ->
// loop thru all pull requests that have been closed and also merged
if (pr.isMerged()) {
def branch = branchesByName.get(pr.getHead().getRef())
if (branch) {
// the branch still exists and has been merged by this PR
// make sure it doesn't have any unmerged commits
def compare = repo.getCompare(defaultBranch, branch)
if (compare.getTotalCommits() == 0) {
// branch has been merged and there are 0 commits since merge. delete it
println "Branch ${repo.getName()}/${branch.getName()} has 0 commits not merged to ${defaultBranchName}. Delete it. PR ${pr.getNumber()} : ${pr.getTitle()}"
if (Config.doDelete) {
deleteBranch(repo, branch)
}
// remove from internal map of branches since the branch has now been deleted in git
branchesByName.remove(branch.getName())
deletedBranchNames.push "${repo.getName()}/${branch.getName()}"
}
}
}
}
return deletedBranchNames
}
def deleteBranch(repo, branch) {
// use a simple curl here because the kohsuke library way of doing it requires 2 api calls when just 1 will do here
String cmd = "curl -X DELETE -H \"Authorization: token ${Config.apiKey}\" ${Config.gitApiUrl}/repos/${Config.orgName}/${repo.getName()}/git/refs/heads/${branch.getName()}"
executeOnShell(cmd)
}
if (args.size() < 1) {
println "Usage: cleanupRepoBranches.groovy <oauthToken> <optionalRepo-name>"
System.exit(1)
}
Config.apiKey = args[0]
def branchesDeleted = []
def errors = []
GitHub github = GitHub.connectToEnterprise(Config.gitApiUrl, Config.apiKey)
if (args.size() > 1) {
String repoName = args[1]
GHRepository repo = github.getRepository("${Config.orgName}/${repoName}")
branchesDeleted = cleanupBranches(repo)
} else {
def repoPrefixes = System.getProperty('branch.cleanup.repoPrefixes', 'pref-,pref2-').split(',')
def answer = System.console().readLine "You have not specified a repoName. If you proceed, this script will list ${Config.doDelete ? 'and delete ' : ''}all branches with a merged pull request and 0 commits left to merge for all repos starting with ${repoPrefixes.join(', ')} in the ${Config.orgName} org. Are you sure? (y/n) "
if (answer == 'y') {
println 'ok! here we go!'
allRepos(github.getOrganization(Config.orgName), repoPrefixes).each { entry ->
try {
branchesDeleted += cleanupBranches(entry.value)
} catch (Exception e) {
errors.push([ message: "Error processing branches for ${entry.key} repo", ex: e ])
}
}
}
}
println "${branchesDeleted.size()} Branches deleted..."
branchesDeleted.each{ branch -> println branch }
println "${errors.size()} errors..."
errors.each{ error ->
println error.message
error.ex.printStackTrace()
println
}
答案 2 :(得分:1)
我相信你可以从pull请求中获取分支,然后在合并之后检查pull请求是否已合并。更多信息请访问:
https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged