public async Task <IReadOnlyList <AdvocateModel> > GetAzureAdvocatesBetaTestersTimerTrigger([TimerTrigger(_runOncePerMonth, RunOnStartup = true)] TimerInfo myTimer, FunctionContext context) { var log = context.GetLogger <GetAdvocatesFunction>(); log.LogInformation($"{nameof(GetAzureAdvocatesBetaTestersTimerTrigger)} Started"); var advocateModels = new List <AdvocateModel>(); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)); var optOutList = await _optOutDatabase.GetAllOptOutModels().ConfigureAwait(false); var currentAdvocateList = await _advocateService.GetCurrentAdvocates(cancellationTokenSource.Token).ConfigureAwait(false); foreach (var advocate in currentAdvocateList) { if (!IsBetaTester(advocate)) { continue; } log.LogInformation($"Beta Tester Found: {advocate.MicrosoftAlias}"); if (!HasUserOptedOut(advocate, optOutList)) { advocateModels.Add(advocate); } } log.LogInformation($"{nameof(GetAzureAdvocatesBetaTestersTimerTrigger)} Completed"); return(advocateModels); }
public async Task OnPostOptOutButtonClicked() { _logger.LogInformation("Opt Out Button Clicked"); var microsoftAlias = await GetCurrentUserAlias().ConfigureAwait(false); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5)); var currentAdvocates = await _advocateService.GetCurrentAdvocates(cancellationTokenSource.Token).ConfigureAwait(false); var matchingAdvocate = currentAdvocates.SingleOrDefault(x => x.MicrosoftAlias == microsoftAlias); if (matchingAdvocate is null) { OutputText = $"Error: Alias not Found\nEnsure your login, {microsoftAlias}, matches the `alias` field in your YAML file on the cloud-developer-advocates repo: https://github.com/MicrosoftDocs/cloud-developer-advocates/tree/live/advocates"; UpdateButtonText(null); UpdateCurrentPreferenceText(null); } else { var userOptOutModel = await _optOutDatabase.GetOptOutModel(microsoftAlias).ConfigureAwait(false); var updatedOptOutModel = userOptOutModel?.HasOptedOut switch { true => new OptOutModel(matchingAdvocate.MicrosoftAlias, false, userOptOutModel.CreatedAt, DateTimeOffset.UtcNow), false => new OptOutModel(matchingAdvocate.MicrosoftAlias, true, userOptOutModel.CreatedAt, DateTimeOffset.UtcNow), _ => new OptOutModel(matchingAdvocate.MicrosoftAlias, true, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow), }; var savedOptOutModel = userOptOutModel switch { null => await _optOutDatabase.InsertOptOutModel(updatedOptOutModel).ConfigureAwait(false), _ => await _optOutDatabase.PatchOptOutModel(updatedOptOutModel).ConfigureAwait(false) }; OutputText = $"Success! {savedOptOutModel.Alias}'s GitHub Readme Preference has been set to {(savedOptOutModel.HasOptedOut ? _optOutText : _optInText)}"; UpdateButtonText(savedOptOutModel); UpdateCurrentPreferenceText(savedOptOutModel); } UpdateLoggedInLabelText(microsoftAlias); } async Task <string> GetCurrentUserAlias() { var user = await _graphServiceClient.Me.Request().GetAsync().ConfigureAwait(false); var email = user.UserPrincipalName; return(email?.Split('@')[0] ?? throw new NullReferenceException()); } void UpdateLoggedInLabelText(in string microsoftAlias) => LoggedInLabelText = $"Logged in as {microsoftAlias}@microsoft.com"; void UpdateCurrentPreferenceText(in OptOutModel?userOptOutModel) => CurrentPreferenceText = userOptOutModel switch { { HasOptedOut : true } => $"Current Preference: {_optOutText}",
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); }
static async Task <AdovocatesTotalContributionsModel> GetTotalContributions(AdvocateService advocateService, GitHubGraphQLApiService gitHubGraphQLApiService, ILogger log, DateTime from, DateTime to, string?requestedTeam, CancellationToken cancellationToken) { int advocateCount = 0, advocateContributorCount = 0; var teamContributionCount = new SortedDictionary <string, int>(); var currentAdvocates = await advocateService.GetCurrentAdvocates(cancellationToken).ConfigureAwait(false); foreach (var advocate in currentAdvocates) { if (requestedTeam is not null && advocate.Team != requestedTeam) { continue; } log.LogInformation($"Found {advocate.Name}"); advocateCount++; var microsoftDocsContributions = await gitHubGraphQLApiService.GetMicrosoftDocsContributionsCollection(advocate.GitHubUsername, from, to).ConfigureAwait(false); log.LogInformation($"Team: {advocate.Team}"); if (!teamContributionCount.ContainsKey(advocate.Team)) { teamContributionCount.Add(advocate.Team, 0); } if (microsoftDocsContributions.TotalPullRequestContributions + microsoftDocsContributions.TotalPullRequestReviewContributions + microsoftDocsContributions.TotalCommitContributions > 0) { log.LogInformation($"Total Contributions: {microsoftDocsContributions.TotalContributions}"); advocateContributorCount++; teamContributionCount[advocate.Team]++; } } return(new AdovocatesTotalContributionsModel(advocateCount, advocateContributorCount, teamContributionCount)); }