Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $WorkItemAssociatedURL = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$($env:BUILD_BUILDID)/workitems?api-version=2.0"
- $WorkItemQueryURL = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/wit/wiql?api-version=2.0"
- $headers = @{
- Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
- }
- $ResponseJSON = Invoke-RestMethod -Uri $WorkItemAssociatedURL -ContentType "application/json" -Method GET -Headers $headers
- $CountWorkitems = $ResponseJSON.count
- $WorkitemUrlArray = $ResponseJSON.value
- for($i = 0; $i -lt $CountWorkitems ; $i++) {
- $WorkItemId = $WorkitemUrlArray[$i].id
- $query = "Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] From WorkItemLinks WHERE (Source.[System.Id] = $WorkItemId and Source.[System.WorkItemType] = 'User Story' and Source.[System.State] <> 'Closed') and ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') and (Target.[System.WorkItemType] = 'Task' and Target.[System.State] <> 'Closed') mode(Recursive)"
- $wiqlBody = '{ "query": "' + $query + '"}'
- Write-Host "WIQL is: $wiqlBody"
- $workItemsOpenTasks = Invoke-RestMethod -Uri $WorkItemQueryURL -Body $wiqlBody -ContentType "application/json" -Method Post -Headers $headers
- # WIQL returns User Story itself and its open Tasks. We expect all tasks are closed and WIQL returns only User Story, so workItemRelations count should be 1
- $relationCount = $workItemsOpenTasks.workItemRelations.count
- Write-Host "Work item and its tasks count: " $relationCount
- if ($relationCount -gt 1) {
- Write-Host "Work item has open tasks, skipped updating state"
- }
- else {
- $body =
- '[
- {
- "op": "add",
- "path": "/fields/System.State",
- "value":"ReadyToTest"
- }
- ]'
- Write-Host "body to post: $body"
- $WorkitemUpdateURL = $WorkitemUrlArray[$i].url + "?api-version=2.0"
- Invoke-RestMethod -Uri $WorkitemUpdateURL -Body $body -ContentType "application/json-patch+json" -Method Patch -Headers $headers
- Write-Host "Updating WorkItem's (#$WorkItemId) state to ReadyToTest"
- }
- }
Add Comment
Please, Sign In to add comment