[Edit]
You can get the current build-def via the IServiceProvider. See this answer.
[/Edit]
I'm trying to add a custom editor for one of our TFS Xaml build workflows.
I started by following this excelent tutorial by Ewald Hofman.
Everything worked as expected, when I click on the [...] button for the workflow parameter the credential dialog is displayed.
What I want to do:
I want to display a selectable list/tree of test suites that are available in the TFS team project. Just like in the Default Labs Workflow:
Obviously, for this to work, I have to be able to communicate the the TFS over its .Net-API (Is it possible to do via SOAP/REST?).
But whenI started to alter the example to fit to my scenario, I naivly tried using the IServiceProvider to get ahold of an IBuildService instance.
public override object EditValue(ITypeDescriptorContext context, ServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
IBuildServer bs = (IBuildServer)provider.GetService(typeof(IBuildServer));
...
This approach did not work, bs is null after this statement.
Then I tried to connect to the TestManagement like this:
var pr = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection/ProjectName"));
var TcmService = pr.GetService<ITestManagementService>();
var t = TcmService.GetTeamProject("ProjectName");
But this failed with a HTTP 404 error that is displayed when I click the edit [...] buttton of the workflow parameter. The url is correct, if i open the page in a browser, the Team Webaccess page is displayed.
TL;DR:
I would like to be able to display a list of test suites of the current tfs in a custom worfklow parameter editor, but I am failing to connect to the TFS.
Other approach:
This site (and some others) recommend using builtin editors. Is it possible to use the builtin editor window of the labs workflow paramaters (the one in the first screenshot)?
Thanks in advance.
Appearantly I was just stupid.
var pr = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection/ProjectName"));
The project name must not be in this URL. With the line below it works just fine.
var pr = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection"));
See my followup question
Related
Im trying to hide unnecessary parts from drop-down menu.
To be precise Notifications, Shared Content, My Submissions, My Workflow Tasks etc..
I was able to remove them via
Control panel -> Users -> Roles -> User -> Permissions
Is there way to achieve same result through Hooks or through config file?
Im running docker with liferay/portal:7.3.0-ga1 image and therefore need to be able to achieve this without GUI.
After various research i came up with this:
private void removePermissions() throws PortalException {
List<String> portletList = Arrays.asList("com_liferay_users_admin_web_portlet_MyOrganizationsPortlet",
"com_liferay_my_account_web_portlet_MyAccountPortlet",
"com_liferay_portal_workflow_web_internal_portlet_UserWorkflowPortlet",
"com_liferay_portal_workflow_task_web_portlet_MyWorkflowTaskPortlet",
"com_liferay_notifications_web_portlet_NotificationsPortlet",
"com_liferay_sharing_web_portlet_SharedAssetsPortlet",
"com_liferay_oauth2_provider_web_internal_portlet_OAuth2ConnectedApplicationsPortlet"
);
String primKey = String.valueOf(company.getPrimaryKey());
for (String portlet : portletList) {
ResourcePermissionLocalServiceUtil.removeResourcePermission(
companyId,
portlet,
ResourceConstants.SCOPE_COMPANY,
primKey,
userRole.getRoleId(),
"ACCESS_IN_CONTROL_PANEL");
}
}
companyId, userRole can be achieved via RoleLocalServiceUtil and CompanyLocalServiceUtil. PrimKey is differs based on scope, this blog helped me a lot understanding it. I sniffed names of portlets through developer console when removing permissions through GUI. I haven't found cleaner solution.
I need to retrieve build task name list from VSTS/TFS build. Is there inbuilt method/library available to support this?
I have already noted (as below sample) that we can retrieve same in release.
import ReleaseClient = require("ReleaseManagement/Core/RestClient");
var rc= ReleaseClient.getClient();
release.environments.forEach(function (env) {
rc.getTasks(VSS.getWebContext().project.id, release.id, env.id).then(function(taskList){
...............
......Some code here
});
}
The documentation doesn't show a direct function call to retrieve the tasks. However, there is a contract for a BuildDefinition, which contains a build property, which is an array of BuildDefinitionStep, each of which has a task property that contains things like the name of the task and the inputs.
The getDefinition() function should give you back a build definition you can work with.
import RestClient = require("TFS/Build/RestClient");
// Get an instance of the client
var client = RestClient.getClient();
var myDefinition client.getDefinition(1234);
When we write C# code for a custom TFS plugin, we are capturing the check-in event. How can we retrieve the project name the user is checking in to?
The Team Project name is always the first portion of the path. For example, if a user checks in a file to:
$/ExampleProject/Folder/File.txt
Then the Team Project name is ExampleProject. The TFS SDK has a helper method that will allow you to get the Team Project name given a source control path:
string teamProjectName =
VersionControlPath.GetTeamProjectName("$/ExampleProject/Folder/File.txt");
Note that you can check into multiple team projects at the same time. For example, your pending changes can contain changes to:
$/Project1/Folder/File.txt
$/Project2/Folder/File.txt
In which case, the user is checking into both Project1 and Project2.
When capturing the Checkin-event using a plugin for TFS 2010 and you loop thru all the CheckinNotification properties (notificationEventArgs as CheckinNotification) I get.....
Changeset,
Comment,
ComputerName,
NotificationInfo,
Options,
CheckinNote,
PolicyOverrideInfo,
ChangesetOwnerName,
WorkspaceOwnerName,
WorkspaceName,
CheckinType,
SubmittedItems,
HasAllItems
The submitted items is a collection, just loop thru the collection...
string myitem="";
CheckinNotification data = notificationEventArgs as CheckinNotification;
if (data != null)
{
Type type = data.GetType();
PropertyInfo[] myproperties = type.GetProperties();
if (property.Name == "SubmittedItems")
{
foreach (var checkin in data.SubmittedItems)
myitem = checkin.ToString();
}
}
}
This will give you all the files that have been checked in. This is only partial code, you need to find the example from nielshebling.de titled TFS 2010: Using plugins to register an event
We have a custom build process (not using MS Build) and during that process I am adding a "fake" build to the global builds list. The reason I am doing that is so that you can select the build for a given work item (found in build). We have a custom field, build included, which is intended to show which build that work item was fixed in. I am having trouble figuring out how to update this field programmatically. The idea is I will have a small app that does this that I will call during the build process, finding all work items since the last build, then updating the field for those work items. Any ideas?
Something like this should work for you:
public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate,
string valueToUpdateTo, int workItemID)
{
// Connect to the TFS Server
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
// Connect to the store of work items.
_store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
// Grab the work item we want to update
WorkItem workItem = _store.GetWorkItem(workItemId);
// Open it up for editing. (Sometimes PartialOpen() works too and takes less time.)
workItem.Open();
// Update the field.
workItem.Fields[fieldToUpdate] = valueToUpdateTo;
// Save your changes. If there is a constraint on the field and your value does not
// meet it then this save will fail. (Throw an exception.) I leave that to you to
// deal with as you see fit.
workItem.Save();
}
An example of calling this would be:
UpdateTFSValue("http://tfs2010dev:8080/tfs", "Integration Build", "Build Name", 1234);
The variable fieldToUpdate should be the name of the field, not the refname (ie. Integration Build, not Microsoft.VSTS.Build.IntegrationBuild)
You could probably get away with using PartialOpen(), but I am not sure.
You will probably need to add Microsoft.TeamFoundation.Client to your project. (And maybe Microsoft.TeamFoundation.Common)
This has changed for TFS 2012, basicly you have to add workItem.Fields[fieldToUpdate].Value
Updated Version of what #Vaccano wrote.
public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate,
string valueToUpdateTo, int workItemID)
{
// Connect to the TFS Server
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
// Connect to the store of work items.
_store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
// Grab the work item we want to update
WorkItem workItem = _store.GetWorkItem(workItemId);
// Open it up for editing. (Sometimes PartialOpen() works too and takes less time.)
workItem.Open();
// Update the field.
workItem.Fields[fieldToUpdate].Value = valueToUpdateTo;
// Save your changes. If there is a constraint on the field and your value does not
// meet it then this save will fail. (Throw an exception.) I leave that to you to
// deal with as you see fit.
workItem.Save();
}
I need a list of changesets (or Work Items) that were made betweend builds (I can label builds if its necessary).
I need that list for our test team (and to publish 'changelist').
Is MSBuild task able to retrieve that list and save as file (then I can process that list further.
Or maybe I need to connect to TFS from C# code and retrieve that list myself (I'm familiar with retrieving WorkItems in C#).
I know this thread is a couple of years old, but I found it when trying to accomplish the same thing.
I've been working on this for a couple of days now, and came up with a solution that accomplishes this specific task. (TFS 2010)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Build.Client;
namespace BranchMergeHistoryTest
{
class Program
{
private static Uri tfsUri = new Uri("http://sctf:8080/tfs");
private static TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
static void Main(string[] args)
{
IBuildServer buildServer = tfs.GetService<IBuildServer>();
IBuildDefinition buildDef = buildServer.GetBuildDefinition("Project", "Specific Build");
IOrderedEnumerable<IBuildDetail> builds = buildServer.QueryBuilds(buildDef).OrderByDescending(build => build.LastChangedOn);
/* I had to use some logic to find the last two builds that had actual changesets attached - we have some builds that don't have attached changesets. You may want to do the same. */
IBuildDetail newestBuild = builds.ElementAt(0);
IBuildDetail priorBuild = builds.ElementAt(1);
string newestBuildChangesetId = newestBuild.Information.GetNodesByType("AssociatedChangeset")[0].Fields["ChangesetId"];
string priorBuildChangesetId = priorBuild.Information.GetNodesByType("AssociatedChangeset")[0].Fields["ChangesetId"];
VersionControlServer vcs = tfs.GetService<VersionControlServer>();
const string sourceBranch = #"$SourceBranch-ProbablyHEAD";
const string targetBranch = #"$TargetBranch-ProbablyRelease";
VersionSpec versionFrom = VersionSpec.ParseSingleSpec(newestBuildChangesetId, null);
VersionSpec versionTo = VersionSpec.ParseSingleSpec(priorBuildChangesetId, null);
ChangesetMergeDetails results = vcs.QueryMergesWithDetails(sourceBranch, VersionSpec.Latest, 0, targetBranch,VersionSpec.Latest, 0, versionFrom, versionTo, RecursionType.Full);
foreach(Changeset change in results.Changesets)
{
Changeset details = vcs.GetChangeset(change.ChangesetId);
// extract info about the changeset
}
}
}
}
Hope this helps the next person trying to accomplish the task!
I know this is old post but I have been digging around for how to accomplish this for many hours and I thought someone else might benefit from what I have put together. I am working with TFS 2013 and this was compiled together from several different sources. I know I don't remember them all at this point but the main ones where:
Get Associated Changesets from Build
Queue a Team Build from another and pass parameters
What I was missing from most articles I found on this subject was how to take the build detail and load the associated changesets or work items. The InformationNodeConverters class was the missing key for this and allows you to get other items as well. Once I had this I was able to come up with the following code that is pretty simple.
Note that if you are running this from a post build powershell script you can use the TF_BUILD_BUILDURI variable. I have also included the code that I came up with to take the summary data retrieved and load the actual item.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
namespace Sample
{
class BuildSample
{
public void LoadBuildAssociatedDetails(Uri tpcUri, Uri buildUri)
{
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(tpcUri);
IBuildServer buildServer = collection.GetService<IBuildServer>();
IBuildDetail buildDetail = buildServer.GetAllBuildDetails(buildUri);
List<IChangesetSummary> changeSets = InformationNodeConverters.GetAssociatedChangesets(buildDetail);
VersionControlServer vcs = collection.GetService<VersionControlServer>();
IEnumerable<Changeset> actualChangeSets = changeSets.Select(x => vcs.GetChangeset(x.ChangesetId));
List<IWorkItemSummary> workItems = InformationNodeConverters.GetAssociatedWorkItems(buildDetail);
WorkItemStore wis = collection.GetService<WorkItemStore>();
IEnumerable<WorkItem> actualWorkItems = workItems.Select(x => wis.GetWorkItem(x.WorkItemId));
}
}
}
TFS will automatically produce a list of all change sets and associated work items checked-in between two successful builds. You will find the lists at the end of the build report.
You could set up a build that is used to communicate with the testers. When that build is build successfully the testers could just look at the build report to see what work items and change sets has been committed since the last build.
If you set up an event listener for the build quality property of a build you could send an email alert to the testers when that builds quality filed changes to a specific version.
We have build labels for each build, they are the same as build number, which is the same as the product version number that our QA and Support operate on.
So, this works for us:
tf.exe history <BRANCH> /version:L<BUILD_NUMBER_FROM>~L<BUILD_NUMBER_TO> /recursive /collection:http://<our TFS server>
results look like this:
Changeset User Date Comment
--------- ----------------- ---------- ------------------------------------- ----------------
3722 Sergei Vorobiev 2013-11-16 Merge changeset 3721 from Main
3720 <redacted>
3719 <redacted>
This blog post may be what you are looking for. You basically go through all the links finding ones with a Uri containing 'changeset'. There doesn't seem to be a specific property for this.
Link
(copied from blog in case of rot)
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.VersionControl.Client;
class ChangesetsFromWorkItems
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Error.Write("Usage: ChangesetsFromWorkItems <server> <workitemid> [workitemid...]");
Environment.Exit(1);
}
TeamFoundationServer server = TeamFoundationServerFactory.GetServer(args[0]);
WorkItemStore wiStore = (WorkItemStore)server.GetService(typeof(WorkItemStore));
VersionControlServer vcs = (VersionControlServer) server.GetService(typeof(VersionControlServer));
int workItemId;
for (int i = 1; i < args.Length; i++)
{
if (!int.TryParse(args[i], out workItemId))
{
Console.Error.WriteLine("ignoring unparseable argument {0}", args[i]);
continue;
}
WorkItem workItem = wiStore.GetWorkItem(workItemId);
List<Changeset> associatedChangesets = new List<Changeset>();
foreach (Link link in workItem.Links)
{
ExternalLink extLink = link as ExternalLink;
if (extLink != null)
{
ArtifactId artifact = LinkingUtilities.DecodeUri(extLink.LinkedArtifactUri);
if (String.Equals(artifact.ArtifactType, "Changeset", StringComparison.Ordinal))
{
// Convert the artifact URI to Changeset object.
associatedChangesets.Add(vcs.ArtifactProvider.GetChangeset(new Uri(extLink.LinkedArtifactUri);
}
}
}
// Do something with the changesets. Changes property is an array, each Change
// has an Item object, each Item object has a path, download method, etc.
}
}
}
We do something similar in our TFS Build process. To do this we created a MSBuild custom task in C# that makes the call to TFS for the items. It is pretty straight forward to create the custom tasks.
Here is an article to get you started with writing MSBuild tasks. http://msdn.microsoft.com/en-us/library/t9883dzc.aspx
I assume you already know how to do the calls to TFS based on your question.
I posted a blog article on how to do this here: Getting a List of Changes After a Specified Build/Label from TFS 2013. It provides a quick and concise function for retrieving the list of files that have been changed since a given Build/Label.
Hope that helps!