如何使用TFS对象模型获取与变更集关联的门控构建定义

时间:2013-12-18 00:03:20

标签: tfs tfs2012 tfsbuild

我想了解开发人员是否使用gated构建来签入。 TFS对象模型是否允许您将签入/更改集与构建定义相关联?我最终希望能够说:

Changeset   Gated?   Build defn
-------------------------------
123          0        NULL
456          1       dev-gated-defn

1 个答案:

答案 0 :(得分:2)

您可以使用TFS API获取此信息。下面是一个示例方法,演示如何写出前七天您感兴趣的详细信息(您可以修改MinFinishTime来更改时间段)。

    /// <summary>
    /// Writes out information about whether gated builds are being used.
    /// </summary>
    private static void _GetBuildInsights()
    {
        using (TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false, new UICredentialsProvider()))
        {
            if (tpp.ShowDialog() == DialogResult.OK)
            {
                TfsTeamProjectCollection projectCollection = tpp.SelectedTeamProjectCollection;
                var buildServer = projectCollection.GetService<IBuildServer>();

                var buildSpec = buildServer.CreateBuildDetailSpec(tpp.SelectedProjects[0].Name);
                buildSpec.InformationTypes = null;
                buildSpec.MinFinishTime = DateTime.Now.AddDays(-7);  // get last seven days of builds

                IBuildDetail[] builds = buildServer.QueryBuilds(buildSpec).Builds;

                Console.WriteLine("Changeset   Gated?   Build defn");
                Console.WriteLine("-------------------------------");

                foreach (IBuildDetail build in builds)
                {
                    IBuildDefinition definition = build.BuildDefinition;
                    if (definition != null)
                    {
                        string changeset = build.SourceGetVersion.Replace("C", string.Empty);  // changesets are prefixed with "C"
                        int gated = definition.ContinuousIntegrationType == ContinuousIntegrationType.Gated ? 1 : 0;
                        Console.WriteLine("{0}         {1}        {2}", changeset, gated, definition.Name);
                    }
                }
            }
        }
    }