How to get, by code, changeset WorkItems on a branching context

Imagine that we have 3 branches: dev / integration / prod.

We can do many check-ins on the dev branch that we link with some WorkItems.

Then, we can apply some of these changesets on integration branch, which generates a new changeset (on integration branch).

We can repeat this operation many times and then, we can apply some of the integration changesets on the prod branch (which will generate a new changeset on prod branch).

Now, for a changeset id, I want to get all linked workitems directly or indirectly (ie: linked to the integrated previous branch changesets).

How to do it?

I propose the following code:

public static IEnumerable<WorkItem> GetWorkItems(string serverURL, string collectionName, NetworkCredential networkCredential, int changeSetId, params string[] branches)
TfsConfigurationServer tfsConfigurationServer = new TfsConfigurationServer(new Uri(serverURL), networkCredential);
return GetWorkItems(tfsConfigurationServer.GetTeamProjectCollection(new Guid(tfsConfigurationServer.CatalogNode.QueryChildren(new[] { CatalogResourceTypes.ProjectCollection },
false, CatalogQueryOptions.None).FirstOrDefault(cn => cn.Resource.DisplayName == collectionName).Resource.Properties["InstanceId"])).GetService<VersionControlServer>(),
changeSetId, branches).Distinct(); } private static IEnumerable<WorkItem> GetWorkItems(VersionControlServer versionControlServer, int changeSetId, IEnumerable<string> branches) {
foreach (WorkItem wi in versionControlServer.GetChangeset(changeSetId).WorkItems)
yield return wi;
string[] subBranches = branches.Take(2).ToArray();
if (subBranches.Length == 2)
foreach (WorkItem wi in versionControlServer.QueryMerges(subBranches[1], VersionSpec.Latest, subBranches[0], VersionSpec.Latest, new ChangesetVersionSpec(changeSetId),
new ChangesetVersionSpec(changeSetId), RecursionType.Full).SelectMany(cm => GetWorkItems(versionControlServer, cm.SourceVersion, branches.Skip(1))))
yield return wi; }

Enjoy Sourire

This entry was posted in 15948, 7671. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *