static async Task <IRateLimitStatus> GetGraphQLRateLimitStatus(IGitHubApiStatusService gitHubApiStatusService)
        {
            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));
            var gitHubApiStatus         = await gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

            return(gitHubApiStatus.GraphQLApi);
        }
Example #2
0
        public async Task <HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req, FunctionContext functionContext)
        {
            var log = functionContext.GetLogger <GetTestToken>();

            foreach (var testTokenPair in _testTokenDictionary)
            {
                log.LogInformation($"Retrieving Rate Limits for {testTokenPair.Key}");

                var timeout = TimeSpan.FromSeconds(2);
                var cancellationTokenSource = new CancellationTokenSource(timeout);

                try
                {
                    _gitHubApiStatusService.SetAuthenticationHeaderValue(new AuthenticationHeaderValue("bearer", testTokenPair.Value));
                    var gitHubApiRateLimits = await _gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

                    log.LogInformation($"\tREST API Rate Limit: {gitHubApiRateLimits.RestApi.RemainingRequestCount}");
                    log.LogInformation($"\tGraphQL API Rate Limit: {gitHubApiRateLimits.GraphQLApi.RemainingRequestCount}");

                    if (gitHubApiRateLimits.RestApi.RemainingRequestCount > 1000 &&
                        gitHubApiRateLimits.GraphQLApi.RemainingRequestCount > 1000)
                    {
                        var gitHubToken = new GitHubToken(testTokenPair.Value, GitHubConstants.OAuthScope, "Bearer");

                        var okResponse = req.CreateResponse(HttpStatusCode.OK);

                        var gitHubTokenJson = JsonConvert.SerializeObject(gitHubToken);

                        await okResponse.WriteStringAsync(gitHubTokenJson).ConfigureAwait(false);

                        return(okResponse);
                    }

                    log.LogInformation($"\tAPI Limits for {testTokenPair.Key} too low");
                    log.LogInformation($"\tRetrieving next token");
                }
                catch (Exception e)
                {
                    log.LogError(e, e.Message);

                    var errorResponse = req.CreateResponse(HttpStatusCode.InternalServerError);
                    await errorResponse.WriteStringAsync(e.ToString()).ConfigureAwait(false);

                    return(errorResponse);
                }
            }
            ;

            var notFoundResponse = req.CreateResponse(HttpStatusCode.NotFound);
            await notFoundResponse.WriteStringAsync("No Valid GitHub Token Found").ConfigureAwait(false);

            return(notFoundResponse);
        }
        public async Task Run([TimerTrigger("0 0 */6 * * *")] TimerInfo timer, FunctionContext context)
        {
            var log = context.GetLogger <UpdateMicrosoftLearnContributors>();

            log.LogInformation($"{nameof(UpdateMicrosoftLearnContributors)} Started");

            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            var gitHubApiStatus         = await _gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

            if (gitHubApiStatus.GraphQLApi.RemainingRequestCount < 4000)
            {
                log.LogError($"Maximum GitHub API Limit Reached. GitHub API Limit will reset in {gitHubApiStatus.GraphQLApi.RateLimitReset_TimeRemaining.Minutes + 1} minute(s). Try again at {gitHubApiStatus.GraphQLApi.RateLimitReset_DateTime.UtcDateTime} UTC");
                return;
            }

            var advocateList = await _advocateService.GetCurrentAdvocates(cancellationTokenSource.Token).ConfigureAwait(false);

            var microsoftLearnPullRequests = new List <RepositoryPullRequest>();

            await foreach (var pullRequestList in _gitHubGraphQLApiService.GetMicrosoftLearnPullRequests().ConfigureAwait(false))
            {
                microsoftLearnPullRequests.AddRange(pullRequestList);
                log.LogInformation($"Added {pullRequestList.Count} Pull Requests from {pullRequestList.FirstOrDefault()?.RepositoryName}");
            }

            var cloudAdvocateContributions = new List <CloudAdvocateGitHubContributorModel>();

            foreach (var cloudAdvocate in advocateList)
            {
                var cloudAdvocateContributorModel = new CloudAdvocateGitHubContributorModel(microsoftLearnPullRequests.Where(x => cloudAdvocate.GitHubUsername.Equals(x.Author?.Login, StringComparison.OrdinalIgnoreCase)).ToList(), cloudAdvocate.GitHubUsername, cloudAdvocate.MicrosoftAlias, cloudAdvocate.RedditUserName, cloudAdvocate.Team, cloudAdvocate.Name);

                cloudAdvocateContributions.Add(cloudAdvocateContributorModel);

                log.LogInformation($"Added {cloudAdvocateContributorModel.PullRequests.Count} Pull Requests for {cloudAdvocate.Name}");
            }

            var blobName = $"Contributions_{DateTime.UtcNow:o}.json";
            await _blobStorageService.UploadCloudAdvocateMicrosoftLearnContributions(cloudAdvocateContributions, blobName).ConfigureAwait(false);
        }
Example #4
0
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest request, ILogger log)
        {
            foreach (var testToken in _testTokenList)
            {
                var timeout = TimeSpan.FromSeconds(2);
                var cancellationTokenSource = new CancellationTokenSource(timeout);

                try
                {
                    _gitHubApiStatusService.SetAuthenticationHeaderValue(new AuthenticationHeaderValue("bearer", testToken));
                    var gitHubApiRateLimits = await _gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false);

                    if (gitHubApiRateLimits.RestApi.RemainingRequestCount > 1000 &&
                        gitHubApiRateLimits.GraphQLApi.RemainingRequestCount > 1000)
                    {
                        var gitHubToken = new GitHubToken(testToken, GitHubConstants.OAuthScope, "Bearer");

                        return(new ContentResult
                        {
                            Content = JsonConvert.SerializeObject(gitHubToken),
                            StatusCode = (int)HttpStatusCode.OK,
                            ContentType = "application/json"
                        });
                    }
                }
                catch (Exception e)
                {
                    return(new ObjectResult(e.ToString())
                    {
                        StatusCode = (int)HttpStatusCode.InternalServerError
                    });
                }
            }
            ;

            return(new NotFoundObjectResult("No Valid GitHub Token Found"));
        }