-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathChangelogConfigurationLoader.cs
More file actions
1217 lines (1080 loc) · 42.7 KB
/
ChangelogConfigurationLoader.cs
File metadata and controls
1217 lines (1080 loc) · 42.7 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.IO.Abstractions;
using Elastic.Changelog.Serialization;
using Elastic.Documentation;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Configuration.Changelog;
using Elastic.Documentation.Configuration.Products;
using Elastic.Documentation.Diagnostics;
using Elastic.Documentation.ReleaseNotes;
using Microsoft.Extensions.Logging;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Elastic.Changelog.Configuration;
/// <summary>
/// Service for loading and validating changelog configuration
/// </summary>
public class ChangelogConfigurationLoader(ILoggerFactory logFactory, IConfigurationContext configurationContext, IFileSystem fileSystem)
{
private readonly ILogger _logger = logFactory.CreateLogger<ChangelogConfigurationLoader>();
private static readonly IDeserializer ConfigurationDeserializer =
new StaticDeserializerBuilder(new ChangelogYamlStaticContext())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeConverter(new YamlLenientListConverter())
.WithTypeConverter(new TypeEntryYamlConverter())
.Build();
/// <summary>
/// Deserializes changelog configuration YAML content.
/// </summary>
internal static ChangelogConfigurationYaml DeserializeConfiguration(string yaml) =>
ConfigurationDeserializer.Deserialize<ChangelogConfigurationYaml>(yaml);
/// <summary>
/// Loads the publish blocker configuration from a changelog.
/// </summary>
/// <param name="fileSystem">The file system to read from.</param>
/// <param name="configPath">The path to the changelog.yml configuration file.</param>
/// <returns>The publish blocker configuration, or null if not found.</returns>
public static PublishBlocker? LoadPublishBlocker(IFileSystem fileSystem, string configPath)
{
if (!fileSystem.File.Exists(configPath))
return null;
var yamlContent = fileSystem.File.ReadAllText(configPath);
var yamlConfig = DeserializeConfiguration(yamlContent);
if (yamlConfig.Rules?.Publish == null)
return null;
var globalMatch = ParseMatchMode(yamlConfig.Rules.Match) ?? MatchMode.Any;
var publishMatchAreas = ParseMatchMode(yamlConfig.Rules.Publish.MatchAreas) ?? globalMatch;
return ParsePublishBlocker(yamlConfig.Rules.Publish, publishMatchAreas);
}
/// <summary>
/// Loads changelog configuration from file or returns default configuration
/// </summary>
public async Task<ChangelogConfiguration?> LoadChangelogConfiguration(IDiagnosticsCollector collector, string? configPath, Cancel ctx)
{
// Determine config file path
var finalConfigPath = configPath ?? fileSystem.Path.Join(fileSystem.Directory.GetCurrentDirectory(), "docs", "changelog.yml");
if (!fileSystem.File.Exists(finalConfigPath))
{
// Use default configuration if file doesn't exist
_logger.LogWarning("Changelog configuration not found at {ConfigPath}, using defaults", finalConfigPath);
return ChangelogConfiguration.Default;
}
try
{
var yamlContent = await fileSystem.File.ReadAllTextAsync(finalConfigPath, ctx);
var yamlConfig = DeserializeConfiguration(yamlContent);
return ParseConfiguration(collector, yamlConfig, finalConfigPath);
}
catch (IOException ex)
{
collector.EmitError(finalConfigPath, $"I/O error loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (UnauthorizedAccessException ex)
{
collector.EmitError(finalConfigPath, $"Access denied loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (YamlException ex)
{
collector.EmitError(finalConfigPath, $"YAML parsing error in changelog configuration: {ex.Message}", ex);
return null;
}
}
private ChangelogConfiguration? ParseConfiguration(IDiagnosticsCollector collector, ChangelogConfigurationYaml yamlConfig, string configPath)
{
var validProductIds = configurationContext.ProductsConfiguration.Products.Keys.ToHashSet(StringComparer.OrdinalIgnoreCase);
// Detect old 'block:' key
if (yamlConfig.Block != null)
{
collector.EmitError(configPath, "'block' is no longer supported. Rename to 'rules'. See changelog.example.yml.");
return null;
}
// Compute values from pivot configuration
IReadOnlyList<string> availableTypes;
IReadOnlyList<string> availableSubtypes;
IReadOnlyList<string>? availableAreas;
Dictionary<string, string>? labelToType;
Dictionary<string, List<string>>? labelToAreas;
Dictionary<string, string>? labelToProducts;
PivotConfiguration? pivot = null;
if (yamlConfig.Pivot != null)
{
// Convert YAML pivot to domain pivot
pivot = ConvertPivot(yamlConfig.Pivot);
// Compute available types from pivot.types keys
if (yamlConfig.Pivot.Types is { Count: > 0 })
{
// Validate types against enum values using TryParse
foreach (var typeName in yamlConfig.Pivot.Types.Keys)
{
if (ChangelogEntryTypeExtensions.TryParse(typeName, out _, ignoreCase: true, allowMatchingMetadataAttribute: true))
continue;
collector.EmitError(configPath, $"Type '{typeName}' in pivot.types is not a valid type. Valid types: {string.Join(", ", ChangelogConfiguration.DefaultTypes)}");
return null;
}
// Validate required types are present
foreach (var requiredType in ChangelogConfiguration.RequiredTypes)
{
var requiredTypeName = requiredType.ToStringFast(true);
if (yamlConfig.Pivot.Types.Keys.Any(k => k.Equals(requiredTypeName, StringComparison.OrdinalIgnoreCase)))
continue;
collector.EmitError(configPath, $"Required type '{requiredTypeName}' is missing from pivot.types. Required types: {string.Join(", ", ChangelogConfiguration.RequiredTypes.Select(t => t.ToStringFast(true)))}");
return null;
}
// Validate subtypes only appear under breaking-change
foreach (var (typeName, typeEntry) in yamlConfig.Pivot.Types)
{
if (typeEntry?.Subtypes is not { Count: > 0 })
continue;
if (!typeName.Equals(ChangelogEntryType.BreakingChange.ToStringFast(true), StringComparison.OrdinalIgnoreCase))
{
collector.EmitError(configPath, $"Type '{typeName}' has subtypes defined, but subtypes are only allowed for 'breaking-change' type.");
return null;
}
// Validate subtype values against enum
foreach (var subtypeName in typeEntry.Subtypes.Keys)
{
if (ChangelogEntrySubtypeExtensions.TryParse(subtypeName, out _, ignoreCase: true, allowMatchingMetadataAttribute: true))
continue;
collector.EmitError(configPath, $"Subtype '{subtypeName}' in pivot.types.{typeName}.subtypes is not a valid subtype. Valid subtypes: {string.Join(", ", ChangelogConfiguration.DefaultSubtypes)}");
return null;
}
}
availableTypes = yamlConfig.Pivot.Types.Keys.ToList();
}
else
availableTypes = ChangelogConfiguration.DefaultTypes;
// Compute available subtypes from pivot.subtypes keys
if (yamlConfig.Pivot.Subtypes != null && yamlConfig.Pivot.Subtypes.Count > 0)
{
// Validate subtypes against enum values using TryParse
foreach (var subtypeName in yamlConfig.Pivot.Subtypes.Keys)
{
if (!ChangelogEntrySubtypeExtensions.TryParse(subtypeName, out _, ignoreCase: true, allowMatchingMetadataAttribute: true))
{
collector.EmitError(configPath, $"Subtype '{subtypeName}' in pivot.subtypes is not a valid subtype. Valid subtypes: {string.Join(", ", ChangelogConfiguration.DefaultSubtypes)}");
return null;
}
}
availableSubtypes = yamlConfig.Pivot.Subtypes.Keys.ToList();
}
else
availableSubtypes = ChangelogConfiguration.DefaultSubtypes;
// Compute available areas from pivot.areas keys
availableAreas = yamlConfig.Pivot.Areas != null && yamlConfig.Pivot.Areas.Count > 0
? yamlConfig.Pivot.Areas.Keys.ToList()
: null;
// Build LabelToType mapping (inverted from pivot.types)
labelToType = BuildLabelToTypeMapping(yamlConfig.Pivot.Types);
// Build LabelToAreas mapping (inverted from pivot.areas)
labelToAreas = BuildLabelToAreasMapping(yamlConfig.Pivot.Areas);
// Validate product IDs in pivot.products keys and build LabelToProducts mapping
if (yamlConfig.Pivot.Products is { Count: > 0 })
{
foreach (var productSpec in yamlConfig.Pivot.Products.Keys)
{
var specParts = productSpec.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (specParts.Length == 0)
continue;
var productId = specParts[0].Replace('_', '-');
if (validProductIds.Contains(productId))
continue;
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"Product '{specParts[0]}' in pivot.products is not in the list of available products from config/products.yml. Available products: {availableProducts}");
return null;
}
}
labelToProducts = BuildLabelToProductsMapping(yamlConfig.Pivot.Products);
}
else
{
// No pivot configuration - use defaults
availableTypes = ChangelogConfiguration.DefaultTypes;
availableSubtypes = ChangelogConfiguration.DefaultSubtypes;
availableAreas = null;
labelToType = null;
labelToAreas = null;
labelToProducts = null;
}
// Process lifecycles
IReadOnlyList<Lifecycle> lifecycles;
var lifecycleValues = yamlConfig.Lifecycles?.Values;
if (lifecycleValues == null || lifecycleValues.Count == 0)
lifecycles = ChangelogConfiguration.DefaultLifecycles;
else
{
var parsedLifecycles = new List<Lifecycle>();
foreach (var lifecycleStr in lifecycleValues)
{
if (!LifecycleExtensions.TryParse(lifecycleStr, out var lifecycle, ignoreCase: true, allowMatchingMetadataAttribute: true))
{
collector.EmitError(configPath, $"Lifecycle '{lifecycleStr}' in changelog.yml is not valid. Valid lifecycles: {string.Join(", ", ChangelogConfiguration.DefaultLifecycles.Select(l => l.ToStringFast(true)))}");
return null;
}
parsedLifecycles.Add(lifecycle);
}
lifecycles = parsedLifecycles;
}
// Process products from products.available
IReadOnlyList<Product>? products = null;
var productIdList = yamlConfig.Products?.Available?.Values;
if (productIdList is { Count: > 0 })
{
var resolvedProducts = new List<Product>();
foreach (var productId in productIdList)
{
var normalizedProductId = productId.Replace('_', '-');
if (!validProductIds.Contains(normalizedProductId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"Product '{productId}' in changelog.yml is not in the list of available products from config/products.yml. Available products: {availableProducts}");
return null;
}
if (configurationContext.ProductsConfiguration.Products.TryGetValue(normalizedProductId, out var product))
resolvedProducts.Add(product);
}
products = resolvedProducts;
}
// Process rules configuration
var rules = ParseRulesConfiguration(collector, yamlConfig.Rules, configPath, validProductIds);
if (rules == null && collector.Errors > 0)
return null;
// Process highlight labels from pivot configuration
var highlightLabels = yamlConfig.Pivot?.Highlight?.Values;
// Process products configuration
ProductsConfig? productsConfig = null;
if (yamlConfig.Products != null)
productsConfig = ParseProductsConfig(collector, yamlConfig.Products, configPath, validProductIds);
// Process bundle configuration
BundleConfiguration? bundleConfig = null;
if (yamlConfig.Bundle != null)
{
bundleConfig = ParseBundleConfiguration(collector, configPath, yamlConfig.Bundle);
if (bundleConfig == null)
return null;
}
// Process extract configuration
var extract = new ExtractConfiguration
{
ReleaseNotes = yamlConfig.Extract?.ReleaseNotes ?? true,
Issues = yamlConfig.Extract?.Issues ?? true,
StripTitlePrefix = yamlConfig.Extract?.StripTitlePrefix ?? false
};
// Process filename strategy
var filenameStrategy = FilenameStrategy.Timestamp;
if (!string.IsNullOrWhiteSpace(yamlConfig.Filename))
{
if (!FilenameStrategyExtensions.TryParse(yamlConfig.Filename, out var parsed, ignoreCase: true, allowMatchingMetadataAttribute: true))
{
var valid = string.Join(", ", FilenameStrategyExtensions.GetValues().Select(v => v.ToStringFast(true)));
collector.EmitError(configPath, $"filename: '{yamlConfig.Filename}' is not valid. Use one of: {valid}");
return null;
}
filenameStrategy = parsed;
}
var labelToAreasReadOnly = labelToAreas?.ToDictionary(
kvp => kvp.Key,
kvp => (IReadOnlyList<string>)kvp.Value,
StringComparer.OrdinalIgnoreCase);
return new ChangelogConfiguration
{
Pivot = pivot,
Types = availableTypes,
SubTypes = availableSubtypes,
Lifecycles = lifecycles,
Areas = availableAreas,
Products = products,
LabelToType = labelToType,
LabelToAreas = labelToAreasReadOnly,
LabelToProducts = labelToProducts,
Rules = rules,
HighlightLabels = highlightLabels,
ProductsConfiguration = productsConfig,
Bundle = bundleConfig,
Extract = extract,
Filename = filenameStrategy
};
}
private static PivotConfiguration ConvertPivot(PivotConfigurationYaml yamlPivot)
{
Dictionary<string, TypeEntry?>? types = null;
if (yamlPivot.Types != null)
{
types = yamlPivot.Types.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value == null
? null
: new TypeEntry
{
Labels = kvp.Value.Labels,
Subtypes = ConvertLenientDictToStringDict(kvp.Value.Subtypes)
});
}
return new PivotConfiguration
{
Types = types,
Subtypes = ConvertLenientDictToStringDict(yamlPivot.Subtypes),
Areas = ConvertLenientDictToStringDict(yamlPivot.Areas),
Products = ConvertLenientDictToStringDict(yamlPivot.Products),
Highlight = JoinLenientList(yamlPivot.Highlight)
};
}
/// <summary>
/// Converts a dictionary with YamlLenientList values to a dictionary with comma-joined string values.
/// </summary>
private static Dictionary<string, string?>? ConvertLenientDictToStringDict(Dictionary<string, YamlLenientList?>? source)
{
if (source == null || source.Count == 0)
return null;
return source.ToDictionary(
kvp => kvp.Key,
kvp => JoinLenientList(kvp.Value)
);
}
/// <summary>
/// Joins a YamlLenientList into a comma-separated string, or returns null.
/// </summary>
private static string? JoinLenientList(YamlLenientList? list) =>
list?.Values is { Count: > 0 } values ? string.Join(", ", values) : null;
private ProductsConfig? ParseProductsConfig(
IDiagnosticsCollector collector,
ProductsConfigYaml yaml,
string configPath,
HashSet<string> validProductIds)
{
// Validate available products
List<string>? available = null;
var availableValues = yaml.Available?.Values;
if (availableValues is { Count: > 0 })
{
available = [];
foreach (var productId in availableValues)
{
var normalizedProductId = productId.Replace('_', '-');
if (!validProductIds.Contains(normalizedProductId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"Product '{productId}' in products_config.available is not in the list of available products from config/products.yml. Available products: {availableProducts}");
return null;
}
available.Add(normalizedProductId);
}
}
// Parse default products
List<DefaultProduct>? defaultProducts = null;
if (yaml.Default is { Count: > 0 })
{
defaultProducts = [];
foreach (var defaultYaml in yaml.Default)
{
if (string.IsNullOrWhiteSpace(defaultYaml.Product))
{
collector.EmitError(configPath, "Default product in products_config.default must have a product ID");
return null;
}
var normalizedProductId = defaultYaml.Product.Replace('_', '-');
if (!validProductIds.Contains(normalizedProductId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"Product '{defaultYaml.Product}' in products_config.default is not in the list of available products from config/products.yml. Available products: {availableProducts}");
return null;
}
defaultProducts.Add(new DefaultProduct
{
Product = normalizedProductId,
Lifecycle = defaultYaml.Lifecycle ?? "ga"
});
}
}
return new ProductsConfig
{
Available = available,
Default = defaultProducts
};
}
private static BundleConfiguration? ParseBundleConfiguration(IDiagnosticsCollector collector, string configPath, BundleConfigurationYaml yaml)
{
if (!string.IsNullOrWhiteSpace(yaml.Repo) && yaml.Repo.Contains('+', StringComparison.Ordinal))
{
collector.EmitError(
configPath,
"bundle.repo must name a single GitHub repository. Remove '+' merged-repo syntax from bundle.repo.");
return null;
}
if (yaml.Profiles is { Count: > 0 })
{
foreach (var kvp in yaml.Profiles)
{
var profileRepo = kvp.Value?.Repo;
if (!string.IsNullOrWhiteSpace(profileRepo) && profileRepo.Contains('+', StringComparison.Ordinal))
{
collector.EmitError(
configPath,
$"bundle.profiles.{kvp.Key}.repo must name a single GitHub repository. Remove '+' merged-repo syntax.");
return null;
}
}
}
IReadOnlyList<string>? linkAllowRepos = null;
if (yaml.LinkAllowRepos != null)
{
var raw = yaml.LinkAllowRepos.Values ?? [];
var list = new List<string>();
foreach (var v in raw)
{
if (string.IsNullOrWhiteSpace(v))
continue;
var trimmed = v.Trim();
if (trimmed.IndexOf('/') < 0 ||
trimmed.IndexOf('/') != trimmed.LastIndexOf('/'))
{
collector.EmitError(
configPath,
$"bundle.link_allow_repos: each entry must be exactly 'owner/repo' (one slash). Invalid: '{v}'.");
return null;
}
var slash = trimmed.IndexOf('/');
if (slash <= 0 || slash >= trimmed.Length - 1)
{
collector.EmitError(
configPath,
$"bundle.link_allow_repos: each entry must be exactly 'owner/repo' (one slash). Invalid: '{v}'.");
return null;
}
list.Add(trimmed);
}
linkAllowRepos = list;
}
Dictionary<string, BundleProfile>? profiles = null;
if (yaml.Profiles is { Count: > 0 })
{
profiles = yaml.Profiles.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value is null
? new BundleProfile()
: new BundleProfile
{
Products = kvp.Value.Products,
Output = kvp.Value.Output,
OutputProducts = kvp.Value.OutputProducts,
Description = kvp.Value.Description,
Repo = kvp.Value.Repo,
Owner = kvp.Value.Owner,
HideFeatures = kvp.Value.HideFeatures?.Values,
ShowReleaseDates = kvp.Value.ShowReleaseDates,
Source = kvp.Value.Source
});
}
return new BundleConfiguration
{
Directory = yaml.Directory,
OutputDirectory = yaml.OutputDirectory,
Resolve = yaml.Resolve ?? true,
Description = yaml.Description,
Repo = yaml.Repo,
Owner = yaml.Owner,
LinkAllowRepos = linkAllowRepos,
ShowReleaseDates = yaml.ShowReleaseDates ?? false,
Profiles = profiles
};
}
/// <summary>
/// Loads changelog configuration from a specific path, treating a missing file as a hard error.
/// Used in profile mode when an explicit config path was provided (e.g. in tests).
/// </summary>
public async Task<ChangelogConfiguration?> LoadChangelogConfigurationRequired(IDiagnosticsCollector collector, string configPath, Cancel ctx)
{
if (!fileSystem.File.Exists(configPath))
{
collector.EmitError(
configPath,
$"Changelog configuration file not found at '{configPath}'. " +
"Either run 'docs-builder changelog init' to create one, " +
"or re-run from the folder where changelog.yml exists."
);
return null;
}
try
{
var yamlContent = await fileSystem.File.ReadAllTextAsync(configPath, ctx);
var yamlConfig = DeserializeConfiguration(yamlContent);
return ParseConfiguration(collector, yamlConfig, configPath);
}
catch (IOException ex)
{
collector.EmitError(configPath, $"I/O error loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (UnauthorizedAccessException ex)
{
collector.EmitError(configPath, $"Access denied loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (YamlDotNet.Core.YamlException ex)
{
collector.EmitError(configPath, $"YAML parsing error in changelog configuration: {ex.Message}", ex);
return null;
}
}
/// <summary>
/// Discovers and loads the changelog configuration for profile mode.
/// Unlike <see cref="LoadChangelogConfiguration"/>, this method treats a missing config file as a
/// hard error. It searches for <c>changelog.yml</c> then <c>docs/changelog.yml</c> relative to the
/// current working directory, so the command works when run from any folder that contains the file.
/// </summary>
public async Task<ChangelogConfiguration?> LoadChangelogConfigurationForProfileMode(IDiagnosticsCollector collector, Cancel ctx)
{
var cwd = fileSystem.Directory.GetCurrentDirectory();
var candidates = new[]
{
fileSystem.Path.Join(cwd, "changelog.yml"),
fileSystem.Path.Join(cwd, "docs", "changelog.yml")
};
var foundPath = candidates.FirstOrDefault(fileSystem.File.Exists);
if (foundPath == null)
{
collector.EmitError(
string.Empty,
"changelog.yml not found. Profile-based commands require a changelog configuration file. " +
"Either run 'docs-builder changelog init' to create one, " +
"or re-run this command from the folder where changelog.yml exists " +
"(e.g. the project root if the file is at docs/changelog.yml)."
);
return null;
}
try
{
var yamlContent = await fileSystem.File.ReadAllTextAsync(foundPath, ctx);
var yamlConfig = DeserializeConfiguration(yamlContent);
return ParseConfiguration(collector, yamlConfig, foundPath);
}
catch (IOException ex)
{
collector.EmitError(foundPath, $"I/O error loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (UnauthorizedAccessException ex)
{
collector.EmitError(foundPath, $"Access denied loading changelog configuration: {ex.Message}", ex);
return null;
}
catch (YamlDotNet.Core.YamlException ex)
{
collector.EmitError(foundPath, $"YAML parsing error in changelog configuration: {ex.Message}", ex);
return null;
}
}
private RulesConfiguration? ParseRulesConfiguration(
IDiagnosticsCollector collector,
RulesConfigurationYaml? rulesYaml,
string configPath,
HashSet<string> validProductIds)
{
if (rulesYaml == null)
return null;
// Parse global match mode
var globalMatch = MatchMode.Any;
if (!string.IsNullOrWhiteSpace(rulesYaml.Match))
{
var parsed = ParseMatchMode(rulesYaml.Match);
if (parsed == null)
{
collector.EmitError(configPath, $"rules.match: '{rulesYaml.Match}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
globalMatch = parsed.Value;
}
// Parse create rules
var createRules = ParseCreateRules(collector, rulesYaml.Create, configPath, validProductIds, "rules.create", globalMatch);
if (createRules == null && collector.Errors > 0)
return null;
// Parse bundle rules
var bundleRules = ParseBundleRules(collector, rulesYaml.Bundle, configPath, validProductIds, globalMatch);
if (bundleRules == null && collector.Errors > 0)
return null;
// Parse publish rules — emit deprecation warning when present
if (rulesYaml.Publish != null)
collector.EmitWarning(configPath, "rules.publish is deprecated and no longer used by the changelog render command. Move type/area filtering to rules.bundle, which applies at bundle time instead of render time.");
// Note: rules.publish is no longer used by changelog render; set to null so it's never applied
// The warning above alerts users they need to migrate to rules.bundle
return new RulesConfiguration
{
Match = globalMatch,
Create = createRules,
Bundle = bundleRules,
Publish = null // rules.publish is retired; filtering happens at bundle time via rules.bundle
};
}
private BundleRules? ParseBundleRules(
IDiagnosticsCollector collector,
BundleRulesYaml? yaml,
string configPath,
HashSet<string> validProductIds,
MatchMode inheritedMatch)
{
if (yaml == null)
return null;
// Validate mutual exclusivity for products
if (yaml.ExcludeProducts?.Values is { Count: > 0 } && yaml.IncludeProducts?.Values is { Count: > 0 })
{
collector.EmitError(configPath, "rules.bundle: cannot have both 'exclude_products' and 'include_products'. Use one or the other.");
return null;
}
// Parse and validate product lists
var excludeProducts = ParseAndValidateProductList(collector, yaml.ExcludeProducts, configPath, validProductIds, "rules.bundle.exclude_products");
if (excludeProducts == null && collector.Errors > 0)
return null;
var includeProducts = ParseAndValidateProductList(collector, yaml.IncludeProducts, configPath, validProductIds, "rules.bundle.include_products");
if (includeProducts == null && collector.Errors > 0)
return null;
// Parse match_products
var matchProducts = inheritedMatch;
if (!string.IsNullOrWhiteSpace(yaml.MatchProducts))
{
var parsed = ParseMatchMode(yaml.MatchProducts);
if (parsed == null)
{
collector.EmitError(configPath, $"rules.bundle.match_products: '{yaml.MatchProducts}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
matchProducts = parsed.Value;
}
// Parse match_areas (inherited from globalMatch if omitted)
var matchAreas = inheritedMatch;
if (!string.IsNullOrWhiteSpace(yaml.MatchAreas))
{
var parsed = ParseMatchMode(yaml.MatchAreas);
if (parsed == null)
{
collector.EmitError(configPath, $"rules.bundle.match_areas: '{yaml.MatchAreas}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
matchAreas = parsed.Value;
}
// Parse global type/area blocker (reusing PublishRulesYaml parsing logic)
var blockerYaml = new PublishRulesYaml
{
MatchAreas = yaml.MatchAreas,
ExcludeTypes = yaml.ExcludeTypes,
IncludeTypes = yaml.IncludeTypes,
ExcludeAreas = yaml.ExcludeAreas,
IncludeAreas = yaml.IncludeAreas
};
var blocker = ParsePublishBlockerFromYaml(collector, blockerYaml, configPath, "rules.bundle", matchAreas);
if (blocker == null && collector.Errors > 0)
return null;
// Parse per-product overrides
Dictionary<string, BundlePerProductRule>? byProduct = null;
if (yaml.Products is { Count: > 0 })
{
byProduct = new Dictionary<string, BundlePerProductRule>(StringComparer.OrdinalIgnoreCase);
foreach (var (productKey, productYaml) in yaml.Products)
{
var productIds = productKey.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var productId in productIds)
{
var normalizedProductId = productId.Replace('_', '-');
if (!validProductIds.Contains(normalizedProductId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"rules.bundle.products: '{productId}' not in available products. Available: {availableProducts}");
return null;
}
if (productYaml == null)
continue;
// Validate mutual exclusivity for products within this context
if (productYaml.ExcludeProducts?.Values is { Count: > 0 } && productYaml.IncludeProducts?.Values is { Count: > 0 })
{
collector.EmitError(configPath, $"rules.bundle.products.{normalizedProductId}: cannot have both 'exclude_products' and 'include_products'. Use one or the other.");
return null;
}
// Parse product lists for this context
var contextExcludeProducts = ParseAndValidateProductList(collector, productYaml.ExcludeProducts, configPath, validProductIds, $"rules.bundle.products.{normalizedProductId}.exclude_products");
if (contextExcludeProducts == null && collector.Errors > 0)
return null;
var contextIncludeProducts = ParseAndValidateProductList(collector, productYaml.IncludeProducts, configPath, validProductIds, $"rules.bundle.products.{normalizedProductId}.include_products");
if (contextIncludeProducts == null && collector.Errors > 0)
return null;
// Mode 3: global rules.bundle product lists are not used for filtering — do not warn about
// subset relationships between global and per-product include/exclude lists (would mislead authors).
// Parse match_products for this context
var contextMatchProducts = matchProducts;
if (!string.IsNullOrWhiteSpace(productYaml.MatchProducts))
{
var parsed = ParseMatchMode(productYaml.MatchProducts);
if (parsed == null)
{
collector.EmitError(configPath, $"rules.bundle.products.{normalizedProductId}.match_products: '{productYaml.MatchProducts}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
contextMatchProducts = parsed.Value;
}
// Validate per-product ineffective patterns
if (contextMatchProducts == MatchMode.Any && contextIncludeProducts is { Count: > 0 })
{
collector.EmitWarning(configPath,
$"Configuration pattern 'match_products: any' with 'include_products' in per-product rule '{normalizedProductId}' provides no selective filtering. " +
"Consider 'match_products: all' for strict filtering or 'exclude_products' for exclusion-based filtering. " +
"See: https://elastic.github.io/docs-builder/contribute/changelog/#ineffective-configuration-patterns");
}
// Detect disjoint products in per-product include_products
if (contextIncludeProducts is { Count: > 1 })
{
var disjointProducts = contextIncludeProducts.Where(p =>
!string.Equals(p, normalizedProductId, StringComparison.OrdinalIgnoreCase)).ToList();
if (disjointProducts.Count > 0)
{
collector.EmitHint(configPath,
$"Per-product rule '{normalizedProductId}' includes disjoint products [{string.Join(", ", disjointProducts)}] " +
"which cannot be included due to single-product rule resolution. " +
"Use separate bundles (each with a single product in output_products or profile output_products), or multi-product changelogs instead. " +
"See: https://elastic.github.io/docs-builder/contribute/changelog/#ineffective-configuration-patterns");
}
}
// Parse type/area blocker
var productMatchAreas = matchAreas;
if (!string.IsNullOrWhiteSpace(productYaml.MatchAreas))
{
var parsedMode = ParseMatchMode(productYaml.MatchAreas);
if (parsedMode == null)
{
collector.EmitError(configPath, $"rules.bundle.products.{normalizedProductId}.match_areas: '{productYaml.MatchAreas}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
productMatchAreas = parsedMode.Value;
}
var productBlockerYaml = new PublishRulesYaml
{
MatchAreas = productYaml.MatchAreas,
ExcludeTypes = productYaml.ExcludeTypes,
IncludeTypes = productYaml.IncludeTypes,
ExcludeAreas = productYaml.ExcludeAreas,
IncludeAreas = productYaml.IncludeAreas
};
var productBlocker = ParsePublishBlockerFromYaml(collector, productBlockerYaml, configPath, $"rules.bundle.products.{normalizedProductId}", productMatchAreas);
if (productBlocker == null && collector.Errors > 0)
return null;
// Create per-product rule if any rules are defined (product filtering OR type/area blocking)
if (productBlocker != null || contextIncludeProducts != null || contextExcludeProducts != null)
{
byProduct[normalizedProductId] = new BundlePerProductRule
{
Blocker = productBlocker,
IncludeProducts = contextIncludeProducts,
ExcludeProducts = contextExcludeProducts,
MatchProducts = contextMatchProducts
};
}
}
}
}
if (yaml.Products is { Count: > 0 })
{
var hasGlobalProductFilters = (excludeProducts?.Count ?? 0) > 0 || (includeProducts?.Count ?? 0) > 0;
if (hasGlobalProductFilters || blocker != null)
{
collector.EmitHint(configPath,
"rules.bundle: When 'products' is present, global include_products, exclude_products, and type/area rules are not applied for filtering; configure filters under each product key or use global-only rules.bundle (no 'products' section). " +
"See: https://elastic.github.io/docs-builder/contribute/changelog/#bundle-rule-modes");
}
}
return new BundleRules
{
ExcludeProducts = excludeProducts,
IncludeProducts = includeProducts,
MatchProducts = matchProducts,
Blocker = blocker,
ByProduct = byProduct?.Count > 0 ? byProduct : null
};
}
private static IReadOnlyList<string>? ParseAndValidateProductList(
IDiagnosticsCollector collector,
YamlLenientList? list,
string configPath,
HashSet<string> validProductIds,
string fieldPath)
{
if (list?.Values is not { Count: > 0 } values)
return null;
var result = new List<string>();
foreach (var rawId in values)
{
var normalizedId = rawId.Replace('_', '-');
if (!validProductIds.Contains(normalizedId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"{fieldPath}: '{rawId}' is not in the list of available products. Available products: {availableProducts}");
return null;
}
result.Add(normalizedId);
}
return result;
}
private CreateRules? ParseCreateRules(
IDiagnosticsCollector collector,
CreateRulesYaml? yaml,
string configPath,
HashSet<string> validProductIds,
string path,
MatchMode inheritedMatch)
{
if (yaml == null)
return null;
// Validate mutual exclusivity
if (yaml.Exclude?.Values is { Count: > 0 } && yaml.Include?.Values is { Count: > 0 })
{
collector.EmitError(configPath, $"{path}: cannot have both 'exclude' and 'include'. Use one or the other.");
return null;
}
// Parse match mode
var match = inheritedMatch;
if (!string.IsNullOrWhiteSpace(yaml.Match))
{
var parsed = ParseMatchMode(yaml.Match);
if (parsed == null)
{
collector.EmitError(configPath, $"{path}.match: '{yaml.Match}' is not valid. Use 'any', 'all', or 'conjunction'.");
return null;
}
match = parsed.Value;
}
var mode = yaml.Include?.Values is { Count: > 0 } ? FieldMode.Include : FieldMode.Exclude;
var labels = mode == FieldMode.Include ? yaml.Include?.Values : yaml.Exclude?.Values;
// Parse per-product overrides
Dictionary<string, CreateRules>? byProduct = null;
if (yaml.Products is { Count: > 0 })
{
byProduct = new Dictionary<string, CreateRules>(StringComparer.OrdinalIgnoreCase);
foreach (var (productKey, productYaml) in yaml.Products)
{
var productIds = productKey.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var productId in productIds)
{
var normalizedProductId = productId.Replace('_', '-');
if (!validProductIds.Contains(normalizedProductId))
{
var availableProducts = string.Join(", ", validProductIds.OrderBy(p => p));
collector.EmitError(configPath, $"{path}.products: '{productId}' not in available products. Available: {availableProducts}");
return null;
}
var productRules = ParseCreateRules(collector, productYaml, configPath, validProductIds, $"{path}.products.{normalizedProductId}", match);
if (productRules == null && collector.Errors > 0)
return null;
if (productRules != null)
byProduct[normalizedProductId] = productRules;
}
}
}
return new CreateRules
{
Labels = labels,
Mode = mode,
Match = match,
ByProduct = byProduct
};
}
private PublishRules? ParsePublishRules(
IDiagnosticsCollector collector,
PublishRulesYaml? yaml,
string configPath,
HashSet<string> validProductIds,
string path,
MatchMode inheritedMatch)
{
if (yaml == null)
return null;
// Parse match_areas
var matchAreas = inheritedMatch;
if (!string.IsNullOrWhiteSpace(yaml.MatchAreas))
{
var parsed = ParseMatchMode(yaml.MatchAreas);
if (parsed == null)
{
collector.EmitError(configPath, $"{path}.match_areas: '{yaml.MatchAreas}' is not valid. Use 'any', 'all', or 'conjunction'.");