internal void EnsurePlasticStatusAttributeExists()
        {
            if (FindQueries.ExistsAttributeName(
                    mRestApi,
                    mTrunkBotConfig.Repository,
                    mTrunkBotConfig.Plastic.StatusAttribute.Name))
            {
                return;
            }

            if (TrunkMergebotApi.Attributes.CreateAttribute(
                    mRestApi,
                    mTrunkBotConfig.Repository,
                    mTrunkBotConfig.Plastic.StatusAttribute.Name,
                    "Attribute automatically created by trunk-bot: " + mBotName))
            {
                return;
            }

            throw new Exception(string.Format(
                                    "Trunkbot {0}: Unable to create " +
                                    "attribute name {1} on repository {2}. " +
                                    "Check the server log.",
                                    mBotName,
                                    mTrunkBotConfig.Repository,
                                    mTrunkBotConfig.Plastic.StatusAttribute.Name));
        }
        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);
        }
        internal void ProcessBranches(object state)
        {
            while (true)
            {
                Branch branch;
                lock (mSyncLock)
                {
                    if (!BranchesQueueStorage.HasQueuedBranches(mBranchesQueueFilePath))
                    {
                        Monitor.Wait(mSyncLock, 1000);
                        continue;
                    }

                    branch          = BranchesQueueStorage.DequeueBranch(mBranchesQueueFilePath);
                    branch.FullName = FindQueries.GetBranchName(
                        mRestApi, branch.Repository, branch.Id);
                }

                mLog.InfoFormat("Processing branch {0} attribute change...", branch.FullName);
                ProcessBranch.Result result = ProcessBranch.TryProcessBranch(
                    mRestApi, branch, mTrunkBotConfig, mBotName, mCodeReviewsTrackedFilePath);

                if (result == ProcessBranch.Result.Ok)
                {
                    mLog.InfoFormat("Branch {0} processing completed.", branch.FullName);
                    continue;
                }

                if (result == ProcessBranch.Result.Failed)
                {
                    mLog.InfoFormat("Branch {0} processing failed.", branch.FullName);
                    continue;
                }

                mLog.InfoFormat("Branch {0} is not ready. It will be queued again.", branch.FullName);

                lock (mSyncLock)
                {
                    if (BranchesQueueStorage.Contains(
                            branch.Repository, branch.Id,
                            mBranchesQueueFilePath))
                    {
                        continue;
                    }

                    BranchesQueueStorage.EnqueueBranch(
                        branch, mBranchesQueueFilePath);
                }

                Thread.Sleep(5000);
            }
        }