|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | | -using System.Net; |
5 | 4 | using System.Net.Http; |
6 | | -using System.Net.Http.Headers; |
7 | | -using System.Threading; |
8 | 5 | using System.Threading.Tasks; |
9 | | -using GitHubApiStatus; |
10 | | -using GitHubReadmeWebTrends.Common; |
11 | 6 | using Microsoft.AspNetCore.Mvc; |
12 | 7 | using Microsoft.Azure.WebJobs; |
13 | 8 | using Microsoft.Azure.WebJobs.Extensions.Http; |
14 | 9 | using Microsoft.Extensions.Logging; |
15 | 10 |
|
16 | | -namespace GitHubReadmeWebTrends.Functions |
| 11 | +namespace AzureAdvocates.Functions |
17 | 12 | { |
18 | 13 | class GetMicrosoftLearnContributors |
19 | 14 | { |
20 | | - const string _defaultTeam = ""; |
| 15 | + readonly BlobStorageService _blobStorageService; |
21 | 16 |
|
22 | | - readonly CloudAdvocateService _cloudAdvocateService; |
23 | | - readonly IGitHubApiStatusService _gitHubApiStatusService; |
24 | | - readonly GitHubGraphQLApiService _gitHubGraphQLApiService; |
25 | | - |
26 | | - public GetMicrosoftLearnContributors(CloudAdvocateService cloudAdvocateService, |
27 | | - IGitHubApiStatusService gitHubApiStatusService, |
28 | | - GitHubGraphQLApiService gitHubGraphQLApiService) |
29 | | - { |
30 | | - _cloudAdvocateService = cloudAdvocateService; |
31 | | - _gitHubApiStatusService = gitHubApiStatusService; |
32 | | - _gitHubGraphQLApiService = gitHubGraphQLApiService; |
33 | | - } |
| 17 | + public GetMicrosoftLearnContributors(BlobStorageService blobStorageService) => _blobStorageService = blobStorageService; |
34 | 18 |
|
35 | 19 | [FunctionName(nameof(GetMicrosoftLearnContributors))] |
36 | 20 | public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = nameof(GetMicrosoftLearnContributors) + "/{from:datetime}/{to:datetime}/{team?}")] HttpRequestMessage req, DateTime from, DateTime to, string? team, ILogger log) |
37 | 21 | { |
38 | 22 | log.LogInformation($"{nameof(GetMicrosoftLearnContributors)} Started"); |
39 | 23 |
|
40 | | - var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2)); |
41 | | - var gitHubApiStatus = await _gitHubApiStatusService.GetApiRateLimits(cancellationTokenSource.Token).ConfigureAwait(false); |
42 | | - if (gitHubApiStatus.GraphQLApi.RemainingRequestCount < 4000) |
43 | | - { |
44 | | - return new ObjectResult($"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") |
45 | | - { |
46 | | - StatusCode = (int)HttpStatusCode.Forbidden |
47 | | - }; |
48 | | - } |
| 24 | + var microsoftLearnContributionsList = await _blobStorageService.GetCloudAdvocateMicrosoftLearnContributors().ConfigureAwait(false); |
49 | 25 |
|
50 | | - var cloudAdvocateList = new List<CloudAdvocateGitHubUserModel>(); |
51 | | - await foreach (var advocate in _cloudAdvocateService.GetAzureAdvocates().ConfigureAwait(false)) |
| 26 | + var filteredCloudAdvocateContributions = new List<CloudAdvocateGitHubContributorModel>(); |
| 27 | + foreach (var advocateContribution in microsoftLearnContributionsList) |
52 | 28 | { |
53 | | - if (team is null || advocate.MicrosoftTeam.Equals(team, StringComparison.OrdinalIgnoreCase)) |
| 29 | + if (team is null || advocateContribution.MicrosoftTeam.Equals(team, StringComparison.OrdinalIgnoreCase)) |
54 | 30 | { |
55 | | - log.LogInformation($"Found Advocate: {advocate.FullName}"); |
56 | | - cloudAdvocateList.Add(advocate); |
57 | | - } |
58 | | - } |
| 31 | + log.LogInformation($"Adding Advocate: {advocateContribution.FullName}"); |
59 | 32 |
|
60 | | - var microsoftLearnPullRequests = new List<RepositoryPullRequest>(); |
61 | | - await foreach (var pullRequestList in _gitHubGraphQLApiService.GetMicrosoftLearnPullRequests().ConfigureAwait(false)) |
62 | | - { |
63 | | - microsoftLearnPullRequests.AddRange(pullRequestList); |
64 | | - log.LogInformation($"Added {pullRequestList.Count} Pull Requests from {pullRequestList.FirstOrDefault()?.RepositoryName}"); |
65 | | - } |
| 33 | + var filteredPullRequests = advocateContribution.PullRequests.Where(x => x.CreatedAt.IsWithinRange(from, to)).ToList(); |
| 34 | + var filteredCloudAdvocateContribution = new CloudAdvocateGitHubContributorModel(filteredPullRequests, advocateContribution.FullName, advocateContribution.GitHubUserName, advocateContribution.MicrosoftAlias, advocateContribution.MicrosoftTeam); |
66 | 35 |
|
67 | | - var cloudAdvocateContributions = new List<GitHubContributorModel>(); |
68 | | - foreach (var cloudAdvocate in cloudAdvocateList) |
69 | | - { |
70 | | - var cloudAdvocateContributorModel = new GitHubContributorModel(microsoftLearnPullRequests.Where(x => x.Author.Equals(cloudAdvocate.GitHubUserName, StringComparison.OrdinalIgnoreCase) && x.CreatedAt.IsWithinRange(from, to)), cloudAdvocate); |
71 | | - |
72 | | - cloudAdvocateContributions.Add(cloudAdvocateContributorModel); |
73 | | - |
74 | | - log.LogInformation($"Added {cloudAdvocateContributorModel.PullRequests.Count} Pull Requests for {cloudAdvocate.FullName}"); |
| 36 | + filteredCloudAdvocateContributions.Add(filteredCloudAdvocateContribution); |
| 37 | + } |
75 | 38 | } |
76 | 39 |
|
77 | | - return new OkObjectResult(cloudAdvocateContributions); |
| 40 | + return new OkObjectResult(filteredCloudAdvocateContributions); |
78 | 41 | } |
79 | 42 | } |
80 | 43 |
|
|
0 commit comments