Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwallis3101/d416678af332b4f28deb0e2a6079f3f6 to your computer and use it in GitHub Desktop.
Save davidwallis3101/d416678af332b4f28deb0e2a6079f3f6 to your computer and use it in GitHub Desktop.
<#
https://stackoverflow.com/questions/57294829/create-a-specific-work-item-bug-for-each-failed-test-case-in-azure-devops-pipe
https://www.c-sharpcorner.com/blogs/create-work-items-in-visual-studio-team-services-using-powershell
https://medium.com/@sandeepsinh/create-work-item-with-rest-api-in-azure-devops-28f979a12f37
https://stackoverflow.com/questions/44707891/tfs-rest-api-for-workitem-missing-relations-section-in-json-returned
#>
<#
.SYNOPSIS
New-WorkItemsFromTestFailure
.DESCRIPTION
Creates work items from test failures
.NOTES
For additional information please contact david@wallis2000.co.uk
.LINK
http://blog.wallis2000.co.uk
#>
[CmdletBinding()]
param ()
If ($null -eq $env:SYSTEM_ACCESSTOKEN) { Throw "Please allow scripts to access the OAuth token"}
$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$testRuns = Invoke-RestMethod -Uri "$($env:SYSTEM_COLLECTIONURI)/$($env:SYSTEM_TEAMPROJECT)/_apis/test/runs?buildUri=$($env:BUILD_BUILDURI)&api-version=5.1" -Method GET -ContentType 'application/json' -headers $headers -ErrorAction Stop
$runId = $testRuns.Value.id
If ($null -eq $runId) { Write-error "RunId not found." -ErrorAction Stop }
$testResults = Invoke-RestMethod -Uri "$($env:SYSTEM_COLLECTIONURI)/$($env:SYSTEM_TEAMPROJECT)/_apis/test/runs/$($runId)/results/?api-version=5.1" -Method GET -ContentType 'application/json' -headers $headers -ErrorAction Stop
$failedTests = $testResults.Value | Where-object {$_.Outcome -eq "failed"}
If (($failedTests | Measure-Object).Count -eq 0) {
return "No Failed Tests - Skipping workitem creation."
}
$wi = @(
@{
op ="Add";
path = "/fields/System.Title";
from = $null;
value = "Build $($env:BUILD_BUILDID) failed"
},
@{
op ="Add";
path = "/fields/System.AreaPath";
from = $null;
value = $env:SYSTEM_TEAMPROJECT
},
@{
op ="Add";
path = "/fields/System.IterationPath";
from = $null;
value = $env:SYSTEM_TEAMPROJECT
},
@{
op ="Add";
path = "/fields/System.AssignedTo";
from = $null;
value = @{
displayName = $env:BUILD_REQUESTEDFOR;
id = $env:BUILD_REQUESTEDFORID;
}
},
@{
op ="Add";
path = "/relations/-";
from = $null;
value = @{
rel = "ArtifactLink";
url = $env:BUILD_BUILDURI;
attributes = @{
name = "Found in build"
}
}
}
)
Invoke-RestMethod -Uri "$($env:SYSTEM_COLLECTIONURI)/$($env:SYSTEM_TEAMPROJECT)/_apis/wit/workitems/`$bug?api-version=5.1" -Method Post -Body (ConvertTo-Json $wi -Depth 10) -ContentType 'application/json-patch+json' -headers $headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment