Get a list of tasks through rest api in azure devops

痞子三分冷 提交于 2021-02-15 07:42:13

问题


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?


回答1:


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:

Then in Postman I use body like this:

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

Response:




回答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:

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



来源:https://stackoverflow.com/questions/60755654/get-a-list-of-tasks-through-rest-api-in-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!