How do I get who broke the last build in TFS 2010

十年热恋 提交于 2019-11-28 13:18:52

问题


Hi
Is there a way to query who broke the last build in TFS 2010.
I know that it is possible to subscribe to the build failed event but I would like to query the TFS to get the last build and the state of the build and if it broke, who broke it.

/Jimmy


回答1:


The following code will get you the most recent build. This is TFS2008, but the call should work fine under TFS2010 as well.

    public static IBuildDetail GetMostRecentBuild(TeamFoundationServer tfs, string teamProject, string buildName)
    {
        IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));

        IBuildDetailSpec buildDetailSpec = buildServer.CreateBuildDetailSpec(teamProject, buildName);

        buildDetailSpec.MaxBuildsPerDefinition = 1;
        buildDetailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending;
        buildDetailSpec.Status = BuildStatus.Failed | BuildStatus.PartiallySucceeded | BuildStatus.Stopped | BuildStatus.Succeeded;

        IBuildQueryResult results = buildServer.QueryBuilds(buildDetailSpec);

        if (results.Failures.Length != 0)
        {
            throw new ApplicationException("this needs to go away and be handled more nicely");
        }

        if (results.Builds.Length == 1)
        {
            return results.Builds[0];
        }
        else
        {
            return null;
        }

    }

Trying to see who broke the build isn't going to be that straightforward, though. What you're going to need to do is down through the results.Builds[] array and find the last build that worked. Once you have that, you can query the team project for all changesets that have occurred since the last successful build. The following code will allow you to do that:

    public static List<Changeset> GetChangesetsSinceDate(TeamFoundationServer tfs, DateTime date, string path)
    {
        VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

        VersionSpec versionFrom = GetDateVSpec(date);
        VersionSpec versionTo = GetDateVSpec(DateTime.Now);

        IEnumerable results = vcs.QueryHistory(path, VersionSpec.Latest, 0, RecursionType.Full, "", versionFrom, versionTo, int.MaxValue, false, true);


        List<Changeset> changes = new List<Changeset>();

        foreach (Changeset changeset in results)
        {
            changes.Add(changeset);
        }

        return changes;
    }

    private static VersionSpec GetDateVSpec(DateTime date)
    {
        //Format is Dyyy-MM-ddTHH:mm  example:  D2009-11-16T14:32
        string dateSpec = string.Format("D{0:yyy}-{0:MM}-{0:dd}T{0:HH}:{0:mm}", date);
        return VersionSpec.ParseSingleSpec(dateSpec, "");
    }

This will give you the list of candidate changesets that may have broken the build. These would be the people you would want to talk to.

That's probably as far as you would want to go with this. You could try to do some magic by matching up the failed project in the buildlog to the files in the changesets, but that's going to mean parsing out a potentially large build log file.



来源:https://stackoverflow.com/questions/4990828/how-do-i-get-who-broke-the-last-build-in-tfs-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!