feat(releaser): CHANGELOG auto-update, --init, and --changelog-file flags
- Automatically write/update CHANGELOG.md on every release, grouped by Breaking Changes / Added / Fixed; file is created if missing and the new section is committed alongside pom.xml in the release commit - Add --init flag: scaffolds a default .releaser.yml in the repo root - Add --changelog-file flag: override the default CHANGELOG.md path - Add CommitFiles() to gitutil so pom.xml and CHANGELOG.md are staged in a single commit - Fix HTTPS push when no token is set: delegate to the git CLI so that system credential helpers, SSH agents, and netrc are honoured Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package changelog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.k3nny.fr/releaser/internal/commits"
|
||||
)
|
||||
|
||||
var headerSubjectRe = regexp.MustCompile(`(?i)^\w+(?:\([^)]*\))?!?\s*:\s*(.+)`)
|
||||
|
||||
// Update inserts a new release section into the CHANGELOG file at path.
|
||||
// If the file does not exist it is created with a standard header.
|
||||
// Only commits with a releasable type (fix, feat, breaking) produce bullets;
|
||||
// if none are found the file is left untouched.
|
||||
func Update(path, tag, version string, messages []string) error {
|
||||
section := buildSection(version, messages)
|
||||
if section == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
existing := ""
|
||||
data, err := os.ReadFile(path)
|
||||
if err == nil {
|
||||
existing = string(data)
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
|
||||
var out string
|
||||
if existing == "" {
|
||||
out = "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\n" +
|
||||
section + "\n"
|
||||
} else {
|
||||
// Insert above the first ## [ heading so newest release is always at top.
|
||||
if idx := strings.Index(existing, "\n## ["); idx >= 0 {
|
||||
out = existing[:idx+1] + section + "\n\n" + existing[idx+1:]
|
||||
} else {
|
||||
out = strings.TrimRight(existing, "\n") + "\n\n" + section + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
return os.WriteFile(path, []byte(out), 0644)
|
||||
}
|
||||
|
||||
func buildSection(version string, messages []string) string {
|
||||
var breaking, feats, fixes []string
|
||||
|
||||
for _, msg := range messages {
|
||||
t := commits.Parse(msg)
|
||||
if t == commits.TypeNone {
|
||||
continue
|
||||
}
|
||||
first := strings.SplitN(strings.TrimSpace(msg), "\n", 2)[0]
|
||||
subject := extractSubject(first)
|
||||
|
||||
switch t {
|
||||
case commits.TypeBreaking:
|
||||
breaking = append(breaking, subject)
|
||||
case commits.TypeFeat:
|
||||
feats = append(feats, subject)
|
||||
case commits.TypeFix:
|
||||
fixes = append(fixes, subject)
|
||||
}
|
||||
}
|
||||
|
||||
if len(breaking)+len(feats)+len(fixes) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
date := time.Now().Format("2006-01-02")
|
||||
var sb strings.Builder
|
||||
fmt.Fprintf(&sb, "## [%s] - %s\n", version, date)
|
||||
writeSection(&sb, "Breaking Changes", breaking)
|
||||
writeSection(&sb, "Added", feats)
|
||||
writeSection(&sb, "Fixed", fixes)
|
||||
|
||||
return strings.TrimRight(sb.String(), "\n")
|
||||
}
|
||||
|
||||
func writeSection(sb *strings.Builder, title string, items []string) {
|
||||
if len(items) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(sb, "\n### %s\n", title)
|
||||
for _, item := range items {
|
||||
fmt.Fprintf(sb, "- %s\n", item)
|
||||
}
|
||||
}
|
||||
|
||||
func extractSubject(header string) string {
|
||||
m := headerSubjectRe.FindStringSubmatch(header)
|
||||
if m != nil {
|
||||
return strings.TrimSpace(m[1])
|
||||
}
|
||||
return strings.TrimSpace(header)
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package changelog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateNewFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "CHANGELOG.md")
|
||||
|
||||
if err := Update(path, "v1.2.0", "1.2.0", []string{
|
||||
"feat: add widget",
|
||||
"fix: off-by-one in parser",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(path)
|
||||
s := string(data)
|
||||
if !strings.Contains(s, "## [1.2.0]") {
|
||||
t.Error("expected version header")
|
||||
}
|
||||
if !strings.Contains(s, "### Added") {
|
||||
t.Error("expected Added section")
|
||||
}
|
||||
if !strings.Contains(s, "### Fixed") {
|
||||
t.Error("expected Fixed section")
|
||||
}
|
||||
if !strings.Contains(s, "add widget") {
|
||||
t.Error("expected feat subject")
|
||||
}
|
||||
if !strings.Contains(s, "off-by-one in parser") {
|
||||
t.Error("expected fix subject")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateExistingFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "CHANGELOG.md")
|
||||
|
||||
// Seed with an older release.
|
||||
os.WriteFile(path, []byte("# Changelog\n\n## [1.1.0] - 2026-01-01\n\n### Added\n- old stuff\n"), 0644)
|
||||
|
||||
if err := Update(path, "v1.2.0", "1.2.0", []string{"feat: new thing"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(path)
|
||||
s := string(data)
|
||||
|
||||
newIdx := strings.Index(s, "## [1.2.0]")
|
||||
oldIdx := strings.Index(s, "## [1.1.0]")
|
||||
if newIdx < 0 || oldIdx < 0 {
|
||||
t.Fatal("both versions should appear in CHANGELOG")
|
||||
}
|
||||
if newIdx > oldIdx {
|
||||
t.Error("new version should appear before old version")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateNoReleasableCommits(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "CHANGELOG.md")
|
||||
|
||||
if err := Update(path, "v1.0.1", "1.0.1", []string{
|
||||
"chore: update deps",
|
||||
"docs: fix typo",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// File should NOT have been created.
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
t.Error("file should not be created when there are no releasable commits")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBreakingSection(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "CHANGELOG.md")
|
||||
|
||||
if err := Update(path, "v2.0.0", "2.0.0", []string{
|
||||
"feat!: redesign API",
|
||||
"fix(core): nil panic",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := os.ReadFile(path)
|
||||
s := string(data)
|
||||
if !strings.Contains(s, "### Breaking Changes") {
|
||||
t.Error("expected Breaking Changes section")
|
||||
}
|
||||
if !strings.Contains(s, "redesign API") {
|
||||
t.Error("expected breaking subject")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateReadError(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Create a directory where the file should be — ReadFile will error.
|
||||
os.Mkdir(filepath.Join(dir, "CHANGELOG.md"), 0755)
|
||||
|
||||
err := Update(filepath.Join(dir, "CHANGELOG.md"), "v1.0.0", "1.0.0", []string{"fix: something"})
|
||||
if err == nil {
|
||||
t.Error("expected error when path is a directory")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user