从TFS

时间:2017-12-19 12:33:08

标签: c# .net tfs

我需要从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的实例。如何从此对象获取当前迭代的路径?

4 个答案:

答案 0 :(得分:2)

您可以使用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");

答案 1 :(得分:1)

我发现这样做的最简单方法是使用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属性。

答案 2 :(得分:0)

我找到了使用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 :(得分:-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;
                }
            }
        }