Exemple #1
0
        public override async Task EvaluateAsync(
            IMergePolicyEvaluationContext context,
            MergePolicyProperties properties)
        {
            var ignoreChecks = new HashSet <string>(properties.Get <string[]>("ignoreChecks") ?? Array.Empty <string>());

            await EvaluateChecksAsync(context, ignoreChecks);
        }
Exemple #2
0
        public Task <IReadOnlyList <IMergePolicy> > BuildMergePoliciesAsync(MergePolicyProperties properties, IPullRequest pr)
        {
            var ignoreChecks = new HashSet <string>(properties.Get <string[]>("ignoreChecks") ?? Array.Empty <string>());
            IReadOnlyList <IMergePolicy> policies = new List <IMergePolicy> {
                new AllChecksSuccessfulMergePolicy(ignoreChecks)
            };

            return(Task.FromResult(policies));
        }
        public override async Task EvaluateAsync(
            IMergePolicyEvaluationContext context,
            MergePolicyProperties properties)
        {
            var ignoreChecks = new HashSet <string>(properties.Get <string[]>("ignoreChecks") ?? Array.Empty <string>());

            IEnumerable <Check> checks = await context.Darc.GetPullRequestChecksAsync(context.PullRequestUrl);

            IEnumerable <Check> notIgnoredChecks = checks.Where(c => !ignoreChecks.Contains(c.Name));

            if (!notIgnoredChecks.Any())
            {
                context.Fail("No un-ignored checks.");
                return;
            }

            ILookup <CheckState, Check> statuses = notIgnoredChecks.ToLookup(
                c =>
            {
                // unify the check statuses to success, pending, and error
                switch (c.Status)
                {
                case CheckState.Success:
                case CheckState.Pending:
                    return(c.Status);

                default:
                    return(CheckState.Error);
                }
            });

            string ListChecks(CheckState state)
            {
                return(string.Join(", ", statuses[state].Select(c => c.Name)));
            }

            if (statuses.Contains(CheckState.Error))
            {
                context.Fail($"Unsuccessful checks: {ListChecks(CheckState.Error)}");
                return;
            }

            if (statuses.Contains(CheckState.Pending))
            {
                context.Pending($"Waiting on checks: {ListChecks(CheckState.Pending)}");
                return;
            }

            context.Succeed($"Successful checks: {ListChecks(CheckState.Success)}");
        }
        public override async Task EvaluateAsync(
            IMergePolicyEvaluationContext context,
            MergePolicyProperties properties)
        {
            var requiredChecks = new HashSet <string>(properties.Get <List <string> >("checks"));

            Dictionary <string, Check> checks =
                (await context.Darc.GetPullRequestChecksAsync(context.PullRequestUrl)).ToDictionary(c => c.Name);

            var missingChecks = new List <string>();
            var failedChecks  = new List <Check>();

            foreach (string requiredCheck in requiredChecks)
            {
                if (checks.TryGetValue(requiredCheck, out Check check))
                {
                    if (check.Status != CheckState.Success)
                    {
                        failedChecks.Add(check);
                    }
                }
                else
                {
                    missingChecks.Add(requiredCheck);
                }
            }

            if (missingChecks.Count < 1 && failedChecks.Count < 1)
            {
                context.Succeed("Required checks passed.");
                return;
            }

            var parts = new List <string>();

            if (failedChecks.Any())
            {
                parts.Add($"Unsuccessful checks: {string.Join(", ", failedChecks.Select(c => c.Name))}");
            }

            if (missingChecks.Any())
            {
                parts.Add($"Missing checks: {string.Join(", ", missingChecks)}");
            }

            context.Fail(string.Join("; ", parts));
        }