public void Run()
    {
        var client = new TeamCity(Constants.BuildServerRoot);

        var projects = client.ListProjects();

        var projectWithFailures = projects.SelectMany(p => client.ListCurrentBuilds(p.Id, new[]
        {
            "master",
            "develop"
        }))
            .Where(b => b.Status == BuildStatus.Failed)
            .GroupBy(b => b.Project)
            .ToList();

        Console.Out.WriteLine("Currently failed builds");
        foreach (var project in projectWithFailures)
        {
            Console.Out.WriteLine("");
            Console.Out.WriteLine("### " + project.Key.Name);
            Console.Out.WriteLine("");
            var buildTypes = project.GroupBy(b => b.BuildType);

            foreach (var buildType in buildTypes)
            {
                Console.Out.WriteLine("* `" + buildType.Key + "`");

                foreach (var failedBuild in buildType)
                {
                    Console.Out.WriteLine("     - [{0} - {1}]({2})", failedBuild.Number, failedBuild.Branch, failedBuild.Url);
                }
            }
        }
    }
Ejemplo n.º 2
0
        public override async Task Execute(string[] parameters, IResponse response)
        {
            var projectName = parameters[1];

            IEnumerable<Project> projects;
            var client = new TeamCity(Constants.BuildServerRoot);
            var allProjects = client.ListProjects();

            var displaySuccessMessage = true;

            switch (projectName)
            {
                case "all projects":
                    projects = allProjects;
                    displaySuccessMessage = false;
                    break;

                case "my repos":
                    var username = response.User.Name;

                    var repoNames = Brain.Get<AvailableRepositories>()
                        .Where(r => r.Caretaker == username)
                        .Select(r => r.Name)
                        .ToList();

                    projects = allProjects.Where(p => repoNames.Any(r => r == p.Name));
                    break;

                default:
                    var project = allProjects.FirstOrDefault(p => p.Name == projectName);

                    if (project == null)
                    {
                        await response.Send($"No build with name `{projectName}` could be found on the buildserver");
                        return;
                    }
                    projects = new[]
                    {
                        project
                    };
                    break;
            }

            var projectWithFailures = projects.SelectMany(p => client.ListCurrentBuilds(p.Id, new[]
            {
                "master",
                "develop"
            }))
                .Where(b => b.Status == BuildStatus.Failed)
                .GroupBy(b => b.Project)
                .ToList();

            foreach (var project in projectWithFailures)
            {
                var sb = new StringBuilder();

                sb.AppendLine("*" + project.Key.Name + "*");
                var buildTypes = project.GroupBy(b => b.BuildType);

                foreach (var buildType in buildTypes)
                {
                    sb.AppendLine("* `" + buildType.Key + "`");

                    foreach (var failedBuild in buildType)
                    {
                        sb.AppendLine($"     - {failedBuild.Number}({failedBuild.Branch}) {failedBuild.Url}");
                    }
                }

                await response.Send(sb.ToString());
            }

            if (projectWithFailures.Count > 0)
            {
                var totalDownTimeT = projectWithFailures.SelectMany(p => p.Select(bt => DateTime.Now - client.GetBuild(bt.Id).FinishedAt)).ToList();

                var totalDownTime = totalDownTimeT.Sum(ts => ts.TotalDays);

                await response.Send($"Summary: {projectWithFailures.SelectMany(p => p).Count()} failed builds, Total down time: {totalDownTime} days");
            }

            if (projectWithFailures.Count == 0 && displaySuccessMessage)
            {
                await response.Send("All builds green for: " + string.Join(",", projects.Select(p => p.Name)));
            }
        }