-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathProgram.cs
231 lines (187 loc) · 7.53 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System.Net;
using System.Text;
using AdvocateValidation;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
int MAX_RETRIES = 3;
bool debug = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") == "Development";
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
if (!debug)
{
builder.AddConsoleFormatter<GitHubActionsConsoleLogFormatter, GitHubActionsConsoleLogFormatterOptions>(o => o.IncludeScopes = true);
builder.AddConsole(opt => opt.FormatterName = nameof(GitHubActionsConsoleLogFormatter));
}
else
{
builder.AddSimpleConsole(o => o.IncludeScopes = true);
builder.SetMinimumLevel(LogLevel.Debug);
}
});
ILogger<Program> logger = loggerFactory.CreateLogger<Program>();
string[] linkTypesToIgnore = ["LinkedIn", "Reddit"];
string _advocatesPath = debug ?
Path.Combine("../../../../", "advocates") :
Path.Combine("../", "advocates");
IDeserializer _yamlDeserializer = new DeserializerBuilder().Build();
List<CloudAdvocateYamlModel> advocateList = [];
string[] advocateFiles = Directory.GetFiles(_advocatesPath);
List<(string, string)> parsingErrors = [];
List<(string, string)> parsingWarnings = [];
await foreach ((string filePath, CloudAdvocateYamlModel advocate) in GetAdvocateYmlFiles(advocateFiles).ConfigureAwait(false))
{
using var scope = logger.BeginScope("File: {filePath}", filePath);
logger.LogDebug("Parsing {filePath}", filePath);
if (string.IsNullOrWhiteSpace(advocate?.Metadata.Alias))
{
parsingErrors.Add((filePath, "Missing Microsoft Alias"));
continue;
}
if (string.IsNullOrWhiteSpace(advocate.Metadata.Team))
{
parsingErrors.Add((filePath, "Missing Team"));
continue;
}
foreach (Connect connect in advocate.Connect)
{
if (linkTypesToIgnore.Contains(connect.Title, StringComparer.OrdinalIgnoreCase))
{
logger.LogDebug("{Title} doesn't like being validated, skipping {Url}.", connect.Title, connect.Url);
continue;
}
try
{
await EnsureValidUri(filePath, connect.Url, connect.Title).ConfigureAwait(false);
}
catch (ValidationException ex)
{
parsingErrors.Add((filePath, ex.Message));
}
catch (ValidationWarningException ex)
{
parsingWarnings.Add((filePath, ex.Message));
}
catch (Exception ex)
{
parsingErrors.Add((filePath, $"Unexpected error with {connect.Title} Url: {connect.Url} - {ex.Message}"));
}
}
try
{
EnsureValidImage(filePath, advocate.Image);
}
catch (ValidationException ex)
{
parsingErrors.Add((filePath, ex.Message));
}
advocateList.Add(advocate);
}
IEnumerable<string> duplicateAliasList = advocateList
.GroupBy(x => x.Metadata.Alias)
.Where(g => g.Count() > 1)
.Select(x => x.Key);
foreach (string duplicateAlias in duplicateAliasList)
{
parsingErrors.Add(("", $"Duplicate Alias Found; ms.author: {duplicateAlias}"));
}
foreach ((string filePath, string parsingWarning) in parsingWarnings)
{
logger.LogWarning(parsingWarning);
}
if (parsingErrors.Count != 0)
{
logger.LogError("Validation Failed");
foreach ((string filePath, string parsingError) in parsingErrors)
{
logger.LogError(parsingError);
}
throw new Exception("Validation Failed");
}
else
{
logger.LogInformation("Validation Completed Successfully");
}
async IAsyncEnumerable<(string filePath, CloudAdvocateYamlModel advocate)> GetAdvocateYmlFiles(IEnumerable<string> files)
{
var ymlFiles = files.Where(x => x.EndsWith(".yml", StringComparison.OrdinalIgnoreCase));
foreach (var filePath in ymlFiles)
{
var text = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
if (text.StartsWith("### YamlMime:Profile") && !text.StartsWith("### YamlMime:ProfileList"))
{
yield return (filePath, ParseAdvocateFromYaml(text));
}
}
}
CloudAdvocateYamlModel ParseAdvocateFromYaml(in string fileText)
{
var stringReaderFile = new StringReader(fileText);
return _yamlDeserializer.Deserialize<CloudAdvocateYamlModel>(stringReaderFile);
}
async Task EnsureValidUri(string filePath, Uri? uri, string uriName)
{
if (uri is null)
throw new ValidationException($"Missing '{uriName}' Url: {uri} - File: {filePath}");
if (!uri.IsWellFormedOriginalString())
throw new ValidationException($"URI for '{uriName}' is malformed. Url: {uri} - File: {filePath}");
if (uri.Scheme == Uri.UriSchemeHttp)
logger.LogWarning("'{uriName}' Url is HTTP, you really should be hosting on HTTPS. Url: {uri}.", uriName, uri);
await ValidateLinkWithRetry(uri, uriName);
}
async Task ValidateLinkWithRetry(Uri uri, string uriName)
{
bool retry = false;
int retryCount = 0;
do
{
if (retryCount >= MAX_RETRIES)
throw new ValidationException($"Validation of '{uriName}' failed after {MAX_RETRIES} retries. Url: {uri}");
HttpClient _client = new();
HttpResponseMessage response = await _client.GetAsync(uri).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
retry = false;
}
else if (response.StatusCode is HttpStatusCode.TooManyRequests)
{
TimeSpan retryAfter = response.Headers.RetryAfter switch
{
{ Delta: TimeSpan delta } => delta,
{ Date: DateTimeOffset date } => date - DateTimeOffset.UtcNow,
_ => TimeSpan.FromMinutes(1)
};
retry = true;
retryCount++;
logger.LogWarning("{Url} exceeded rate limiting of host. Retrying in {TotalSeconds} seconds.", uri, retryAfter.TotalSeconds);
await Task.Delay(retryAfter).ConfigureAwait(false);
}
else if (response.StatusCode is HttpStatusCode.NotFound)
{
throw new ValidationException($"Failed to resolve URI for '{uriName}' ({response.StatusCode}). Url: {uri}");
}
else if (!response.IsSuccessStatusCode)
{
throw new ValidationWarningException($"Failed to resolve URI for '{uriName}' ({response.StatusCode}) but we're going to ignore it. Url: {uri}");
}
}
while (retry);
}
void EnsureValidImage(in string filePath, in Image? cloudAdvocateImage)
{
if (cloudAdvocateImage is null || string.IsNullOrWhiteSpace(cloudAdvocateImage.Src))
throw new ValidationException($"Image Source Missing: {filePath}");
string filePathRelativeToValidation = Path.Combine(_advocatesPath, cloudAdvocateImage.Src);
if (!File.Exists(filePathRelativeToValidation))
throw new ValidationException($"Image Source Missing: {filePathRelativeToValidation}");
using FileStream fileStream = new(filePathRelativeToValidation, FileMode.Open);
using BinaryReader binaryReader = new(fileStream, Encoding.UTF8);
System.Drawing.Size imageSize = ImageService.GetDimensions(binaryReader);
if (imageSize.Height <= 0)
throw new ValidationException($"Invalid Image Height (must be greater than 0): {filePath}");
if (imageSize.Width <= 0)
throw new ValidationException($"Invalid Image Width (must be greater than 0): {filePath}");
if (imageSize.Height != imageSize.Width)
throw new ValidationException($"Invalid Image (Height and Width must be equal - {imageSize.Height} x {imageSize.Width}): {filePath}");
if (cloudAdvocateImage.Alt is null)
throw new ValidationException($"Image Alt Text Missing: {filePath}");
}