使用 QueryBuilds 从 TFS 查询构建的性能非常低

Querying builds from TFS with QueryBuilds has very low performance

我正在尝试访问本地 TFS 2015 服务器中具有“无限期保留”标志 (BuildDetail.KeepForever 属性) 的每个构建,但 QueryBuilds() 函数花费的时间太长获取所有构建。

TFS 2015 'Retain Indefinitely' Web GUI Menu Option

我真正需要的只是 KeepForeverDropLocation 属性。

我发现使用 IBuildDetailSpec 界面可以获得更高的效率,但我找不到可以让我获得 KeepForever 属性.[=18 的选项=]

我当前的代码片段:

public void BackupOnlyRetainedBuilds(string TeamProjectName, string DestinationPath)
{
    defs[...]

    Uri configurationServerUri = new Uri("http://builder:8080/tfs");
    TfsTeamProjectCollection server = new TfsTeamProjectCollection(configurationServerUri);

    //get builds server
    buildServer = (IBuildServer)server.GetService(typeof(IBuildServer));

    //set up an array of all build definition from a spcific team project.
    IBuildDefinition[] bda = buildServer.QueryBuildDefinitions(TeamProjectName);

    //check each build definition.
    foreach (var buildDefinition in bda)
    {
        //set an array of builds history.
        IBuildDetail[] bha = buildDefinition.QueryBuilds();

        //check each build from build history build details.
        foreach (var buildDetails in bha)
        {
            //check if build is retained.
            if (buildDetails.KeepForever == true)
            {
                string dropLocationPath = buildDetails.DropLocation;

                //check if drop folder exists.
                if (Directory.Exists(dropLocationPath))
                {
                    //create all of the directories.
                    foreach (string dirPath in Directory.GetDirectories(dropLocationPath, "*",
                        SearchOption.AllDirectories))
                        Directory.CreateDirectory(dirPath.Replace(dropLocationPath, DestinationPath));

                    //copy all the files & Replaces any files with the same name.
                    foreach (string newPath in Directory.GetFiles(dropLocationPath, "*.*",
                        SearchOption.AllDirectories))
                        File.Copy(newPath, newPath.Replace(dropLocationPath, DestinationPath), true);
                }
            }
        }
    }
}

你可以告诉 QueryBuilds 不要抓取所有的构建细节,而只抓取你感兴趣的,从我的 tfsbuild.exe 补丁版本中:

buildDetailSpec = this.BuildServer.CreateBuildDetailSpec(...)
buildDetailSpec.QueryDeletedOption = !forDestroy ? QueryDeletedOption.IncludeDeleted : QueryDeletedOption.OnlyDeleted;
buildDetailSpec.InformationTypes = null;
IBuildQueryResult buildQueryResult = this.BuildServer.QueryBuilds(buildDetailSpec);

或者使用 QueryBuildsUri 并传递 (string[]) null, QueryOptions.None:

IBuildDetail[] buildDetailArray = this.BuildServer.QueryBuildsByUri(list2.ToArray(), (string[]) null, QueryOptions.None, QueryDeletedOption.IncludeDeleted);

您可能需要将一组特定的 InformationTypes 传递给它,具体取决于您需要作为备份工具一部分的数据。