Skip to content

Instantly share code, notes, and snippets.

@Mozketo
Created January 13, 2012 05:51
Show Gist options
  • Save Mozketo/1604842 to your computer and use it in GitHub Desktop.
Save Mozketo/1604842 to your computer and use it in GitHub Desktop.
C# TFS hints
class TfsConnection
{
public static TfsTeamProjectCollection Connect()
{
string url = "https://xxx:8081/tfs/";
ICredentials credential = CredentialCache.DefaultCredentials;
var server = new TfsTeamProjectCollection(new Uri(url), credential);
return server;
}
}
/// <summary>
///
/// </summary>
public IOrderedEnumerable<IBuildDefinition> GetBuildDefinitions(string projectStartsWith)
{
TfsTeamProjectCollection server = TfsConnection.Connect();
var vcs = server.GetService<VersionControlServer>();
var teamProjects = vcs.GetAllTeamProjects(true);
var buildDefinitions = new List<IBuildDefinition>();
IBuildServer buildServer = server.GetService<IBuildServer>();
foreach (var teamProject in teamProjects)
{
buildDefinitions.AddRange(buildServer.QueryBuildDefinitions(teamProject.Name));
}
return buildDefinitions.OrderBy(b => b.Name);
}
/// <summary>
///
/// </summary>
public void IterateBuildRetentionPolicies() {
TfsTeamProjectCollection server = TfsConnection.Connect();
IBuildServer buildServer = server.GetService<IBuildServer>();
foreach (var teamProject in teamProjects)
{
IBuildDefinition[] buildDefinitions = buildServer.QueryBuildDefinitions(teamProject.Name);
foreach (var buildDefinition in buildDefinitions)
{
if (buildDefinition != null)
{
var retentionPolicies = buildDefinition.RetentionPolicyList;
foreach (var retentionPolicy in retentionPolicies)
{
if (retentionPolicy.NumberToKeep > 10)
{
Console.WriteLine(String.Format("{0} is keeping {1} builds", buildDefinition.Name, retentionPolicy.NumberToKeep));
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment