簡體   English   中英

如何使用多分支插件僅構建已批准的拉取請求

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

我使用multibranch 插件來掃描新的拉取請求,並且我需要將我的構建限制為僅批准拉取請求。 我在掃描批准的拉取請求后對其進行過濾,但是存儲庫掃描器只能判斷拉取請求是否有新的提交。

我嘗試了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 狀態環境變量,事情會簡單得多:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM