-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathChangelogInlineRenderer.cs
More file actions
474 lines (410 loc) · 16.1 KB
/
ChangelogInlineRenderer.cs
File metadata and controls
474 lines (410 loc) · 16.1 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
// 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.Text;
using Elastic.Documentation;
using Elastic.Documentation.ReleaseNotes;
namespace Elastic.Markdown.Myst.Directives.Changelog;
/// <summary>
/// Renders changelog bundles as inline markdown content for the {changelog} directive.
/// Uses pre-loaded and cached bundle data from <see cref="ChangelogBlock"/>.
/// </summary>
public static class ChangelogInlineRenderer
{
public static string? RenderChangelogMarkdown(ChangelogBlock block)
{
if (!block.Found || block.LoadedBundles.Count == 0)
return "_No changelog entries._";
var sb = new StringBuilder();
var typeFilter = block.TypeFilter;
// Render each bundle as a version section (already sorted by semver descending)
var isFirst = true;
foreach (var bundle in block.LoadedBundles)
{
if (!isFirst)
_ = sb.AppendLine();
var bundleMarkdown = RenderSingleBundle(
bundle,
block.Subsections,
block.PublishBlocker,
block.PrivateRepositories,
block.HideFeatures,
typeFilter,
block.LinkVisibility);
_ = sb.Append(bundleMarkdown);
isFirst = false;
}
return sb.ToString();
}
private static string RenderSingleBundle(
LoadedBundle bundle,
bool subsections,
PublishBlocker? publishBlocker,
HashSet<string> privateRepositories,
HashSet<string> hideFeatures,
ChangelogTypeFilter typeFilter,
ChangelogLinkVisibility linkVisibility)
{
var titleSlug = ChangelogTextUtilities.TitleToSlug(bundle.Version);
// Filter entries based on publish blocker configuration
var filteredEntries = FilterEntries(bundle.Entries, publishBlocker);
// Filter entries based on hide-features (from bundle metadata)
filteredEntries = FilterEntriesByHideFeatures(filteredEntries, hideFeatures);
// Apply type filter
filteredEntries = FilterEntriesByType(filteredEntries, typeFilter);
// Group entries by type
var entriesByType = filteredEntries
.GroupBy(e => e.Type)
.ToDictionary(g => g.Key, g => g.ToList());
var hideLinks = linkVisibility switch
{
ChangelogLinkVisibility.KeepLinks => false,
ChangelogLinkVisibility.HideLinks => true,
_ => ShouldHideLinksForRepo(bundle.Repo, privateRepositories)
};
var displayVersion = VersionOrDate.FormatDisplayVersion(bundle.Version);
return GenerateMarkdown(displayVersion, titleSlug, bundle.Repo, bundle.Owner, entriesByType, subsections, hideLinks, typeFilter, publishBlocker, bundle.Data?.Description, bundle.Data?.ReleaseDate, bundle.Data?.ShowReleaseDates ?? false);
}
/// <summary>
/// Filters entries based on the type filter.
/// </summary>
private static IReadOnlyList<ChangelogEntry> FilterEntriesByType(
IReadOnlyList<ChangelogEntry> entries,
ChangelogTypeFilter typeFilter) => typeFilter switch
{
ChangelogTypeFilter.All => entries,
ChangelogTypeFilter.BreakingChange => entries.Where(e => e.Type == ChangelogEntryType.BreakingChange).ToList(),
ChangelogTypeFilter.Deprecation => entries.Where(e => e.Type == ChangelogEntryType.Deprecation).ToList(),
ChangelogTypeFilter.KnownIssue => entries.Where(e => e.Type == ChangelogEntryType.KnownIssue).ToList(),
ChangelogTypeFilter.Highlight => entries.Where(e => e.Highlight == true).ToList(),
_ => entries.Where(e => !ChangelogBlock.SeparatedTypes.Contains(e.Type)).ToList() // Default: exclude separated types
};
/// <summary>
/// Filters entries based on hide-features configuration from bundle metadata.
/// Entries with matching feature-id values are excluded from the output.
/// </summary>
private static IReadOnlyList<ChangelogEntry> FilterEntriesByHideFeatures(
IReadOnlyList<ChangelogEntry> entries,
HashSet<string> hideFeatures)
{
if (hideFeatures.Count == 0)
return entries;
return entries
.Where(e => string.IsNullOrWhiteSpace(e.FeatureId) || !hideFeatures.Contains(e.FeatureId))
.ToList();
}
/// <summary>
/// Determines if links should be hidden for a bundle based on its repository.
/// For merged bundles (e.g., "elasticsearch+kibana+private-repo"), returns true
/// if ANY component repository is in the private repositories set.
/// </summary>
public static bool ShouldHideLinksForRepo(string bundleRepo, HashSet<string> privateRepositories)
{
if (privateRepositories.Count == 0)
return false;
// Split on '+' to handle merged bundles (e.g., "elasticsearch+kibana+private-repo")
var repos = bundleRepo.Split('+', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
// Hide links if ANY component repo is private
return repos.Any(privateRepositories.Contains);
}
/// <summary>
/// Filters entries based on publish blocker configuration.
/// </summary>
private static IReadOnlyList<ChangelogEntry> FilterEntries(
IReadOnlyList<ChangelogEntry> entries,
PublishBlocker? publishBlocker)
{
if (publishBlocker is not { HasBlockingRules: true })
return entries;
return entries.Where(e => !publishBlocker.ShouldBlock(e)).ToList();
}
private static string GenerateMarkdown(
string title,
string titleSlug,
string repo,
string owner,
Dictionary<ChangelogEntryType, List<ChangelogEntry>> entriesByType,
bool subsections,
bool hideLinks,
ChangelogTypeFilter typeFilter,
PublishBlocker? publishBlocker,
string? description = null,
DateOnly? releaseDate = null,
bool showReleaseDates = false)
{
var sb = new StringBuilder();
// Get entries by category
var features = entriesByType.GetValueOrDefault(ChangelogEntryType.Feature, []);
var enhancements = entriesByType.GetValueOrDefault(ChangelogEntryType.Enhancement, []);
var security = entriesByType.GetValueOrDefault(ChangelogEntryType.Security, []);
var bugFixes = entriesByType.GetValueOrDefault(ChangelogEntryType.BugFix, []);
var docs = entriesByType.GetValueOrDefault(ChangelogEntryType.Docs, []);
var regressions = entriesByType.GetValueOrDefault(ChangelogEntryType.Regression, []);
var other = entriesByType.GetValueOrDefault(ChangelogEntryType.Other, []);
var breakingChanges = entriesByType.GetValueOrDefault(ChangelogEntryType.BreakingChange, []);
var deprecations = entriesByType.GetValueOrDefault(ChangelogEntryType.Deprecation, []);
var knownIssues = entriesByType.GetValueOrDefault(ChangelogEntryType.KnownIssue, []);
// Get highlighted entries from all types
var highlights = entriesByType.Values
.SelectMany(e => e)
.Where(e => e.Highlight == true)
.ToList();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"## {title}");
// Add release date if present and ShowReleaseDates is enabled
if (showReleaseDates && releaseDate is { } date)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"_Released: {date.ToString("MMMM d, yyyy", CultureInfo.InvariantCulture)}_");
}
// Add description if present
if (!string.IsNullOrEmpty(description))
{
_ = sb.AppendLine();
_ = sb.AppendLine(description);
}
// Check if we have any content at all
var hasAnyContent = features.Count > 0 || enhancements.Count > 0 || security.Count > 0 ||
bugFixes.Count > 0 || docs.Count > 0 || regressions.Count > 0 || other.Count > 0 ||
breakingChanges.Count > 0 || deprecations.Count > 0 || knownIssues.Count > 0 ||
highlights.Count > 0;
if (!hasAnyContent)
{
_ = sb.AppendLine(GetEmptyMessage(typeFilter));
return sb.ToString();
}
// Special case: When filtering by highlight, render only highlights without type-based sections
if (typeFilter == ChangelogTypeFilter.Highlight)
{
if (highlights.Count > 0)
RenderDetailedEntries(sb, highlights, repo, owner, groupBySubtype: false, hideLinks, publishBlocker);
return sb.ToString();
}
if (breakingChanges.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Breaking changes [{repo}-{titleSlug}-breaking-changes]");
RenderDetailedEntries(sb, breakingChanges, repo, owner, groupBySubtype: true, hideLinks, publishBlocker);
}
if (highlights.Count > 0 && typeFilter == ChangelogTypeFilter.All)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Highlights [{repo}-{titleSlug}-highlights]");
RenderDetailedEntries(sb, highlights, repo, owner, groupBySubtype: false, hideLinks, publishBlocker);
}
if (security.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Security [{repo}-{titleSlug}-security]");
RenderEntriesByArea(sb, security, repo, owner, subsections, hideLinks, publishBlocker);
}
if (knownIssues.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Known issues [{repo}-{titleSlug}-known-issues]");
RenderDetailedEntries(sb, knownIssues, repo, owner, groupBySubtype: false, hideLinks, publishBlocker);
}
if (deprecations.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Deprecations [{repo}-{titleSlug}-deprecations]");
RenderDetailedEntries(sb, deprecations, repo, owner, groupBySubtype: false, hideLinks, publishBlocker);
}
if (features.Count > 0 || enhancements.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Features and enhancements [{repo}-{titleSlug}-features-enhancements]");
var combined = features.Concat(enhancements).ToList();
RenderEntriesByArea(sb, combined, repo, owner, subsections, hideLinks, publishBlocker);
}
if (bugFixes.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Fixes [{repo}-{titleSlug}-fixes]");
RenderEntriesByArea(sb, bugFixes, repo, owner, subsections, hideLinks, publishBlocker);
}
if (docs.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Documentation [{repo}-{titleSlug}-docs]");
RenderEntriesByArea(sb, docs, repo, owner, subsections, hideLinks, publishBlocker);
}
if (regressions.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Regressions [{repo}-{titleSlug}-regressions]");
RenderEntriesByArea(sb, regressions, repo, owner, subsections, hideLinks, publishBlocker);
}
if (other.Count > 0)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"### Other changes [{repo}-{titleSlug}-other]");
RenderEntriesByArea(sb, other, repo, owner, subsections, hideLinks, publishBlocker);
}
return sb.ToString();
}
private static void RenderEntriesByArea(
StringBuilder sb,
List<ChangelogEntry> entries,
string repo,
string owner,
bool subsections,
bool hideLinks,
PublishBlocker? publishBlocker)
{
if (subsections)
{
// Group by area and sort when subsections is enabled
var groupedByArea = entries.GroupBy(e => publishBlocker.GetPreferredArea(e)).OrderBy(g => g.Key).ToList();
foreach (var areaGroup in groupedByArea)
{
if (!string.IsNullOrWhiteSpace(areaGroup.Key))
{
var header = ChangelogTextUtilities.FormatAreaHeader(areaGroup.Key);
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"**{header}**");
}
foreach (var entry in areaGroup)
RenderSingleEntry(sb, entry, repo, owner, hideLinks);
}
}
else
{
foreach (var entry in entries)
RenderSingleEntry(sb, entry, repo, owner, hideLinks);
}
}
private static void RenderSingleEntry(StringBuilder sb, ChangelogEntry entry, string repo, string owner, bool hideLinks)
{
_ = sb.Append("* ");
_ = sb.Append(ChangelogTextUtilities.Beautify(entry.Title));
RenderEntryLinks(sb, entry, repo, owner, hideLinks);
if (!string.IsNullOrWhiteSpace(entry.Description))
{
_ = sb.AppendLine();
var indented = ChangelogTextUtilities.Indent(entry.Description);
_ = sb.AppendLine(indented);
}
}
private static void RenderEntryLinks(StringBuilder sb, ChangelogEntry entry, string repo, string owner, bool hideLinks)
{
if (hideLinks)
{
_ = sb.AppendLine();
foreach (var pr in entry.Prs ?? [])
{
_ = sb.Append(" ");
_ = sb.AppendLine(ChangelogTextUtilities.FormatPrLink(pr, repo, hidePrivateLinks: true, owner));
}
foreach (var issue in entry.Issues ?? [])
{
_ = sb.Append(" ");
_ = sb.AppendLine(ChangelogTextUtilities.FormatIssueLink(issue, repo, hidePrivateLinks: true, owner));
}
return;
}
_ = sb.Append(' ');
foreach (var pr in entry.Prs ?? [])
{
_ = sb.Append(ChangelogTextUtilities.FormatPrLink(pr, repo, hidePrivateLinks: false, owner));
_ = sb.Append(' ');
}
foreach (var issue in entry.Issues ?? [])
{
_ = sb.Append(ChangelogTextUtilities.FormatIssueLink(issue, repo, hidePrivateLinks: false, owner));
_ = sb.Append(' ');
}
_ = sb.AppendLine();
}
private static void RenderDetailedEntries(
StringBuilder sb,
List<ChangelogEntry> entries,
string repo,
string owner,
bool groupBySubtype,
bool hideLinks,
PublishBlocker? publishBlocker)
{
var grouped = groupBySubtype
? entries.GroupBy(e => e.Subtype?.ToStringFast(true) ?? string.Empty).OrderBy(g => g.Key).ToList()
: entries.GroupBy(e => publishBlocker.GetPreferredArea(e)).OrderBy(g => g.Key).ToList();
foreach (var group in grouped)
{
if (!string.IsNullOrWhiteSpace(group.Key))
{
var header = groupBySubtype
? ChangelogTextUtilities.FormatSubtypeHeader(group.Key)
: ChangelogTextUtilities.FormatAreaHeader(group.Key);
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"**{header}**");
}
foreach (var entry in group)
RenderDetailedEntry(sb, entry, repo, owner, hideLinks);
}
}
private static void RenderDetailedEntry(StringBuilder sb, ChangelogEntry entry, string repo, string owner, bool hideLinks)
{
_ = sb.AppendLine();
_ = sb.AppendLine(CultureInfo.InvariantCulture, $"::::{{dropdown}} {ChangelogTextUtilities.Beautify(entry.Title)}");
_ = sb.AppendLine(entry.Description ?? "% Describe the change");
_ = sb.AppendLine();
RenderDetailedEntryLinks(sb, entry, repo, owner, hideLinks);
// Impact section
_ = sb.AppendLine(!string.IsNullOrWhiteSpace(entry.Impact)
? "**Impact**<br>" + entry.Impact
: "% **Impact**<br>_Add a description of the impact_");
_ = sb.AppendLine();
// Action section
_ = sb.AppendLine(!string.IsNullOrWhiteSpace(entry.Action)
? "**Action**<br>" + entry.Action
: "% **Action**<br>_Add a description of what action to take_");
_ = sb.AppendLine("::::");
}
private static void RenderDetailedEntryLinks(StringBuilder sb, ChangelogEntry entry, string repo, string owner, bool hideLinks)
{
var hasPrs = entry.Prs is { Count: > 0 };
var hasIssues = entry.Issues is { Count: > 0 };
if (!hasPrs && !hasIssues)
return;
if (hideLinks)
{
foreach (var pr in entry.Prs ?? [])
_ = sb.AppendLine(ChangelogTextUtilities.FormatPrLink(pr, repo, hidePrivateLinks: true, owner));
foreach (var issue in entry.Issues ?? [])
_ = sb.AppendLine(ChangelogTextUtilities.FormatIssueLink(issue, repo, hidePrivateLinks: true, owner));
_ = sb.AppendLine("For more information, check the pull request or issue above.");
_ = sb.AppendLine();
return;
}
_ = sb.Append("For more information, check ");
var first = true;
foreach (var pr in entry.Prs ?? [])
{
if (!first)
_ = sb.Append(' ');
_ = sb.Append(ChangelogTextUtilities.FormatPrLink(pr, repo, hidePrivateLinks: false, owner));
first = false;
}
foreach (var issue in entry.Issues ?? [])
{
if (!first)
_ = sb.Append(' ');
_ = sb.Append(ChangelogTextUtilities.FormatIssueLink(issue, repo, hidePrivateLinks: false, owner));
first = false;
}
_ = sb.AppendLine(".");
_ = sb.AppendLine();
}
/// <summary>
/// Gets the appropriate empty message based on the type filter.
/// Matches messages used by CLI renderers for consistency.
/// </summary>
private static string GetEmptyMessage(ChangelogTypeFilter typeFilter) =>
typeFilter switch
{
ChangelogTypeFilter.BreakingChange => "_There are no breaking changes associated with this release._",
ChangelogTypeFilter.Deprecation => "_There are no deprecations associated with this release._",
ChangelogTypeFilter.KnownIssue => "_There are no known issues associated with this release._",
_ => "_No new features, enhancements, or fixes._"
};
}