【问题标题】:Get current iteration from TFS从 TFS 获取当前迭代
【发布时间】:2018-06-01 21:34:38
【问题描述】:

我需要从 TFS 项目中获取当前迭代的路径。我可以使用 REST API 查询 <server>/<project>/_apis/work/teamsettings/iterations?$timeframe=current&api-version=v2.0-preview 但我不想执行查询和解析 JSON 响应。我想在.NET client libraries for VSTS (and TFS) 中使用适当的API。

我有一个VssConnection 的实例。如何从这个对象中获取当前迭代的路径?

【问题讨论】:

    标签: c# .net tfs


    【解决方案1】:

    这里有一个案例提供了解决方案:Get the current iteration path from TFS

     private static XmlNode currentIterationNode;
        TfsTeamProjectCollection tpc = TFSConncetion(@"http://tfs/url");
    
        ICommonStructureService4 css = tpc.GetService<ICommonStructureService4>();;
        WorkItemStore workItemStore = new WorkItemStore(tpc);
    
            foreach (Project teamProject in workItemStore.Projects)
            {
                if (teamProject.Name.Equals("TeamProjectNameGoesHere"))
                {
                    NodeInfo[] structures = css.ListStructures(teamProject.Uri.ToString());
                    NodeInfo iterations = structures.FirstOrDefault(n => n.StructureType.Equals("ProjectLifecycle"));
    
                    if (iterations != null)
                    {
                        XmlElement iterationsTree = css.GetNodesXml(new[] { iterations.Uri }, true);
                        XmlNodeList nodeList = iterationsTree.ChildNodes;
                        currentIterationNode = FindCurrentIteration(nodeList);
                        String currentIterationPath = currentIterationNode.Attributes["Path"].Value;
                    }
                }
            }
    

    【讨论】:

    • 您似乎错过了在代码中包含FindCurrentIteration 方法的定义。
    【解决方案2】:

    我找到了使用VssConnection的解决方案:

    var workClient = connection.GetClient<WorkHttpClient>();
    var iterations = workClient.GetTeamIterationsAsync(new TeamContext("project-name")).Result;
    
    var currentDate = DateTime.Now.Date;
    var currentIterationPath = iterations
        .Select(i => new { i.Path, i.Attributes })
        .FirstOrDefault(i => currentDate >= i.Attributes.StartDate &&
                             currentDate <= i.Attributes.FinishDate)
        ?.Path;
    

    【讨论】:

    • 我认为加载所有迭代路径并遍历它们在大型团队项目中不会有良好的性能。
    【解决方案3】:

    我发现最简单的方法是使用 ICommonStructureService4TeamSettingsConfigurationService 方法:

    static TfsTeamProjectCollection _tfs = TfsTeamProjectCollectionFactory
        .GetTeamProjectCollection("<tfsUri>")
    
    (...)
    
    static string GetCurrentIterationPath()
    {
        var css = _tfs.GetService<ICommonStructureService4>();
    
        var teamProjectName = "<teamProjectName>";
        var project = css.GetProjectFromName(teamProjectName);
    
        var teamName = "<teamName>";
        var teamSettingsStore = 
            _tfs.GetService<TeamSettingsConfigurationService>();
    
        var settings = teamSettingsStore
            .GetTeamConfigurationsForUser(new[] { project.Uri })
            .Where(c => c.TeamName == teamName)
            .FirstOrDefault();
    
        if (settings == null)
        {
            var currentUser = System.Threading.Thread.CurrentPrincipal.Identity.Name;
            throw new InvalidOperationException(
                $"User '{currentUser}' doesn't have access to '{teamName}' team project.");
        }
    
        return settings.TeamSettings.CurrentIterationPath;
    }
    

    并返回 TeamSettings.CurrentIterationPath 属性。

    【讨论】:

      【解决方案4】:

      您可以使用 WorkHttpClient 获取当前迭代,而无需迭代:

      var creds = new VssBasicCredential(string.Empty, "personalaccesstoken");
      VssConnection connection = new VssConnection(new Uri("url"), creds);
      var workClient = connection.GetClient<WorkHttpClient>();
      var teamContext = new TeamContext(teamId);
      teamContext.ProjectId = projectId;
      var currentIteration = await workClient.GetTeamIterationsAsync(teamContext, "current");
      

      【讨论】:

        猜你喜欢
        • 2015-10-03
        • 1970-01-01
        • 2022-11-20
        • 2011-01-06
        • 1970-01-01
        • 2017-01-24
        • 2020-10-02
        • 2017-10-30
        • 2021-05-09
        相关资源
        最近更新 更多