在TFS二次開發中,咱們可能會根據某一些狀況對各個項目的PBI、BUG等工做項進行統計。在本文中將大略講解若是進行這些數據統計。css
一:鏈接TFS服務器,而且獲得以後須要使用到的類方法。node
/// <summary> /// tfs的 /// </summary> private TfsTeamProjectCollection server; private WorkItemStore workstore; private TeamSettingsConfigurationService configSvc; private TfsTeamService teamService; public String TfsUri { get; set; } /// <summary> /// 初始化TFSServer /// </summary> /// <param name="model"></param> public TFSServerDto(string tfsUri) { this.TfsUri = tfsUri; Uri uri = new Uri(TfsUri); server = new TfsTeamProjectCollection(uri); workstore = (WorkItemStore)server.GetService(typeof(WorkItemStore)); configSvc = server.GetService<TeamSettingsConfigurationService>(); teamService = server.GetService<TfsTeamService>(); }
二:獲取到本TFS Server 指定團隊的全部項目sql
/// <summary> /// 獲取項目集合 /// </summary> /// <returns></returns> public ProjectCollection GetProjectList() { return workstore.Projects; } public Project GetProject(int projectId) { return workstore.Projects.GetById(projectId); }
三:根據工做項類型獲取工做項集合服務器
/// <summary> /// 獲取工做項統計 /// </summary> /// <param name="workitemtype">工做項類型:Bug,Impediment,Product Backlog Item,Task,Task Case</param> /// <returns></returns> public WorkItemCollection GetWorkItemCollection(string workitemtype, string projectName) { WorkItemCollection queryResults = GetWorkItemCollection(workitemtype,projectName, string.Empty); return queryResults; } /// <summary> /// 獲取工做項統計 /// </summary> /// <param name="workitemtype">工做項類型:Bug,Impediment,Product Backlog Item,Task,Task Case等</param> /// <param name="condition">查詢條件:針對Bug類型的 State=‘New,Approved,Committed,Done,Removed' /// 針對Impediment類型的State='Open' /// 針對Product Backlog Item類型的State='New' /// 針對Task類型的 State='To Do' /// 針對Task Case類型的 State='Design' /// <returns></returns> public WorkItemCollection GetWorkItemCollection(string workitemtype,string projectName,string condition) { string sql = @"Select [Title] From WorkItems Where [Work Item Type] = '{0}' and [System.TeamProject] = '{1}' "; if (!string.IsNullOrEmpty(condition)) { sql +=" and "+ condition; } sql = string.Format(sql, workitemtype, projectName); WorkItemCollection queryResults = workstore.Query(sql); return queryResults; }
四:獲取Sprint信息和開始、結束時間ui
/// <summary> /// 獲取項目的Sprint信息 /// </summary> /// <param name="projectUri">project的Uri信息</param> /// <returns></returns> public TeamSettings GetSprintInfo(String projectUri) { TeamFoundationTeam team = teamService.QueryTeams(projectUri).First(); IList<Guid> teamGuids = new List<Guid>() { team.Identity.TeamFoundationId }; TeamConfiguration config = configSvc.GetTeamConfigurations(teamGuids).FirstOrDefault(); var members = team.GetMembers(server, MembershipQuery.Direct); var users = members.Where(m => !m.IsContainer); return config.TeamSettings; } /// <summary> /// 獲取項目Sprint的關鍵信息如開始時間和結束時間 /// </summary> /// <param name="projectUri"></param> /// <returns></returns> public IEnumerable<ScheduleInfo> GetIterationDates(string projectUri) { var css = server.GetService<ICommonStructureService4>(); NodeInfo[] structures = css.ListStructures(projectUri); NodeInfo iterations = structures.FirstOrDefault(n => n.StructureType.Equals("ProjectLifecycle")); List<ScheduleInfo> schedule = null; if (iterations != null) { string projectName = css.GetProject(projectUri).Name; XmlElement iterationsTree = css.GetNodesXml(new[] { iterations.Uri }, true); GetIterationDates(iterationsTree.ChildNodes[0], projectName, ref schedule); } return schedule; } /// <summary> /// 經過解析XML獲得Sprint的開始和結束時間 /// </summary> /// <param name="node"></param> /// <param name="projectName"></param> /// <param name="schedule"></param> private void GetIterationDates(XmlNode node, string projectName, ref List<ScheduleInfo> schedule) { if (schedule == null) schedule = new List<ScheduleInfo>(); if (node != null) { string iterationPath = node.Attributes["Path"].Value; if (!string.IsNullOrEmpty(iterationPath)) { // Attempt to read the start and end dates if they exist. string strStartDate = (node.Attributes["StartDate"] != null) ? node.Attributes["StartDate"].Value : null; string strEndDate = (node.Attributes["FinishDate"] != null) ? node.Attributes["FinishDate"].Value : null; DateTime? startDate = null, endDate = null; if (!string.IsNullOrEmpty(strStartDate) && !string.IsNullOrEmpty(strEndDate)) { bool datesValid = true; // Both dates should be valid. startDate = DateTime.Parse(strStartDate); endDate = DateTime.Parse(strEndDate); schedule.Add(new ScheduleInfo { Path = iterationPath.Replace(string.Concat("\\", projectName, "\\Iteration"), projectName), StartDate = startDate, EndDate = endDate }); } } // Visit any child nodes (sub-iterations). if (node.FirstChild != null) { // The first child node is the <Children> tag, which we'll skip. for (int nChild = 0; nChild < node.ChildNodes[0].ChildNodes.Count; nChild++) GetIterationDates(node.ChildNodes[0].ChildNodes[nChild], projectName, ref schedule); } } }
五:獲取團隊成員名單this
/// <summary> /// 獲取項目的Sprint信息 /// </summary> /// <param name="projectUri">project的Uri信息</param> /// <returns></returns> public IEnumerable<TeamFoundationIdentity> GetMemberInfo(String projectUri) { TeamFoundationTeam team = teamService.QueryTeams(projectUri).First(); var members = team.GetMembers(server, MembershipQuery.Direct); var users = members.Where(m => !m.IsContainer); return users; }