[英]How to create a comment in Azure DevOps PR in case of build failure?
I have a custom build step that fails under certain conditions during my Pull Request build, within Azure DevOps.我在 Azure DevOps 中的 Pull Request 构建期间有一个自定义构建步骤,该步骤在某些情况下会失败。
I would like to extend it further by raising a PR comment, similar to this sort of thing in GitHub: https://developer.github.com/v3/issues/comments/#create-a-comment我想通过提出 PR 评论来进一步扩展它,类似于 GitHub 中的这种事情: https : //developer.github.com/v3/issues/comments/#create-a-comment
I don't have code samples to add here as I could not find useful examples to build upon.我没有要在此处添加的代码示例,因为我找不到可用于构建的有用示例。 I use PowerShell for my custom build step - how do I achieve this when running a PR build of my branch?我将 PowerShell 用于我的自定义构建步骤 - 在运行我的分支的 PR 构建时如何实现这一点?
I can help with an example.我可以帮忙举个例子。 There is a bunch of value in posting custom messages\\status to PRs from your pipelines.从您的管道向 PR 发布自定义消息\\状态有很多价值。
First things first, make sure your build service has permissions to contribute to pull requests in your repository.首先,请确保您的构建服务有权为您的存储库中的拉取请求做出贡献。
Then you want to add a conditional PowerShell step.然后你想添加一个有条件的 PowerShell 步骤。 This one is just based on it being a PR build, but you might want to add a depends on failure for the previous step, based on your workflow.这个只是基于它是一个 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
So the basic workflow is:所以基本的工作流程是:
#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
}
And you end up with a nice markdown table with custom status information in your PR!你最终会得到一个漂亮的降价表,在你的 PR 中包含自定义状态信息!
If you mean creating PR comment in the build pipeline, then you can add a PowerShell task in your pipeline and run the script to call the REST API ( Pull Request Thread Comments - Create ).如果您的意思是在构建管道中创建 PR 注释,那么您可以在管道中添加一个 PowerShell 任务并运行脚本来调用 REST API( 拉取请求线程注释 - 创建)。
Below PowerShell script for your reference:以下 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)}
Building on the great answers already provided, this is the equivalent inline YAML pipeline script:基于已经提供的出色答案,这是等效的内联 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.