Skip to content

Commit

Permalink
fix: numbered releases should include date in changelog heading.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Apr 12, 2024
1 parent 85bd311 commit 3416fc7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
20 changes: 10 additions & 10 deletions changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func init() {

// RenderCommits takes a slice of commits and returns a markdown-formatted string,
// including the category header.
func RenderCommits(commits *[]vcs.TagCommits, groupIntoSections bool, unreleasedVersionName string) string {
func RenderCommits(
commits *[]vcs.TagCommits,
groupIntoSections bool,
releaseUnreleased bool,
unreleasedVersionName string,
) string {
if commits == nil {
logrus.Debug("no commits to render")
return ""
Expand All @@ -58,7 +63,7 @@ func RenderCommits(commits *[]vcs.TagCommits, groupIntoSections bool, unreleased
var unreleased bool
var versionName string
if tagCommits.Name == vcs.UnreleasedVersionName {
unreleased = true
unreleased = !releaseUnreleased
versionName = unreleasedVersionName
} else {
unreleased = false
Expand Down Expand Up @@ -195,6 +200,7 @@ func GetUpdatedChangelog(
currentVersion, vPrefix := semver.GetCurrentVersion(repoPath, orderBy)

var nextVersion string
var releaseUnreleased bool
if beforeTag == "" {
// determine next version only based on unreleased commits
unreleasedCommits := (*commits)[0].Commits
Expand All @@ -205,18 +211,12 @@ func GetUpdatedChangelog(
return vcs.ReleaseMetadata{}, "", fmt.Errorf("could not determine next version")
}

// update unreleased version name to next version to ensure date is rendered
for _, commit := range *commits {
if commit.Name == vcs.UnreleasedVersionName {
commit.Name = nextVersion
break
}
}
releaseUnreleased = true
} else {
nextVersion = vcs.UnreleasedVersionName
}

rendered := RenderCommits(commits, true, nextVersion)
rendered := RenderCommits(commits, true, releaseUnreleased, nextVersion)

lines, err := ReadFile(changelogFile)
if err != nil {
Expand Down
48 changes: 44 additions & 4 deletions changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package changelog

import (
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand All @@ -32,6 +33,7 @@ import (
func TestRenderCommits(t *testing.T) {
type args struct {
groupIntoSections bool
releaseUnreleased bool
commits *[]vcs.TagCommits
}

Expand Down Expand Up @@ -113,7 +115,7 @@ func TestRenderCommits(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RenderCommits(tt.args.commits, tt.args.groupIntoSections, vcs.UnreleasedVersionName); got != tt.want {
if got := RenderCommits(tt.args.commits, tt.args.groupIntoSections, tt.args.releaseUnreleased, vcs.UnreleasedVersionName); got != tt.want {
t.Errorf("RenderCommits() got = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -181,6 +183,8 @@ func TestGetUpdatedChangelog(t *testing.T) {
time.Now(),
)

today := time.Now().Format("2006-01-02")

type args struct {
config cfg.SinceConfig
changelogFile string
Expand Down Expand Up @@ -225,21 +229,57 @@ func TestGetUpdatedChangelog(t *testing.T) {
Sha: unreleasedCommitSha.String(),
VPrefix: false,
},
wantUpdatedChangelog: `# Changelog
wantUpdatedChangelog: fmt.Sprintf(`# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.2.0] - %v
### Added
- feat: unreleased change
### Changed
- docs: adds changelog
`, today),
wantErr: false,
},
{
name: "multiple versions",
args: args{
changelogFile: path.Join(repoWithTagsAndUnreleasedChanges, "CHANGELOG.md"),
orderBy: vcs.TagOrderSemver,
repoPath: repoWithTagsAndUnreleasedChanges,
afterTag: "0.0.1",
},
wantMetadata: vcs.ReleaseMetadata{
NewVersion: "0.2.0",
OldVersion: "0.1.0",
RepoPath: repoWithTagsAndUnreleasedChanges,
Sha: unreleasedCommitSha.String(),
VPrefix: false,
},
wantUpdatedChangelog: fmt.Sprintf(`# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.2.0]
## [0.2.0] - %[1]v
### Added
- feat: unreleased change
### Changed
- docs: adds changelog
`,
## [0.1.0] - %[1]v
### Added
- feat: second update
`, today),
wantErr: false,
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/project_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ func listCommits(
if err != nil {
return "", err
}
return changelog.RenderCommits(commits, true, vcs.UnreleasedVersionName), nil
return changelog.RenderCommits(commits, true, false, vcs.UnreleasedVersionName), nil
}

0 comments on commit 3416fc7

Please sign in to comment.