As part of the testing process, you will no doubt need to maintain items in source control. Updating tests results, for instance.
If you use TFS 2010, you may have already discovered how wonderfully sparse and obtuse the API documentation is.
I have worked out how to do some basic tasks, but By God it was a struggle...
using System;
using System.Linq;
using System.Web.Services;
using System.Web.Script.Services;
using System.IO;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Net;
/// <summary>
/// Summary description for TF
/// </summary>
[WebService(Namespace = "http://vipitech.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class TF : System.Web.Services.WebService {
public TF () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string updateMasterResults(string[] TFparm)
//this is entered with an array parameter [testPC name, check in Comment, test reference, test reference...]
{
string testPC = TFparm[0];
int l = TFparm.Length;
string changeCount = "";
string checkin = "";
string ciComment = TFparm[1];
//all the TFS_ variables will need changing to suit your environment
NetworkCredential netCred = new NetworkCredential(TFS_ID, TFS_pwd, TFS_domain);
var tfs = new TfsTeamProjectCollection(new Uri(TFS_uri), netCred);
var versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
var testWS = versionControl.GetWorkspace(TFS_Workspace, versionControl.AuthorizedUser);
for (int i = 2; i < l; i++)
{
string runCode = TFparm[i];
TFget(testWS, runCode, "testOutput.txt");
copyResultsFile(testPC, runCode);
changeCount = TFcheckOut(testWS, runCode);
}
checkin = "" + testWS.CheckIn(testWS.GetPendingChanges().ToArray(), ciComment);
return "Changeset " + checkin + " created with " + changeCount + " master results updated";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void copyResultsFile(string testPC, string runCode)
{
string testFolder = @"C:\TFS\blah\MasterResults\" + runCode + @"\";
Directory.CreateDirectory(testFolder);
File.SetAttributes(testFolder + "RunSummary.txt", FileAttributes.Normal);
string sourceFolder = @"\\" + testPC + @"\blah\MasterResults\" + runCode + @"\";
//replace master with current result
File.Copy(sourceFolder + "ctestOutput.txt", testFolder + "testOutput.txt", true);
}
[WebMethod]
[ScriptMethod]
public string TFget(Workspace testWS, string TFtest, string getFile)
{
string gots = "";
if (TFtest != "")
{
try
{
GetStatus status = testWS.Get(new GetRequest(@"C:\TFS\blah\MasterResults\" + TFtest + @"\" + getFile,
RecursionType.None, VersionSpec.Latest),
GetOptions.GetAll);//.GetAll is important, else nothing happens
gots = status.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
return gots;
}
[WebMethod]
[ScriptMethod]
public string TFcheckOut(Workspace testWS, string TFtest)
{
string MRsandbox = @"$/VIPitech_Test/VipitechMasterResults/";
if (TFtest != "")
{
try
{
testWS.PendEdit(MRsandbox + TFtest + @"/RunSummary.txt");
}
catch (Exception ex)
{
return ex.Message;
}
}
int changeCount = testWS.GetPendingChanges().ToArray().Length;
return changeCount.ToString();
}
}
Note that I am returning strings from most of these functions but not using them (they were for debugging purposes - as is the HelloWorld() function, of course).