jenkins - 如何使用多分支插件仅构建已批准的拉取请求
问题描述
我使用多分支插件来扫描新的拉取请求,并且我需要将我的构建限制为仅批准的拉取请求。我在扫描批准的拉取请求后过滤它们,但是存储库扫描器只能判断拉取请求是否有新的提交。
我尝试了PR 评论构建插件并将该Trigger build on pull request review
属性添加到 github 分支源中无济于事 - 添加此属性似乎对扫描程序如何处理拉取请求没有任何影响。
我可以告诉存储库扫描程序触发新评论的构建吗?只有在批准后,还有其他方法可以构建拉取请求吗?
谢谢!
解决方案
我不得不承认没有办法让分支扫描考虑非 git 对象(例如 github 评论)。
我最终创建了一个管道,其中保留了所有尚未批准的拉取请求的列表(使用 github API),一旦拉取请求被批准,它将触发对其进行构建。
感觉很老套,但不幸的是,这是我能想到的仅在批准的基础上构建的唯一方法……重要提示:此解决方案需要使用现有的多分支作业。所以这就是我所做的:
首先查询现有的拉取请求及其状态(安装httpRequest插件):
// Send a query to github and get the response JSON
def githubQuery(Map args = [:]) {
def formattedQuery = args.query.replaceAll('\n', ' ').replaceAll('"', '\\\\"')
def response = httpRequest(
authentication: args.auth,
httpMode: 'POST',
requestBody: """{ "query": "${formattedQuery}" }""",
url: "https://api.github.com/graphql"
)
(response.status == 200) ? readJSON(text: response.content) : [:]
}
def getPRStatus(Map args = [:]) {
// Build the query to get all open pull requests and their status
def query = """query {
organization(login: "${args.organization}") {
repositories(first: 30) {
nodes {
name
pullRequests(first: 100, states: [OPEN]) {
nodes {
number
state
reviews(first: 10, states: [APPROVED]) {
totalCount
}
}
}
}
}
}
}"""
def response = githubQuery(args + [query: query])
def repositories = response?.data.organization.repositories.nodes
// Organize the pull requests into approved and unapproved groups
repositories?.collectEntries { repo ->
// Take out draft pull requests
def prs = repo.pullRequests.nodes.findAll { it.state != "DRAFT" }
def repoPrs = [
unapproved: prs.findAll { it.reviews.totalCount == 0 },
approved: prs.findAll { it.reviews.totalCount > 0 }
].collectEntries { category, categoryPrs ->
[ category, categoryPrs.collect { it.number } ]
}
[ repo.name, repoPrs ]
}
}
然后将每个拉取请求的状态与之前投票的状态进行比较,并仅构建那些将其状态更改为已批准的请求:
def monitorRecentlyApprovedPRs(Map args = [:]) {
def prMap = getPRStatus(args)
// Build recently approved pull requests on each repository
prMap.each { repoName, repoPrs ->
// Get previously unapproved pull requests
def previouslyUnapproved = currentBuild.previousBuild?.buildVariables?."${repoName}"?.tokenize(",").collect { it.toInteger() } ?: []
// Build recently approved pull requests
repoPrs.approved.intersect(previouslyUnapproved).each { prNumber ->
build job: "/${args.multibranch}/PR-${prNumber}", wait: false
}
env."${repoName}" = repoPrs.unapproved.join(",")
}
}
调用时monitorRecentlyApprovedPRs
,您必须提供以下参数:
monitorRecentlyApprovedPRs organization: "YOUR-ORGANIZATION", auth: "GITHUB-CREDENTIALS", multibranch: "PATH-TO-THE-MULTIBRANCH-JOB-IN-JENKINS"
最后,更新多分支Jenkinsfile
以跳过未批准的 PR:
def shouldBuildPR(Map args = [:]) {
// Get pull request info
def query = """query {
organization(login: "${args.organization}") {
repository(name: "${args.repo}") {
pullRequest(number: ${args.pr}) {
state
reviews(first: 10, states: [APPROVED]) {
totalCount
}
}
}
}
}"""
def response = githubQuery(args + [query: query])
def prInfo = response?.data.organization.repository.pullRequest
def shouldBuild = (
// Skip merged pull requests
prInfo.state != "MERGED" &&
// Check for draft state
(prInfo.state != "DRAFT") &&
// Check for approval
(prInfo.reviews.totalCount > 0)
)
shouldBuild
}
要调用shouldBuildPR
,您将提供以下参数:
shouldBuildPR(organization: "YOUR-ORGANIZATION", repo: "PR-REPO", auth: "GITHUB-CREDENTIALS", pr: env.CHANGE_ID)
如果返回值是false
,您应该停止管道的其余执行。如果多分支管道插件提供 PR 状态环境变量,事情会简单得多:)
推荐阅读
- wordpress - 如何修复访问控制允许来源错误
- javascript - 如何使用 hyperledger Fabric 区块链应用程序修复“错误:14 不可用:TCP 写入失败”
- javascript - 使用 Autohotkey 运行书签
- javascript - 大于/小于符号不会被转义
- json - 如何在 SWIFT 中处理 JSON 中的空值
- ios - 无法为私有仓库添加带有 url 的源
- c++ - 关闭套接字如何影响对方读取
- asp.net - 是否可以扩展 IEntityHistoryStore 以存储每个表而不是在同一个表中跟踪所有内容?
- r - 如何获得所有变量的 woe(证据权重)值以及如何在 r 中应用逻辑回归模型?
- javascript - 数据绑定在哪里属于单页应用程序(JS 和 HTML)