如何在 tfs Java sdk 中查询链接的工作项

How to query linked workitem in tfs Java sdk

如何在 TFS 中查询链接的工作项 java sdk。 下面是从 tfs 查询正常工作项的代码:

public static void main(final String[] args) {
        final TFSTeamProjectCollection tpc = SnippetSettings.connectToTFS();

        final Project project = tpc.getWorkItemClient().getProjects().get(SnippetSettings.PROJECT_NAME);
        final WorkItemClient workItemClient = project.getWorkItemClient();

        // Define the WIQL query.
        final String wiqlQuery = "Select ID, Title from WorkItems where (State = 'Active') order by Title"; //$NON-NLS-1$

        // Run the query and get the results.
        final WorkItemCollection workItems = workItemClient.query(wiqlQuery);
        System.out.println("Found " + workItems.size() + " work items."); //$NON-NLS-1$ //$NON-NLS-2$
        System.out.println();

        // Write out the heading.
        System.out.println("Query: " + wiqlQuery); //$NON-NLS-1$
        System.out.println();
        System.out.println("ID\tTitle"); //$NON-NLS-1$

        // Output the results of the query.
        final int maxToPrint = 20;
        for (int i = 0; i < workItems.size(); i++) {
            if (i >= maxToPrint) {
                System.out.println("[...]"); //$NON-NLS-1$
                break;
            }

            final WorkItem workItem = workItems.getWorkItem(i);
            System.out.println(workItem.getID() + "\t" + workItem.getTitle()); //$NON-NLS-1$
        }
    }

虽然 运行 使用上述代码的链接工作项查询我收到一条错误消息:

com.microsoft.tfs.core.clients.workitem.exceptions.ValidationException: TF248021: You have specified a query string that is not valid when you use the query method for a flat list of work items. You cannot specify a parameterized query or a query string for linked work items with the query method you specified.

当您对 flat 查询执行 RunQuery() 时,您将获得成功的响应。但是,如果您在链接查询(treeone-hop WIQL)上执行 RunQuery(),您将看到此错误消息。

要执行链接查询,您必须在查询 class 上使用 RunLinkQuery() method 而不是 RunQuery()。 returns 一个 WorkItemLinkInfo 对象数组,其中包含以下字段:SourceId、TargetId、LinkTypeId 和 IsLocked。

下面是一个树查询的例子:

SELECT [System.Id], [System.State], [System.WorkItemType] 
FROM WorkItemLinks 
WHERE 
(
    Source.[System.TeamProject] = 'TFSTestProject' 
    AND Source.[System.WorkItemType] = 'Requirement' 
    AND Source.[System.State] NOT IN ('Proposed', 'Completed')
) 
AND [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward' 
AND Target.[System.WorkItemType] <> '' 
ORDER BY [System.Id] mode(Recursive)

下面是在 c# 中执行树查询的示例:

private List<WorkItemViewModel> GetWorkItemTree(string query)
{
    var treeQuery = new Query(_workItemStore, query);
    var links = treeQuery.RunLinkQuery();

    var workItemIds = links.Select(l => l.TargetId).ToArray();

    query = "SELECT * FROM WorkItems";
    var flatQuery = new Query(_workItemStore, query, workItemIds);
    var workItemCollection = flatQuery.RunQuery();

    var workItemList = new List<WorkItemViewModel>();

    for (int i = 0; i < workItemCollection.Count; i++)
    {
        var workItem = workItemCollection[i];

        if (workItem.Type.Name == "Requirement")
        {                   
            var model = new WorkItemViewModel()
            {
                Id = workItem.Id,
                RequestNo = workItem.Fields["MyCompany.RequestNumber"].Value.ToString(),
                Title = workItem.Title,
                WorkItemType = workItem.Fields["MyCompany.WorkItemType"].Value.ToString(),
                Priority = workItem.Fields["MyCompany.Priority"].Value.ToString()
            };

            workItemList.Add(model);
        }
        else
        {
            switch (workItem.Type.Name)
            {
                case "Task":
                    var task = new TFSTask()
                    {
                        Name = workItem.Title,
                        Activity = workItem.Fields["MyCompany.Activity"].Value.ToString(),
                        Start = (DateTime?)workItem.Fields["MyCompany.ActivityStart"].Value,
                        Due = (DateTime?)workItem.Fields["MyCompany.ActivityFinish"].Value,
                        Status = workItem.State
                    };

                    workItemList.Last().Tasks.Add(task);
                    break;
                case "Issue":
                    var issue = new TFSIssue()
                    {
                        Name = workItem.Title,
                        Created = workItem.CreatedDate,
                        Due = (DateTime?)workItem.Fields["MyCompany.ActivityDue"].Value,
                        Status = workItem.State
                    };
                    workItemList.Last().Issues.Add(issue);
                    break;
                default:
                    break;
            }
        }
    }

    return workItemList;
}

有用的链接:

顺便说一下,您还可以使用 TFS Rest api 来 运行 链接查询:

https://docs.microsoft.com/en-us/rest/api/vsts/wit/wiql/query%20by%20wiql