/// <inheritdoc />
        public async Task ProcessPayload(PullRequestEventPayload payload, CancellationToken cancellationToken)
        {
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }
            if (payload.Action != "opened")
            {
                throw new NotSupportedException();
            }

            if (await gitHubManager.UserHasWriteAccess(payload.PullRequest.User).ConfigureAwait(false))
            {
                return;
            }

            var allPrs = await gitHubManager.GetOpenPullRequests().ConfigureAwait(false);

            string result = null;

            foreach (var I in allPrs.Where(x => x.User.Id == payload.PullRequest.User.Id && x.Id != payload.PullRequest.Id).Select(x => x.Number))
            {
                result = (result != null) ? result + String.Format(CultureInfo.InvariantCulture, ", #{0}", I) : String.Format(CultureInfo.InvariantCulture, "#{0}", I);
            }

            if (result != null)
            {
                await gitHubManager.CreateComment(payload.PullRequest.Number, stringLocalizer["TooManyPRs", result]).ConfigureAwait(false);

                await gitHubManager.Close(payload.PullRequest.Number).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Checks all open PRs for if they should have the 'Merge Conflict' tag
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the running operation</returns>
        async Task CheckMergeConflicts()
        {
            Task AddMergeConflictTag(PullRequest pullRequest)
            {
                return(gitHubManager.AddLabel(pullRequest.Number, "Merge Conflict"));
            };

            async Task RefreshPR(PullRequest pullRequest)
            {
                //wait 10s for refresh then give up
                await Task.Delay(10000).ConfigureAwait(false);

                pullRequest = await gitHubManager.GetPullRequest(pullRequest.Number).ConfigureAwait(false);

                if (pullRequest.Mergeable.HasValue && !pullRequest.Mergeable.Value)
                {
                    await AddMergeConflictTag(pullRequest).ConfigureAwait(false);
                }
            };

            var tasks = new List <Task>();

            var prs = await gitHubManager.GetOpenPullRequests().ConfigureAwait(false);

            foreach (var I in prs)
            {
                if (I.Mergeable.HasValue)
                {
                    if (I.Mergeable.Value)
                    {
                        continue;
                    }
                    else
                    {
                        tasks.Add(AddMergeConflictTag(I));
                    }
                }
                else
                {
                    tasks.Add(RefreshPR(I));
                }
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);
        }
 public async Task <IActionResult> SignOut(int prNumber)
 {
     gitHubManager.ExpireAuthorization(Response.Cookies);
     return(RedirectToAction("ReviewPullRequest", "PullRequest", new { number = prNumber > 0 ? prNumber : (await gitHubManager.GetOpenPullRequests().ConfigureAwait(false))[0].Number }));
 }