如何检索附加到工作项的Wiki链接

时间:2020-11-10 23:51:15

标签: python azure-devops wiql

我正在尝试使用azure-devops python包从Azure DevOps中提取Wiki链接。使用WIQL可以做到这一点吗?可以按以下步骤检索项目到项目的链接:

        SELECT * FROM workitemLinks 
        WHERE (Source.[System.AreaPath] Under 'devOpsTesting\\testArea')
        AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward')
        AND (Source.[System.Id] = 3)  
        ORDER BY [System.Id]
        MODE (Recursive)
    

对于Wiki链接是否有类似的解决方案,如果没有,人们将如何检索它们?

1 个答案:

答案 0 :(得分:1)

我们无法通过WIQL查询列出Wiki链接的工作链接。如果该链接不包含工作项,则无法获得所需的结果。

作为测试结果,我发现如果将Wiki链接到工作项,则字段External Link Count的值将大于0

作为一种解决方法,我们可以使用REST API列出External Link Count大于0的所有工作项。

REST API

POST https://dev.azure.com/{Org name}/{Project name}/{Team name}/_apis/wit/wiql?api-version=6.0

请求正文:

 {"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' AND [External Link Count] > '0' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}

现在,我们可以列出工作项ID。

结果:

enter image description here

然后,我们通过REST API检查工作项的详细信息。

GET https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/{Work item ID}?$expand=all&api-version=6.1-preview.3

我们需要注意relations.attributes.name字段,如果工作项链接了wiki,我们可以通过此字段进行检查。

结果:

enter image description here

更新1

检索附加到工作项的Wiki链接

通过REST API和Power Shell列出工作项ID。

Power Shell脚本:

$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$WorkItemQueryURL = "https://dev.azure.com/{Org name}/{project}/{team name}/_apis/wit/wiql?api-version=6.0" 

$body =@"
{
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' AND [External Link Count] > '0' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}
"@
$WorkItem = Invoke-RestMethod -Uri $WorkItemQueryURL -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

#Write-host $WorkItem.workItems.id



ForEach ($ID in $WorkItem.workItems.id)
{
 
 $WorkItemInfoURL = "https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/$($ID)?" + "$" + "expand=1&api-version=6.1-preview.3" 
 $WorkItemInfo = Invoke-RestMethod -Uri $WorkItemInfoURL -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

 #Write-host   $WorkItemInfo.relations.attributes.name

 if ($WorkItemInfo.relations.attributes.name -eq "Wiki Page"){  
  Write-host $ID
 }

}

结果:

enter image description here

Update2

其他API:

GET https://dev.azure.com/{Org name}/{project name}/_apis/wit/workitems/{wok item ID}?$expand=all&api-version=6.1-preview.3

结果:

我们可以通过响应URL链接获取Wiki名称。

enter image description here