Пример #1
0
        public async Task <MergePolicyEvaluationResult> EvaluateAsync(
            string prUrl,
            IRemote darc,
            IReadOnlyList <MergePolicyDefinition> policyDefinitions)
        {
            var context = new MergePolicyEvaluationContext(prUrl, darc);

            foreach (MergePolicyDefinition definition in policyDefinitions)
            {
                if (MergePolicies.TryGetValue(definition.Name, out MergePolicy policy))
                {
                    using (Logger.BeginScope("Evaluating Merge Policy {policyName}", policy.Name))
                    {
                        context.CurrentPolicy = policy;
                        await policy.EvaluateAsync(context, new MergePolicyProperties(definition.Properties));
                    }
                }
                else
                {
                    context.CurrentPolicy = null;
                    context.Fail($"Unknown Merge Policy: '{definition.Name}'");
                }
            }

            return(context.Result);
        }
Пример #2
0
        private async Task <string> CheckMergePolicyInternalAsync(
            IRemote darc,
            IList <MergePolicyDefinition> policyDefinitions,
            InProgressPullRequest pr)
        {
            var results = new List <(MergePolicy policy, MergePolicyEvaluationResult result)>();

            if (policyDefinitions != null)
            {
                foreach (MergePolicyDefinition policyDefinition in policyDefinitions)
                {
                    var context = new MergePolicyEvaluationContext(pr.Url, darc, Logger, policyDefinition.Properties);
                    if (PolicyEvaluators.TryGetValue(policyDefinition.Name, out MergePolicy policyEvaluator))
                    {
                        MergePolicyEvaluationResult result = await policyEvaluator.EvaluateAsync(context);

                        results.Add((policyEvaluator, result));
                    }
                    else
                    {
                        results.Add((null, context.Fail($"Unknown Merge Policy '{policyDefinition.Name}'")));
                    }
                }
            }

            if (results.Count == 0)
            {
                await UpdateStatusCommentAsync(
                    darc,
                    pr,
                    $@"## Auto-Merge Status
This pull request will not be merged automatically because the subscription with id '{SubscriptionId}' does not have any merge policies.");

                return(NotMergedNoPolicies());
            }

            string DisplayPolicy((MergePolicy policy, MergePolicyEvaluationResult result) p)
            {
                (MergePolicy policy, MergePolicyEvaluationResult result) = p;
                if (policy == null)
                {
                    return($"- ❌ **{result.Message}**");
                }

                if (result.Succeeded)
                {
                    return($"- ✔️ **{policy.DisplayName}** Succeeded");
                }

                return($"- ❌ **{policy.DisplayName}** {result.Message}");
            }

            if (results.Any(r => !r.result.Succeeded))
            {
                await UpdateStatusCommentAsync(
                    darc,
                    pr,
                    $@"## Auto-Merge Status
This pull request has not been merged because the subscription with id '{SubscriptionId}' is waiting on the following merge policies.

{string.Join("\n", results.OrderBy(r => r.policy == null ? " " : r.policy.Name).Select(DisplayPolicy))}");

                return(NotMergedFailedPolicies(results.Where(r => !r.result.Succeeded), pr.Url));
            }

            await UpdateStatusCommentAsync(
                darc,
                pr,
                $@"## Auto-Merge Status
This pull request has been merged because the following merge policies have succeeded.

{string.Join("\n", results.OrderBy(r => r.policy == null ? " " : r.policy.Name).Select(DisplayPolicy))}");

            await darc.MergePullRequestAsync(pr.Url, null);

            return(Merged(policyDefinitions, pr.Url));
        }