TFS 2012 - ISubscriber 插件。如何在执行插件时获取项目名称以及可能包含触发 WorkItem 的查询列表?
TFS 2012 - ISubscriber plugin. How to get name of project and possibly list of queries containing triggering WorkItem while plugin is being executed?
所以,我有以下 ISubscriber 代码:
public class Subscriber : ISubscriber
{
public string Namek
{
get { return "Subscriber"; }
}
public SubscriberPriority Priority
{
get { return SubscriberPriority.Normal; }
}
public EventNotificationStatus ProcessEvent (TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out Microsoft.TeamFoundation.Common.ExceptionPropertyCollection properties)
{
statusCode = 0;
properties = null;
statusMessage = String.Empty;
try
{
if(notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
{
var ev = notificationArgs as WorkItemChangedEvent;
...........
}
}
}
public Type[] SubscribedTypes()
{
return new Type[1] = { typeof(WorkItemChangedEvent) };
}
}
代码有效;这意味着在安装插件并且 TFS 识别它之后,在 ProcessEvent 方法中放置一个断点就可以了。
我的问题是我正在尝试获取项目的名称和属于 WorkItem 的 "Shared" 查询,其更改触发了此 运行 Subscriber 插件,但我不能似乎找到了这样的引用(我也不知道它们是否通过此接口提供,因为 WorkItemChangedEvent 上的信息太少了)。
有什么建议吗?
WorkItemChangedEvent ev = notificationEventArgs as WorkItemChangedEvent;
您可以使用 Tfs.Aggregator 代码作为类似任务的参考 reference
您应该按照以下行来解决您的问题:
- 获取项目工作项在
- 获取项目"Shared Queries"
- 获取每个查询结果的"Work Item Ids"
- 检查哪一个包含当前更改的"Work Item Id"
所以代码应该是这样的:
var tfsCollection = new TfsTeamProjectCollection(new Uri(tfsUri));
var tfsStore = tfsCollection.GetService<WorkItemStore>();
var workItemId = ev.CoreFields.IntegerFields[0].NewValue;
var eventWorkItem = tfsStore.GetWorkItem(workItemId);
var project = eventWorkItem.Project;
var queryHierarchy = project.QueryHierarchy;
var queryFolder = queryHierarchy as QueryFolder;
var queryItem = queryFolder["Shared Queries"];
queryFolder = queryItem as QueryFolder;
//below is the list including the queries which include work item this event is attached
var queriesIncludingWorkItem = new List<QueryDefinition>();
if (queryFolder != null)
{
foreach (QueryDefinition query in queryFolder)
{
var wiCollection = tfsStore.Query(query.QueryText);
foreach (WorkItem workItem in wiCollection)
{
if (workItem.Id == eventWorkItem.Id)
{
queriesIncludingWorkItem.Add(query);
break;
}
}
}
}
顺便说一句,阿法克;在将 dll 部署到 "Plugins" 文件夹后,您需要回收 iis 应用程序池,以便有时看到更改。
所以,我有以下 ISubscriber 代码:
public class Subscriber : ISubscriber
{
public string Namek
{
get { return "Subscriber"; }
}
public SubscriberPriority Priority
{
get { return SubscriberPriority.Normal; }
}
public EventNotificationStatus ProcessEvent (TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out Microsoft.TeamFoundation.Common.ExceptionPropertyCollection properties)
{
statusCode = 0;
properties = null;
statusMessage = String.Empty;
try
{
if(notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
{
var ev = notificationArgs as WorkItemChangedEvent;
...........
}
}
}
public Type[] SubscribedTypes()
{
return new Type[1] = { typeof(WorkItemChangedEvent) };
}
}
代码有效;这意味着在安装插件并且 TFS 识别它之后,在 ProcessEvent 方法中放置一个断点就可以了。
我的问题是我正在尝试获取项目的名称和属于 WorkItem 的 "Shared" 查询,其更改触发了此 运行 Subscriber 插件,但我不能似乎找到了这样的引用(我也不知道它们是否通过此接口提供,因为 WorkItemChangedEvent 上的信息太少了)。
有什么建议吗?
WorkItemChangedEvent ev = notificationEventArgs as WorkItemChangedEvent;
您可以使用 Tfs.Aggregator 代码作为类似任务的参考 reference
您应该按照以下行来解决您的问题:
- 获取项目工作项在
- 获取项目"Shared Queries"
- 获取每个查询结果的"Work Item Ids"
- 检查哪一个包含当前更改的"Work Item Id"
所以代码应该是这样的:
var tfsCollection = new TfsTeamProjectCollection(new Uri(tfsUri));
var tfsStore = tfsCollection.GetService<WorkItemStore>();
var workItemId = ev.CoreFields.IntegerFields[0].NewValue;
var eventWorkItem = tfsStore.GetWorkItem(workItemId);
var project = eventWorkItem.Project;
var queryHierarchy = project.QueryHierarchy;
var queryFolder = queryHierarchy as QueryFolder;
var queryItem = queryFolder["Shared Queries"];
queryFolder = queryItem as QueryFolder;
//below is the list including the queries which include work item this event is attached
var queriesIncludingWorkItem = new List<QueryDefinition>();
if (queryFolder != null)
{
foreach (QueryDefinition query in queryFolder)
{
var wiCollection = tfsStore.Query(query.QueryText);
foreach (WorkItem workItem in wiCollection)
{
if (workItem.Id == eventWorkItem.Id)
{
queriesIncludingWorkItem.Add(query);
break;
}
}
}
}
顺便说一句,阿法克;在将 dll 部署到 "Plugins" 文件夹后,您需要回收 iis 应用程序池,以便有时看到更改。