SprintDocumentation

Code Snippet
  1. using Microsoft.TeamFoundation.Client;
  2. using Microsoft.TeamFoundation.Framework.Client;
  3. using Microsoft.TeamFoundation.Framework.Common;
  4. using Microsoft.TeamFoundation.ProcessConfiguration.Client;
  5. using Microsoft.TeamFoundation.Server;
  6. using Microsoft.TeamFoundation.WorkItemTracking.Client;
  7. using RazorTemplateingSample.RazorTemplating;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Xml;
  14. namespace Centric.MVV.Tools.Reporting
  15. {
  16.     public class SprintDocumentation
  17.     {
  18.         private readonly TfsTeamProjectCollection tfsServer;
  19.         private readonly WorkItemStore tfsStore;
  20.         private readonly Project tfsSProject;
  21.         private TfsTeamService teamService;
  22.         private TswaClientHyperlinkService hyperLinkService;
  23.         private List<TeamFoundationTeam> allTeams;
  24.         public delegate void DoLog(String aMessage, bool isError);
  25.         public event DoLog Log;
  26.         private string templatesPath;
  27.         private void OnLog(String aMessage, bool isError)
  28.         {
  29.             if (Log != null)
  30.                 Log(aMessage, isError);
  31.         }
  32.         //public string[] ValidApplications { get; set; }
  33.         private IRazorTemplateGenerator generator;
  34.         public SprintDocumentation(string server, string project, string templatesPath)
  35.         {
  36.             tfsServer = new TfsTeamProjectCollection(new Uri(server));
  37.             tfsStore = (WorkItemStore)tfsServer.GetService(typeof(WorkItemStore));
  38.             tfsSProject = tfsStore.Projects[project];
  39.             hyperLinkService = tfsServer.GetService<TswaClientHyperlinkService>();
  40.             teamService = tfsServer.GetService<TfsTeamService>();
  41.             this.templatesPath = templatesPath;
  42.         }
  43.         public List<Team> GetTeams()
  44.         {
  45.             List<Team> teams = new List<Team>();
  46.             allTeams = teamService.QueryTeams(tfsSProject.Uri.AbsoluteUri) as List<TeamFoundationTeam>;
  47.             allTeams.ForEach(t => teams.Add(new Team { Name = t.Name }));
  48.             return teams;
  49.         }
  50.         public List<TeamMember> GetTeamMembers(string teamName)
  51.         {
  52.             List<TeamFoundationTeam> allTeams = teamService.QueryTeams(tfsSProject.Uri.AbsoluteUri) as List<TeamFoundationTeam>;
  53.             var team = allTeams.Find(t => t.Name.Contains(teamName));
  54.             TeamFoundationIdentity[] tfsMembers = team.GetMembers(tfsServer, MembershipQuery.Direct);
  55.             List<TeamMember> members = new List<TeamMember>();
  56.             for (int i = 0; i < tfsMembers.Length; i++)
  57.             {
  58.                 members.Add(new TeamMember { Name = tfsMembers[i].DisplayName });
  59.             }
  60.             return members;
  61.         }
  62.         public List<WorkItemInfo> GetIterationWorkItems(string iteration, bool getChildren)
  63.         {
  64.             var workItems = GetWorkItemInfoListForIteration(iteration);
  65.             List<WorkItemInfo> wis = new List<WorkItemInfo>();
  66.             for (int i = 0; i < workItems.Count; i++)
  67.             {
  68.                 WorkItem wi = workItems[i];
  69.                 WorkItemInfo wii = ProcessWorkItem(wi, getChildren);
  70.                 if (wii.Type != WorkItemType.Task)
  71.                 {
  72.                     wis.Add(wii);
  73.                 }
  74.             }
  75.             wis.Sort((x, y) => x.Priority.CompareTo(y.Priority));
  76.             return wis;
  77.         }
  78.         private WorkItemCollection GetWorkItemInfoListForIteration(string iterationPath)
  79.         {
  80.             string select = String.Format(„SELECT System.ID FROM workitems WHERE [Iteration Path] = ‘” + iterationPath + „‘”);
  81.             return tfsStore.Query(select);
  82.         }
  83.         private int ProcessWorkItemField(WorkItem wi, int fieldID, bool original, int defaultValue)
  84.         {
  85.             var field = wi.Fields.TryGetById(fieldID);
  86.             if (field != null)
  87.             {
  88.                 if (original)
  89.                 {
  90.                     return field.OriginalValue == null ? defaultValue : Int32.Parse(field.OriginalValue.ToString());
  91.                 }
  92.                 else
  93.                 {
  94.                     return field.Value == null ? defaultValue : Int32.Parse(field.Value.ToString());
  95.                 }
  96.             }
  97.             return defaultValue;
  98.         }
  99.         private string ProcessWorkItemField(WorkItem wi, int fieldID, bool original, string defaultValue)
  100.         {
  101.             var field = wi.Fields.TryGetById(fieldID);
  102.             if (field != null)
  103.             {
  104.                 if (original)
  105.                 {
  106.                     return field.OriginalValue == null ? defaultValue : field.OriginalValue.ToString();
  107.                 }
  108.                 else
  109.                 {
  110.                     return field.Value == null ? defaultValue : field.Value.ToString();
  111.                 }
  112.             }
  113.             return defaultValue;
  114.         }
  115.         private string GetWorkItemDescription(WorkItem wi, WorkItemType type)
  116.         {
  117.             string desc = String.Empty;
  118.             switch (type)
  119.             {
  120.                 case WorkItemType.Bug: desc = wi.Fields.TryGetById(10191).Value.ToString(); break;
  121.                 case WorkItemType.ChangeRequest: desc = wi.Fields.TryGetById(52).Value.ToString(); break;
  122.                 case WorkItemType.ProductBacklogItem:
  123.                 case WorkItemType.Task:
  124.                 case WorkItemType.TestCase:
  125.                 default: desc = wi.Fields.TryGetById(10212).Value.ToString(); break;
  126.             }
  127.             return desc;
  128.         }
  129.         private WorkItemInfo ProcessWorkItem(WorkItem wi, bool getChildren)
  130.         {
  131.             WorkItemInfo wii = new WorkItemInfo();
  132.             WorkItemType type = (WorkItemType)Enum.Parse(typeof(WorkItemType), wi.Type.Name.Replace(” „, String.Empty));
  133.             wii.ID = wi.Id;
  134.             wii.Type = type;
  135.             wii.TFSUrl = hyperLinkService.GetWorkItemEditorUrl(wii.ID).AbsoluteUri;
  136.             wii.Title = wi.Title;
  137.             wii.Description = GetWorkItemDescription(wi, type);
  138.             wii.CreatedBy = wi.CreatedBy;
  139.             wii.CreatedDate = wi.CreatedDate;
  140.             wii.AssignedTo = ProcessWorkItemField(wi, 24, false, String.Empty);
  141.             wii.Effort = ProcessWorkItemField(wi, 10213, false, 0);
  142.             wii.OriginalEffort = ProcessWorkItemField(wi, 10213, true, 0);
  143.             wii.Priority = ProcessWorkItemField(wi, 10211, false, 0);
  144.             wii.OriginalPriority = ProcessWorkItemField(wi, 10211, true, 0);
  145.             wii.AcceptanceCriteria = ProcessWorkItemField(wi, 10214, false, String.Empty);
  146.             wii.State = wi.State;
  147.             if (wi.Rev > 0)
  148.             {
  149.                 wii.WorkedOn = new List<string>();
  150.                 foreach (Revision revision in wi.Revisions)
  151.                 {
  152.                     string val = revision.Fields[„Assigned To”].Value.ToString();
  153.                     if (!String.IsNullOrEmpty(val) && !wii.WorkedOn.Contains(val))
  154.                     {
  155.                         wii.WorkedOn.Add(val);
  156.                     }
  157.                 }
  158.             }
  159.             if (getChildren)
  160.             {
  161.                 if (wi.WorkItemLinks.Count > 0)
  162.                 {
  163.                     wii.ChildWorkItems = new List<WorkItemInfo>();
  164.                 }
  165.                 foreach (WorkItemLink link in wi.WorkItemLinks)
  166.                 {
  167.                     if (link.BaseType == BaseLinkType.WorkItemLink && link.LinkTypeEnd.Name == „Child”)
  168.                     {
  169.                         WorkItem linkedWi = tfsStore.GetWorkItem(link.TargetId);
  170.                         wii.ChildWorkItems.Add(ProcessWorkItem(linkedWi, getChildren));
  171.                     }
  172.                 }
  173.             }
  174.             return wii;
  175.         }
  176.         public ProjectIterations GetAllIterations()
  177.         {
  178.             ProjectIterations result = new ProjectIterations();
  179.             var configSvc = tfsServer.GetService<TeamSettingsConfigurationService>();
  180.             var configs = configSvc.GetTeamConfigurationsForUser(new[] { tfsSProject.Uri.ToString() });
  181.             ICommonStructureService4 css = tfsServer.GetService<ICommonStructureService4>();
  182.             NodeInfo[] structures = css.ListStructures(tfsSProject.Uri.ToString());
  183.             NodeInfo iterations = structures[0];
  184.             XmlElement iterationTree = css.GetNodesXml(new[] { iterations.Uri }, true);
  185.             BuildIterations(iterationTree.ChildNodes, css, ref result);
  186.             result.Sort((x, y) => y.StartDate.Value.CompareTo(x.StartDate.Value));
  187.             return result;
  188.         }
  189.         private void BuildIterations(XmlNodeList iterationsTree, ICommonStructureService4 css, ref ProjectIterations iterations)
  190.         {
  191.             foreach (XmlNode item in iterationsTree)
  192.             {
  193.                 if (item.OuterXml.Contains(„YOUR_TEAM_NAME”))
  194.                 {
  195.                     var nodeId = GetNodeID(item.OuterXml);
  196.                     var nodeInfo = css.GetNode(nodeId);
  197.                     if (nodeInfo.StartDate != null && nodeInfo.FinishDate != null)
  198.                     {
  199.                         ProjectIteration pi = new ProjectIteration();
  200.                         pi.Name = nodeInfo.Name;
  201.                         pi.StartDate = nodeInfo.StartDate;
  202.                         pi.EndDate = nodeInfo.FinishDate;
  203.                         pi.IterationPath = nodeInfo.Path.Replace(„\\Iteration”, „”).TrimStart(‘\\’);
  204.                         iterations.Add(pi);
  205.                     }
  206.                     if (item.ChildNodes.Count > 0)
  207.                     {
  208.                         BuildIterations(item.ChildNodes, css, ref iterations);
  209.                     }
  210.                 }
  211.             }
  212.         }
  213.         private string GetNodeID(string xml)
  214.         {
  215.             var first = „NodeID=\””;
  216.             var start = xml.IndexOf(first) + first.Length;
  217.             var end = xml.IndexOf(„\””, start);
  218.             return xml.Substring(start, (end – start));
  219.         }
  220.         public List<string> GetTemplates()
  221.         {
  222.             List<string> templates = new List<string>();
  223.             FileInfo[] files = new DirectoryInfo(templatesPath).GetFiles(„*.html”);
  224.             foreach (FileInfo file in files)
  225.             {
  226.                 templates.Add(file.Name.Replace(„.html”, String.Empty));
  227.             }
  228.             return templates;
  229.         }
  230.         public void InitTemplates()
  231.         {
  232.             if (generator == null)
  233.             {
  234.                 generator = new RazorTemplateGenerator();
  235.                 FileInfo[] files = new DirectoryInfo(templatesPath).GetFiles(„*.html”);
  236.                 foreach (FileInfo file in files)
  237.                 {
  238.                     string t = file.OpenText().ReadToEnd();
  239.                     generator.RegisterTemplate<SprintDataModel>(file.Name.Replace(„.html”, String.Empty), t);
  240.                 }
  241.                 generator.CompileTemplates();
  242.             }
  243.         }
  244.         public string Generate(string template, SprintDataModel model)
  245.         {
  246.             InitTemplates();
  247.             return generator.GenerateOutput(model, template);
  248.         }
  249.     }
  250. }

One Reply to “SprintDocumentation”

  1. Pingback: How to connect to TFS from C# — Felix Vătuiu

Lasă un răspuns