通过Azure Devops中的Rest API获取任务列表 [英] Get a list of tasks through rest api in azure devops

查看:55
本文介绍了通过Azure Devops中的Rest API获取任务列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取在Azure DevOps中的特定项目中完成的任务列表.我想使用Azure DevOps的REST api在Jenkinsfile中使用此列表.有可能吗?

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?

到目前为止,我已经能够获得一项特定任务的状态(因此需要指定ID),所以它对我来说不是很好.我还尝试了查询,并且已经能够在ADO中创建查询,该查询列出了处于完成"状态的任务,但是我找不到通过REST api将查询结果获取到Jenkinsfile的方法.有什么帮助吗?

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?

推荐答案

我已经对上述脚本进行了升级,但在脚本末尾确实得到了一系列任务,而不是常规的json.然后,您可以将其转换为数组,列表或集合,并遍历每个对象.脚本下方:

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

现在响应是这样的:

现在我要使用它,转换为集合并遍历jenkinsfile中的每个项目.感谢您的帮助Lance Li-MSFT.:)

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

这篇关于通过Azure Devops中的Rest API获取任务列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆