Search This Blog

Monday, February 29, 2016

TFS integration

Since a couple of months I work with Team Foundation Server and I must say it really surprised me. At first I thought it would be an okish source control, and a sub par bug tracking tool (I was testing the express edition). Honestly the express edition is really bad compared to the full one.

We since installed the full version and here surprise, the bug tracking is good, the build server is actually useful (with the tests being run when I commit or on schedule), and there is so many useful features that it would really need a full article on it. Maybe later on I will do it.

For me TFS was also important to integrate with our own software such that in case of issues the software would automatically create a bug report. However people have the bad habit to reload multiple times the page even if it doesn't work ( Funny quote from Einstien ) and of course we end up having tons of duplicated bugs.

After a bit of playing and further researches I found it's possible to query the bugs and then just modify the existing one:

public static int AddBug(string title, string message)
{
    lock (lockObject)
    {
        Uri collectionUri = new Uri("http://tfs.psi.ch/tfs/DefaultCollection");
 
        var creds = new TfsClientCredentials(new SimpleWebTokenCredential("PSICH\\" + System.Configuration.ConfigurationManager.AppSettings["alfrescoUser"], System.Configuration.ConfigurationManager.AppSettings["alfrescoPassword"]));
        var tfs = new TfsTeamProjectCollection(collectionUri);
        // Get the work item store
        WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
        Project tp = wis.Projects["Inventory"];
 
        WorkItemType wit = tp.WorkItemTypes["Bug"];
 
        var titleWithoutDate = title.Split(new string[] { " - " }, StringSplitOptions.None)[0];
 
        var existingWit = wis.Query("SELECT * FROM WorkItems WHERE [System.State] = 'New' AND [System.Title] CONTAINS '" + titleWithoutDate + "'").OfType<WorkItem>().FirstOrDefault();
        if (existingWit != null)
        {
            try
            {
                existingWit = wit.Store.GetWorkItem(existingWit.Id);
                existingWit.Fields["Repro Steps"].Value += "<br>-------------------------------------------<br>Reproduced again on " + DateTime.Now.ToString(System.Configuration.ConfigurationManager.AppSettings["ShortDate"]);
                existingWit.Save();
            }
            catch
            {
            }
            return existingWit.Id;
        }
 
        WorkItem wi = new WorkItem(wit);
        wi.Title = title;
        wi.Fields["Repro Steps"].Value = message;
        wi.State = "New";
        wi.Save();
        return wi.Id;
    }
}
So now it will search for a bug on the same URL and don't report twice the bug.

No comments:

Post a Comment