I have been trying to connect to the our TFS2015 server via web api. I have not done this before. My goal is to get last successful build, get all changesets from him and for each changeset an info from which branch it was forwarded to release branch. I cant connect to it and not sure what is my next step.
This is my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
VssConnection connection = new VssConnection(new Uri("http://srv-tfs2015:8080/tfs/"), new VssCredentials(useDefaultCredentials: true));
Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient buildKlijent = connection.GetClient<Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient>();
var lastBuild = buildKlijent.GetLatestBuildAsync("Argosy", "Argosy32New");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The lastBuild contains an VssResourceNotFountException with a message:
API resource location 54481611-01f4-47f3-998f-160da0f0c229 is not registered on http://srv-tfs2015:8080/tfs/.
Build resides within team project Argosy which is a part of ArgosyCollection on the server and it's a XAML build definition. Not sure if that makes any difference.
Can you help me point to the right direction?
The documentation of Get Latest says you have to use rest api version 5.1. But TFS 2015 (API and TFS version mapping) supports only 2.4. So you have to use Get a list of build and detect the last build from the resulting list through the GetBuildsAsync
method.
As example to view last 10 builds of build definitions:
private static void ListBuildDefinitions(string TeamProjectName)
{
List<BuildDefinitionReference> buildDefs = BuildClient.GetDefinitionsAsync(TeamProjectName).Result;
foreach(BuildDefinitionReference buildDef in buildDefs)
{
Console.WriteLine("+================BUILD DEFINITION=======================================================");
Console.WriteLine(" ID:{0, -9}|NAME:{1, -35}|PATH:{2}", buildDef.Id, buildDef.Name, buildDef.Path);
Console.WriteLine(" REV:{0, -8}|QUEUE:{1, -34}|QUEUE STATUS:{2}", buildDef.Revision, (buildDef.Queue != null) ? buildDef.Queue.Name : "", buildDef.QueueStatus);
ListBuilds(TeamProjectName, buildDef);
}
}
private static void ListBuilds(string TeamProjectName, BuildDefinitionReference buildDef)
{
List<Build> builds = BuildClient.GetBuildsAsync(TeamProjectName, new List<int> { buildDef.Id }).Result;
if (builds.Count > 0)
{
Console.WriteLine("+====================BUILDS================================================================================");
Console.WriteLine("+ ID | NUMBER | STATUS | START DATE | FINISH DATE | COMMITS");
Console.WriteLine("+----------------------------------------------------------------------------------------------------------");
for (int i = 0; i < builds.Count && i < 10; i++)
{
var changes = BuildClient.GetBuildChangesAsync(TeamProjectName, builds[i].Id).Result;
Console.WriteLine(" {0, -12}|{1, -22}|{2, -17}|{3, -20}|{4, -20}|{5}", builds[i].Id, builds[i].BuildNumber, builds[i].Status,
(builds[i].StartTime.HasValue) ? builds[i].StartTime.Value.ToString() : "",
(builds[i].FinishTime.HasValue) ? builds[i].FinishTime.Value.ToString() : "", changes.Count);
}
}
else
Console.WriteLine("+=======================================================================================");
}
Check available functions for TFS 2015 here: Pre-Version 4.1 REST API documentation