以编程方式列出WorkItem State原因

时间:2016-08-19 07:50:51

标签: tfs

我们有一个自定义的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);

但是我没有得到任何结果。我想得到一个清单,列出我可以关闭错误的所有原因(不可重现,固定等)

1 个答案:

答案 0 :(得分:1)

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

另一种方法是通过WorkItemType.Export()方法导出workitemtype定义,然后从中获取信息。 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;
}