public TestOccurrencesCollection Diff(TestOccurrencesCollection other)
        {
            var failed  = other.Failed.Except(this.Failed, new TestOccurrenceComparerByName());
            var success = other.Success.Except(this.Success, new TestOccurrenceComparerByName());

            return(new TestOccurrencesCollection(failed.Union(success)));
        }
 public bool EqualsByFailed(TestOccurrencesCollection other)
 {
     return(this.Failed.Select(x => x.Name).SequenceEqual(other.Failed.Select(x => x.Name)));
 }
Example #3
0
        public void CheckBuild(Action <BuildResult> sendMessage)
        {
            var build = _client.Builds.ByBuildLocator(_buildLocator).FirstOrDefault();

            if (build == null)
            {
                Console.WriteLine("No builds found by given locator");
            }
            else if (build.Id == _lastCheckedBuildId)
            {
                Console.WriteLine("Build {0} - already checked last time", build.Id);
            }
            else
            {
                Console.WriteLine("Found new build {0}: {1}", build.Number, build.Status);

                var changes = _client.Changes.ByLocator(
                    ChangeLocator.WithBuildId(long.Parse(build.Id)))
                              .FirstOrDefault();
                var author = changes != null ? changes.Username : "******";

                if (build.Status != "SUCCESS")
                {
                    var exactBuild = _client.Builds.ByBuildId(build.Id);
                    var reason     = exactBuild.StatusText;

                    var testOccurrences =
                        new TestOccurrencesCollection(_client.TestOccurrences.ByBuildId(build.Id, 1500));

                    var now = DateTimeProvider.UtcNow;
                    if ((!_lastFailedTime.HasValue ||
                         (now - _lastFailedTime.Value) >= _timeConfig.StillBrokenDelay)
                        //||
                        //(String.IsNullOrEmpty(_lastReason) || _lastReason != reason)
                        //||
                        /*(_lastTimeTests == null || !testOccurrences.EqualsByFailed(_lastTimeTests))*/)
                    {
                        _lastCheckedBuildId = build.Id;
                        var failReason = GetReason(reason);

                        BuildResult buildResult;
                        if (!_wasBroken)
                        {
                            string detailedReason;

                            if (failReason == FailReason.Tests)
                            {
                                detailedReason = testOccurrences.Show();
                                _lastTimeTests = testOccurrences;
                            }
                            else
                            {
                                detailedReason = reason; //TODO: get error message from build log
                            }

                            _lastBastard = author;
                            _wasBroken   = true;

                            buildResult = new BuildResult
                            {
                                Number         = build.Number,
                                Branch         = _name,
                                Author         = _lastBastard,
                                ReasonText     = reason,
                                WebUrl         = build.WebUrl,
                                DetailedReason = detailedReason,
                                Status         = BuildStatus.Broken
                            };
                        }
                        else
                        {
                            var detailedReason = "";

                            if (failReason == FailReason.Tests)
                            {
                                var diff = _lastTimeTests.Diff(testOccurrences);
                                detailedReason += diff.Show(true, "New broken tests", true, "Fixed tests");

                                _lastTimeTests = testOccurrences;
                            }
                            else
                            {
                                detailedReason = reason;
                            }

                            buildResult = new BuildResult
                            {
                                Number         = build.Number,
                                Branch         = _name,
                                Author         = _lastBastard,
                                ReasonText     = reason,
                                WebUrl         = build.WebUrl,
                                DetailedReason = detailedReason,
                                Status         = BuildStatus.StillBroken
                            };
                        }

                        _lastFailedTime = now;
                        _lastReason     = reason;
                        if (!_muted)
                        {
                            sendMessage(buildResult);
                        }
                    }
                    else
                    {
                        Console.WriteLine(
                            "Build is broken but it's too early to notify: lastFailedTime={0}, now={1}, stillBrokenDelay={2}",
                            _lastFailedTime.Value.ToString("hh:mm:ss.ffff"),
                            now.ToString("hh:mm:ss.ffff"),
                            _timeConfig.StillBrokenDelay.TotalMilliseconds);
                    }
                }
                else
                {
                    if (_wasBroken)
                    {
                        var result = new BuildResult
                        {
                            Number = build.Number,
                            Branch = _name,
                            Author = author,
                            Status = BuildStatus.Fixed
                        };
                        sendMessage(result);
                        _wasBroken = false;
                    }
                    _lastFailedTime = null;
                    _lastReason     = null;
                    _wasBroken      = false;
                    _lastTimeTests  = null;
                }
            }
        }