Skip to content

Commit 1aec307

Browse files
clean up
1 parent f8e02f5 commit 1aec307

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

internal/changelog/renderer.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -338,50 +338,42 @@ func buildTitleByComponents(entries []Entry) string {
338338
}
339339
}
340340

341-
func addInclude(fs afero.Fs, version string, dest string, templ string) {
342-
// Get minor version
343-
re := regexp.MustCompile(`^\d+\.\d+`)
344-
matches := re.FindStringSubmatch(version)
345-
if len(matches) == 0 {
341+
func addInclude(fs afero.Fs, version, dest, templ string) {
342+
// Extract minor version (e.g., "8.12" from "8.12.1")
343+
minorVersion := regexp.MustCompile(`^\d+\.\d+`).FindString(version)
344+
if minorVersion == "" {
346345
fmt.Printf("Could not get minor version from: %v\n", version)
347346
return
348347
}
349-
minorVersion := matches[0]
350348

351-
// Get include directory
352-
includeDirRe := regexp.MustCompile(`/release-notes/.+$`)
353-
includeDirMatches := includeDirRe.FindStringSubmatch(dest)
354-
if len(includeDirMatches) == 0 {
349+
// Extract include directory (e.g., "/release-notes/...")
350+
includeDir := regexp.MustCompile(`/release-notes/.+$`).FindString(dest)
351+
if includeDir == "" {
355352
fmt.Printf("Could not derive include directory from: %v\n", dest)
356353
return
357354
}
358-
includeDir := includeDirMatches[0]
359355

360-
// Get the snippet file listing all patches for the minor
361356
minorFilePath := fmt.Sprintf("%s/%s/%s.md", dest, templ, minorVersion)
357+
templateTypeFilePath := fmt.Sprintf("%s/%s.md", dest, templ)
358+
359+
// Read or create the minor file
362360
minorFileContent, err := afero.ReadFile(fs, minorFilePath)
363-
// If no file exists for the specified minor:
364-
// * Create the file
365-
// * Include it in the snippet file listing all minors
366361
if err != nil {
367362
// Create the file
368-
afero.WriteFile(fs, minorFilePath, nil, changelogFilePerm)
369-
fmt.Printf("Created new include file: %s\n", minorFilePath)
370-
// Include it in the snippet file listing all minors
371-
templateTypeFilePath := fmt.Sprintf("%s/%s.md", dest, templ)
372-
templateTypeFileContent, err := afero.ReadFile(fs, templateTypeFilePath)
373-
if err != nil {
374-
fmt.Printf("Could not get file: %s\n", templateTypeFilePath)
375-
return
363+
if err := afero.WriteFile(fs, minorFilePath, nil, changelogFilePerm); err == nil {
364+
fmt.Printf("Created new include file: %s\n", minorFilePath)
365+
}
366+
// Prepend new minor version include to the template type file (e.g. "breaking-changes")
367+
if templateTypeFileContent, err := afero.ReadFile(fs, templateTypeFilePath); err == nil {
368+
newMinorInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, templ, minorVersion)
369+
newContent := fmt.Sprintf("%s\n\n%s", newMinorInclude, templateTypeFileContent)
370+
afero.WriteFile(fs, templateTypeFilePath, []byte(newContent), changelogFilePerm)
376371
}
377-
// Add the new minor version to the top of the existing content
378-
newMinorVersionInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, templ, minorVersion)
379-
newContent := fmt.Sprintf("%s\n\n%s", newMinorVersionInclude, templateTypeFileContent)
380-
afero.WriteFile(fs, templateTypeFilePath, []byte(newContent), changelogFilePerm)
372+
minorFileContent = nil // ensure it's empty for next step
381373
}
382374

383-
// Add new patch version to top of the existing content
384-
newPatchVersionInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, version, templ)
385-
newContent := fmt.Sprintf("%s\n\n%s", newPatchVersionInclude, minorFileContent)
375+
// Prepend new patch version include to the minor file
376+
newPatchInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, version, templ)
377+
newContent := fmt.Sprintf("%s\n\n%s", newPatchInclude, minorFileContent)
386378
afero.WriteFile(fs, minorFilePath, []byte(newContent), changelogFilePerm)
387379
}

0 commit comments

Comments
 (0)