10

在 Azure DevOps 中,我有一个自定义构建步骤在我的拉取请求构建期间在某些条件下失败。

我想通过提出 PR 评论来进一步扩展它,类似于 GitHub 中的这类事情: https ://developer.github.com/v3/issues/comments/#create-a-comment

我没有要在此处添加的代码示例,因为我找不到可以构建的有用示例。我将 PowerShell 用于我的自定义构建步骤 - 在运行我的分支的 PR 构建时如何实现这一点?

4

3 回答 3

28

我可以帮忙举个例子。将自定义消息\状态从您的管道发布到 PR 有很多价值。

首先,确保您的构建服务有权为您的存储库中的拉取请求做出贡献。

权限

然后,您要添加条件 PowerShell 步骤。这只是基于它是一个 PR 构建,但您可能希望根据您的工作流程为上一步添加一个取决于失败。

- task: PowerShell@2
  condition: eq(variables['Build.Reason'], 'PullRequest')
  displayName: Post Message to PR
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)  
  inputs:
      targetType: filePath
      filePath: PostToPR.ps1

所以基本的工作流程是:

  • 构建 Markdown 消息
  • 构建 JSON 主体
  • 将消息发布到 PR

PostToPR.ps1

#Going to create the comment in an Active state, assuming it needs to be resolved
#See https://docs.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.sourcecontrol.webapi.commentthreadstatus?view=azure-devops-dotnet
$StatusCode = 1 

$Stuff = $env:Build_Repository_Name
$Things = "Other things you might want in the message"

#Build Up a Markdown Message to 
$Markdown = @"
## Markdown Message here
|Column0 |Column1|
|--------|---------|
|$Stuff|$Things|  
"@

#Build the JSON body up
$body = @"
{
    "comments": [
      {
        "parentCommentId": 0,
        "content": "$Markdown",
        "commentType": 1
      }
    ],
    "status": $StatusCode 
  }
"@

Write-Debug $Body
#Post the message to the Pull Request
#https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20threads?view=azure-devops-rest-5.1
try {
    $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:Build_Repository_Name)/pullRequests/$($env:System_PullRequest_PullRequestId)/threads?api-version=5.1"
    Write-Host "URL: $url"
    $response = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Body $Body -ContentType application/json
  if ($response -ne $Null) {
    Write-Host "*******************Bingo*********************************"
  }
}
catch {
  Write-Error $_
  Write-Error $_.Exception.Message
}

最后你会得到一个漂亮的降价表,在你的 PR 中包含自定义状态信息!

在此处输入图像描述

于 2020-02-04T02:43:38.887 回答
5

基于已经提供的出色答案,这是等效的内联 YAML 管道脚本:

  - powershell: |
      $body = @"
      {
          "comments": [
            {
              "parentCommentId": 0,
              "content": "Your comment here",
              "commentType": 1
            }
          ],
          "status": 4
        }
      "@
      $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:Build_Repository_Name)/pullRequests/$($env:System_PullRequest_PullRequestId)/threads?api-version=5.1"
      $result = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $(System.AccessToken)"} -Body $Body -ContentType application/json
    displayName: Post comment on PR
于 2021-01-04T14:15:30.690 回答
3

如果您的意思是在构建管道中创建 PR 评论,那么您可以在管道中添加 PowerShell 任务并运行脚本以调用 REST API(拉取请求线程评论 - 创建)。

下面的 PowerShell 脚本供您参考:

Param(
   [string]$baseurl = "https://dev.azure.com/{organization}",
   [string]$projectName = "0508-t",
   [string]$repositoryId = "62c8ce54-a7bb-4e08-8ed7-40b27831bd8b",
   [string]$pullRequestId = "35",
   [string]$threadId = "229",
   [string]$user = "",
   [string]$token = "PAT"  
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
write-host $WorkitemType

#Create Jason body

function CreateJsonBody
{

    $value = @"
{
  "content": "Test Comment 0204",
  "parentCommentId": 1,
  "commentType": 1
}
"@

 return $value
}

$json = CreateJsonBody

$uri = "$baseurl/$projectName/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/threads/$threadId/comments?api-version=5.1"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
于 2020-02-04T03:09:27.757 回答