Skip to content

Instantly share code, notes, and snippets.

@mika76
Last active August 29, 2015 14:07
Show Gist options
  • Save mika76/d2b6defbf20d7cfc7800 to your computer and use it in GitHub Desktop.
Save mika76/d2b6defbf20d7cfc7800 to your computer and use it in GitHub Desktop.
TFS Changeset list
// Easiest in Linqpad
// Add References:
//Microsoft.TeamFoundation.Client
//Microsoft.TeamFoundation.Common
//Microsoft.TeamFoundation.VersionControl.Client
//Microsoft.TeamFoundation.WorkItemTracking.Client
// Merged some stuff from http://pascallaurin42.blogspot.com/2012/07/tfs-queries-generating-changelog-from.html
// and http://stackoverflow.com/questions/8429040/tfs-2010-how-to-produce-a-changelog-ie-list-of-work-items-between-two-releas
var tfsServer = new Uri("http://server:8080/tfs");
var branch = "$/System/Main";
var fromVersion = new ChangesetVersionSpec(4372);
var toVersion = VersionSpec.Latest;
//----------------------
var tfs = TfsTeamProjectCollectionFactory
.GetTeamProjectCollection(tfsServer);
tfs.EnsureAuthenticated();
var versionControl = tfs.GetService<VersionControlServer>();
var history = versionControl.QueryHistory(branch, VersionSpec.Latest, 0,
RecursionType.Full, null, fromVersion, toVersion, int.MaxValue, false,
false, false);
// Merged items
var mergedHistory = versionControl.GetItems(branch, RecursionType.Full).Items
.Select(x => versionControl.QueryMergesWithDetails(
null,
null,
0,
x.ServerItem,
VersionSpec.Latest,
0,
fromVersion,
toVersion,
RecursionType.Full
))
.SelectMany(x => x.Changesets);
var union = history.OfType<Changeset>().Union(mergedHistory);
// changesets
union
.Select(x => new
{
x.ChangesetId,
x.CreationDate,
x.Committer,
x.Comment,
WorkItems = x.WorkItems.Select(wi => new
{
wi.Id,
wi.Title,
wi.Description,
wi.State,
wi.Reason
})
})
.Dump("Branch history of: " + branch +
" from " + fromVersion.DisplayString + " to " + toVersion.DisplayString);
// workitems
union
//.OrderBy(x => x.ChangesetId)
.SelectMany(x => x.WorkItems)
.OrderBy(x => x.Id)
.Select(wi => string.Format("[{0}] {1} #{2} - {3}\r\n{4}",
wi.Reason, wi.Type.Name, wi.Id, wi.Title, wi.Description))
.GroupBy(x => x)
.Select(g => g.First())
.Dump("Changelog of branch: " + branch +
" from " + fromVersion.DisplayString + " to " + toVersion.DisplayString);
@mika76
Copy link
Author

mika76 commented Mar 7, 2015

Hey, sorry didn't see your comment till now.

I found showing branch and merge history very hard as well. I think it also depends on what exactly you would like to show. Here I was wanting only work items linked to changesets, and I get those through merge history as well.

@ktsjackson
Copy link

I'm using a different chunk of code and I'm having trouble at this point: tfs.EnsureAuthenticated();

I get the error below.

TeamFoundationServiceUnavailableException: TF31002: Unable to connect to this Team Foundation Server: http://optimus:8080/tfs.
Team Foundation Server Url: http://optimus:8080/tfs.

Possible reasons for failure include:

  • The name, port number, or protocol for the Team Foundation Server is incorrect.
  • The Team Foundation Server is offline.
  • The password has expired or is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment