gpt4 book ai didi

jenkins - 如何使用多分支插件仅构建已批准的拉取请求

转载 作者:行者123 更新时间:2023-12-04 10:50:39 24 4
gpt4 key购买 nike

我使用 multibranch plugin扫描新的拉取请求,我需要将我的构建限制为仅批准拉取请求。我在扫描批准的拉取请求后对其进行过滤,但是存储库扫描器只能判断拉取请求是否有新的提交。

我试过PR comment build plugin并添加了 Trigger build on pull request review属性到 github 分支源无济于事 - 添加此属性似乎对扫描器处理拉取请求的方式没有任何影响。

我可以告诉存储库扫描器触发新评论的构建吗?有没有其他方法可以仅在批准后构建拉取请求?

谢谢!

最佳答案

我不得不接受无法让分支扫描考虑非 git 对象(例如 github 评论)。

我最终制作了一个管道,其中包含所有尚未批准的拉取请求的列表(使用 github API),一旦拉取请求获得批准,它将触发构建。

感觉很hacky,但不幸的是,这是我能想到的仅在批准的基础上构建的唯一方法......
重要说明:此解决方案需要使用现有的多分支作业。
所以这就是我所做的:

首先查询现有的拉取请求及其状态(安装 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 状态环境变量,事情会简单得多:)

关于jenkins - 如何使用多分支插件仅构建已批准的拉取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59488945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com