Skip to content

Instantly share code, notes, and snippets.

@bzbetty
Created December 19, 2012 20:10
Show Gist options
  • Save bzbetty/4340037 to your computer and use it in GitHub Desktop.
Save bzbetty/4340037 to your computer and use it in GitHub Desktop.
public class CreateDefaultQueriesJob : ITeamFoundationJobExtension
{
public TeamFoundationJobExecutionResult Run(TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage)
{
resultMessage = "";
try
{
TeamFoundationLocationService service = requestContext.GetService<TeamFoundationLocationService>();
Uri selfReferenceUri = service.GetSelfReferenceUri(requestContext, service.GetDefaultAccessMapping(requestContext));
TfsTeamProjectCollection tfsTeamProjectCollection = new TfsTeamProjectCollection(selfReferenceUri);
var workitemStore = tfsTeamProjectCollection.GetService<WorkItemStore>();
var jobDataXmlNode = jobDefinition.Data;
// Expects node like <WorkItem>31</WorkItem>
var areaPath = (from wi in XDocument.Parse(jobDataXmlNode.OuterXml).Elements() select wi.Value).First();
var nodes = areaPath.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
var queryHierarchy = workitemStore.Projects[nodes[0]].QueryHierarchy;
QueryFolder currentLocation = queryHierarchy.FirstOrDefault(i => i.Name == "Shared Queries") as QueryFolder;
foreach (var path in nodes.Skip(2))
{
if (currentLocation.FirstOrDefault(i => i.Name == path) as QueryFolder != null)
{
currentLocation = currentLocation.FirstOrDefault(i => i.Name == path) as QueryFolder;
}
else
{
var folder = new QueryFolder(path);
currentLocation.Add(folder);
currentLocation = folder;
queryHierarchy.Save();
}
}
queryHierarchy.Project.Store.RefreshCache();
if (currentLocation.FirstOrDefault(i => i.Name == "Open Bugs") == null)
{
var query = new QueryDefinition("Open Bugs", string.Format("SELECT [System.Id], [System.Title], [System.AssignedTo], [System.State], [Microsoft.VSTS.Common.Priority] FROM WorkItems WHERE [System.TeamProject] = @project and [System.WorkItemType] = 'Bug' and [System.State] = 'Open' and [System.AreaPath] under '{0}' ORDER BY [System.Id]", areaPath.Substring(1).Replace("\\Area", "")));
currentLocation.Add(query);
}
if (currentLocation.FirstOrDefault(i => i.Name == "Closed Bugs") == null)
{
var query = new QueryDefinition("Closed Bugs", string.Format("SELECT [System.Id], [System.Title], [System.AssignedTo], [System.State], [Microsoft.VSTS.Common.Priority] FROM WorkItems WHERE [System.TeamProject] = @project and [System.WorkItemType] = 'Bug' and [System.State] = 'Closed' and [System.AreaPath] under '{0}' ORDER BY [System.Id]", areaPath.Substring(1).Replace("\\Area", "")));
currentLocation.Add(query);
}
queryHierarchy.Save();
}
catch (RequestCanceledException)
{
return TeamFoundationJobExecutionResult.Stopped;
}
catch (Exception exception)
{
resultMessage = exception.ToString();
return TeamFoundationJobExecutionResult.Failed;
}
return TeamFoundationJobExecutionResult.Succeeded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment