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

时间:2016-11-11 13:36:16

标签: c# tfs

我已经创建了TFS 2012 Serverside事件处理程序来强制执行代码审查策略。 我能够为代码审查请求和响应创建工作项。但 完成审核流程后,当我要检查已审核的代码时,无法在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();

1 个答案:

答案 0 :(得分:0)

似乎您没有将工作项链接到相关的更改。以下代码显示了如何执行此操作:

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 Extension: Add work item to pending changes via API的类似问题,也请查看此链接:C# Programmatically Checking in code changes with TFS API while associating the changeset to a Work Item