在azure devops中通过rest api获取任务列表

编程入门 行业动态 更新时间:2024-10-21 03:33:41
本文介绍了在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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 09:29:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/801891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   devops   azure   api   rest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!