0

I'm trying to get a list of tasks which are completed in particular project in Azure DevOps. I want to use Azure DevOps's REST api to use this list in Jenkinsfile. Is it possible?

So far i've been able to get status of one-particular task (so ID needs to be specified) so it doesn't work for me very well. I also experimented with queries and i've been able to create query in ADO which list tasks that are in "Done" state but i can't find a way to get query result through REST api to Jenkinsfile. Any help with that?

konpro11
  • 51
  • 8
  • It sounds very similar to [this question](https://stackoverflow.com/questions/58706951/get-the-list-of-skipped-tasks-on-azure-devops-pipeline/58708043#58708043). The difference is that you probably need `"result":"succeeded"` – Yan Sklyarenko Mar 19 '20 at 12:44
  • Hi konpro11, is there any update for this issue? – LoLance Mar 23 '20 at 08:06

2 Answers2

2

I also experimented with queries and i've been able to create query in ADO which list tasks that are in "Done" state but i can't find a way to get query result through REST api to Jenkinsfile. Any help with that?

You can use the Powershell script to call the rest api, try this script(I use this rest api):

$token = "xxx"

$url="https://dev.azure.com/YourOrgName/YourProjectName/_apis/wit/wiql?api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  "query": "SELECT [System.Id],[System.Title],[System.State] FROM workitems WHERE [System.TeamProject] = @project AND [system.WorkItemType] = 'Task' AND [System.State]='Done' "
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json

Write-Host "result = $($response | ConvertTo-Json -Depth 100)"

It will return all the WorkItems whose Type=Task and State=Done with their corresponding IDs and Urls.

You should enter your own PAT in $token, and replace the YourOrgName and YourProjectName with your own Azure Devops OrganizationName and ProjectName.

As for how to call PS script in jenkinsfile, you can check Microsoft PowerShell Support for Pipeline and Executing powershell command directly in jenkins pipeline.

Edit1:

In web portal I have these wits:

enter image description here

Then in Postman I use body like this:

enter image description here

{
  "query": "SELECT [System.Id],[System.Title],[System.State] FROM workitems WHERE [System.TeamProject] = @project AND [system.WorkItemType] = 'Task' AND [System.State] = 'Done'"
}

Response:

enter image description here

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thanks for response. I tried to use above script, but im getting 401 error, so wrong authorizartion. I'm wondering if the $token variable should hold only the PAT or a username as well. – konpro11 Mar 23 '20 at 08:28
  • Make sure your account have the access to change work items, and then [create a pat](https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#create-personal-access-tokens-to-authenticate-access) with Full access to do the test again. – LoLance Mar 23 '20 at 08:33
  • @konpro11 The `$token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"` should work, and if the pat with full access works while your another pat doesn't work. the issue result from your first pat needs some more permissions, let me know which permissions you choose. – LoLance Mar 23 '20 at 08:35
  • Yes, it worked now, it turns out that i was putting in wrong token. However, i'm still getting 401 error, but this time it also gives the results back. First the error, then the results. Any idea why? Also, for every work item, it gives back id and url. What would be an easiest way to get only id's in list? – konpro11 Mar 23 '20 at 08:55
  • I checked also some of the items that were turned as a result. Some of them aren't in Done state, there are also "In Progress" and "To Do" state. – konpro11 Mar 23 '20 at 09:01
  • My fault, I forget to add `AND [System.State]='Done' ` when I write the wiql statement. Add this line to the `"query":` and it would work. I've just updated the answer. – LoLance Mar 23 '20 at 09:08
  • For 401, I'm not sure about this cause I can't reproduce the same issue in my machine. Do you run that rest api using Powershell or Powershell task? And for ` What would be an easiest way to get only id's in list? `, I'm afraid this is not supported, it will always return both ids and urls, see [workItemReference](https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/wiql/query%20by%20wiql?view=azure-devops-rest-5.1#workitemreference), it's by design... – LoLance Mar 23 '20 at 09:12
  • I have added the [System.State] = 'Done' but it still gives the same result, with all tasks, not only the ones in 'Done' State. Does it work correctly in your environment? – konpro11 Mar 24 '20 at 10:46
  • @konpro11 Yes, it works in my environment, you can check my Edit1 in answer. I've just added details about how I test my answer before writing it here... I guess maybe you can firstly use Powershell or Postman to do the test, then move the script to your jenkins environment. Let me know if it helps :) – LoLance Mar 25 '20 at 02:32
2

I've upgraded above script do get an array of tasks at the end of the script, instead of regular json. You can then convert it into array, list or collection and iterate through every object. Below the script:

$token = "xxx"



$url="https://dev.azure.com/YourOrgName/YourProjectName/_apis/wit/wiql?api-version=5.1""

$tokenInBasic = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  "query": "SELECT [System.Id],[System.Title],[System.State] FROM workItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] = 'Task' AND [System.State] = 'Done'"
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $tokenInBasic"} -Method Post -Body $JSON -ContentType application/json
$listOfTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
  $listOfTasks.Add($workitem.id)

}
$listOfTasks = $listOfTasks -join ','
$listOfTasks

Response is now like that:

ps1 response

Now i'm taking it, converting to collection and iterating through each item in jenkinsfile. Thanks for the help Lance Li-MSFT. :)

konpro11
  • 51
  • 8