使用工作项查询语言 (wiql) 为 Visual Studio 团队服务 (VSTS) 编写和执行自定义查询的过程

Procedure to write and execute custom queries for Visual Studio Team Services(VSTS) using Work Item Query Language(wiql)

我想使用 "WIQL" 从 VSTS 中提取数据并使用该数据进行一些报告。 1) 有人可以告诉我是否可以在 PowerShell 中使用 "WIQL" 吗?如果是这样,请告诉我在哪里可以找到一些示例或演示?

2) 另外,是否有任何其他客户端工具支持 "WIQL" 对 VSTS 进行自定义查询。如果是这样,请告诉我在哪里可以找到与此相关的一些演示或示例?

是的,可以在 PowerShell 中使用 WIQL,简单的方法是您可以使用 PowerShell 调用 Work Item Query REST API

例如:

$vstsAccount = "XX"
$projectName = "XX"
$user = ""
$token = "personal access token"
Function QueryWorkItem{



# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

# Construct the REST URL to obtain Build ID
$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/wit/wiql?api-version=1.0"


$body = "{
    'query':'Select [System.Id],[System.Title] From WorkItems Where [System.Id] = 123'
}"

# Invoke the REST call and capture the results (notice this uses the PATCH method)
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body

Write-Output ("work item title: '$($result.WorkItems[0].URL)'")

    }

    QueryWorkItem

更多信息,您可以参考this文章。

对于第二期,您可以自己构建一个应用程序,例如用C#语言编写控制台应用程序。

  1. 安装Microsoft.TeamFoundationServer.ExtendedClient
  2. 简单的 C# 代码

 var u = new Uri("https://XXX.visualstudio.com");
                VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(new NetworkCredential("user name", "password")));
                var connection = new VssConnection(u, c);
                var workitemClient = connection.GetClient<WorkItemTrackingHttpClient>();

                var result = workitemClient.QueryByWiqlAsync(new Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.Wiql() { Query= "Select [System.Id],[System.Title] From WorkItems Where [System.Id] = 123" },"Scrum2015").Result;
                Console.WriteLine(result.WorkItems.First().Url);

更新 1:

TFS/VSTS SDK 或扩展 SDK 可以在 PowerShell 中使用。例如:

if((Get-PSSnapIn -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}
$Tfs2015AssembliesPath="[vs Installation path]\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.Build.Client.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.Build.Common.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.Git.Client.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.SourceControl.WebApi.dll"
#Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.TestManagement.Client.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.VersionControl.Client.dll"
Add-Type -Path "$Tfs2015AssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Function GetWorkItems{
    param([string]$teamProjectName,[string]$address)
    $credentials = New-Object System.Net.NetworkCredential("[user name]", "[password]")
    $tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection((New-Object System.URI($address)))
    $wis = $tfsCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
    $wiqlQuery = "SELECT [System.ID], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] FROM WorkItems WHERE [System.TeamProject] = 'Scrum2015' AND  [State] = 'New' AND  [System.WorkItemType] in ('Bug','User Story')  ORDER BY [System.ID]";
    $witCollection = $wis.Query($wiqlQuery);

# add logical to export to excel.
    Foreach($witItem in $witCollection)
        {

            Write-Host $witItem.Title
        }
}
GetWorkItems "[project name]" "[tfs/vsts address]"