使用工作项查询语言(wiql)编写和执行Visual Studio Team Services(VSTS)的自定义查询的过程

时间:2016-10-19 23:56:53

标签: powershell azure-devops wiql

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

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

1 个答案:

答案 0 :(得分:3)

是的,可以在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#代码
  3.  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:

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

    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]"