【问题标题】:TFS Azure 2017 workitem change event handler - branch relationTFS Azure 2017 工作项更改事件处理程序 - 分支关系
【发布时间】:2021-08-20 17:28:21
【问题描述】:

我们在 tfs 中有一个插件。此插件会在任何工作项更改时通知我们。我们可以获得工作项详细信息,例如 id、变更集列表,如下所示。但是我们还需要工作项相关的分支名称。

您能帮帮我吗,当工作项在下面的代码中更改时,有什么方法可以获取相关的分支名称。

谢谢

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

        try
        {
            if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
            {
                WorkItemChangedEvent ev = notificationEventArgs as WorkItemChangedEvent;
                int workItemId = Convert.ToInt32(ev.CoreFields.IntegerFields[0].NewValue);
                
                WriteLog("1WorkItemChangedEventHandler WorkItem " + ev.WorkItemTitle + " - Id " + workItemId.ToString() + " was modified");
                

            }

        }
        catch (Exception)
        {
        }

        return EventNotificationStatus.ActionPermitted;
    }

【问题讨论】:

    标签: tfs azure-devops branch tfs-workitem


    【解决方案1】:

    您可以使用workItemStore.GetWorkItem(workItemId ) 获取所有工作项信息。在这里检查类似的问题:How can I get a reference to the TFS WorkItem in a WorkItemChangedEvent?

    如果你使用 git,你可以试试下面的例子来获取分支引用:

    int workitemId = YOUR_ID;
    
    WorkItemStore wiStore = new WorkItemStore("{YOUR_URI}");
    
    WorkItem wi = wiStore.GetWorkItem(workitemId);            
    
    foreach (Link link in wi.Links)
    {
        if (link.BaseType == BaseLinkType.ExternalLink)
        {
            var brList = ((ExternalLink)link).LinkedArtifactUri.Split(new string[] { "%2F" }, StringSplitOptions.RemoveEmptyEntries).ToList();
    
            brList.RemoveAt(0); //remove project and repo giuds
            brList.RemoveAt(0);
            brList[0] = brList[0].Replace("GB", ""); // remove GB prefix
    
            Console.WriteLine("Branch: {0}", string.Join("/", brList));
        }
    }
    

    【讨论】:

    • 是的,我使用它,但没有分支信息。那里的分行信息在哪里?我们还需要分支名称,例如 test、master 等。
    • @Pronto 我已经添加了一个如何获取 git 分支的示例。
    猜你喜欢
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多