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

How to build only approved pull requests using the multi-branch plugin

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

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

我可以告诉存储库扫描程序触发基于新评论的构建吗?有没有其他方法只有在批准后才能构建拉取请求?

谢谢!

我不得不承认没有办法让分支扫描考虑非 git 对象(例如 github 评论)。

我最终制作了一个管道,其中保留了所有尚未批准的拉取请求的列表(使用 git 中心 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 状态环境变量,事情会简单得多:)