I want to automate testing of newly created build agents in TFS.
Now, I have written code to queue builds of particular build definition on TFS.
Problem I am facing is to queue build definition on agents of given controller.
Previously there was one method which I've commented in my code
for doing this, i.e. IbuildRequest.buildagent, but now it has been deprecated in newer tfs api.
Edit:
I have already tested IbuildRequest.BuildController property which tends to randomly pick agent, which is currently free.
What I want to know is can I force build definition to use one build Agent using tfs api.
using System;
using System.Linq;
using System.Security.Principal;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace BuildAgentsTestApp
{
class Program
{
static void Main(string[] args)
{
Uri collectionURI = new Uri("https://tfs-uri");
var tfsCreds = new TfsClientCredentials(new WindowsCredential(), true);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(collectionURI,tfsCreds);
WindowsIdentity user = WindowsIdentity.GetCurrent();
tfs.EnsureAuthenticated();
if (tfs.HasAuthenticated)
{
WorkItemStore workItemStore = tfs.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects["myProjectName"];
IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
IBuildDefinition buildDef = buildServer.GetBuildDefinition(teamProject.Name, "myBuildDefinitionName");
var controller = GetRequestedController(buildServer, "myControllerName");
var AgentsList = GetAgentCollection(controller);
//Queue each build definition
IBuildRequest buildRequest = buildDef.CreateBuildRequest();
buildRequest.GetOption = GetOption.Custom;
//buildRequest.BuildAgent = AgentsList.First();
buildServer.QueueBuild(buildRequest);
}
Console.WriteLine("Build Queued!");
Console.ReadKey();
}
static IBuildController GetRequestedController(IBuildServer reqBuildServer, string reqControllerName)
{
var requiredController = reqBuildServer.QueryBuildControllers()
.Where(ctrl =>
ctrl.Name == reqControllerName
).FirstOrDefault();
return requiredController;
}
static List<IBuildAgent> GetAgentCollection(IBuildController controller)
{
var ListOfAgents = from agent in controller.Agents
select agent;
return ListOfAgents.ToList();
}
}
}
Related
For a project I'm working on I have to get a Pull Request and Repository from an Azure DevOps WorkItem ID.
I'm using the Microsoft.TeamFoundationServer.Client NuGet-Package for this.
Now i also want to be able to check if a build pipeline ran successfully before moving on to further steps.
After trying to figure it out myself and not finding a single article on how to do that, I'm just gonna ask the question myself.
So, I already have:
the WorkItem object
the GitRepository object
the PullRequest object
And I want:
some form of pipeline object of that specific Pull Request/Commit
I hope there even is a way to get that.
Any help or references are apprechiated. Thanks!
I'm working on I have to get a Pull Request and Repository from an
Azure DevOps WorkItem ID.
For pull request that related to work item, I can write a C# code for you. But for the repository, I think there doesn't have a relationship between repository itself and work item in DevOps concept(link commit and work item is possible.).
Now i also want to be able to check if a build pipeline ran
successfully before moving on to further steps.
Do you mean you want the pipeline run status related to pull request? I checked the sdk definition, there doesn't have such definition, also no in the REST API. A possible solution is following the f12 to capture the API to get the build pipeline run id and it status.
Just a demo:
using System;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
namespace GetPipelineResults
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string url_string = "https://dev.azure.com/xxx/";
string personalAccessToken = "xxx";
Uri orgUrl = new Uri(url_string);
string project = "xxx";
int workitemId = 122;
var workitem = GetPullRequestAndRepositoryFromWorkItemId(orgUrl,personalAccessToken,workitemId);
var pullRequestUrl = workitem.Result.Relations[0].Url.ToString();
var pullRequestUrl2 = pullRequestUrl.Substring(pullRequestUrl.LastIndexOf('/') + 1);
string[] pullRequestUrl2Array = pullRequestUrl2.Split("%2F");
string pullRequestIdString = pullRequestUrl2Array[pullRequestUrl2Array.Length - 1];
Console.WriteLine(pullRequestIdString);
}
//Get Pull request from work item id
public static async Task<WorkItem> GetPullRequestAndRepositoryFromWorkItemId(Uri orgUrl, string personalAccessToken, int workItemId)
{
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
WorkItemExpand workItemExpand = WorkItemExpand.All;
var workItem = workItemTrackingHttpClient.GetWorkItemAsync(workItemId, expand: workItemExpand).Result;
return workItem;
}
}
}
Given:
I have a project/solution containing tests. It is written in C# and is using nUnit.
What I want:
Somehow to retrieve all the tests in the Project/Solution without executing them and also giving me the CodeFilePath and LineNumber for each test.
What I tried / investigated:
dotnet test --list-tests : is giving me nothing (just the displayname)
NUnit3TestAdapter : is at somepoint exposing those values (see NUnitTestAdapter).
But how can I retrieve them?
Thanks #Charlie for pointing out a possible solution. I did basically exactly as you stated.
IMPORTANT NOTICE:
This seems to not work with dot.net CORE tests currently because of this issue: https://github.com/nunit/nunit-console/issues/710
Also DiaSession runs into an exception when used like this in dot.net CORE :-(
using System;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.Engine;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var testAssemblyPath = #"C:\src\qata\src\tests\external\SomethingProject.Tests\bin\Debug\net461\SomethingProject.Tests.dll";
var package = new TestPackage(testAssemblyPath);
var testEngine = new TestEngine();
var runner = testEngine.GetRunner(package);
var nUnitXml = runner.Explore(TestFilter.Empty);
var session = new DiaSession(testAssemblyPath);
foreach (XmlNode testNode in nUnitXml.SelectNodes("//test-case"))
{
var testName = testNode.Attributes["fullname"]?.Value;
var className = testNode.Attributes["classname"]?.Value;
var methodName = testNode.Attributes["methodname"]?.Value;
var navigationData = session.GetNavigationData(className, methodName);
Console.WriteLine($"{testName} - {navigationData.FileName} - {navigationData.MinLineNumber}.");
}
}
}
}
public ActionResult Connect()
{
List<string> Collected = new List<string>(10);
Uri configurationServerUri = new Uri("xxxxxxtfsurl");
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer
(configurationServerUri);
ITeamProjectCollectionService tpcService =
configurationServer.GetService<ITeamProjectCollectionService>();
foreach (TeamProjectCollection tpc in
tpcService.GetCollections())
{
Collected.Add(tpc.Name);
}
ViewBag.List = Collected;
return PartialView();
}
I retrieved the collection using controller in MVC but can any one help me to retrieve the projects from specific collection
If you just want to retrieve the team projects from specific collection, then you can use below code sample:
Install Nuget package Microsoft.TeamFoundationServer.ExtendedClient.
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
class Program
{
static void Main(string[] args)
{
string collection = #"http://ictfs2015:8080/tfs/DefaultCollection";
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(collection));
tfs.EnsureAuthenticated();
VersionControlServer vcs = tfs.GetService<VersionControlServer>();
TeamProject[] teamProjects = vcs.GetAllTeamProjects(true);
foreach (TeamProject proj in teamProjects)
{
System.Console.WriteLine(string.Format("Team Project: {0}", proj.Name));
}
System.Console.ReadLine();
}
}
If you want to retrieve all the collections and their team projects, then you can try below code sample:
using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
namespace GetProjectList
{
class Program
{
static void Main(String[] args)
{
// Connect to Team Foundation Server
// Server is the name of the server that is running the application tier for Team Foundation.
// Port is the port that Team Foundation uses. The default port is 8080.
// VDir is the virtual path to the Team Foundation application. The default path is tfs.
Uri tfsUri = (args.Length < 1) ?
new Uri("http://ictfs2015:8080/tfs") : new Uri(args[0]);
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
// Get the catalog of team project collections
ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
new[] { CatalogResourceTypes.ProjectCollection },
false, CatalogQueryOptions.None);
// List the team project collections
foreach (CatalogNode collectionNode in collectionNodes)
{
// Use the InstanceId property to get the team project collection
Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
// Print the name of the team project collection
Console.WriteLine("Collection: " + teamProjectCollection.Name);
// Get a catalog of team projects for the collection
ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren(
new[] { CatalogResourceTypes.TeamProject },
false, CatalogQueryOptions.None);
// List the team projects in the collection
foreach (CatalogNode projectNode in projectNodes)
{
Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName);
}
}
// Display the project list on cosole window
Console.ReadLine();
}
}
}
I need to find out how many labels are there inside each module of a collection in Team Foundation repository.
I am using TFS 2013.
I know we can get it from Visual Studio. But we need a script which gets us the number of labels as output.
Can anyone help me out in getting a C# or a Powershell code to obtain the same?
TIA
You can use .NET Client Libraries to get this: .NET client libraries for Visual Studio Team Services (and TFS)
Code sample:
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace GetLabels
{
class Program
{
static void Main(string[] args)
{
string tfscollection = "http://xxx:8080/tfs/defaultcollection";
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfscollection));
VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
string labelname = null;
string labelscope = "$/";
string owner = null;
bool includeitem = false;
int labelnumber;
VersionControlLabel[] labels = vcs.QueryLabels(labelname,labelscope,owner,includeitem);
labelnumber = labels.Length;
Console.WriteLine(labelnumber);
Console.ReadLine();
}
}
}
Our company recently updated TFS to 2015 update 1. After that context menu item named Drop folder disappeared from completed builds. I found nothing about it and how to bring it back. When I click Open on completed build, VS opens web version of TFS where I forced to click through the menus and copy drop folder path manually. So I decided to write a simple extension that will add this item to the menu.
Some googling brought me to this page. But it seems that the example code is quite old and not working in VS2015:
IVsTeamFoundationBuild vsTfBuild = (IVsTeamFoundationBuild)GetService(typeof(IVsTeamFoundationBuild));
IBuildDetail[] builds = vsTfBuild.BuildExplorer.CompletedView.SelectedBuilds;
Property SelectedBuilds is always empty. I suppose that it relates to old window from VS2010. It returns items that are instance of IBuildDetail interface.
So I found this piece of code here:
var teamExplorer = (ITeamExplorer)ServiceProvider.GetService(typeof(ITeamExplorer));
var page = teamExplorer.CurrentPage;
var buildsPageExt = (IBuildsPageExt)page.GetExtensibilityService(typeof(IBuildsPageExt));
var build = buildsPageExt.SelectedBuilds[0];
Here build is the instance of IBuildModel interface. It lacks DropLocation property.
Is there any way to found drop location of selected build? Or maybe latest build?
You can use IBuildDedetail.DropLocation in .NET client libraries for Visual Studio Team Services (and TFS). Basic code for your reference:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
namespace BuildAPI
{
class Program
{
static void Main(string[] args)
{
string project = "http://xxx.xxx.xxx.xxx";
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(project));
IBuildServer ibs = tpc.GetService<IBuildServer>();
var builds = ibs.QueryBuilds("TeamProjectName");
foreach (IBuildDetail ibd in builds)
{
Console.WriteLine(ibd.DropLocation);
Console.ReadLine();
}
}
}
}
So, after digging through TFS API, I ended up with this workaround.
private void MenuItemCallback(object sender, EventArgs e)
{
var context = (ITeamFoundationContextManager)ServiceProvider.GetService(typeof(ITeamFoundationContextManager));
IBuildServer buildServer = context.CurrentContext.TeamProjectCollection.GetService<IBuildServer>();
var teamExplorer = (ITeamExplorer)ServiceProvider.GetService(typeof(ITeamExplorer));
var buildsPageExt = (IBuildsPageExt)teamExplorer.CurrentPage.GetExtensibilityService(typeof(IBuildsPageExt));
var menuCommand = (MenuCommand)sender;
if (menuCommand.CommandID.Guid == CommandSetCompleted)
{
foreach (var buildDetail in buildsPageExt.SelectedBuilds)
Process.Start("explorer.exe", GetBuild(buildServer, buildDetail).DropLocation);
}
if (menuCommand.CommandID.Guid == CommandSetFavorite)
{
var definitions = buildsPageExt.SelectedFavoriteDefinitions.Concat(buildsPageExt.SelectedXamlDefinitions).ToArray();
foreach (var build in GetLatestSuccessfulBuild(buildServer, definitions))
Process.Start("explorer.exe", build.DropLocation);
}
}
private IBuildDetail GetBuild(IBuildServer buildServer, IBuildModel buildModel)
{
Uri buildUri = new Uri(buildModel.GetType().GetProperty("UriToOpen").GetValue(buildModel).ToString());
return buildServer.GetBuild(buildUri);
}
private IBuildDetail[] GetLatestSuccessfulBuild(IBuildServer buildServer, IDefinitionModel[] buildDefinitions)
{
var spec = buildServer.CreateBuildDetailSpec(buildDefinitions.Select(bd => bd.Uri));
spec.MaxBuildsPerDefinition = 1;
spec.QueryOrder = BuildQueryOrder.FinishTimeDescending;
spec.Status = BuildStatus.Succeeded;
var builds = buildServer.QueryBuilds(spec);
return builds.Builds;
}