【问题标题】:How do I get the code reviewer(s) of a build in Azure DevOps pipeline?如何在 Azure DevOps 管道中获得构建的代码审阅者?
【发布时间】:2020-08-29 02:25:58
【问题描述】:

给定构建 ID,我如何在 azure DevOps 管道中获取代码审阅者姓名? 假设,构建来自主分支 - 在拉取请求中审查代码后,开发人员合并他们的功能分支。这是一项政策,没有人直接将他们的更改提交给 master。这意味着,每个构建背后都有一个代码审查员。我怎么得到它?

谢谢!

【问题讨论】:

  • 构建不是代码审查,PR 是。您可以查找已构建的分支并查看它是否来自 PR 分支,然后从该分支向后工作到 PR ID。
  • @DanielMann 我知道构建没有经过代码审查。但是构建总是来自主分支。并且 master 中的任何代码更改始终来自至少由一名开发人员审查的 PR。问题是如何知道给定构建 ID 的 PR。或者基本上如何知道与构建相关的 PR?请记住,您可以拥有 build1、build2、...buildn,每个都在不同的日期时间使用不同的 PR。所以我不能简单地依赖最新的 PR。我唯一的参考是一个构建,我如何从中获得 PR?

标签: build pull-request azure-devops-rest-api build-pipeline tfs-code-review


【解决方案1】:

您可以使用下面的Rest api 来获取 PR 审稿人。

1、首先调用build rest api下面的buildId。在响应中,您将从构建的 sourceVersion 和存储库 id 中获取提交 id。

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1

2,在获得提交ID和存储库ID之后。您可以调用 commit rest api 从响应中的 cmets 获取关联的 PR id。

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?api-version=5.1

3、然后调用pullrequest reviewer rest api获取Reviewers。

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/reviewers?api-version=5.1

以下是 powershell 中的示例脚本。请参阅this link 以获取个人访问令牌

$buildId= " "

$burl =" https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"

$PAT="personel access token"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$buildInfo = Invoke-RestMethod -Uri $curl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get CommitId and repoId
$commitId = $buildInfo.sourceVersion
$repoId=$buildInfo.repository.id

#commit rest api
$curl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/commits/$($commitId)?api-version=5.1"

$commitInfo = Invoke-RestMethod -Uri $curl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"
#get PR id
$prId = $commitInfo.comment.split(" ")[2].TrimEnd(":")

$prurl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($repoId)/pullRequests/$($prId)/reviewers?api-version=5.1"

Invoke-RestMethod -Uri $prurl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo1)} -Method get -ContentType "application/json"

如果您可以使用给定的 buildId 从 UI 页面中的管道运行历史记录中找到构建。这会容易得多。您可以直接从标题中获取 PR id。见下图。

您也可以单击上面屏幕截图中显示的提交 ID,查看提交的详细信息,您将在其中获得相关的 PR。

【讨论】:

    【解决方案2】:

    这是我最终的工作。使用上面的 Levi 的代码 sn-p 并修复了一行以获取在各种场景中工作的拉取请求 ID。感谢李维斯的帮助!希望它可以帮助某人。

    
    $PAT="personel access token"
    $base64EncodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    $basicAuth = @{Authorization = "Basic $base64EncodedPAT" }
    $buildId= "..."
    
    function GetCodeReviewers() {
        #Get build info
        $buildUrl = "https://dev.azure.com/OrgName/ProjName/_apis/build/builds/$($buildId)?api-version=5.1"
        $buildInfo = Invoke-RestMethod -Method Get -Uri $buildUrl -Headers $basicAuth
    
        # Get Commit Info
        $commitUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/commits/$($buildInfo.sourceVersion)?api-version=5.1"
        $commitInfo = Invoke-RestMethod -Uri $commitUrl  -Method Get -Headers $basicAuth
    
        #Get Code Reviewers
        $comment = $commitInfo.comment
        #$pullRequestId = $comment.split(" ")[2].TrimEnd(":") # it turns out, the 3rd item may not always be the PullRequestID so the next line may not work for all scenarios
        #note that, a comment could come as follows:
        # case 1: Merge PR 1234: some other text here including story or bug numbers
        # case 2: Merge pull request 1234 some additional text goes here including story or bug numbers
        # The following will pick the first number - which I assume will always be the PullRequestID
        $pullRequestId = $null
        $pullRequestId = $comment.Replace(':', '').Split(" ").Trim() | Where-Object {[int]::TryParse($_, $pullRequestId)} | Select-Object -First 1
        $pullRequestUrl = "https://dev.azure.com/OrgName/ProjName/_apis/git/repositories/$($buildInfo.repository.id)/pullRequests/$($pullRequestId)/reviewers?api-version=5.1"
        $reviewers = Invoke-RestMethod -Uri $pullRequestUrl -Method Get -Headers $basicAuth
    
        return $reviewers.value
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2022-06-10
      • 1970-01-01
      • 2022-11-24
      • 2021-09-18
      • 2020-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多