|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO.Abstractions; |
| 7 | +using System.Text; |
| 8 | +using Elastic.Documentation.ReleaseNotes; |
| 9 | +using Nullean.ScopedFileSystem; |
| 10 | +using static System.Globalization.CultureInfo; |
| 11 | +using static Elastic.Documentation.ChangelogEntryType; |
| 12 | + |
| 13 | +namespace Elastic.Changelog.Rendering.Markdown; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Renderer for generating clean GitHub Flavored Markdown in a single changelog.md file |
| 17 | +/// </summary> |
| 18 | +public class ChangelogGfmRenderer(ScopedFileSystem fileSystem) : MarkdownRendererBase(fileSystem) |
| 19 | +{ |
| 20 | + /// <inheritdoc /> |
| 21 | + public override string OutputFileName => "changelog.md"; |
| 22 | + |
| 23 | + /// <inheritdoc /> |
| 24 | + public override async Task RenderAsync(ChangelogRenderContext context, Cancel ctx) |
| 25 | + { |
| 26 | + var entriesByType = context.EntriesByType; |
| 27 | + var features = entriesByType.GetValueOrDefault(Feature, []); |
| 28 | + var enhancements = entriesByType.GetValueOrDefault(Enhancement, []); |
| 29 | + var security = entriesByType.GetValueOrDefault(Security, []); |
| 30 | + var bugFixes = entriesByType.GetValueOrDefault(BugFix, []); |
| 31 | + var docs = entriesByType.GetValueOrDefault(Docs, []); |
| 32 | + var regressions = entriesByType.GetValueOrDefault(Regression, []); |
| 33 | + var other = entriesByType.GetValueOrDefault(Other, []); |
| 34 | + var breakingChanges = entriesByType.GetValueOrDefault(BreakingChange, []); |
| 35 | + var deprecations = entriesByType.GetValueOrDefault(Deprecation, []); |
| 36 | + var knownIssues = entriesByType.GetValueOrDefault(KnownIssue, []); |
| 37 | + |
| 38 | + // Check for highlights |
| 39 | + var highlights = entriesByType.Values |
| 40 | + .SelectMany(e => e) |
| 41 | + .Where(e => e.Highlight == true) |
| 42 | + .ToList(); |
| 43 | + |
| 44 | + var sb = new StringBuilder(); |
| 45 | + |
| 46 | + // Main heading - clean without anchors |
| 47 | + _ = sb.AppendLine(InvariantCulture, $"## {context.Title}"); |
| 48 | + |
| 49 | + // Release date if present |
| 50 | + if (context.BundleReleaseDate is { } releaseDate) |
| 51 | + { |
| 52 | + _ = sb.AppendLine(); |
| 53 | + _ = sb.AppendLine(InvariantCulture, $"_Released: {releaseDate.ToString("MMMM d, yyyy", InvariantCulture)}_"); |
| 54 | + } |
| 55 | + |
| 56 | + // Add description if present |
| 57 | + if (!string.IsNullOrEmpty(context.BundleDescription)) |
| 58 | + { |
| 59 | + _ = sb.AppendLine(); |
| 60 | + _ = sb.AppendLine(context.BundleDescription); |
| 61 | + } |
| 62 | + |
| 63 | + _ = sb.AppendLine(); |
| 64 | + |
| 65 | + // Helper to check if all entries in a collection are hidden |
| 66 | + bool AllEntriesHidden(IReadOnlyCollection<ChangelogEntry> entries) => |
| 67 | + entries.Count > 0 && entries.All(entry => |
| 68 | + ChangelogRenderUtilities.ShouldHideEntry(entry, context.FeatureIdsToHide, context)); |
| 69 | + |
| 70 | + // Render highlights first if any exist |
| 71 | + if (highlights.Count > 0) |
| 72 | + { |
| 73 | + _ = sb.AppendLine("### Highlights"); |
| 74 | + RenderEntriesByArea(sb, highlights, context); |
| 75 | + _ = sb.AppendLine(); |
| 76 | + } |
| 77 | + |
| 78 | + // Features and enhancements |
| 79 | + if (features.Count > 0 || enhancements.Count > 0) |
| 80 | + { |
| 81 | + var combined = features.Concat(enhancements).ToList(); |
| 82 | + if (!AllEntriesHidden(combined)) |
| 83 | + { |
| 84 | + _ = sb.AppendLine("### Features and enhancements"); |
| 85 | + RenderEntriesByArea(sb, combined, context); |
| 86 | + _ = sb.AppendLine(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Breaking changes |
| 91 | + if (breakingChanges.Count > 0 && !AllEntriesHidden(breakingChanges)) |
| 92 | + { |
| 93 | + _ = sb.AppendLine("### Breaking changes"); |
| 94 | + RenderEntriesByArea(sb, breakingChanges, context); |
| 95 | + _ = sb.AppendLine(); |
| 96 | + } |
| 97 | + |
| 98 | + // Deprecations |
| 99 | + if (deprecations.Count > 0 && !AllEntriesHidden(deprecations)) |
| 100 | + { |
| 101 | + _ = sb.AppendLine("### Deprecations"); |
| 102 | + RenderEntriesByArea(sb, deprecations, context); |
| 103 | + _ = sb.AppendLine(); |
| 104 | + } |
| 105 | + |
| 106 | + // Bug fixes and security updates |
| 107 | + if (security.Count > 0 || bugFixes.Count > 0) |
| 108 | + { |
| 109 | + var combined = security.Concat(bugFixes).ToList(); |
| 110 | + if (!AllEntriesHidden(combined)) |
| 111 | + { |
| 112 | + _ = sb.AppendLine("### Bug fixes"); |
| 113 | + RenderEntriesByArea(sb, combined, context); |
| 114 | + _ = sb.AppendLine(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Known issues |
| 119 | + if (knownIssues.Count > 0 && !AllEntriesHidden(knownIssues)) |
| 120 | + { |
| 121 | + _ = sb.AppendLine("### Known issues"); |
| 122 | + RenderEntriesByArea(sb, knownIssues, context); |
| 123 | + _ = sb.AppendLine(); |
| 124 | + } |
| 125 | + |
| 126 | + // Documentation |
| 127 | + if (docs.Count > 0 && !AllEntriesHidden(docs)) |
| 128 | + { |
| 129 | + _ = sb.AppendLine("### Documentation"); |
| 130 | + RenderEntriesByArea(sb, docs, context); |
| 131 | + _ = sb.AppendLine(); |
| 132 | + } |
| 133 | + |
| 134 | + // Regressions |
| 135 | + if (regressions.Count > 0 && !AllEntriesHidden(regressions)) |
| 136 | + { |
| 137 | + _ = sb.AppendLine("### Regressions"); |
| 138 | + RenderEntriesByArea(sb, regressions, context); |
| 139 | + _ = sb.AppendLine(); |
| 140 | + } |
| 141 | + |
| 142 | + // Other changes |
| 143 | + if (other.Count > 0 && !AllEntriesHidden(other)) |
| 144 | + { |
| 145 | + _ = sb.AppendLine("### Other changes"); |
| 146 | + RenderEntriesByArea(sb, other, context); |
| 147 | + _ = sb.AppendLine(); |
| 148 | + } |
| 149 | + |
| 150 | + // Check if we have any visible content |
| 151 | + var hasAnyVisibleContent = highlights.Count > 0 || |
| 152 | + (!AllEntriesHidden(features) && features.Count > 0) || |
| 153 | + (!AllEntriesHidden(enhancements) && enhancements.Count > 0) || |
| 154 | + (!AllEntriesHidden(breakingChanges) && breakingChanges.Count > 0) || |
| 155 | + (!AllEntriesHidden(deprecations) && deprecations.Count > 0) || |
| 156 | + (!AllEntriesHidden(security) && security.Count > 0) || |
| 157 | + (!AllEntriesHidden(bugFixes) && bugFixes.Count > 0) || |
| 158 | + (!AllEntriesHidden(knownIssues) && knownIssues.Count > 0) || |
| 159 | + (!AllEntriesHidden(docs) && docs.Count > 0) || |
| 160 | + (!AllEntriesHidden(regressions) && regressions.Count > 0) || |
| 161 | + (!AllEntriesHidden(other) && other.Count > 0); |
| 162 | + |
| 163 | + if (!hasAnyVisibleContent) |
| 164 | + { |
| 165 | + _ = sb.AppendLine("_There are no new features, enhancements, or fixes associated with this release._"); |
| 166 | + _ = sb.AppendLine(); |
| 167 | + } |
| 168 | + |
| 169 | + await WriteOutputFileAsync(context.OutputDir, context.TitleSlug, sb.ToString(), ctx); |
| 170 | + } |
| 171 | + |
| 172 | + private static void RenderEntriesByArea( |
| 173 | + StringBuilder sb, |
| 174 | + IReadOnlyCollection<ChangelogEntry> entries, |
| 175 | + ChangelogRenderContext context) |
| 176 | + { |
| 177 | + var groupedByArea = context.Subsections |
| 178 | + ? entries.GroupBy(e => ChangelogRenderUtilities.GetComponent(e, context)).OrderBy(g => g.Key).ToList() |
| 179 | + : entries.GroupBy(e => ChangelogRenderUtilities.GetComponent(e, context)).ToList(); |
| 180 | + |
| 181 | + foreach (var areaGroup in groupedByArea) |
| 182 | + { |
| 183 | + // Check if all entries in this area group are hidden |
| 184 | + var allEntriesHidden = areaGroup.All(entry => |
| 185 | + ChangelogRenderUtilities.ShouldHideEntry(entry, context.FeatureIdsToHide, context)); |
| 186 | + |
| 187 | + if (context.Subsections && !string.IsNullOrWhiteSpace(areaGroup.Key)) |
| 188 | + { |
| 189 | + var header = ChangelogTextUtilities.FormatAreaHeader(areaGroup.Key); |
| 190 | + if (allEntriesHidden) |
| 191 | + _ = sb.Append("% "); |
| 192 | + _ = sb.AppendLine(InvariantCulture, $"**{header}**"); |
| 193 | + _ = sb.AppendLine(); |
| 194 | + } |
| 195 | + |
| 196 | + foreach (var entry in areaGroup) |
| 197 | + { |
| 198 | + var (entryRepo, entryOwner, entryHideLinks, shouldHide) = ChangelogRenderUtilities.GetEntryContext(entry, context); |
| 199 | + |
| 200 | + if (shouldHide) |
| 201 | + _ = sb.Append("% "); |
| 202 | + _ = sb.Append("* "); |
| 203 | + _ = sb.Append(ChangelogTextUtilities.Beautify(entry.Title)); |
| 204 | + |
| 205 | + var hasCommentedLinks = false; |
| 206 | + if (entryHideLinks) |
| 207 | + { |
| 208 | + foreach (var pr in entry.Prs ?? []) |
| 209 | + { |
| 210 | + var formatted = ChangelogTextUtilities.FormatPrLink(pr, entryRepo, entryHideLinks, entryOwner); |
| 211 | + if (string.IsNullOrEmpty(formatted)) |
| 212 | + continue; |
| 213 | + |
| 214 | + _ = sb.AppendLine(); |
| 215 | + if (shouldHide) |
| 216 | + _ = sb.Append("% "); |
| 217 | + _ = sb.Append(" "); |
| 218 | + _ = sb.Append(formatted); |
| 219 | + hasCommentedLinks = true; |
| 220 | + } |
| 221 | + |
| 222 | + foreach (var issue in entry.Issues ?? []) |
| 223 | + { |
| 224 | + var formatted = ChangelogTextUtilities.FormatIssueLink(issue, entryRepo, entryHideLinks, entryOwner); |
| 225 | + if (string.IsNullOrEmpty(formatted)) |
| 226 | + continue; |
| 227 | + |
| 228 | + _ = sb.AppendLine(); |
| 229 | + if (shouldHide) |
| 230 | + _ = sb.Append("% "); |
| 231 | + _ = sb.Append(" "); |
| 232 | + _ = sb.Append(formatted); |
| 233 | + hasCommentedLinks = true; |
| 234 | + } |
| 235 | + |
| 236 | + if (hasCommentedLinks) |
| 237 | + _ = sb.AppendLine(); |
| 238 | + } |
| 239 | + else |
| 240 | + { |
| 241 | + var linkParts = new List<string>(); |
| 242 | + foreach (var pr in entry.Prs ?? []) |
| 243 | + { |
| 244 | + var s = ChangelogTextUtilities.FormatPrLink(pr, entryRepo, entryHideLinks, entryOwner); |
| 245 | + if (!string.IsNullOrEmpty(s)) |
| 246 | + linkParts.Add(s); |
| 247 | + } |
| 248 | + |
| 249 | + foreach (var issue in entry.Issues ?? []) |
| 250 | + { |
| 251 | + var s = ChangelogTextUtilities.FormatIssueLink(issue, entryRepo, entryHideLinks, entryOwner); |
| 252 | + if (!string.IsNullOrEmpty(s)) |
| 253 | + linkParts.Add(s); |
| 254 | + } |
| 255 | + |
| 256 | + if (linkParts.Count > 0) |
| 257 | + { |
| 258 | + _ = sb.Append(' '); |
| 259 | + var first = true; |
| 260 | + foreach (var s in linkParts) |
| 261 | + { |
| 262 | + if (!first) |
| 263 | + _ = sb.Append(' '); |
| 264 | + _ = sb.Append(s); |
| 265 | + first = false; |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + if (!context.HideDescriptions && !string.IsNullOrWhiteSpace(entry.Description)) |
| 271 | + { |
| 272 | + _ = sb.AppendLine(entryHideLinks && hasCommentedLinks ? " " : ""); |
| 273 | + _ = sb.AppendLine(); |
| 274 | + var indented = ChangelogTextUtilities.Indent(entry.Description); |
| 275 | + if (shouldHide) |
| 276 | + { |
| 277 | + // Comment out each line of the description |
| 278 | + var indentedLines = indented.Split('\n'); |
| 279 | + foreach (var line in indentedLines) |
| 280 | + { |
| 281 | + _ = sb.Append("% "); |
| 282 | + _ = sb.AppendLine(line); |
| 283 | + } |
| 284 | + } |
| 285 | + else |
| 286 | + _ = sb.AppendLine(indented); |
| 287 | + } |
| 288 | + else |
| 289 | + _ = sb.AppendLine(); |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | +} |
0 commit comments