如果构建失败,如何在 Azure DevOps PR 中创建评论? [英] How to create a comment in Azure DevOps PR in case of build failure?

查看:10
本文介绍了如果构建失败,如何在 Azure DevOps PR 中创建评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Azure DevOps 中的 Pull Request 构建期间有一个自定义构建步骤,该步骤在某些情况下会失败.

我想通过提出 PR 评论来进一步扩展它,类似于 GitHub 中的这种事情:

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

- 任务:PowerShell@2条件:eq(变量['Build.Reason'],'PullRequest')显示名称:向 PR 发布消息环境:SYSTEM_ACCESSTOKEN: $(System.AccessToken)输入:目标类型:文件路径文件路径:PostToPR.ps1

所以基本的工作流程是:

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

PostToPR.ps1

#Going 以Active状态创建评论,假设需要解析#参见 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 =您可能希望在消息中包含的其他内容";#建立一个降价消息$Markdown = @"## Markdown 消息在这里|第 0 列 |第 1 列||--------|---------||$东西|$东西|"@#构建JSON主体$body = @"{评论":[{parentCommentId":0,内容":$Markdown",评论类型":1}],状态":$StatusCode}"@写调试 $Body#将消息发布到Pull Request#https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20threads?view=azure-devops-rest-5.1尝试 {$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$($env:Build_Repository_Name)/pullRequests/$($env:System_PullRequest_PullRequestId)/threads?api-version=5.1"写主机URL:$url"$response = Invoke-RestMethod -Uri $url -Method POST -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Body $Body -ContentType application/json如果 ($response -ne $Null) {写主机"*******************宾果游戏************************************"}}抓住 {写错误 $_写错误 $_.Exception.Message}

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

I have a custom build step that fails under certain conditions during my Pull Request build, within Azure DevOps.

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

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?

解决方案

I can help with an example. There is a bunch of value in posting custom messagesstatus to PRs from your pipelines.

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. 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.

- 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:

  • Build the Markdown Message
  • Build the JSON Body
  • Post the Message to the 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
}

And you end up with a nice markdown table with custom status information in your PR!

这篇关于如果构建失败,如何在 Azure DevOps PR 中创建评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆