Esempio n. 1
0
        private PullRequest BuildPullRequest(TFSAdvanced.Updater.Models.PullRequests.PullRequest x, Build build)
        {
            PullRequest pullRequestDto = new PullRequest
            {
                Id          = x.pullRequestId,
                Title       = x.title,
                Url         = x.remoteUrl,
                CreatedDate = x.creationDate,
                Creator     = new User
                {
                    Name    = x.createdBy.displayName,
                    IconUrl = x.createdBy.imageUrl
                },
                Repository = new Repository
                {
                    Name    = x.repository.name,
                    Url     = x.repository.remoteUrl,
                    Project = new Project
                    {
                        Name = x.repository.project.name,
                        Url  = x.repository.project.remoteUrl
                    }
                },
                MergeStatus        = x.mergeStatus == "conflicts" ? MergeStatus.Failed : MergeStatus.Succeeded,
                IsAutoCompleteSet  = x.completionOptions != null,
                HasEnoughReviewers = x.hasEnoughReviewers,
                AcceptedReviewers  = x.acceptedReviewers,
                RequiredReviewers  = x.requiredReviewers,
                Reviewers          = new List <Reviewer>()
            };

            if (x.reviewers != null)
            {
                foreach (var reviewer in x.reviewers)
                {
                    if (reviewer.isContainer)
                    {
                        continue;
                    }

                    // Only ignore the review of the creator if the vote is approved or noresponse
                    if (reviewer.id == x.createdBy.id && (reviewer.vote == (int)Vote.Approved || reviewer.vote == (int)Vote.NoResponse))
                    {
                        continue;
                    }
                    var reviewerDto = new Reviewer
                    {
                        Name    = reviewer.displayName,
                        IconUrl = reviewer.imageUrl
                    };
                    switch ((Vote)reviewer.vote)
                    {
                    case Vote.Approved:
                        reviewerDto.ReviewStatus = ReviewStatus.Approved;
                        break;

                    case Vote.ApprovedWithSuggestions:
                        reviewerDto.ReviewStatus = ReviewStatus.ApprovedWithSuggestions;
                        break;

                    case Vote.NoResponse:
                        reviewerDto.ReviewStatus = ReviewStatus.NoResponse;
                        break;

                    case Vote.Rejected:
                        reviewerDto.ReviewStatus = ReviewStatus.Rejected;
                        break;

                    case Vote.WaitingForAuthor:
                        reviewerDto.ReviewStatus = ReviewStatus.WaitingForAuthor;
                        break;
                    }
                    pullRequestDto.Reviewers.Add(reviewerDto);
                }
            }

            if (build == null)
            {
                pullRequestDto.BuildStatus = BuildStatus.NoBuild;
            }
            else
            {
                pullRequestDto.buildId     = build.Id;
                pullRequestDto.BuildUrl    = build.Url;
                pullRequestDto.BuildStatus = build.BuildStatus;
            }

            return(pullRequestDto);
        }
        protected override void Update()
        {
            var pullRequestCount = 0;

            Parallel.ForEach(repositoryRepository.GetAll(), new ParallelOptions {
                MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM
            }, repository =>
            {
                var repositoryPullRequests = new ConcurrentBag <PullRequest>();
                if (string.IsNullOrEmpty(repository.PullRequestUrl))
                {
                    return;
                }
                IList <TFSAdvanced.Updater.Models.PullRequests.PullRequest> pullRequests = GetPullRequests(repository);
                if (pullRequests == null)
                {
                    return;
                }
                Parallel.ForEach(pullRequests, new ParallelOptions {
                    MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM
                }, pullRequest =>
                {
                    try
                    {
                        CommitLink commitId = null;
                        if (pullRequest.lastMergeCommit != null)
                        {
                            commitId = pullRequest.lastMergeCommit;
                        }
                        else if (pullRequest.lastMergeSourceCommit != null)
                        {
                            commitId = pullRequest.lastMergeSourceCommit;
                        }

                        if (commitId == null)
                        {
                            Logger.LogWarning($"Unable to get last merge commit for the pullrequest ({pullRequest.pullRequestId}) {pullRequest.description}");
                            return;
                        }

                        if (string.IsNullOrEmpty(commitId.commitId))
                        {
                            Logger.LogWarning($"Unable to get the last commitID for the pull request ({pullRequest.pullRequestId}) {pullRequest.description}");
                            return;
                        }
                        Build build = buildRepository.GetBuildBySourceVersion(repository, commitId.commitId);

                        PullRequest pullRequestDto       = BuildPullRequest(pullRequest, build);
                        pullRequestDto.Repository        = repository;
                        pullRequestDto.Url               = BuildPullRequestUrl(pullRequest, RequestData.BaseAddress);
                        pullRequestDto.RequiredReviewers = repository.MinimumApproverCount;

                        foreach (TFSAdvanced.Updater.Models.PullRequests.Reviewer reviewer in pullRequest.reviewers)
                        {
                            // Container reviewers do not count
                            if (reviewer.isContainer)
                            {
                                continue;
                            }
                            if (reviewer.vote == (int)Vote.Approved)
                            {
                                pullRequestDto.AcceptedReviewers++;
                            }
                        }
                        pullRequestDto.LastUpdated = DateTime.Now;
                        repositoryPullRequests.Add(pullRequestDto);
                        Interlocked.Increment(ref pullRequestCount);
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e, "Error parsing pull request");
                    }
                });
                if (repositoryPullRequests.Any())
                {
                    PullRequestRepository.Update(repositoryPullRequests.ToList());
                }
            });
            updateStatusRepository.UpdateStatus(new UpdateStatus {
                LastUpdate = DateTime.Now, UpdatedRecords = pullRequestCount, UpdaterName = GetType().Name
            });
        }