【问题标题】:Listing WorkItem State Reasons programmatically以编程方式列出工作项状态原因
【发布时间】:2019-01-21 12:38:27
【问题描述】:

我们有一个自定义的 TFS 工作流程,我希望能够从 TFS 访问我可以关闭 Bug 的原因(将状态从 Active 更改为 Closed),这样我们就不必每次都更新代码调整我们的流程。

这是我目前所拥有的:

  WorkItemType wiType = this.GetWorkItemStore().Projects[this.ProjectName].WorkItemTypes["Bug"];
  var reason = wiType.FieldDefinitions["Reason"];
  var state = wiType.FieldDefinitions["State"];

  var filterList = new FieldFilterList();      
  FieldFilter filter = new FieldFilter(wiType.FieldDefinitions[CoreField.State], "Active");
  filterList.Add(filter);

    var allowedReasons = reason.FilteredAllowedValues(filterList);

但是我没有得到任何结果。我想列出我可以关闭错误的所有原因(不可重现、已修复等)

【问题讨论】:

    标签: tfs


    【解决方案1】:

    据我所知,没有任何简单的方法可以直接通过 API 进行转换,因为 API 直接从数据库中读取允许的值。

    另一种方法是通过 WorkItemType.Export() 方法导出工作项类型定义,然后从中获取信息。 Vaccano 在this question 中的回答提供了您可以使用的整个代码示例。

    编辑以举例说明我如何使用上述建议解决此问题:

    public static List<Transition> GetTransistions(this WorkItemType workItemType)
    {
      List<Transition> currentTransistions;
    
      // See if this WorkItemType has already had it's transistions figured out.
      _allTransistions.TryGetValue(workItemType, out currentTransistions);
      if (currentTransistions != null)
        return currentTransistions;
    
      // Get this worktype type as xml
      XmlDocument workItemTypeXml = workItemType.Export(false);
    
      // Create a dictionary to allow us to look up the "to" state using a "from" state.
      var newTransistions = new List<Transition>();
    
      // get the transistions node.
      XmlNodeList transitionsList = workItemTypeXml.GetElementsByTagName("TRANSITIONS");
    
      // As there is only one transistions item we can just get the first
      XmlNode transitions = transitionsList[0];
    
      // Iterate all the transitions
      foreach (XmlNode transition in transitions)
      {
        XmlElement defaultReasonNode = transition["REASONS"]["DEFAULTREASON"];
        var defaultReason = defaultReasonNode.Attributes["value"].Value;
    
        var otherReasons = new List<string>();
        XmlNodeList otherReasonsNodes = transition["REASONS"].SelectNodes("REASON");
        foreach (XmlNode reasonNode in otherReasonsNodes)
        {
          var reason = reasonNode.Attributes["value"].Value;
          otherReasons.Add(reason);
        }
    
        // save off the transistion 
        newTransistions.Add(new Transition
        {
          From = transition.Attributes["from"].Value,
          To = transition.Attributes["to"].Value,
          DefaultReason = defaultReason,
          OtherReasons = otherReasons
        });
    
      }
    
      // Save off this transition so we don't do it again if it is needed.
      _allTransistions.Add(workItemType, newTransistions);
    
      return newTransistions;
    }
    

    【讨论】:

    • 您的回答使我找到了解决方案,因此我没有创建自己的解决方案,而是从您的链接问题中复制了改编后的代码 - 请随时编辑!
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多