Skip to content

Instantly share code, notes, and snippets.

@riosengineer
Created September 23, 2024 12:52
Show Gist options
  • Save riosengineer/521d014b74731dd9c0f6127dd834f132 to your computer and use it in GitHub Desktop.
Save riosengineer/521d014b74731dd9c0f6127dd834f132 to your computer and use it in GitHub Desktop.
Post Markdown Reports to Azure DevOps Pull Request Comments via ADO API
#Status codes https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.sourcecontrol.webapi.commentthreadstatus?view=azure-devops-dotnet
$statusCode = 1
# Finding all ACE Markdown reports in directory
$markdownFiles = Get-ChildItem -Path . -Recurse -Filter ace_*.md
foreach ($markdownFile in $markdownFiles) {
# MD Content & JSON Body
Write-Host "Attempting to post markdown $markdownFile to PR"
$content = Get-Content -Path $markdownFile -Raw
Show-Markdown -Path $markdownFile
$body = @"
{
"comments": [
{
"parentCommentId": 0,
"content": "$content",
"commentType": 1
}
],
"status": $StatusCode
}
"@
Write-Debug $Body
# Post content to PR
# https://learn.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=7.0"
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment