-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathReleaseNotesSerialization.cs
More file actions
414 lines (355 loc) · 13.3 KB
/
ReleaseNotesSerialization.cs
File metadata and controls
414 lines (355 loc) · 13.3 KB
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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Globalization;
using System.IO.Abstractions;
using System.Text.RegularExpressions;
using Elastic.Documentation.Configuration.Serialization;
using Elastic.Documentation.ReleaseNotes;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Elastic.Documentation.Configuration.ReleaseNotes;
/// <summary>
/// Centralized YAML serialization for release notes/changelog operations.
/// Provides static deserializers and serializers configured for different use cases.
/// </summary>
public static partial class ReleaseNotesSerialization
{
/// <summary>
/// Regex to normalize "version:" to "target:" in changelog YAML files.
/// Used for backward compatibility with older changelog formats.
/// </summary>
[GeneratedRegex(@"(\s+)version:", RegexOptions.Multiline)]
public static partial Regex VersionToTargetRegex();
private static readonly IDeserializer YamlDeserializer =
new StaticDeserializerBuilder(new YamlStaticContext())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
private static readonly ISerializer YamlSerializer =
new StaticSerializerBuilder(new YamlStaticContext())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull | DefaultValuesHandling.OmitEmptyCollections)
.WithQuotingNecessaryStrings()
.DisableAliases()
.Build();
/// <summary>
/// Gets the raw YAML deserializer for changelog entry DTOs.
/// Used by bundling service for direct deserialization with error handling.
/// </summary>
public static IDeserializer GetEntryDeserializer() => YamlDeserializer;
/// <summary>
/// Deserializes a changelog entry YAML content to domain type.
/// </summary>
public static ChangelogEntry DeserializeEntry(string yaml)
{
var yamlDto = YamlDeserializer.Deserialize<ChangelogEntryDto>(yaml);
return ToEntry(yamlDto);
}
/// <summary>
/// Converts a raw YAML DTO to domain type.
/// Used by bundling service that handles deserialization separately for error handling.
/// </summary>
public static ChangelogEntry ConvertEntry(ChangelogEntryDto dto) => ToEntry(dto);
/// <summary>
/// Converts a BundledEntry to a ChangelogEntry.
/// Used when inline entry data is provided in bundles.
/// </summary>
public static ChangelogEntry ConvertBundledEntry(BundledEntry entry) => ToEntry(entry);
/// <summary>
/// Deserializes bundled changelog data YAML content to domain type.
/// </summary>
public static Bundle DeserializeBundle(string yaml)
{
var yamlDto = YamlDeserializer.Deserialize<BundleDto>(yaml);
return ToBundle(yamlDto);
}
/// <summary>
/// Serializes a changelog entry to YAML.
/// </summary>
public static string SerializeEntry(ChangelogEntry entry)
{
var dto = ToDto(entry);
return YamlSerializer.Serialize(dto);
}
/// <summary>
/// Serializes bundled changelog data to YAML.
/// </summary>
public static string SerializeBundle(Bundle bundle)
{
var dto = ToDto(bundle);
return YamlSerializer.Serialize(dto);
}
#region Manual Mapping Methods
private static ChangelogEntry ToEntry(ChangelogEntryDto dto) => new()
{
Prs = dto.Prs ?? (dto.Pr != null ? [dto.Pr] : null),
Issues = dto.Issues,
Type = ParseEntryType(dto.Type),
Subtype = ParseEntrySubtype(dto.Subtype),
Products = dto.Products?.Select(ToProductReference).ToList(),
Areas = dto.Areas,
Title = dto.Title ?? "",
Description = dto.Description,
Impact = dto.Impact,
Action = dto.Action,
FeatureId = dto.FeatureId,
Highlight = dto.Highlight
};
private static ChangelogEntry ToEntry(BundledEntry entry) => new()
{
Prs = entry.Prs,
Issues = entry.Issues,
Type = entry.Type ?? ChangelogEntryType.Invalid,
Subtype = entry.Subtype,
Products = entry.Products,
Areas = entry.Areas,
Title = entry.Title ?? "",
Description = entry.Description,
Impact = entry.Impact,
Action = entry.Action,
FeatureId = entry.FeatureId,
Highlight = entry.Highlight
};
private static ProductReference ToProductReference(ProductInfoDto dto) => new()
{
ProductId = dto.Product ?? "",
Target = dto.Target,
Lifecycle = ParseLifecycle(dto.Lifecycle)
};
private static Bundle ToBundle(BundleDto dto) => new()
{
Products = dto.Products?.Select(ToBundledProduct).ToList() ?? [],
Description = dto.Description,
ReleaseDate = ParseReleaseDate(dto.ReleaseDate),
ShowReleaseDates = dto.ShowReleaseDates ?? false,
HideFeatures = dto.HideFeatures ?? [],
Entries = dto.Entries?.Select(ToBundledEntry).ToList() ?? []
};
private static BundledProduct ToBundledProduct(BundledProductDto dto) => new()
{
ProductId = dto.Product ?? "",
Target = dto.Target,
Lifecycle = ParseLifecycle(dto.Lifecycle),
Repo = dto.Repo,
Owner = dto.Owner
};
private static BundledEntry ToBundledEntry(BundledEntryDto dto) => new()
{
File = dto.File != null ? ToBundledFile(dto.File) : null,
Type = ParseEntryTypeNullable(dto.Type),
Title = dto.Title,
Products = dto.Products?.Select(ToProductReference).ToList(),
Description = dto.Description,
Impact = dto.Impact,
Action = dto.Action,
FeatureId = dto.FeatureId,
Highlight = dto.Highlight,
Subtype = ParseEntrySubtype(dto.Subtype),
Areas = dto.Areas,
Prs = dto.Prs ?? (dto.Pr != null ? [dto.Pr] : null),
Issues = dto.Issues
};
private static BundledFile ToBundledFile(BundledFileDto dto) => new()
{
Name = dto.Name ?? "",
Checksum = dto.Checksum ?? ""
};
private static ChangelogEntryType ParseEntryType(string? value)
{
if (string.IsNullOrEmpty(value))
return ChangelogEntryType.Invalid;
return ChangelogEntryTypeExtensions.TryParse(value, out var result, ignoreCase: true, allowMatchingMetadataAttribute: true)
? result
: ChangelogEntryType.Invalid;
}
private static ChangelogEntryType? ParseEntryTypeNullable(string? value)
{
if (string.IsNullOrEmpty(value))
return null;
return ChangelogEntryTypeExtensions.TryParse(value, out var result, ignoreCase: true, allowMatchingMetadataAttribute: true)
? result
: null;
}
private static ChangelogEntrySubtype? ParseEntrySubtype(string? value)
{
if (string.IsNullOrEmpty(value))
return null;
return ChangelogEntrySubtypeExtensions.TryParse(value, out var result, ignoreCase: true, allowMatchingMetadataAttribute: true)
? result
: null;
}
private static Lifecycle? ParseLifecycle(string? value)
{
if (string.IsNullOrEmpty(value))
return null;
return LifecycleExtensions.TryParse(value, out var result, ignoreCase: true, allowMatchingMetadataAttribute: true)
? result
: null;
}
private static DateOnly? ParseReleaseDate(string? value) =>
DateOnly.TryParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date)
? date
: null;
// Reverse mappings (Domain → DTO) for serialization
private static ChangelogEntryDto ToDto(ChangelogEntry entry) => new()
{
Prs = entry.Prs?.ToList(),
Issues = entry.Issues?.ToList(),
Type = EntryTypeToString(entry.Type),
Subtype = EntrySubtypeToString(entry.Subtype),
Products = entry.Products?.Select(ToDto).ToList(),
Areas = entry.Areas?.ToList(),
Title = entry.Title,
Description = entry.Description,
Impact = entry.Impact,
Action = entry.Action,
FeatureId = entry.FeatureId,
Highlight = entry.Highlight
};
private static ProductInfoDto ToDto(ProductReference product) => new()
{
Product = product.ProductId,
Target = product.Target,
Lifecycle = LifecycleToString(product.Lifecycle)
};
private static BundleDto ToDto(Bundle bundle) => new()
{
Products = bundle.Products.Select(ToDto).ToList(),
Description = bundle.Description,
ReleaseDate = bundle.ReleaseDate?.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
ShowReleaseDates = bundle.ShowReleaseDates ? bundle.ShowReleaseDates : null,
HideFeatures = bundle.HideFeatures.Count > 0 ? bundle.HideFeatures.ToList() : null,
Entries = bundle.Entries.Select(ToDto).ToList()
};
private static BundledProductDto ToDto(BundledProduct product) => new()
{
Product = product.ProductId,
Target = product.Target,
Lifecycle = LifecycleToString(product.Lifecycle),
Repo = product.Repo,
Owner = product.Owner
};
private static BundledEntryDto ToDto(BundledEntry entry) => new()
{
File = entry.File != null ? ToDto(entry.File) : null,
Type = EntryTypeNullableToString(entry.Type),
Title = entry.Title,
Products = entry.Products?.Select(ToDto).ToList(),
Description = entry.Description,
Impact = entry.Impact,
Action = entry.Action,
FeatureId = entry.FeatureId,
Highlight = entry.Highlight,
Subtype = EntrySubtypeToString(entry.Subtype),
Areas = entry.Areas?.ToList(),
Prs = entry.Prs?.ToList(),
Issues = entry.Issues?.ToList()
};
private static BundledFileDto ToDto(BundledFile file) => new()
{
Name = file.Name,
Checksum = file.Checksum
};
// Reverse enum conversion helpers
private static string? EntryTypeToString(ChangelogEntryType value) =>
value != ChangelogEntryType.Invalid ? value.ToStringFast(true) : null;
private static string? EntryTypeNullableToString(ChangelogEntryType? value) =>
value?.ToStringFast(true);
private static string? EntrySubtypeToString(ChangelogEntrySubtype? value) =>
value?.ToStringFast(true);
private static string? LifecycleToString(Lifecycle? value) =>
value?.ToStringFast(true);
#endregion
/// <summary>
/// Normalizes a YAML string by converting "version:" fields to "target:" for backward compatibility.
/// Also strips comment lines.
/// </summary>
/// <param name="yaml">The raw YAML content.</param>
/// <returns>The normalized YAML content.</returns>
public static string NormalizeYaml(string yaml)
{
// Skip comment lines
var yamlLines = yaml.Split('\n');
var yamlWithoutComments = string.Join('\n', yamlLines.Where(line => !line.TrimStart().StartsWith('#')));
// Normalize version to target
return VersionToTargetRegex().Replace(yamlWithoutComments, "$1target:");
}
/// <summary>
/// Loads the publish blocker configuration from a changelog.yml file.
/// Uses AOT-compatible deserialization via YamlStaticContext.
/// Supports the new 'rules:' format with include/exclude modes.
/// </summary>
/// <param name="fileSystem">The file system to read from.</param>
/// <param name="configPath">The path to the changelog.yml configuration file.</param>
/// <param name="productId">Optional product ID to load product-specific blocker.</param>
/// <returns>The publish blocker configuration, or null if not found or not configured.</returns>
private static PublishBlocker? ParsePublishBlocker(PublishRulesMinimalDto? dto, MatchMode matchAreas)
{
if (dto == null)
return null;
var excludeTypes = dto.ExcludeTypes?.Count > 0 ? dto.ExcludeTypes.ToList() : null;
var includeTypes = dto.IncludeTypes?.Count > 0 ? dto.IncludeTypes.ToList() : null;
var excludeAreas = dto.ExcludeAreas?.Count > 0 ? dto.ExcludeAreas.ToList() : null;
var includeAreas = dto.IncludeAreas?.Count > 0 ? dto.IncludeAreas.ToList() : null;
var types = excludeTypes ?? includeTypes;
var areas = excludeAreas ?? includeAreas;
if (types == null && areas == null)
return null;
return new PublishBlocker
{
Types = types,
TypesMode = includeTypes != null ? FieldMode.Include : FieldMode.Exclude,
Areas = areas,
AreasMode = includeAreas != null ? FieldMode.Include : FieldMode.Exclude,
MatchAreas = matchAreas
};
}
private static MatchMode? ParseMatchMode(string? value) =>
value?.ToLowerInvariant() switch
{
"any" => MatchMode.Any,
"all" => MatchMode.All,
"conjunction" => MatchMode.Conjunction,
_ => null
};
}
/// <summary>
/// Minimal DTO for changelog configuration - only includes rules configuration.
/// Used for AOT-compatible lightweight loading of publish blocker configuration.
/// Registered with YamlStaticContext for source-generated deserialization.
/// </summary>
public sealed class ChangelogConfigMinimalDto
{
/// <summary>Rules configuration section (new format).</summary>
public RulesConfigMinimalDto? Rules { get; set; }
}
/// <summary>
/// Minimal DTO for rules configuration.
/// Registered with YamlStaticContext for source-generated deserialization.
/// </summary>
public sealed class RulesConfigMinimalDto
{
/// <summary>Global match mode ("any" or "all").</summary>
public string? Match { get; set; }
/// <summary>Publish rules configuration.</summary>
public PublishRulesMinimalDto? Publish { get; set; }
}
/// <summary>
/// Minimal DTO for publish rules configuration.
/// Registered with YamlStaticContext for source-generated deserialization.
/// </summary>
public sealed class PublishRulesMinimalDto
{
/// <summary>Match mode for areas ("any" or "all").</summary>
public string? MatchAreas { get; set; }
/// <summary>Entry types to exclude from publishing.</summary>
public List<string>? ExcludeTypes { get; set; }
/// <summary>Entry types to include for publishing.</summary>
public List<string>? IncludeTypes { get; set; }
/// <summary>Entry areas to exclude from publishing.</summary>
public List<string>? ExcludeAreas { get; set; }
/// <summary>Entry areas to include for publishing.</summary>
public List<string>? IncludeAreas { get; set; }
/// <summary>Per-product publish rule overrides.</summary>
public Dictionary<string, PublishRulesMinimalDto?>? Products { get; set; }
}