Beispiel #1
0
        public async Task <WebhookResponse> Handle(PullRequestReviewEvent payload)
        {
            var submittedAction = payload.Action.Equals("submitted", StringComparison.OrdinalIgnoreCase);

            if (!submittedAction)
            {
                logger.WithExtraData(new { payloadAction = payload.Action }).Information("Received Review event on action other than 'submitted', ignoring");
                return(new WebhookResponse(false, "Received Review event on action other than 'submitted', ignoring"));
            }

            var isApprovedState = payload.Review.State.Equals("approved", StringComparison.OrdinalIgnoreCase);

            if (!isApprovedState)
            {
                logger.WithExtraData(new { payloadAction = payload.Action, reviewState = payload.Review.State }).Information($"Received submit Review event on event other than 'approved', ignoring");
                return(new WebhookResponse(false, "Received submit Review event on action other than 'submitted', ignoring"));
            }

            var owner = payload.Repository.Owner.Login;
            var repo  = payload.Repository.Name;
            var prId  = payload.PullRequest.Number;

            var mergeRequest = await mergeRequestsRepository.Get(owner, repo, prId);

            if (mergeRequest == null)
            {
                logger.WithExtraData(new { owner, repo, prId }).Warning("Received Review event on unknown PR, Miro can't handle this");
                return(new WebhookResponse(false, "Received Review event on unknown PR, Miro can't handle this"));
            }

            logger.WithMergeRequestData(mergeRequest).Information("Received Approved Review event on PR, Trying to merge");
            var merged = await mergeOperations.TryToMerge(mergeRequest);

            return(new WebhookResponse(true, $"Received Approved Review event on PR, did branch merge: {merged}"));
        }
        private async Task <WebhookResponse> HandleSynchronizeEvent(PullRequestEvent payload)
        {
            var owner           = payload.Repository.Owner.Login;
            var repo            = payload.Repository.Name;
            var prId            = payload.Number;
            var sha             = payload.PullRequest.Head.Sha;
            var extraLoggerData = new { owner, repo, prId, sha };


            var mergeRequest = await mergeRequestRepository.Get(owner, repo, prId);

            if (mergeRequest == null)
            {
                logger.WithExtraData(extraLoggerData).Information("Synchronize event on unknown Pull Request, ignore");
                return(new WebhookResponse(false, "Synchronize event on unknown Pull Request, ignore"));
            }

            var config = await repoConfigManager.GetConfig(owner, repo);

            logger.WithMergeRequestData(mergeRequest).Information("Handling synchronize event, updating sha");

            var updatedMergeRequest = await mergeRequestRepository.UpdateShaAndClearStatusChecks(mergeRequest.Owner, mergeRequest.Repo, mergeRequest.PrId, sha);

            if (config.IsWhitelistStrict() && updatedMergeRequest.ReceivedMergeCommand)
            {
                logger.WithMergeRequestData(updatedMergeRequest).Information("Repository has a whitelist-strict merge policy, resolving miro check on PR");
                await miroMergeCheck.ResolveMiroMergeCheck(updatedMergeRequest);
            }

            return(new WebhookResponse(true, "Handling synchronize event, sha updated"));
        }
Beispiel #3
0
        private async Task <bool> UpdateNextPrByStrategy(PushEvent payload, RepoConfig repoConfig)
        {
            var owner = payload.Repository.Owner.Login;
            var repo  = payload.Repository.Name;

            logger.WithExtraData(new { owner, repo, strategy = repoConfig.UpdateBranchStrategy }).Information("Updating next PR by strategy");

            List <MergeRequest> prsToUpdate = null;

            switch (repoConfig.UpdateBranchStrategy)
            {
            case "oldest":
                var singlePr = await mergeRequestRepository.GetOldestPr(owner, repo);

                if (singlePr != null)
                {
                    prsToUpdate = new List <MergeRequest> {
                        singlePr
                    }
                }
                ;
                break;

            case "all":
                var allPrs = await mergeRequestRepository.Get(owner, repo);

                if (allPrs != null && allPrs.Any())
                {
                    prsToUpdate = allPrs.Where(x => x.ReceivedMergeCommand).ToList();
                }
                break;

            case "none":
            default:
                break;
            }
            if (prsToUpdate == null || !prsToUpdate.Any())
            {
                logger.WithExtraData(new { owner, repo }).Warning($"Could not find next PRs to update based on after PR was merged");
                return(false);
            }
            var tasks       = prsToUpdate.Select(pr => UpdateSinglePr(pr, repoConfig));
            var completions = await Task.WhenAll(tasks);

            return(completions.Any(x => x));
        }
Beispiel #4
0
        private async Task <WebhookResponse> HandleMiroCommand(IssueCommentEvent issueCommentEvent, string miroCommand)
        {
            var owner = issueCommentEvent.Repository.Owner.Login;
            var repo  = issueCommentEvent.Repository.Name;
            var prId  = issueCommentEvent.Issue.Number;

            logger.WithExtraData(new { miroCommand, owner, repo, prId }).Information($"Handling miro comment command");

            var mergeRequest = await mergeRequestRepository.Get(owner, repo, prId);

            if (mergeRequest == null)
            {
                logger.WithExtraData(new { miroCommand, owner, repo, prId }).Warning($"Received miro command on unknown PR, Miro can't handle this");
                return(new WebhookResponse(false, "Received miro command on unknown PR, Miro can't handle this"));
            }


            switch (miroCommand)
            {
            case CommentsConsts.CancelCommand:
                return(await HandleMiroCancelCommand(owner, repo, prId));

            case CommentsConsts.MergeCommand:
                return(await HandleMiroMergeCommand(owner, repo, prId));

            case CommentsConsts.InfoCommand:
                return(await PrintMergeInfo(mergeRequest));

            case CommentsConsts.WipCommand:
                return(await HandleMiroWipCommand(owner, repo, prId));

            default:
                logger.WithExtraData(new { miroCommand }).Error("Comment was supposed to contain a miro command but did not");
                return(new WebhookResponse(false, "Comment doesn't contain a miro command, ignoring"));
            }
        }