1

2010 Beta 2 API を使用していくつかの一般的な TFS タスクを実行する Windows アプリを開発しています (新しいチーム プロジェクト、新しい作業項目、選択的ビルドなどの作成など)。

既存の作業項目を編集する過程で、WI の状態の変化に応じて「理由」フィールドの値を自動的に設定できるはずです (Visual Studio を模倣します)。(例)- バグを編集するとき、状態がアクティブから解決済みに変わると、デフォルトの理由は「修正済み」になり、状態がアクティブからクローズドになると、同様にデフォルトの理由 =「延期」になります。(作業項目タイプ定義 xml ファイルで定義されています。) バグが初めて編集されたときの初期状態はアクティブであるため、この遷移は簡単にキャプチャして、フォームの単純なイベント ハンドラー内に実装できます。

Resolved to Closed (Reason=Fixed)、Resolved to Active (Reason=Test failed/Not fix)、Closed to Active (Reason=Reactivated/Regression) などの残りの遷移を実装する方法を知りたいです。

WorkItem.GetNextState(current_state,action) というメソッドがあることは知っていますが、特定のアクションが必要なため、これは役に立ちません。

これまでに行ったことを以下に示します。

void cmbBugState_SelectedIndexChanged(object sender, EventArgs e)
    {
        //private enum bugWorkFlows{"Fixed","Deferred","Duplicate","As Designed","Cannot Reproduce","Obsolete","Test Failed","Not Fixed","Reactivated","Regression"}
        string[] activeToResolvedReasons = { "Fixed", "Deferred", "Duplicate", "As Designed", "Cannot Reproduce", "Obsolete" };
        string[] resolvedToActiveReasons = { "Test Failed", "Not fixed" };
        string[] resolvedToClosedReasons = activeToResolvedReasons;
        string[] closedToActiveReasons = { "Reactivated", "Regression" };
        string[] activeToClosedReasons = activeToResolvedReasons;

        cmbBugReason.Items.AddRange(activeToResolvedReasons);
        // Set the default reason according to change of state of the work item.
        if (cmbBugState.SelectedItem.ToString() == "Resolved")
        {
            cmbBugReason.Enabled = true;
            cmbBugReason.SelectedItem = activeToResolvedReasons[0];
        }
        if (cmbBugState.SelectedItem.ToString() == "Closed")
        {
            cmbBugReason.Enabled = true;
            cmbBugReason.SelectedItem = activeToResolvedReasons[1];
        }
    }

フォームでこれらのイベントを処理する方法を誰かが示すことができますか?

ありがとう、タラ。

4

1 に答える 1

1

GetNextState を試してみました。私が必要としているものに対して十分に信頼できるものではありませんでした。

そのため、状態「A」から状態「B」に移行するときに非常にうまく機能する「独自の」状態遷移コードを作成しました。少し長いですが、探しているものが含まれているはずです。

補足: これは GetNextState メソッドを使用しないため、何らかの方法で次の状態を取得する必要があります。これを行う方法は、問題の作業項目タイプの XML をダウンロードすることです。それを解析し、それを使用して遷移リスト (_ allTransistions) を作成します。

これを行うために必要な TFS 2010 のアクセス許可レベルは、Team Foundation 管理者またはプロジェクト管理者です。(ちなみに、TFS 2008 および 2005 では、すべての有効なユーザーがこれを実行できました。)

これを使用する完全なコードは、codeplex のTFS Aggregatorプロジェクトの WorkItemHelpers.cs ファイルにあります。

public static void TransitionToState(this WorkItem workItem, string state, string commentPrefix)
{
    // Set the sourceWorkItem's state so that it is clear that it has been moved.
    string originalState = (string)workItem.Fields["State"].Value;

    // Try to set the state of the source work item to the "Deleted/Moved" state (whatever is defined in the file).

    // We need an open work item to set the state
    workItem.TryOpen();

    // See if we can go directly to the planned state.
    workItem.Fields["State"].Value = state;


    if (workItem.Fields["State"].Status != FieldStatus.Valid)
    {
        // Revert back to the orginal value and start searching for a way to our "MovedState"
        workItem.Fields["State"].Value = workItem.Fields["State"].OriginalValue;

        // If we can't then try to go from the current state to another state.  Saving each time till we get to where we are going.
        foreach (string curState in workItem.Type.FindNextState((string)workItem.Fields["State"].Value, state))
        {
            string comment;
            if (curState == state)
                comment = commentPrefix + Environment.NewLine + "  State changed to " + state;
            else
                comment = commentPrefix + Environment.NewLine + "  State changed to " + curState + " as part of move toward a state of " + state;

            bool success = ChangeWorkItemState(workItem, originalState, curState, comment);
            // If we could not do the incremental state change then we are done.  We will have to go back to the orginal...
            if (!success)
                break;
        }
    }
    else
    {
        // Just save it off if we can.
        string comment = commentPrefix + "\n   State changed to " + state;
        ChangeWorkItemState(workItem, originalState, state, comment);

    }
}
private static bool ChangeWorkItemState(this WorkItem workItem, string orginalSourceState, string destState, String comment)
{
    // Try to save the new state.  If that fails then we also go back to the orginal state.
    try
    {
        workItem.TryOpen();
        workItem.Fields["State"].Value = destState;
        workItem.History = comment;
        workItem.Save();
        return true;
    }
    catch (Exception)
    {
        // Revert back to the original value.
        workItem.Fields["State"].Value = orginalSourceState;
        return false;
    }
}

/// <summary>
/// Used to find the next state on our way to a destination state.
/// (Meaning if we are going from a "Not-Started" to a "Done" state, 
/// we usually have to hit a "in progress" state first.
/// </summary>
/// <param name="wiType"></param>
/// <param name="fromState"></param>
/// <param name="toState"></param>
/// <returns></returns>
public static IEnumerable<string> FindNextState(this WorkItemType wiType, string fromState, string toState)
{
    var map = new Dictionary<string, string>();
    var edges = wiType.GetTransitions().ToDictionary(i => i.From, i => i.To);
    var q = new Queue<string>();
    map.Add(fromState, null);
    q.Enqueue(fromState);
    while (q.Count > 0)
    {
        var current = q.Dequeue();
        foreach (var s in edges[current])
        {
            if (!map.ContainsKey(s))
            {
                map.Add(s, current);
                if (s == toState)
                {
                    var result = new Stack<string>();
                    var thisNode = s;
                    do
                    {
                        result.Push(thisNode);
                        thisNode = map[thisNode];
                    } while (thisNode != fromState);
                    while (result.Count > 0)
                        yield return result.Pop();
                    yield break;
                }
                q.Enqueue(s);
            }
        }
    }
    // no path exists
}

private static readonly Dictionary<WorkItemType, List<Transition>> _allTransistions = new Dictionary<WorkItemType, List<Transition>>();

/// <summary>
/// Deprecated
/// Get the transitions for this <see cref="WorkItemType"/>
/// </summary>
/// <param name="workItemType"></param>
/// <returns></returns>
public static List<Transition> GetTransitions(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 transitionXML in transitions)
    {
        // See if we have this from state already.
        string fromState = transitionXML.Attributes["from"].Value;
        Transition transition = newTransistions.Find(trans => trans.From == fromState);
        if (transition != null)
        {
            transition.To.Add(transitionXML.Attributes["to"].Value);
        }
        // If we could not find this state already then add it.
        else
        {
            // save off the transistion (from first so we can look up state progression.
            newTransistions.Add(new Transition
            {
                From = transitionXML.Attributes["from"].Value,
                To = new List<string> { transitionXML.Attributes["to"].Value }
            });
        }
    }

    // Save off this transition so we don't do it again if it is needed.
    _allTransistions.Add(workItemType, newTransistions);

    return newTransistions;
}
于 2011-03-01T23:32:20.430 に答える