using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using Microsoft.TeamFoundation.Client;
using System.Net;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Xml;
namespace MergeWitModifications
{
struct Info
{
public string Project { get; set; }
public string WorkItemType { get; set; }
}
public class AutoModifyWit
{
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="tfsServer">The uri to the tfs server, in the form
/// of http://MyTfsServer:8080.</param>
public AutoModifyWit(string tfsServer)
{
TfsServer = tfsServer;
}
/// <summary>
/// The uri to the TFS server that contains the work item types that
/// will be modified
/// </summary>
private string TfsServer { get; set; }
/// <summary>
/// Executes the modifications to the work items.
/// </summary>
/// <param name="teamProjects">The list of team projects that must be
/// modified. Pass null or the empty array when you want to update all
/// team projects.</param>
/// <param name="workItemTypes">The list of work item types that must be
/// modified. Pass null or the empty array when you want to update all
/// work item types.</param>
public void Start(string[] teamProjects, string[] workItemTypes)
{
// The base folder where the backup of the work item type definition
// will be stored.
string backupDirectory = string.Format(@"{0}\Backup{1:yyyyMMddhhmm}\", Directory.GetCurrentDirectory(), DateTime.Now);
// Iterate through all work item types in all team projects. When
// you do this without creating a new tfs instance, a write
// access violation will occur (a bug in the TFS SDK?)
foreach (var info in GetAllWITs())
{
// Check whether the team project and/or the work item type should be skipped
if ((teamProjects != null && teamProjects.Length != 0 &&
!new List<string>(teamProjects).Contains(info.Project)) ||
(workItemTypes != null && workItemTypes.Length != 0 &&
!new List<string>(workItemTypes).Contains(info.WorkItemType)))
continue;
Debug.WriteLine(info.Project + ", " + info.WorkItemType);
// Open the work item store
using (var tfs = new TeamFoundationServer(TfsServer,
CredentialCache.DefaultCredentials))
{
var store = (WorkItemStore) tfs.GetService(typeof (WorkItemStore));
// For the Team Project
var project = store.Projects[info.Project];
// For the Work Item Type
var workItemType = project.WorkItemTypes[info.WorkItemType];
// Download the work item type from the server
var doc = workItemType.Export(false);
// Backup the original work item type to support a fallback scenario
Directory.CreateDirectory(backupDirectory + @"\" + info.Project);
doc.Save(backupDirectory + @"\" + info.Project + @"\" + info.WorkItemType + @".xml");
///////////////////////////////////
///
/// TODO: Make the modifications to the work item type
/// handled in part 2.
///
///////////////////////////////////
// Validate the modifications to the work item type
WorkItemType.Validate(project, doc.OuterXml);
// Upload the work item type to TFS
project.WorkItemTypes.Import(doc.OuterXml);
}
}
}
/// <summary>
/// Returns the list of all available work item types on the server
/// </summary>
private List<Info> GetAllWITs()
{
var list = new List<Info>();
// Open the work item store
using (var tfs = new TeamFoundationServer(TfsServer,
CredentialCache.DefaultCredentials))
{
var store = (WorkItemStore) tfs.GetService(typeof (WorkItemStore));
// For all Team Projects
foreach (Project project in store.Projects)
{
// For all Work Item Types
foreach (WorkItemType workItemType in project.WorkItemTypes)
{
// Add the work item type
list.Add(new Info
{
Project = project.Name,
WorkItemType = workItemType.Name
});
}
}
}
return list;
}
}
}
using MergeWitModifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MergeWitModificationsTest
{
/// <summary>
///This is a test class for AutoModifyWitTest and is intended
///to contain all AutoModifyWitTest Unit Tests
///</summary>
[TestClass()]
public class AutoModifyWitTest
{
/// <summary>
/// Starts the modifications of the work item types.
///</summary>
[TestMethod()]
public void StartAutoModifyWit()
{
// The server that contains the work item types that must be modified
const string tfsServer = "http://MyTfsServer:8080";
// The list of team projects that must be modified. Pass null or the
// empty array when you want to update all team projects.
var teamProjects = new string[] { };
// The list of work item types that must be modified. Pass null or the
// empty array when you want to update all work item types.
var workItemTypes = new string[] { };
var target = new AutoModifyWit(tfsServer);
target.Start(teamProjects, workItemTypes)
}
}
}