If
you are using Team Foundation Server with Visual Studio, it will be
useful for you to integrate your projects bug system with the TFS Work
Items system. You can write a simple module for your program which will
handles the exceptions and collects needed information and send them to
the TFS.
So how
can we do something like that? First you have to write an error
management module which will handles exceptions, errors, etc. and
generates understandable information from them. After generating this
information lastly you have to send this information to the TFS.
In
addition to this it will be helpful if you create your error information
title unique in some cases to check that is this bug already entered as
a Work Item Bug or not? For example you can combine type of the
exception and last stack frame(s) which this error occurred in.
Anyway
let us write a simple code which will enter our error as Work Item Bug
to the TFS by programmatically. The code is so simple; you can also find
more complicated codes on the web.
|
const string USERNAME = "XXXX";
const string PASSWORD = "XXXX";
const string DOMAIN = "MyDomain";
const string TFS_SERVER = "http://mytfsserver:8080";
const string PROJECT = "LANCET";
protected void Page_Load(object sender, EventArgs e)
{
string title = Request.QueryString["Title"];
string description = Request.QueryString["Description"];
string email = Request.QueryString["Email"];
string force = Request.QueryString["Force"];
if (string.IsNullOrEmpty(force))
force = "1";
try
{
NetworkCredential account = new NetworkCredential(USER_NAME,
PASSWORD, DOMAIN);
TeamFoundationServer server = new TeamFoundationServer(TFS_SERVER,
account);
Project project = null;
server.Authenticate();
WorkItemStore store = new WorkItemStore(server);
project = store.Projects[PROJECT];
if (project == null)
throw new Exception("Project could not found");
string wiql = "SELECT [System.Id], [System.Title],
[System.Description] " +
"FROM WorkItems " +
"WHERE [System.TeamProject] = '" + PROJECT + "' " +
"AND [System.Title] = '" + title + "'";
WorkItemCollection collection = store.Query(wiql);
StringBuilder tmp = new StringBuilder();
tmp.Append(title);
if (!string.IsNullOrEmpty(description))
{
tmp.Append("\r\n");
tmp.Append("-----------------------------------------------------------------");
tmp.AppendLine();
tmp.AppendLine(description);
}
if (!string.IsNullOrEmpty(email))
{
tmp.AppendLine();
tmp.Append("-----------------------------------------------------------------");
tmp.AppendLine();
tmp.AppendLine(email);
}
if (collection.Count == 0 || force == "1")
{
WorkItem item = new WorkItem(project.WorkItemTypes["bug"]);
item.Title = title;
item.AreaPath = @"CPM\Inbox";
item.State = "Active";
item.Description = tmp.ToString();
item.Save();
}
else
{
WorkItem item = collection[0];
item.Open();
item.State = "Active";
item.History += "\r\n\r\n" + DateTime.Now.ToString()
+ " ----------------------------------------------------\r\n" +
tmp.ToString();
item.Save();
}
Response.Write("SUCCESS - Bug successfully inserted to the TFS");
}
catch (Exception ex)
{
Response.Write("ERR-" + ex.Message);
}
}
|
As you
see here, first we try to connect to the TFS server with the given
network credentials, then we search for the project which will own this
bug. After that we are trying to search that is there any Work Item
which has the same title (which is unique for us). If we find an
existing entry than we re-activate that task and add a history to it. If
we don’t find any entry for this title then we add a new Bug case for
this. Actually as I said before the code is so simple, you can change it
according to your necessity.
Finally
we have to talk about one more thing. What if the machine which our
program runs on and generates exception does not have any access
privileges to the TFS server machine? Because of some security reasons
you may don’t want to open your system. In this case you can use a
Website to solve this problem. Create a local web page which takes
needed information from Request and runs the code above. So you can call
this web page from your program and everything will be worked fine.