如何在 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("IDtTitle"); //$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.

当你在平面查询上执行 RunQuery() 时,你会得到一个成功的响应。但是,如果在链接查询(单跃点WIQL)上执行 RunQuery(),则会看到此错误消息。

若要执行链接查询,必须在 Query 类上使用 RunLinkQuery() 方法而不是 RunQuery()。这将返回一个 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;
}

有用的链接:

  • https://github.com/prabuw/prabuw.github.io/blob/master/_posts/2012-09-18-retrieving-work-items-using-the-team-foundation-server-api.markdown
  • https://blogs.msdn.microsoft.com/jsocha/2012/02/22/retrieving-tfs-results-from-a-tree-query/

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

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

最新更新