Exemplo n.º 1
0
        public async Task ProcessWebhookAsync(string eventName, string json, ILogger logger, CancellationToken cancellationToken)
        {
            await Policy
            .Handle <AbuseException>()
            .RetryAsync(3, async(ex, retryCount) =>
            {
                logger.LogWarning("Abuse exception detected, attempting retry.");
                var abuseEx = (AbuseException)ex;

                logger.LogInformation("Waiting for {seconds} before retrying.", abuseEx.RetryAfterSeconds);
                await Task.Delay(TimeSpan.FromSeconds((double)abuseEx.RetryAfterSeconds));
            })
            .ExecuteAsync(async() =>
            {
                if (eventName == "check_run")
                {
                    var handler = new CheckRunHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "issue_comment")
                {
                    var handler = new IssueCommentHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "pull_request")
                {
                    var handler = new PullRequestHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
            });
        }
        public async Task ProcessWebhookAsync(string eventName, string json, ILogger logger, CancellationToken cancellationToken)
        {
            await Policy
            .Handle <AbuseException>()
            .Or <RateLimitExceededException>()
            .RetryAsync(3, async(ex, retryCount) =>
            {
                TimeSpan retryDelay = TimeSpan.FromSeconds(10);     // Default.

                switch (ex)
                {
                case AbuseException abuseException:
                    retryDelay = TimeSpan.FromSeconds((double)abuseException.RetryAfterSeconds);
                    logger.LogWarning("Abuse exception detected. Retry after seconds is: {retrySeconds}",
                                      abuseException.RetryAfterSeconds
                                      );
                    break;

                case RateLimitExceededException rateLimitExceededException:
                    retryDelay = rateLimitExceededException.GetRetryAfterTimeSpan();
                    logger.LogWarning(
                        "Rate limit exception detected. Limit is: {limit}, reset is: {reset}, retry seconds is: {retrySeconds}",
                        rateLimitExceededException.Limit,
                        rateLimitExceededException.Reset,
                        retryDelay.TotalSeconds
                        );
                    break;
                }

                logger.LogInformation("Waiting for {seconds} before retrying.", retryDelay.TotalSeconds);
                await Task.Delay(retryDelay);
            })
            .ExecuteAsync(async() =>
            {
                if (eventName == "check_run")
                {
                    var handler = new CheckRunHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger, limiter);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "issue_comment")
                {
                    var handler = new IssueCommentHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger, limiter);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "pull_request")
                {
                    var handler = new PullRequestHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger, limiter, pullRequestTracker);
                    await handler.HandleAsync(json, cancellationToken);
                }
            });
        }
Exemplo n.º 3
0
        public async Task ProcessWebhookAsync(HttpRequest request, ILogger logger, CancellationToken cancellationToken)
        {
            var json = await ReadAndVerifyBodyAsync(request, cancellationToken);

            if (request.Headers.TryGetValue(GitHubEventHeader, out StringValues eventName))
            {
                if (eventName == "check_run")
                {
                    var handler = new CheckRunHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "check_suite")
                {
                    var handler = new CheckSuiteHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "issue_comment")
                {
                    var handler = new IssueCommentHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else if (eventName == "pull_request")
                {
                    var handler = new PullRequestHandler(globalConfigurationProvider, gitHubClientProvider, repositoryConfigurationProvider, logger);
                    await handler.HandleAsync(json, cancellationToken);
                }
                else
                {
                    throw new CheckEnforcerUnsupportedEventException(eventName);
                }
            }
            else
            {
                throw new CheckEnforcerException($"Could not find header '{GitHubEventHeader}'.");
            }
        }