gpt4 book ai didi

c# - 如何从 TFS 中检索链接的 "versioned item"

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:13 26 4
gpt4 key购买 nike

我有工作代码(感谢 John Socha-Leialoha)使用 TFS API 检索工作项,以及它们所有链接的工作项(下面的代码)。但是,我想要做的是访问每个工作项的链接文件的名称(TFS 将其称为“版本化项”)。在 TFS GUI 中,您可以将文件链接到工作项。假设工作项 1234 链接到文件 foo.txt。现在,当我运行此查询以查找链接项时,该文件不在列表中 - 仅返回其他子 WI 或父 WI。如果我完全在 GUI 中创建和运行查询,结果是一样的。我如何找出哪些文件链接到给定的 WI?我现在唯一能做的就是在 TFS GUI 中查看 WI,它显示在右下角的文件列表中。

也许我只需要做一个普通的平面查询,获取 WI 字段,然后链接文件的名称就会以某种方式成为该 WI 的字段之一?我不需要下载链接文件,我只需要文件名/位置。

返回所有链接 WI 的代码在这里:

public List<string> GetLinkedItems()
{
//executes a linked item query, returning work items, as well as the items that are link to them.
//gets digital asset work item that contains the given part number in the Assoc. Parts field
var result = new List<string>();
var tpc = new TfsTeamProjectCollection(new Uri(_tfsUri));
var workItemStore = (WorkItemStore) tpc.GetService(typeof (WorkItemStore));
//and [Schilling.TFS.TechPub.AssocParts] CONTAINS '101-4108'
var query =
"SELECT [System.Id], [System.Links.LinkType], [System.TeamProject]," +
" [System.WorkItemType], [System.Title], [System.AssignedTo]," +
" [System.State] FROM WorkItemLinks " +
" WHERE ([Source].[System.TeamProject] = 'Tech Pubs' AND " +
" [Source].[System.WorkItemType] = 'DigitalAsset' AND " +
" [Source].[System.State] <> '') And " +
" ([System.Links.LinkType] <> '') And " +
" ([Target].[System.WorkItemType] <> '') " +
" ORDER BY [System.Id] mode(MayContain)";
var treeQuery = new Query(workItemStore, query);
//Note we need to call RunLinkQuery here, not RunQuery, because we are doing a link item type of query
var links = treeQuery.RunLinkQuery();

//// Build the list of work items for which we want to retrieve more information//
int[] ids = (from WorkItemLinkInfo info in links
select info.TargetId).Distinct().ToArray();

//
// Next we want to create a new query that will retrieve all the column values from the original query, for
// each of the work item IDs returned by the original query.
//
var detailsWiql = new StringBuilder();
detailsWiql.AppendLine("SELECT");
bool first = true;

foreach (FieldDefinition field in treeQuery.DisplayFieldList)
{
detailsWiql.Append(" ");
if (!first)
detailsWiql.Append(",");
detailsWiql.AppendLine("[" + field.ReferenceName + "]");
first = false;
}
detailsWiql.AppendLine("FROM WorkItems");
//
// Get the work item details
//
var flatQuery = new Query(workItemStore, detailsWiql.ToString(), ids);
WorkItemCollection details = flatQuery.RunQuery();

return
(from WorkItem wi in details
select wi.Id + ", " + wi.Project.Name + ", " + wi.Title + ", " + wi.State).ToList();
}

最佳答案

工作项查询只能显示 WorkItemLinkType 链接。要获取其他类型的链接(即源代码管理中的文件),您需要浏览 WorkItem 对象本身的链接列表。下面是一段代码,可以让您获得链接到工作项的文件的工件:

TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
new Uri("http://<server>:8080/tfs/<collection>"));

WorkItemStore wiStore = tpc.GetService<WorkItemStore>();

VersionControlServer vc = tpc.GetService<VersionControlServer>();

WorkItem task = wiStore.GetWorkItem(<work item with a linked file>);

var externalLinks = task.Links.OfType<ExternalLink>();

foreach (var link in externalLinks)
{
XmlDocument artifact = vc.ArtifactProvider.GetArtifactDocument(new Uri(link.LinkedArtifactUri));
}

XML 文档包含使用 GetItem() 方法从 VersionControlServer 获取正确文件版本所需的所有必要信息。

关于c# - 如何从 TFS 中检索链接的 "versioned item",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com