-
Notifications
You must be signed in to change notification settings - Fork 469
/
ReleaseTrackingHelper.cs
515 lines (448 loc) · 22.8 KB
/
ReleaseTrackingHelper.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.PooledObjects;
using Microsoft.CodeAnalysis.Text;
#if MICROSOFT_CODEANALYSIS_ANALYZERS
using Analyzer.Utilities.Extensions;
#endif
namespace Microsoft.CodeAnalysis.ReleaseTracking
{
internal static class ReleaseTrackingHelper
{
internal const string ShippedFileName = "AnalyzerReleases.Shipped.md";
internal const string UnshippedFileName = "AnalyzerReleases.Unshipped.md";
internal const string DisabledText = "Disabled";
internal const string UndetectedText = @"`<Undetected>`";
internal const string ReleasePrefix = "## Release";
internal const string TableTitleNewRules = "### New Rules";
internal const string TableTitleRemovedRules = "### Removed Rules";
internal const string TableTitleChangedRules = "### Changed Rules";
internal const string TableHeaderNewOrRemovedRulesLine1 = @"Rule ID | Category | Severity | Notes";
internal const string TableHeaderNewOrRemovedRulesLine2 = @"--------|----------|----------|-------";
internal const string TableHeaderChangedRulesLine1 = @"Rule ID | New Category | New Severity | Old Category | Old Severity | Notes";
internal const string TableHeaderChangedRulesLine2 = @"--------|--------------|--------------|--------------|--------------|-------";
internal static Version UnshippedVersion { get; } = new Version(int.MaxValue, int.MaxValue);
internal static ReleaseTrackingData ReadReleaseTrackingData(
string path,
SourceText sourceText,
Action<string, Version, string, SourceText, TextLine> onDuplicateEntryInRelease,
Action<TextLine, InvalidEntryKind, string, SourceText> onInvalidEntry,
bool isShippedFile)
{
var releaseTrackingDataByRulesBuilder = new Dictionary<string, ReleaseTrackingDataForRuleBuilder>();
var currentVersion = UnshippedVersion;
ReleaseTrackingHeaderKind? expectedHeaderKind = isShippedFile ? ReleaseTrackingHeaderKind.ReleaseHeader : ReleaseTrackingHeaderKind.TableHeaderTitle;
ReleaseTrackingRuleEntryKind? currentRuleEntryKind = null;
using var versionsBuilder = PooledHashSet<Version>.GetInstance();
foreach (TextLine line in sourceText.Lines)
{
string lineText = line.ToString().Trim();
if (string.IsNullOrWhiteSpace(lineText) || lineText.StartsWith(";", StringComparison.Ordinal))
{
// Skip blank and comment lines.
continue;
}
// Parse release header if applicable.
switch (expectedHeaderKind)
{
case ReleaseTrackingHeaderKind.ReleaseHeader:
// Parse new release, if any.
if (lineText.StartsWith(ReleasePrefix, StringComparison.OrdinalIgnoreCase))
{
// Expect new table after this line.
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderTitle;
// Parse the release version.
string versionString = lineText[ReleasePrefix.Length..].Trim();
if (!Version.TryParse(versionString, out var version))
{
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
}
else
{
currentVersion = version;
versionsBuilder.Add(version);
}
continue;
}
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
case ReleaseTrackingHeaderKind.TableHeaderTitle:
if (lineText.StartsWith(TableTitleNewRules, StringComparison.OrdinalIgnoreCase))
{
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
currentRuleEntryKind = ReleaseTrackingRuleEntryKind.New;
}
else if (lineText.StartsWith(TableTitleRemovedRules, StringComparison.OrdinalIgnoreCase))
{
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Removed;
}
else if (lineText.StartsWith(TableTitleChangedRules, StringComparison.OrdinalIgnoreCase))
{
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1;
currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Changed;
}
else
{
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
}
continue;
case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1:
if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine1, StringComparison.OrdinalIgnoreCase))
{
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2;
continue;
}
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2:
expectedHeaderKind = null;
if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine2, StringComparison.OrdinalIgnoreCase))
{
continue;
}
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1:
if (lineText.StartsWith(TableHeaderChangedRulesLine1, StringComparison.OrdinalIgnoreCase))
{
expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2;
continue;
}
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2:
expectedHeaderKind = null;
if (lineText.StartsWith(TableHeaderChangedRulesLine2, StringComparison.OrdinalIgnoreCase))
{
continue;
}
OnInvalidEntry(line, InvalidEntryKind.Header);
return ReleaseTrackingData.Default;
default:
// We might be starting a new release or table.
if (lineText.StartsWith("## ", StringComparison.OrdinalIgnoreCase))
{
goto case ReleaseTrackingHeaderKind.ReleaseHeader;
}
else if (lineText.StartsWith("### ", StringComparison.OrdinalIgnoreCase))
{
goto case ReleaseTrackingHeaderKind.TableHeaderTitle;
}
break;
}
RoslynDebug.Assert(currentRuleEntryKind != null);
var parts = lineText.Split('|').Select(s => s.Trim()).ToArray();
if (IsInvalidEntry(parts, currentRuleEntryKind.Value))
{
// Report invalid entry, but continue parsing remaining entries.
OnInvalidEntry(line, InvalidEntryKind.Other);
continue;
}
// New or Removed rule entry:
// "Rule ID | Category | Severity | Notes"
// " 0 | 1 | 2 | 3 "
//
// Changed rule entry:
// "Rule ID | New Category | New Severity | Old Category | Old Severity | Notes"
// " 0 | 1 | 2 | 3 | 4 | 5 "
string ruleId = parts[0];
InvalidEntryKind? invalidEntryKind = TryParseFields(parts, categoryIndex: 1, severityIndex: 2,
out var category, out var defaultSeverity, out var enabledByDefault);
if (invalidEntryKind.HasValue)
{
OnInvalidEntry(line, invalidEntryKind.Value);
}
ReleaseTrackingLine releaseTrackingLine;
if (currentRuleEntryKind.Value == ReleaseTrackingRuleEntryKind.Changed)
{
invalidEntryKind = TryParseFields(parts, categoryIndex: 3, severityIndex: 4,
out var oldCategory, out var oldDefaultSeverity, out var oldEnabledByDefault);
if (invalidEntryKind.HasValue)
{
OnInvalidEntry(line, invalidEntryKind.Value);
}
// Verify at least one field is changed for the entry:
if (string.Equals(category, oldCategory, StringComparison.OrdinalIgnoreCase) &&
defaultSeverity == oldDefaultSeverity &&
enabledByDefault == oldEnabledByDefault)
{
OnInvalidEntry(line, InvalidEntryKind.Other);
return ReleaseTrackingData.Default;
}
releaseTrackingLine = new ChangedRuleReleaseTrackingLine(ruleId,
category, enabledByDefault, defaultSeverity,
oldCategory, oldEnabledByDefault, oldDefaultSeverity,
line.Span, sourceText, path, isShippedFile);
}
else
{
releaseTrackingLine = new NewOrRemovedRuleReleaseTrackingLine(ruleId,
category, enabledByDefault, defaultSeverity, line.Span, sourceText,
path, isShippedFile, currentRuleEntryKind.Value);
}
if (!releaseTrackingDataByRulesBuilder.TryGetValue(ruleId, out var releaseTrackingDataForRuleBuilder))
{
releaseTrackingDataForRuleBuilder = new ReleaseTrackingDataForRuleBuilder();
releaseTrackingDataByRulesBuilder.Add(ruleId, releaseTrackingDataForRuleBuilder);
}
releaseTrackingDataForRuleBuilder.AddEntry(currentVersion, releaseTrackingLine, out var hasExistingEntry);
if (hasExistingEntry)
{
onDuplicateEntryInRelease(ruleId, currentVersion, path, sourceText, line);
}
}
var builder = ImmutableSortedDictionary.CreateBuilder<string, ReleaseTrackingDataForRule>();
foreach (var (ruleId, value) in releaseTrackingDataByRulesBuilder)
{
var releaseTrackingDataForRule = new ReleaseTrackingDataForRule(ruleId, value);
builder.Add(ruleId, releaseTrackingDataForRule);
}
return new ReleaseTrackingData(builder.ToImmutable(), versionsBuilder.ToImmutable());
// Local functions
void OnInvalidEntry(TextLine line, InvalidEntryKind invalidEntryKind)
=> onInvalidEntry(line, invalidEntryKind, path, sourceText);
static bool IsInvalidEntry(string[] parts, ReleaseTrackingRuleEntryKind currentRuleEntryKind)
{
// Expected entry for New or Removed rules has 3 or 4 parts:
// "Rule ID | Category | Severity | Notes"
//
// Expected entry for Changed rules has 5 or 6 parts:
// "Rule ID | New Category | New Severity | Old Category | Old Severity | Notes"
//
// NOTE: Last field 'Helplink' is optional for both cases.
if (parts.Length is < 3 or > 6)
{
return true;
}
return currentRuleEntryKind switch
{
ReleaseTrackingRuleEntryKind.New => parts.Length > 4,
ReleaseTrackingRuleEntryKind.Removed => parts.Length > 4,
ReleaseTrackingRuleEntryKind.Changed => parts.Length <= 4,
_ => throw new NotImplementedException()
};
}
static InvalidEntryKind? TryParseFields(
string[] parts, int categoryIndex, int severityIndex,
out string category,
out DiagnosticSeverity? defaultSeverity,
out bool? enabledByDefault)
{
InvalidEntryKind? invalidEntryKind = null;
category = parts[categoryIndex];
if (category.Equals(UndetectedText, StringComparison.OrdinalIgnoreCase))
{
invalidEntryKind = InvalidEntryKind.UndetectedField;
}
var severityPart = parts[severityIndex];
if (Enum.TryParse(severityPart, ignoreCase: true, out DiagnosticSeverity parsedSeverity))
{
defaultSeverity = parsedSeverity;
enabledByDefault = true;
}
else
{
defaultSeverity = null;
if (string.Equals(severityPart, DisabledText, StringComparison.OrdinalIgnoreCase))
{
enabledByDefault = false;
}
else if (severityPart.Equals(UndetectedText, StringComparison.OrdinalIgnoreCase))
{
enabledByDefault = null;
invalidEntryKind = InvalidEntryKind.UndetectedField;
}
else
{
enabledByDefault = null;
invalidEntryKind = InvalidEntryKind.Other;
}
}
return invalidEntryKind;
}
}
}
internal enum InvalidEntryKind
{
Header,
UndetectedField,
Other
}
#pragma warning disable CA1815 // Override equals and operator equals on value types
internal sealed class ReleaseTrackingData
#pragma warning restore CA1815 // Override equals and operator equals on value types
{
public static readonly ReleaseTrackingData Default = new();
public ImmutableSortedDictionary<string, ReleaseTrackingDataForRule> ReleaseTrackingDataByRuleIdMap { get; }
public ImmutableHashSet<Version> Versions { get; }
private ReleaseTrackingData()
: this(ImmutableSortedDictionary<string, ReleaseTrackingDataForRule>.Empty, ImmutableHashSet<Version>.Empty)
{
}
internal ReleaseTrackingData(ImmutableSortedDictionary<string, ReleaseTrackingDataForRule> releaseTrackingDataByRuleIdMap, ImmutableHashSet<Version> versions)
{
ReleaseTrackingDataByRuleIdMap = releaseTrackingDataByRuleIdMap;
Versions = versions;
}
public bool TryGetLatestReleaseTrackingLine(
string ruleId,
[NotNullWhen(returnValue: true)] out Version? version,
[NotNullWhen(returnValue: true)] out ReleaseTrackingLine? releaseTrackingLine)
=> TryGetLatestReleaseTrackingLineCore(ruleId, maxVersion: null, out version, out releaseTrackingLine);
public bool TryGetLatestReleaseTrackingLine(
string ruleId,
Version maxVersion,
[NotNullWhen(returnValue: true)] out Version? version,
[NotNullWhen(returnValue: true)] out ReleaseTrackingLine? releaseTrackingLine)
=> TryGetLatestReleaseTrackingLineCore(ruleId, maxVersion, out version, out releaseTrackingLine);
private bool TryGetLatestReleaseTrackingLineCore(
string ruleId,
Version? maxVersion,
[NotNullWhen(returnValue: true)] out Version? version,
[NotNullWhen(returnValue: true)] out ReleaseTrackingLine? releaseTrackingLine)
{
version = null;
releaseTrackingLine = null;
if (!ReleaseTrackingDataByRuleIdMap.TryGetValue(ruleId, out var releaseTrackingDataForRule) ||
releaseTrackingDataForRule.ReleasesByVersionMap.IsEmpty)
{
return false;
}
foreach (var (releaseVersion, releaseLine) in releaseTrackingDataForRule.ReleasesByVersionMap)
{
if (maxVersion == null || releaseVersion <= maxVersion)
{
version = releaseVersion;
releaseTrackingLine = releaseLine;
return true;
}
}
return false;
}
}
#pragma warning disable CA1815 // Override equals and operator equals on value types
internal readonly struct ReleaseTrackingDataForRule
#pragma warning restore CA1815 // Override equals and operator equals on value types
{
public string RuleId { get; }
public ImmutableSortedDictionary<Version, ReleaseTrackingLine> ReleasesByVersionMap { get; }
internal ReleaseTrackingDataForRule(string ruleId, ReleaseTrackingDataForRuleBuilder builder)
{
RuleId = ruleId;
ReleasesByVersionMap = builder.ToImmutable();
}
}
internal sealed class ReleaseTrackingDataForRuleBuilder
{
private readonly ImmutableSortedDictionary<Version, ReleaseTrackingLine>.Builder _builder
= ImmutableSortedDictionary.CreateBuilder<Version, ReleaseTrackingLine>(ReverseComparer.Instance);
public void AddEntry(Version version, ReleaseTrackingLine releaseTrackingLine, out bool hasExistingEntry)
{
hasExistingEntry = _builder.ContainsKey(version);
_builder[version] = releaseTrackingLine;
}
public ImmutableSortedDictionary<Version, ReleaseTrackingLine> ToImmutable()
=> _builder.ToImmutable();
private sealed class ReverseComparer : IComparer<Version>
{
public static readonly IComparer<Version> Instance = new ReverseComparer();
private ReverseComparer() { }
public int Compare([AllowNull] Version x, [AllowNull] Version y)
{
return (x?.CompareTo(y)).GetValueOrDefault() * -1;
}
}
}
internal abstract class ReleaseTrackingLine
{
public string RuleId { get; }
public string Category { get; }
public bool? EnabledByDefault { get; }
public DiagnosticSeverity? DefaultSeverity { get; }
public TextSpan Span { get; }
public SourceText SourceText { get; }
public string Path { get; }
public bool IsShipped { get; }
public bool IsRemovedRule => Kind == ReleaseTrackingRuleEntryKind.Removed;
public ReleaseTrackingRuleEntryKind Kind { get; }
internal static ReleaseTrackingLine Create(
string ruleId, string category, bool? enabledByDefault,
DiagnosticSeverity? defaultSeverity,
TextSpan span, SourceText sourceText,
string path, bool isShipped, ReleaseTrackingRuleEntryKind kind)
{
return new NewOrRemovedRuleReleaseTrackingLine(ruleId, category,
enabledByDefault, defaultSeverity, span, sourceText, path, isShipped, kind);
}
protected ReleaseTrackingLine(string ruleId, string category, bool? enabledByDefault,
DiagnosticSeverity? defaultSeverity,
TextSpan span, SourceText sourceText,
string path, bool isShipped, ReleaseTrackingRuleEntryKind kind)
{
RuleId = ruleId;
Category = category;
EnabledByDefault = enabledByDefault;
DefaultSeverity = defaultSeverity;
Span = span;
SourceText = sourceText;
Path = path;
IsShipped = isShipped;
Kind = kind;
}
}
internal sealed class NewOrRemovedRuleReleaseTrackingLine : ReleaseTrackingLine
{
internal NewOrRemovedRuleReleaseTrackingLine(
string ruleId, string category, bool? enabledByDefault,
DiagnosticSeverity? defaultSeverity,
TextSpan span, SourceText sourceText,
string path, bool isShipped, ReleaseTrackingRuleEntryKind kind)
: base(ruleId, category, enabledByDefault, defaultSeverity, span, sourceText, path, isShipped, kind)
{
Debug.Assert(kind is ReleaseTrackingRuleEntryKind.New or ReleaseTrackingRuleEntryKind.Removed);
}
}
internal sealed class ChangedRuleReleaseTrackingLine : ReleaseTrackingLine
{
public string OldCategory { get; }
public bool? OldEnabledByDefault { get; }
public DiagnosticSeverity? OldDefaultSeverity { get; }
internal ChangedRuleReleaseTrackingLine(
string ruleId, string category, bool? enabledByDefault,
DiagnosticSeverity? defaultSeverity,
string oldCategory, bool? oldEnabledByDefault,
DiagnosticSeverity? oldDefaultSeverity,
TextSpan span, SourceText sourceText,
string path, bool isShipped)
: base(ruleId, category, enabledByDefault, defaultSeverity, span, sourceText, path, isShipped, ReleaseTrackingRuleEntryKind.Changed)
{
OldCategory = oldCategory;
OldEnabledByDefault = oldEnabledByDefault;
OldDefaultSeverity = oldDefaultSeverity;
}
}
internal enum ReleaseTrackingHeaderKind
{
ReleaseHeader,
TableHeaderTitle,
TableHeaderNewOrRemovedRulesLine1,
TableHeaderNewOrRemovedRulesLine2,
TableHeaderChangedRulesLine1,
TableHeaderChangedRulesLine2,
}
internal enum ReleaseTrackingRuleEntryKind
{
New,
Removed,
Changed,
}
}