Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Created May 15, 2014 14:36
Show Gist options
  • Save sjwaight/ab0b8bc9219692743bc6 to your computer and use it in GitHub Desktop.
Save sjwaight/ab0b8bc9219692743bc6 to your computer and use it in GitHub Desktop.
// utilising RestSharp and Json.NET - get via Nuget.
private WorkItem GetWorkItem(string workItemIdentifier)
{
var restClient = new RestClient("https://account.visualstudio.com/defaultcollection");
restClient.Authenticator = new HttpBasicAuthenticator("username", "password");
// code removed here
var changes = GetWorkItemHistoryComments(restClient, workItemIdentifier);
// code removed here
}
private IEnumerable<WorkItemCommentChange> GetWorkItemHistoryComments(RestClient activeClient, string workItemIdentifier)
{
var request = new RestRequest("/_apis/wit/workitems/{id}/updates");
request.AddParameter("api-version", "1.0-preview");
request.AddUrlSegment("id", workItemIdentifier);
var response = activeClient.Execute(request);
dynamic json = JObject.Parse(response.Content);
var changes = new List<WorkItemCommentChange>();
string revisionDate = string.Empty;
string revisedBy = string.Empty;
string comments = string.Empty;
// each one of these represents a revision
foreach(var val in json.value)
{
// a revision contains multiple fields
foreach(var ufield in val.fields)
{
if (ufield.field.id == -4) // System.ChangedDate
{
revisionDate = ufield.updatedValue; // "02/01/2014 01:21:00" (MM/dd/yyyy and UTC)
}
if (ufield.field.id == 9) // System.ChangedBy
{
revisedBy = ufield.updatedValue; // "Simon Waight"
}
if(ufield.field.id == 54) // System.History
{
comments = ufield.updatedValue; // can be plain text or HTML
}
}
// we only care about comments
if(!string.IsNullOrEmpty(comments))
{
changes.Add(new WorkItemCommentChange
{
ChangeDate = DateTime.ParseExact(revisionDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal),
ChangedBy = revisedBy,
CommentText = comments
});
comments = revisedBy = revisionDate = string.Empty;
}
}
return changes;
}
public class WorkItemCommentChange
{
public DateTime ChangeDate { get; set; }
public string ChangedBy { get; set; }
public string CommentText { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment