次の方法で共有


How to query list of Re-activated bugs

In TFS 2008/2010, I don’t see any easiest way to query the list of all reactivated bugs; and, the no. of times they have been re-activated. Most of the time, it becomes more tedious and time consuming job for anyone to get the list of re-activated bugs. And, some times, we just end-up querying all the bugs and manually reviewing their revision history. And, this makes one’s life cumbersome when the bug count to be reviewed is more. So, in order to get rid/minimize the manual effort, one can follow the below step to come-up with a small piece of code that can pull the list of all re-activated bugs with the following details:

 

  1. Bug Id
  2. Last Resolved Date
  3. Last Resolved By
  4. Last Re-Activated Date
  5. Last Re-Activated By
  6. Re-Activated Count

Step 1 – Connect to TFS and get the TFS WorkItemStore

public WorkItemStore GetWorkItemStore(Uri tfsServiceUri)

{

    WorkItemStore tfsWorkItemStore = null;

    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsServiceUri);

    tfsWorkItemStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

    return tfsWorkItemStore;

}

Step 2 – Get the TFS Project instance from the WorkItemStore

public Project GetProject()

{

    Project prjTeamProject = null;

    prjTeamProject = GetWorkItemStore().Projects[TFSProject];

    return prjTeamProject;

}

Step 3 – Have this generic method to get the WorkItemCollection for given work Item query

public WorkItemCollection GetWorkItemCollection(string wiqlQuery)

{

    WorkItemCollection wrkItemCollection = null;

    wrkItemCollection = GetProject().Store.Query(wiqlQuery);

    return wrkItemCollection;

}

Step 4 – Loop through revision history of each work item - of type Bug, to know whether there is a state change? And, if there is change, then check whether they are changed from “Active->Resolved/Closed->Active”? If this whole cycle is complete? i.e., the bug is activated at least once after Resolved/Closed? Then, confirm that the bug is re-activated by increase “reactivated” counter as done below:

public static void GetReactivateBugReport(TFSCommon tfsCommon, string teamProject, string strIterationPath)

{

    string wiqAllBugs = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '{0}' AND [System.IterationPath] UNDER '{1}' AND [System.WorkItemType] = 'Bug'";

    // Build your WorkItem query for given project and Iteration path.

    string tfsQuery = string.Format(wiqAllBugs, teamProject, strIterationPath);

    // Get the WorkItemCollection for given Project and Iteration Path

    WorkItemCollection wiBugCollection = tfsCommon.GetWorkItemCollection(tfsQuery);

    if (wiBugCollection.Count > 0)

    {

        // Save the output as .csv format

        using (StreamWriter streamWriter = new StreamWriter("ReactivatedBugReport.csv"))

        {

            // Write the column heading

            streamWriter.WriteLine("ID,Resolved Date,Resolved By,ReActivated Date, ReActivated By,ReActivate Count");

            // Loop through the workitems

            foreach (WorkItem wItem in wiBugCollection)

            {

                string wiId = string.Empty;

                string strLastResolvedBy = string.Empty;

                string strLastResolvedDate = string.Empty;

                string strReActivatedBy = string.Empty;

                string strReActivatedDate = string.Empty;

                int iReactivatedCount = 0;

                int iResolvedCount = 0;

                // Loop through the all revision of the above work item

                foreach (Revision revision in wItem.Revisions)

                {

                    // If State is changed from Active to resolved and if it’s not resolved for the first time, then store the value to temp variables to till you reach the last resolved by and resolved date.

                    if (revision.GetTagLine().Contains("Edited (Active to Resolved"))

                    {

                        if (iResolvedCount <= iReactivatedCount)

                        {

                            wiId = revision.WorkItem.Id.ToString();

                            strLastResolvedDate = revision.Fields[CoreField.ChangedDate].Value.ToString();

                            strLastResolvedBy = revision.Fields[CoreField.ChangedBy].Value.ToString();

                        }

                        ++iResolvedCount;

                    }

                    // Same as above. But, this time check for Resolved to Active and store the values till you reach the last Re-activated Date and Re-activated By.

                    else if (revision.GetTagLine().Contains("Edited (Resolved to Active") || revision.GetTagLine().Contains("Edited (Closed to Active"))

                    {

                        strReActivatedBy = revision.Fields[CoreField.ChangedBy].Value.ToString();

                        strReActivatedDate = revision.Fields[CoreField.ChangedDate].Value.ToString();

                        // Increase the counter to get the no. of times the bug has been re-activated

                        ++iReactivatedCount;

                    }

                }

                // Write to file only if bug re-activate at least once.

                if (iReactivatedCount > 0)

                {

                    streamWriter.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", wiId, strLastResolvedDate, strLastResolvedBy,strReActivatedDate,strReActivatedBy,iReactivatedCount.ToString()));

                }

            }

        }

    }

}

Note:

  1. The above code doesn’t guarantee that “All the listed bugs are re-activated for valid reasons”. Instead, it queries based on the state changed from “Active->Resolved/Closed->Active”. And, end of the day its user responsibility to review and ensure that the bugs are re-activated for valid reasons.
  2. The above code takes some amount of time to generate the results. Because, it validates all the revision entery of each and every work item.

- thiruV