Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过在TFS中创建新的工作项来自动创建任务?_Tfs_Workitem - Fatal编程技术网

如何通过在TFS中创建新的工作项来自动创建任务?

如何通过在TFS中创建新的工作项来自动创建任务?,tfs,workitem,Tfs,Workitem,我想知道在创建工作项时是否可以创建新的链接任务 有人能告诉我怎么做吗?我已经阅读了一些以前用于此场景的旧代码。以下代码在新bug设置为approved时创建一个链接任务 代码将筛选到特定团队项目,并使用特定帐户进行连接。在插件工作之前,您需要输入这些。然后可以修改此代码以创建所需的任务 有关服务器插件的一般介绍以及如何将下面的代码转换为功能插件,请参阅 使用Microsoft.TeamFoundation.Framework.Server; 使用制度; 使用System.Collections.

我想知道在创建工作项时是否可以创建新的链接任务


有人能告诉我怎么做吗?

我已经阅读了一些以前用于此场景的旧代码。以下代码在新bug设置为approved时创建一个链接任务

代码将筛选到特定团队项目,并使用特定帐户进行连接。在插件工作之前,您需要输入这些。然后可以修改此代码以创建所需的任务

有关服务器插件的一般介绍以及如何将下面的代码转换为功能插件,请参阅

使用Microsoft.TeamFoundation.Framework.Server;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.TeamFoundation.Common;
使用Microsoft.TeamFoundation.WorkItemTracking.Server;
使用Microsoft.TeamFoundation.WorkItemTracking.Client;
使用Microsoft.TeamFoundation.Client;
Net系统;
使用系统集合;
命名空间TfsExtension.CreateTaskForBug
{
公共类CreateTaskForBugEventHandler:IsSubscriber
{
常量字符串projectName=“”;
公共字符串名
{
得到
{
返回“CreateTaskForBugEventHandler”;
}
}
公共订阅优先级
{
得到
{
返回SubscriberPriority.Normal;
}
}
公共事件NotificationStatus ProcessEvent(
TeamFoundationRequestContext请求上下文,
NotificationType NotificationType,
对象notificationEventArgs,
输出int状态码,
输出字符串状态消息,
out ExceptionProperty集合属性)
{
statusCode=0;
属性=null;
statusMessage=String.Empty;
尝试
{
ProcessNotification(notificationType、notificationEventArgs、requestContext);
}
捕获(异常)
{
TeamFoundationApplicationCore.LogException(“错误处理事件”,异常);
}
返回EventNotificationStatus.ActionAllowed;
}
私有静态void ProcessNotification(NotificationType NotificationType、对象notificationEventArgs、TeamFoundationRequestContext requestContext)
{
如果(notificationType==notificationType.Notification&¬ificationEventArgs为WorkItemChangedEvent)
{
var ev=notificationEventArgs作为WorkItemChangedEvent;
if(ev.PortfolioProject==projectName)
{
字符串workItemType=(来自ev.CoreFields.StringFields中的字段
其中field.Name==“工作项类型”
选择field.NewValue).Single();
if(workItemType==“Bug”)
{
ProcessBug(ev,requestContext);
}
}
}
}
私有静态void ProcessBug(WorkItemChangedEvent ev、TeamFoundationRequestContext requestContext)
{
var stateChange=(来自ev.ChangedFields.StringFields中的字段
其中field.Name==“State”&&field.NewValue==“Approved”
选择字段);
if(stateChange!=null)
{
AddChildTaskToBug(ev,requestContext);
}
}
私有静态void AddChildTaskToBug(WorkItemChangedEvent ev、TeamFoundationRequestContext requestContext)
{
WorkItemStore wiStore=GetWorkItemStore(requestContext);
WorkItem witem=wiStore.GetWorkItem(ev.CoreFields.IntegerFields[0].NewValue);
项目团队项目=witem.Project;
int bugID=witem.Id;
字符串bugTitle=witem.Fields[“System.Title”].Value.ToString();
字符串bugsassignedto=witem.Fields[“System.AssignedTo”].Value.ToString();
字符串bugAreaPath=witem.Fields[“System.AreaPath”].Value.ToString();
字符串bugIterationPath=witem.Fields[“System.IterationPath”].Value.ToString();
字符串bugChangedBy=witem.Fields[“System.ChangedBy”].OriginalValue.ToString();
字符串bugTeamProject=witem.Project.Name;
字符串childTaskTitle=“Resolve bug”+bugID+“-”+bugTitle;
if(CreateResolutionTask(wiStore、bugID、childTaskTitle))
{
witem=CreateWorkItem(wiStore、teamProject、bugID、bugTitle、bugAssignedTo、bugAreaPath、bugIterationPath);
如果(有效期(witem))
{
witem.Save();
亲子关系(威斯托雷、维特姆、布吉德);
}
}
}
私有静态布尔值有效(WorkItem witem)
{
ArrayList validationErrors=witem.Validate();
返回validationErrors.Count==0;
}
私有静态无效链接ParentAndChild(工作项存储wiStore、工作项witem、int bugID)
{
var linkType=wiStore.WorkItemLinkTypes[CoreLinkTypeReferenceNames.Hierarchy];
var parentWorkItem=wiStore.GetWorkItem(bugID);
int taskID=witem.Id;
var childWorkItem=wiStore.GetWorkItem(taskID);
parentWorkItem.Links.Add(新的WorkItemLink(linkType.ForwardEnd,childWorkItem.Id));
parentWorkItem.Save();
}
私有静态工作项CreateWorkItem(工作项存储区wiStore、项目团队项目、int bugID、字符串bugTitle、字符串bugAssignedTo、字符串bugAreaPath、字符串bugIterationPath)
{
WorkItemTypeCollection workItemTypes=wiStore.Projects[teamProject.Name].Wo
using Microsoft.TeamFoundation.Framework.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;
using System.Net;
using System.Collections;

namespace TfsExtension.CreateTaskForBug
{
    public class CreateTaskForBugEventHandler : ISubscriber
    {
        const string projectName = "<Enter your project name here>";

        public string Name
        {
            get
            {
                return "CreateTaskForBugEventHandler";
            }
        }

        public SubscriberPriority Priority
        {
            get
            {
                return SubscriberPriority.Normal;
            }
        }

        public EventNotificationStatus ProcessEvent(
            TeamFoundationRequestContext requestContext,
            NotificationType notificationType,
            object notificationEventArgs,
            out int statusCode,
            out string statusMessage,
            out ExceptionPropertyCollection properties)
        {
            statusCode = 0;
            properties = null;
            statusMessage = String.Empty;

            try
            {
                ProcessNotification(notificationType, notificationEventArgs, requestContext);
            }
            catch (Exception exception)
            {
                TeamFoundationApplicationCore.LogException("Error processing event", exception);
            }
            return EventNotificationStatus.ActionPermitted;

        }

        private static void ProcessNotification(NotificationType notificationType, object notificationEventArgs, TeamFoundationRequestContext requestContext)
        {
            if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
            {
                var ev = notificationEventArgs as WorkItemChangedEvent;

                if (ev.PortfolioProject == projectName)
                {
                    string workItemType = (from field in ev.CoreFields.StringFields
                                           where field.Name == "Work Item Type"
                                           select field.NewValue).Single();

                    if (workItemType == "Bug")
                    {
                        ProcessBug(ev, requestContext);

                    }
                }
            }
        }

        private static void ProcessBug(WorkItemChangedEvent ev, TeamFoundationRequestContext requestContext)
        {
            var stateChange = (from field in ev.ChangedFields.StringFields
                               where field.Name == "State" && field.NewValue == "Approved"
                               select field).SingleOrDefault();

            if (stateChange != null)
            {
                AddChildTaskToBug(ev, requestContext);
            }
        }

        private static void AddChildTaskToBug(WorkItemChangedEvent ev, TeamFoundationRequestContext requestContext)
        {
            WorkItemStore wiStore = GetWorkItemStore(requestContext);
            WorkItem witem = wiStore.GetWorkItem(ev.CoreFields.IntegerFields[0].NewValue);

            Project teamProject = witem.Project;
            int bugID = witem.Id;

            string bugTitle = witem.Fields["System.Title"].Value.ToString();
            string bugAssignedTo = witem.Fields["System.AssignedTo"].Value.ToString();
            string bugAreaPath = witem.Fields["System.AreaPath"].Value.ToString();
            string bugIterationPath = witem.Fields["System.IterationPath"].Value.ToString();
            string bugChangedBy = witem.Fields["System.ChangedBy"].OriginalValue.ToString();
            string bugTeamProject = witem.Project.Name;

            string childTaskTitle = "Resolve bug " + bugID + " - " + bugTitle;

            if (CreateResolutionTask(wiStore, bugID, childTaskTitle))
            {
                witem = CreateWorkItem(wiStore, teamProject, bugID, bugTitle, bugAssignedTo, bugAreaPath, bugIterationPath);

                if (IsValid(witem))
                {
                    witem.Save();

                    LinkParentAndChild(wiStore, witem, bugID);
                }
            }
        }

        private static bool IsValid(WorkItem witem)
        {
            ArrayList validationErrors = witem.Validate();
            return validationErrors.Count == 0;
        }

        private static void LinkParentAndChild(WorkItemStore wiStore, WorkItem witem, int bugID)
        {
            var linkType = wiStore.WorkItemLinkTypes[CoreLinkTypeReferenceNames.Hierarchy];

            var parentWorkItem = wiStore.GetWorkItem(bugID);
            int taskID = witem.Id;
            var childWorkItem = wiStore.GetWorkItem(taskID);

            parentWorkItem.Links.Add(new WorkItemLink(linkType.ForwardEnd, childWorkItem.Id));
            parentWorkItem.Save();
        }

        private static WorkItem CreateWorkItem(WorkItemStore wiStore, Project teamProject, int bugID, string bugTitle, string bugAssignedTo, string bugAreaPath, string bugIterationPath)
        {
            WorkItemTypeCollection workItemTypes = wiStore.Projects[teamProject.Name].WorkItemTypes;
            WorkItemType wiType = workItemTypes["Task"];

            WorkItem witem = new WorkItem(wiType);
            witem.Fields["System.Title"].Value = "Resolve bug " + bugID + " - " + bugTitle;
            witem.Fields["System.AssignedTo"].Value = bugAssignedTo;
            witem.Fields["System.AreaPath"].Value = bugAreaPath;
            witem.Fields["System.IterationPath"].Value = bugIterationPath;
            witem.Fields["Microsoft.VSTS.Common.Activity"].Value = "Bug Resolution";
            return witem;
        }

        private static bool CreateResolutionTask(WorkItemStore wiStore, int bugID, string childTaskTitle)
        {
            WorkItem parentBug = wiStore.GetWorkItem(bugID);
            WorkItemLinkCollection links = parentBug.WorkItemLinks;
            foreach (WorkItemLink wil in links)
            {
                if (wil.LinkTypeEnd.Name == "Child")
                {
                    WorkItem childTask = wiStore.GetWorkItem(wil.TargetId);

                    if ((childTask.Title == childTaskTitle) && (childTask.State != "Closed"))
                    {
                        return false;
                    }

                }
            }

            return true;
        }

        private static Uri GetTFSUri(TeamFoundationRequestContext requestContext)
        {
            var locationService = requestContext.GetService<TeamFoundationLocationService>();
            return new Uri(locationService.GetServerAccessMapping(requestContext).AccessPoint + "/" + requestContext.ServiceHost.Name);
        }

        private static WorkItemStore GetWorkItemStore(TeamFoundationRequestContext requestContext)
        {
            NetworkCredential netCred = new NetworkCredential(
                   "<username>",
                   "<password>");

            WindowsCredential windowsCred = new WindowsCredential(netCred);
            var credentials = new TfsClientCredentials(windowsCred);
            credentials.AllowInteractive = true;

            var tpc = new TfsTeamProjectCollection(
                GetTFSUri(requestContext),
                credentials);

            tpc.Authenticate();

            return tpc.GetService<WorkItemStore>();
        }

        public Type[] SubscribedTypes()
        {
            return new Type[1] { typeof(WorkItemChangedEvent) };
        }
    }
}