void ProcessCodeReviewChangedEvent(string message)
        {
            CodeReviewChangeEvent e =
                ParseEvent.Parse <CodeReviewChangeEvent>(message);

            if (!ShouldBeProcessed(
                    e.Repository,
                    e.BranchFullName,
                    mTrunkBotConfig.Repository,
                    mTrunkBotConfig.BranchPrefix))
            {
                return;
            }

            Review review = new Review(
                e.Repository, e.CodeReviewId, e.BranchId, e.CodeReviewStatus, e.CodeReviewTitle);

            if (review.IsDeleted())
            {
                ReviewsStorage.DeleteReview(review, mCodeReviewsTrackedFilePath);

                if (!mTrunkBotConfig.Plastic.IsBranchAttrFilterEnabled)
                {
                    List <Review> remainingBranchReviews = ReviewsStorage.GetBranchReviews(
                        e.Repository, e.BranchId, mCodeReviewsTrackedFilePath);

                    if (remainingBranchReviews != null && remainingBranchReviews.Count > 0)
                    {
                        return;
                    }

                    lock (mSyncLock)
                    {
                        BranchesQueueStorage.RemoveBranch(
                            e.Repository, e.BranchId, mBranchesQueueFilePath);
                    }
                }
                return;
            }

            ReviewsStorage.WriteReview(review, mCodeReviewsTrackedFilePath);

            if (mTrunkBotConfig.Plastic.IsBranchAttrFilterEnabled)
            {
                return;
            }

            lock (mSyncLock)
            {
                EnqueueBranch(
                    mBranchesQueueFilePath,
                    e.Repository,
                    e.BranchId,
                    e.BranchFullName,
                    e.BranchOwner,
                    e.BranchComment);

                Monitor.Pulse(mSyncLock);
            }
        }
        internal void LoadBranchesToProcess()
        {
            mLog.Info("Retrieving branches to process...");

            if (mTrunkBotConfig.Plastic.IsApprovedCodeReviewFilterEnabled)
            {
                List <BranchWithReview> branchesWithReviews =
                    FindQueries.FindPendingBranchesWithReviews(
                        mRestApi,
                        mTrunkBotConfig.Repository,
                        mTrunkBotConfig.BranchPrefix ?? string.Empty,
                        mTrunkBotConfig.Plastic.StatusAttribute.Name,
                        mTrunkBotConfig.Plastic.StatusAttribute.MergedValue);

                HashSet <string> branchIdsProcessed = new HashSet <string>();
                List <Branch>    branchesToEnqueue  = new List <Branch>();

                foreach (BranchWithReview branchWithReview in branchesWithReviews)
                {
                    ReviewsStorage.WriteReview(
                        branchWithReview.Review,
                        mCodeReviewsTrackedFilePath);

                    if (mTrunkBotConfig.Plastic.IsBranchAttrFilterEnabled)
                    {
                        continue;
                    }

                    if (branchIdsProcessed.Contains(branchWithReview.Branch.Id))
                    {
                        continue;
                    }

                    branchIdsProcessed.Add(branchWithReview.Branch.Id);
                    branchesToEnqueue.Add(branchWithReview.Branch);
                }

                BranchesQueueStorage.WriteQueuedBranches(
                    branchesToEnqueue, mBranchesQueueFilePath);
            }

            if (!mTrunkBotConfig.Plastic.IsBranchAttrFilterEnabled)
            {
                return;
            }

            List <Branch> branches = FindQueries.FindResolvedBranches(
                mRestApi,
                mTrunkBotConfig.Repository,
                mTrunkBotConfig.BranchPrefix ?? string.Empty,
                mTrunkBotConfig.Plastic.StatusAttribute.Name,
                mTrunkBotConfig.Plastic.StatusAttribute.ResolvedValue);

            BranchesQueueStorage.WriteQueuedBranches(branches, mBranchesQueueFilePath);
        }
        static void SetBranchReviewsAsPending(
            RestApi restApi,
            string repoName,
            string branchId,
            string codeReviewsStorageFile)
        {
            List <Review> branchReviews = ReviewsStorage.GetBranchReviews(
                repoName, branchId, codeReviewsStorageFile);

            foreach (Review review in branchReviews)
            {
                TrunkMergebotApi.CodeReviews.Update(
                    restApi,
                    repoName,
                    review.ReviewId,
                    Review.PENDING_STATUS_ID,
                    review.ReviewTitle);
            }
        }
示例#4
0
        static bool AreAllCodeReviewsApprovedAtLeastOne(
            string branchRepository, string branchId, string codeReviewsStorageFile)
        {
            List <Review> branchReviews =
                ReviewsStorage.GetBranchReviews(branchRepository, branchId, codeReviewsStorageFile);

            if (branchReviews == null || branchReviews.Count == 0)
            {
                return(false);
            }

            foreach (Review branchReview in branchReviews)
            {
                if (!branchReview.IsApproved())
                {
                    return(false);
                }
            }

            return(true);
        }
        internal static void SetTaskAsMerged(
            RestApi restApi,
            Branch branch,
            string taskNumber,
            string message,
            TrunkBotConfiguration botConfig,
            string codeReviewsStorageFile)
        {
            try
            {
                if (botConfig.Plastic.IsApprovedCodeReviewFilterEnabled)
                {
                    ReviewsStorage.DeleteBranchReviews(
                        branch.Repository, branch.Id, codeReviewsStorageFile);
                }

                TrunkMergebotApi.ChangeBranchAttribute(
                    restApi, branch.Repository, branch.FullName,
                    botConfig.Plastic.StatusAttribute.Name,
                    botConfig.Plastic.StatusAttribute.MergedValue);

                if (taskNumber != null && botConfig.Issues != null)
                {
                    TrunkMergebotApi.Issues.SetIssueField(
                        restApi, botConfig.Issues.Plug, botConfig.Issues.ProjectKey,
                        taskNumber, botConfig.Issues.StatusField.Name,
                        botConfig.Issues.StatusField.MergedValue);
                }

                Notifier.NotifyTaskStatus(
                    restApi, branch.Owner, message,
                    botConfig.Notifications);
            }
            catch (Exception ex)
            {
                Notifier.NotifyException(
                    restApi, branch, message,
                    "merged", ex, botConfig.Notifications);
            }
        }