如果构建失败,如何在 Azure DevOps PR 中创建评论?
How to create a comment in Azure DevOps PR in case of build failure?
我在 Azure DevOps 中的合并请求构建过程中有一个自定义构建步骤在某些情况下失败。
我想通过提出 PR 评论进一步扩展它,类似于 GitHub 中的此类事情:
https://developer.github.com/v3/issues/comments/#create-a-comment
我没有要在此处添加的代码示例,因为我找不到有用的示例来构建。我在自定义构建步骤中使用 PowerShell - 运行 我的分支的 PR 构建时如何实现此目的?
我可以帮忙举个例子。从您的管道向 PR 发布自定义 messages\status 有很多价值。
首先,确保您的构建服务有权参与您存储库中的拉取请求。
然后你想添加一个有条件的 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 正文
- Post 给 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
}
你最终得到了一个漂亮的降价 table,在你的 PR 中包含自定义状态信息!
如果您的意思是在构建管道中创建 PR 评论,那么您可以在您的管道中添加一个 PowerShell 任务和 运行 脚本来调用 REST API (Pull Request Thread Comments - Create) .
以下 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)}
基于已经提供的出色答案,这是等效的内联 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
我在 Azure DevOps 中的合并请求构建过程中有一个自定义构建步骤在某些情况下失败。
我想通过提出 PR 评论进一步扩展它,类似于 GitHub 中的此类事情: https://developer.github.com/v3/issues/comments/#create-a-comment
我没有要在此处添加的代码示例,因为我找不到有用的示例来构建。我在自定义构建步骤中使用 PowerShell - 运行 我的分支的 PR 构建时如何实现此目的?
我可以帮忙举个例子。从您的管道向 PR 发布自定义 messages\status 有很多价值。
首先,确保您的构建服务有权参与您存储库中的拉取请求。
然后你想添加一个有条件的 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 正文
- Post 给 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
}
你最终得到了一个漂亮的降价 table,在你的 PR 中包含自定义状态信息!
如果您的意思是在构建管道中创建 PR 评论,那么您可以在您的管道中添加一个 PowerShell 任务和 运行 脚本来调用 REST API (Pull Request Thread Comments - Create) .
以下 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)}
基于已经提供的出色答案,这是等效的内联 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