Пример #1
0
        private Status CreateStatus(JenkinsBuild latestBuild)
        {
            var newStatus = new JenkinsStatus(GetState(latestBuild))
            {
                Name    = $"{latestBuild.DisplayName} ({TimeSpan.FromMilliseconds(latestBuild.Duration):g})",
                Time    = DateTimeOffset.FromUnixTimeMilliseconds(latestBuild.Timestamp).UtcDateTime,
                Details = latestBuild.Description,
            };

            newStatus.BuildNumber       = latestBuild.Number;
            newStatus.DisplayName       = latestBuild.DisplayName;
            newStatus.Building          = latestBuild.Building;
            newStatus.Duration          = new TimeSpan(latestBuild.Duration * TicksInMillisecond);
            newStatus.EstimatedDuration = new TimeSpan(latestBuild.EstimatedDuration * TicksInMillisecond);
            newStatus.Culprits          = latestBuild.Culprits;
            newStatus.Url = JenkinsApi.UrlHelper.BuildUrl(this, newStatus.BuildNumber);

            newStatus.CommittedToThisBuild = latestBuild.ChangeSets?.SelectMany(p => p.ChangeSetItems)
                                             .Select(p => p.Author)
                                             .GroupBy(p => p.FullName)
                                             .Select(p => p.FirstOrDefault())
                                             .ToList();

            return(newStatus);
        }
Пример #2
0
        protected override async Task ExecuteQuery(CancellationToken cancellationToken)
        {
            await base.ExecuteQuery(cancellationToken);

            if (this.PreviousCheckStatus != null && this.CurrentStatus is JenkinsStatus currentStatus && this.Snapshots.Count != 0)
            {
                if (currentStatus.BuildNumber - this.PreviousCheckStatus.BuildNumber <= 1)
                {
                    return;
                }

                log.Error($"It was necessary to reevaluate history of jenkins job {Configuration.GetConfigurationByKey(Connector.Category)?.Value?.Trim()} / {Configuration.Name}, prev build number {this.PreviousCheckStatus.BuildNumber}, current build number {currentStatus.BuildNumber}");
                for (var i = currentStatus.BuildNumber - 1; i > this.PreviousCheckStatus.BuildNumber; i--)
                {
                    var build = await this.api.GetJenkinsBuild(this, i, cancellationToken);

                    if (build != null)
                    {
                        this.AddSnapshot(this.CreateStatus(build));
                    }
                }

                this.PreviousCheckStatus = this.Snapshots.FirstOrDefault()?.Status as JenkinsStatus;
            }
        }
Пример #3
0
        protected override async Task <List <Status> > GetHistory(CancellationToken cancellationToken)
        {
            var builds = await this.api.GetBuilds(this, cancellationToken, 1, MaxSnapshots + 1);

            var statuses = builds.Select(this.CreateStatus).ToList();

            this.PreviousCheckStatus = statuses.OfType <JenkinsStatus>().FirstOrDefault();
            return(statuses);
        }
Пример #4
0
        protected override bool ShouldTakeSnapshot(Status status)
        {
            var shouldTakeSnapshot = base.ShouldTakeSnapshot(status);

            if (shouldTakeSnapshot && status is JenkinsStatus jenkinsStatus)
            {
                this.PreviousCheckStatus = jenkinsStatus;
            }

            return(shouldTakeSnapshot);
        }
Пример #5
0
        protected override async Task ExecuteQuery(CancellationToken cancellationToken, params string[] args)
        {
            var job = await this.api.GetJenkinsJob(this, cancellationToken);

            if (job?.LastBuild?.Number == null)
            {
                return;
            }

            var latestBuild = await this.api.GetJenkinsBuild(this, job.LastBuild.Number, cancellationToken);

            this.CurrentStatus = CreateStatus(latestBuild);
            if (this.Snapshots.Count == 0 && this.MaxSnapshots > 0 && job.FirstBuild.Number < job.LastBuild.Number)
            {
                var startBuildNumber = latestBuild.Building ? latestBuild.Number - 1 : latestBuild.Number;
                var lastHistoryBuild = Math.Max(startBuildNumber - this.MaxSnapshots + 1, 0);
                lastHistoryBuild = Math.Max(lastHistoryBuild, job.FirstBuild.Number);
                log.Debug("Retrieving history from the server for builds {builds}", new { StartBuild = lastHistoryBuild, EndBuild = startBuildNumber - 1 });

                JenkinsStatus buildStatus = null;
                for (int i = lastHistoryBuild; i <= startBuildNumber; i++)
                {
                    buildStatus = CreateStatus(await this.api.GetJenkinsBuild(this, i, cancellationToken));
                    log.Debug("Retrieved status {buildStatus}", buildStatus);
                    this.AddSnapshot(buildStatus);
                }

                this.PreviousCheckStatus = buildStatus;
            }

            if (this.PreviousCheckStatus != null && this.CurrentStatus is JenkinsStatus currentStatus)
            {
                if (currentStatus.BuildNumber - this.PreviousCheckStatus.BuildNumber > 1)
                {
                    var start = this.PreviousCheckStatus.BuildNumber + 1;
                    for (int i = start; i < currentStatus.BuildNumber; i++)
                    {
                        var buildStatus = CreateStatus(await this.api.GetJenkinsBuild(this, i, cancellationToken));
                        if (this.ShouldTakeSnapshot(buildStatus))
                        {
                            this.AddSnapshot(buildStatus);
                        }
                    }
                }
            }
        }
Пример #6
0
        protected override bool ShouldTakeSnapshot(Status status)
        {
            log.Trace("Checking if snapshot should be taken...");
            if (status is JenkinsStatus currentStatus)
            {
                if (this.PreviousCheckStatus != null)
                {
                    if (status.State != ObservationState.Running && (status.State != this.PreviousCheckStatus.State || this.PreviousCheckStatus.BuildNumber != currentStatus.BuildNumber))
                    {
                        log.Debug("Snapshot should be taken. {stat}", new { PreviousCheckStatus = this.PreviousCheckStatus, CurrentStatus = currentStatus });
                        this.PreviousCheckStatus = currentStatus;
                        return(true);
                    }
                }
                else
                {
                    this.PreviousCheckStatus = currentStatus;
                }
            }

            return(false);
        }