如何在TFS workitem event中 直接找到TFS URL?

  • 192
  • 0

如何在TFS workitem event中 直接找到TFS URL?

在 TFS workitem event 中 若需要使用其他TFS workitrm API 如: 取得其他workitem欄位資料等

需要先取得目前的TFS URL後 才能呼叫相關的API進行

以下網址提供了範例程式碼 經過驗證是可行的  有需要的朋友請自行參考

https://gist.github.com/kria/a823bc71bba366382507

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Server;

using System;
using System.Linq;

namespace WorkItemModifier
{
    public class MySubscriber : ISubscriber
    {
        public Type[] SubscribedTypes()
        {
            return new Type[] { typeof(WorkItemChangedEvent) };
        }
        public string Name
        {
            get { return "WorkItemModifier"; }
        }
        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;
            statusMessage = string.Empty;
            properties = null;

            if (notificationType == NotificationType.Notification)
            {
                var ev = notificationEventArgs as WorkItemChangedEvent;

                TeamFoundationLocationService locationService = requestContext.GetService<TeamFoundationLocationService>();
                var tfsUri = locationService.GetSelfReferenceUri(requestContext, locationService.GetDefaultAccessMapping(requestContext));
                var changer = Microsoft.TeamFoundation.Framework.Client.IdentityHelper.CreateDescriptorFromSid(ev.ChangerSid);

                var tfs = new TfsTeamProjectCollection(tfsUri, changer);
                var store = tfs.GetService<WorkItemStore>();
                var workItemId = ev.CoreFields.IntegerFields.Single(f => f.ReferenceName == "System.Id").NewValue;
                var workItem = store.GetWorkItem(workItemId);

                var remaining = (double)workItem.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value;
                if (remaining == 5)
                {
                    workItem.Fields["Microsoft.VSTS.Scheduling.RemainingWork"].Value = 6;
                }

                workItem.Save();  // This will generate a new WorkItemChangedEvent, make sure to not create an infinite loop
            }

            return EventNotificationStatus.ActionPermitted;
        }
    }

 

風來疏竹,風過而竹不留聲;雁度寒潭,雁去而潭不留影。故君子事來而心始現,事去而心隨空。