Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. $WorkItemAssociatedURL = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$($env:BUILD_BUILDID)/workitems?api-version=2.0"
  2. $WorkItemQueryURL = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/wit/wiql?api-version=2.0"
  3.  
  4. $headers = @{
  5. Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
  6. }
  7. $ResponseJSON = Invoke-RestMethod -Uri $WorkItemAssociatedURL -ContentType "application/json" -Method GET -Headers $headers
  8.  
  9. $CountWorkitems = $ResponseJSON.count
  10. $WorkitemUrlArray = $ResponseJSON.value
  11.  
  12. for($i = 0; $i -lt $CountWorkitems ; $i++) {
  13. $WorkItemId = $WorkitemUrlArray[$i].id
  14.  
  15. $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)"
  16.  
  17. $wiqlBody = '{ "query": "' + $query + '"}'
  18.  
  19. Write-Host "WIQL is: $wiqlBody"
  20. $workItemsOpenTasks = Invoke-RestMethod -Uri $WorkItemQueryURL -Body $wiqlBody -ContentType "application/json" -Method Post -Headers $headers
  21.  
  22. # 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
  23. $relationCount = $workItemsOpenTasks.workItemRelations.count
  24. Write-Host "Work item and its tasks count: " $relationCount
  25.  
  26. if ($relationCount -gt 1) {
  27. Write-Host "Work item has open tasks, skipped updating state"
  28. }
  29. else {
  30. $body =
  31. '[
  32. {
  33. "op": "add",
  34. "path": "/fields/System.State",
  35. "value":"ReadyToTest"
  36. }
  37. ]'
  38.  
  39. Write-Host "body to post: $body"
  40. $WorkitemUpdateURL = $WorkitemUrlArray[$i].url + "?api-version=2.0"
  41. Invoke-RestMethod -Uri $WorkitemUpdateURL -Body $body -ContentType "application/json-patch+json" -Method Patch -Headers $headers
  42.  
  43. Write-Host "Updating WorkItem's (#$WorkItemId) state to ReadyToTest"
  44. }
  45.  
  46.  
  47. }
Add Comment
Please, Sign In to add comment