如何在TFS中以编程方式签入时关联已审阅的工作项

本文关键字:关联 工作 TFS 方式签 编程 | 更新日期: 2023-09-27 18:17:40

我创建了TFS 2012服务器端事件处理程序来执行代码审查策略。我能够为代码审查请求和响应创建工作项。但在完成审查过程后,当我要签入审查的代码时,无法在visual studio 2013的团队资源管理器窗口的相关工作项部分中看到审查的工作项。下面是我的代码,

var workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));
WorkItemType wiType = workItemStore.Projects[0].WorkItemTypes["Code Review Request"];
WorkItem workItem = new WorkItem(wiType);
workItem.Fields["System.AssignedTo"].Value = "XXXXXX"; 
//ev.ChangesetOwner.DisplayName;
workItem.Fields["Microsoft.VSTS.CodeReview.ContextType"].Value = "Shelveset";
workItem.Fields["Microsoft.VSTS.CodeReview.Context"].Value = shelveset.Name;
workItem.Fields["Microsoft.VSTS.CodeReview.ContextOwner"].Value = shelveset.OwnerName;
workItem.Fields["System.AreaPath"].Value = project.Name;
workItem.Fields["System.IterationPath"].Value = project.Name;
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";
workItem.Fields["System.Description"].Value = "Code Review Request  ";
workItem.Fields["System.Title"].Value = "Code Review Request " + System.DateTime.Now.ToString();
var invalidFields = workItem.Validate();
if (workItem.IsValid())
  workItem.Save();
var responseId = workItem.Id;
var type = workItemStore.Projects[0].WorkItemTypes["Code Review Response"]; 
workItem = new WorkItem(type);
workItem.Fields["System.AssignedTo"].Value = "xxxxxxxx"; 
workItem.Fields["System.AreaPath"].Value = project.Name; 
workItem.Fields["System.IterationPath"].Value = project.Name;
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";
workItem.Fields["Microsoft.VSTS.Common.ReviewedBy"].Value = "xxxxxxxx";
workItem.Fields["System.Title"].Value = "Code Review Response " + System.DateTime.Now.ToString();
WorkItemLinkTypeEnd linkTypeEnd = workItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"];
workItem.Links.Add(new RelatedLink(linkTypeEnd, responseId));
if (workItem.IsValid())
  workItem.Save();

如何在TFS中以编程方式签入时关联已审阅的工作项

似乎您没有将您的工作项链接到相关更改。下面的代码展示了如何做到这一点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace APPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://xxx.xxx.xxx.xxx:8080/tfs/DefaultCollection";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
            WorkItemStore wis = ttpc.GetService<WorkItemStore>();
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            int wid = 82;
            int cid = 332;
            WorkItem wi = wis.GetWorkItem(wid);
            Changeset cs = vcs.GetChangeset(cid);
            ExternalLink el = new ExternalLink(wis.RegisteredLinkTypes["Fixed in Changeset"], cs.ArtifactUri.AbsoluteUri);
            wi.Links.Add(el);
            wi.Save();     
        }
    }
}

关于TFS - VS扩展的类似问题:通过API将工作项添加到未决更改中,并检查此链接:c#编程地使用TFS API检入代码更改,同时将更改集关联到工作项