public async Task InvokeHandlers <TPayload>(string json, IJobCancellationToken jobCancellationToken) where TPayload : ActivityPayload
        {
            logger.LogTrace("Beginning payload processing job: {0}");
            var cancellationToken = jobCancellationToken.ShutdownToken;

            var payload = new SimpleJsonSerializer().Deserialize <TPayload>(json);
            var tasks   = new List <Task>();

            async Task RunHandler(IPayloadHandler <TPayload> payloadHandler)
            {
                try
                {
                    await payloadHandler.ProcessPayload(payload, cancellationToken).ConfigureAwait(false);
                }
                //To be expected
                catch (OperationCanceledException e)
                {
                    logger.LogDebug(e, "Payload handler processing cancelled!");
                }
                catch (NotSupportedException e)
                {
                    logger.LogTrace(e, "Payload handler does not support payload!");
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Payload handler threw exception!");
                }
            };

            await componentProvider.Initialize(cancellationToken).ConfigureAwait(false);

            foreach (var handler in componentProvider.GetPayloadHandlers <TPayload>())
            {
                tasks.Add(RunHandler(handler));
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            if (autoMergeHandler is IPayloadHandler <TPayload> asHandler)
            {
                logger.LogTrace("Running auto merge payload handler.");
                try
                {
                    await asHandler.ProcessPayload(payload, cancellationToken).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Failed running auto merge handler!");
                }
            }
            else
            {
                logger.LogTrace("Not running auto merge handler to to payload type of {0}.", typeof(TPayload).FullName);
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> ReviewPullRequest(int number, CancellationToken cancellationToken)
        {
            var prTask    = gitHubManager.GetPullRequest(number);
            var tokenTask = gitHubManager.CheckAuthorization(Request.Cookies, cancellationToken);
            var componentInitializeTask = componentProvider.Initialize(cancellationToken);
            var pr = await prTask.ConfigureAwait(false);

            await componentInitializeTask.ConfigureAwait(false);

            if (pr.State.Value == ItemState.Open)
            {
                var tasks = componentProvider.MergeRequirements.Select(x => x.EvaluateFor(pr, cancellationToken));
                await Task.WhenAll(tasks).ConfigureAwait(false);


                var resultDic = new Dictionary <IMergeRequirement, AutoMergeStatus>();
                foreach (var I in Enumerable.Zip(componentProvider.MergeRequirements, tasks, (x, y) => new Tuple <IMergeRequirement, AutoMergeStatus>(x, y.Result)))
                {
                    ++I.Item2.RequiredProgress;
                    ++I.Item2.Progress;
                    resultDic.Add(I.Item1, I.Item2);
                }

                ViewBag.Statuses          = resultDic;
                ViewBag.PullRequestClosed = false;
            }
            else
            {
                ViewBag.PullRequestClosed = true;
            }

            ViewBag.Title                  = stringLocalizer["PullRequest", number];
            ViewBag.Modules                = stringLocalizer["ManageModules"];
            ViewBag.PRNumber               = number;
            ViewBag.RepoOwner              = gitHubConfiguration.RepoOwner;
            ViewBag.RepoName               = gitHubConfiguration.RepoName;
            ViewBag.PullRequestAuthor      = pr.User.Login;
            ViewBag.PullRequestAuthorID    = pr.User.Id;
            ViewBag.PullRequestTitle       = pr.Title;
            ViewBag.PullRequestNumber      = pr.Number;
            ViewBag.PullRequestHref        = pr.HtmlUrl;
            ViewBag.PullRequestAuthorLogin = pr.User.Login;

            ViewBag.CloseMessage      = stringLocalizer["CloseMessage"];
            ViewBag.MergeRequirements = stringLocalizer["MergeRequirements"];


            var token = await tokenTask.ConfigureAwait(false);

            if (token == null)
            {
                ViewBag.AuthHref     = Url.Action("Begin", "Authorization", new { prNumber = number });
                ViewBag.AuthTitle    = stringLocalizer["SignIn"];
                ViewBag.IsMaintainer = false;
                ViewBag.UserIsAuthor = false;
            }
            else
            {
                var user = await gitHubManager.GetUserLogin(token, cancellationToken).ConfigureAwait(false);

                ViewBag.UserIsAuthor = pr.User.Id == user.Id;
                ViewBag.IsMaintainer = await gitHubManager.UserHasWriteAccess(user).ConfigureAwait(false);

                ViewBag.UserLogin = user.Login;
                ViewBag.AuthHref  = Url.Action("SignOut", "Authorization", new { prNumber = number });
                ViewBag.AuthTitle = stringLocalizer["SignOut", user.Login];
            }

            ViewBag.ModuleViews = new List <string>();

            await componentProvider.AddViewVars(pr, (object)ViewBag, cancellationToken).ConfigureAwait(false);

            return(View());
        }