Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Verify OptOut Database Status Again Before Openeing Pull Request
Browse files Browse the repository at this point in the history
  • Loading branch information
brminnick committed Dec 9, 2020
1 parent db34c50 commit 94b51fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
8 changes: 4 additions & 4 deletions GitHubReadmeWebTrends.Common/Database/OptOutDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ public class OptOutDatabase

public OptOutDatabase(ILogger<OptOutDatabase> logger) => _logger = logger;

public IReadOnlyList<OptOutModel> GetAllOptOutModels()
public async Task<IReadOnlyList<OptOutModel>> GetAllOptOutModels()
{
using var connection = new DatabaseContext();

var optOutDatabaseModelList = connection.OptOutDatabaseModel?.ToList() ?? Enumerable.Empty<OptOutDatabaseModel>();
var optOutDatabaseModelList = await connection.OptOutDatabaseModel.ToListAsync().ConfigureAwait(false);

return optOutDatabaseModelList.Select(x => OptOutDatabaseModel.ToOptOutModel(x)).ToList();
}

public OptOutModel? GetOptOutModel(string alias)
public async Task<OptOutModel?> GetOptOutModel(string alias)
{
using var connection = new DatabaseContext();

var optOutDatabaseModel = connection.OptOutDatabaseModel?.SingleOrDefault(x => x.Alias == alias);
var optOutDatabaseModel = await connection.OptOutDatabaseModel.SingleOrDefaultAsync(x => x.Alias == alias).ConfigureAwait(false);

if (optOutDatabaseModel is null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GetAdvocatesFunction
false;
#endif

readonly static IReadOnlyList<string> _betaTesterAliases = new[] { "bramin", "shboyer", "sicotin", "jopapa", "masoucou", "jamont", "v-gmohapi", "judubois", "ropreddy", "sakriema", "juyoo", "mijam", "ninarasi" };
readonly static IReadOnlyList<string> _betaTesterAliases = new[] { "bramin" };

readonly YamlService _yamlService;
readonly OptOutDatabase _optOutDatabase;
Expand All @@ -39,7 +39,7 @@ public async Task GetAzureAdvocatesBetaTestersTimerTrigger([TimerTrigger(_runOnc
{
log.LogInformation($"{nameof(GetAzureAdvocatesBetaTestersTimerTrigger)} Started");

var optOutList = _optOutDatabase.GetAllOptOutModels();
var optOutList = await _optOutDatabase.GetAllOptOutModels().ConfigureAwait(false);

await foreach (var gitHubUser in _cloudAdvocateService.GetAzureAdvocates().ConfigureAwait(false))
{
Expand All @@ -61,7 +61,7 @@ public async Task GetAzureAdvocatesTimerTrigger([TimerTrigger(_runOncePerMonth)]
{
log.LogInformation($"{nameof(GetAzureAdvocatesTimerTrigger)} Started");

var optOutList = _optOutDatabase.GetAllOptOutModels();
var optOutList = await _optOutDatabase.GetAllOptOutModels().ConfigureAwait(false);

await foreach (var gitHubUser in _cloudAdvocateService.GetAzureAdvocates().ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using GitHubReadmeWebTrends.Common;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.WebUtilities;
Expand All @@ -11,10 +12,8 @@
namespace GitHubReadmeWebTrends.Functions
{
//Inspired by https://github.com/spboyer/ca-readme-tracking-links-action/
public static class VerifyWebTrendsFunction
class VerifyWebTrendsFunction
{
const string _webTrendsQueryKey = "WT.mc_id";

static readonly IReadOnlyList<string> _microsoftDomainsList = new[]
{
"microsoft.com",
Expand All @@ -24,11 +23,14 @@ public static class VerifyWebTrendsFunction
};

//https://stackoverflow.com/a/64286141/5953643
static readonly Regex _urlRegex = new Regex(@"(?<!`)(`(?:`{2})?)(?:(?!\1).)*?\1|((?:ht|f)tps?://[\w-]+(?>\.[\w-]+)+(?:[\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?)", RegexOptions.Singleline);
static readonly Regex _localeRegex = new Regex("^/\\w{2}-\\w{2}");
static readonly Regex _urlRegex = new(@"(?<!`)(`(?:`{2})?)(?:(?!\1).)*?\1|((?:ht|f)tps?://[\w-]+(?>\.[\w-]+)+(?:[\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?)", RegexOptions.Singleline);

readonly OptOutDatabase _optOutDatabase;

public VerifyWebTrendsFunction(OptOutDatabase optOutDatabase) => _optOutDatabase = optOutDatabase;

[FunctionName(nameof(VerifyWebTrendsFunction))]
public static void Run([QueueTrigger(QueueConstants.VerifyWebTrendsQueue)] (Repository, CloudAdvocateGitHubUserModel) data, ILogger log,
public async Task Run([QueueTrigger(QueueConstants.VerifyWebTrendsQueue)] (Repository, CloudAdvocateGitHubUserModel) data, ILogger log,
[Queue(QueueConstants.OpenPullRequestQueue)] ICollector<Repository> openPullRequestCollector)
{
log.LogInformation($"{nameof(VerifyWebTrendsFunction)} Started");
Expand All @@ -42,8 +44,17 @@ public static void Run([QueueTrigger(QueueConstants.VerifyWebTrendsQueue)] (Repo

if (!updatedReadme.Equals(repository.ReadmeText))
{
log.LogInformation($"Updated Readme for {repository.Owner} {repository.Name}");
openPullRequestCollector.Add(new Repository(repository.Id, repository.Owner, repository.Name, repository.DefaultBranchOid, repository.DefaultBranchPrefix, repository.DefaultBranchName, repository.IsFork, updatedReadme));
var optOutModel = await _optOutDatabase.GetOptOutModel(gitHubUser.MicrosoftAlias).ConfigureAwait(false);

if (optOutModel?.HasOptedOut is true)
{
log.LogInformation($"Ignoring Readme Because {gitHubUser.FullName} has opted out");
}
else
{
log.LogInformation($"Updated Readme for {repository.Owner} {repository.Name}");
openPullRequestCollector.Add(new Repository(repository.Id, repository.Owner, repository.Name, repository.DefaultBranchOid, repository.DefaultBranchPrefix, repository.DefaultBranchName, repository.IsFork, updatedReadme));
}
}

log.LogInformation($"{nameof(VerifyWebTrendsFunction)} Completed");
Expand Down Expand Up @@ -85,8 +96,14 @@ static string UpdateUrl(in string url, in string eventName, in string channel, i

return url;
}
}

static class UriBuilderExtensions
{
const string _webTrendsQueryKey = "WT.mc_id";
static readonly Regex _localeRegex = new("^/\\w{2}-\\w{2}");

static bool ContainsWebTrendsQuery(this string url)
public static bool ContainsWebTrendsQuery(this string url)
{
var uriBuilder = new UriBuilder(url);
var queryStringDictionary = QueryHelpers.ParseQuery(uriBuilder.Query);
Expand All @@ -104,9 +121,9 @@ static bool ContainsWebTrendsQuery(this string url)
return false;
}

static void RemoveLocale(this UriBuilder builder) => builder.Path = _localeRegex.Replace(builder.Path, string.Empty);
public static void RemoveLocale(this UriBuilder builder) => builder.Path = _localeRegex.Replace(builder.Path, string.Empty);

static void AddWebTrendsQuery(this UriBuilder builder, in string team, in string devOpsId, in string alias)
public static void AddWebTrendsQuery(this UriBuilder builder, in string team, in string devOpsId, in string alias)
{
var webTrendsQueryValue = $"{team}-{devOpsId}-{alias}";

Expand Down
7 changes: 2 additions & 5 deletions GitHubReadmeWebTrends.Website/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using GitHubReadmeWebTrends.Common;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -36,7 +35,7 @@ public IndexModel(OptOutDatabase optOutDatabase, CloudAdvocateService cloudAdvoc
public async Task OnGet()
{
var microsoftAlias = await GetCurrentUserAlias().ConfigureAwait(false);
var userOptOutModel = GetOptOutModel(microsoftAlias);
var userOptOutModel = await _optOutDatabase.GetOptOutModel(microsoftAlias).ConfigureAwait(false);

UpdateButtonText(userOptOutModel);
UpdateLoggedInLabelText(microsoftAlias);
Expand Down Expand Up @@ -66,7 +65,7 @@ public async Task OnPostOptOutButtonClicked()
}
else
{
var userOptOutModel = GetOptOutModel(microsoftAlias);
var userOptOutModel = await _optOutDatabase.GetOptOutModel(microsoftAlias).ConfigureAwait(false);
var updatedOptOutModel = userOptOutModel?.HasOptedOut switch
{
true => new OptOutModel(matchingAzureAdvocate.MicrosoftAlias, false, userOptOutModel.CreatedAt, DateTimeOffset.UtcNow),
Expand Down Expand Up @@ -97,8 +96,6 @@ async Task<string> GetCurrentUserAlias()
return email?.Split('@')[0] ?? throw new NullReferenceException();
}

OptOutModel GetOptOutModel(string microsoftAlias) => _optOutDatabase.GetAllOptOutModels().FirstOrDefault(x => x.Alias.Equals(microsoftAlias, StringComparison.OrdinalIgnoreCase));

void UpdateLoggedInLabelText(in string microsoftAlias) => LoggedInLabelText = $"Logged in as {microsoftAlias}@microsoft.com";

void UpdateCurrentPreferenceText(in OptOutModel? userOptOutModel) => CurrentPreferenceText = userOptOutModel switch
Expand Down

0 comments on commit 94b51fc

Please sign in to comment.