diff --git a/changelog/output/format.go b/changelog/output/format.go index 0c53461..5336d99 100644 --- a/changelog/output/format.go +++ b/changelog/output/format.go @@ -18,12 +18,12 @@ type Entry struct { type Component struct { Name string `json:"name"` Title string `json:"title"` + Date string `json:"date"` Description string `json:"description"` } type Release struct { Tag string `json:"tag"` - Date string `json:"date"` Components *[]Component `json:"components"` } @@ -42,20 +42,20 @@ func (c *Changelog) AddEntry(entry Entry) error { slog.Info("Inserting first release", "entry", entry) *c.Releases = append(*c.Releases, Release{ Tag: entry.Tag, - Date: entry.Date, Components: &[]Component{}}) } else { - // check if this release exist already - found := false + + // check if it already exists + exists := false for _, r := range *c.Releases { if r.Tag == entry.Tag { - found = true slog.Info("Release already exists", "tag", entry.Tag) + exists = true break } } - if !found { + if !exists { // find the right place to insert the release added := false for i, r := range *c.Releases { @@ -69,25 +69,27 @@ func (c *Changelog) AddEntry(entry Entry) error { slog.Error("unable to parse version", "tag", entry.Tag, "error", err) return err } + if v0.Equal(v1) { + slog.Info("Release already exists", "tag", entry.Tag) + break + } if v0.LessThan(v1) { slog.Info("Inserting release", "tag", entry.Tag, "before", r.Tag) // add release before the current release *c.Releases = slices.Insert(*c.Releases, i, Release{ Tag: entry.Tag, - Date: entry.Date, Components: &[]Component{}, }) added = true + break } - break } if !added { // add release at the end - slog.Info("Adding release at the end", "tag", entry.Tag, "at the end") + slog.Info("Adding release at the end", "tag", entry.Tag) *c.Releases = append(*c.Releases, Release{ Tag: entry.Tag, - Date: entry.Date, Components: &[]Component{}, }) } diff --git a/changelog/output/format_test.go b/changelog/output/format_test.go index 4a22c8d..b47f0ef 100644 --- a/changelog/output/format_test.go +++ b/changelog/output/format_test.go @@ -14,12 +14,6 @@ func TestChangelog_AddEntry(t *testing.T) { Component: "frontend", Description: "Initial frontend version", }) - changelog.AddEntry(Entry{ - Tag: "1.19.3", - Name: "Initial Release", - Component: "backend", - Description: "Initial backend version", - }) changelog.AddEntry(Entry{ Tag: "1.22.1", Name: "Feature 1", @@ -32,6 +26,12 @@ func TestChangelog_AddEntry(t *testing.T) { Component: "backend", Description: "This cool backend feature", }) + changelog.AddEntry(Entry{ + Tag: "1.19.3", + Name: "Initial Release", + Component: "backend", + Description: "Initial backend version", + }) changelog.AddEntry(Entry{ Tag: "1.25.0", Name: "Feature 2", @@ -63,17 +63,17 @@ func TestChangelog_AddEntry(t *testing.T) { } // expect that the first release is 1.2.0 - if (*changelog.Releases)[0].Tag != "1.2.0" { + if (*changelog.Releases)[0].Tag != "1.25.0" { t.Errorf("expected first release to be 1.2.0, got %s", (*changelog.Releases)[0].Tag) } // expect that the second release is 1.1.0 - if (*changelog.Releases)[1].Tag != "1.1.0" { + if (*changelog.Releases)[1].Tag != "1.22.1" { t.Errorf("expected second release to be 1.1.0, got %s", (*changelog.Releases)[1].Tag) } // expect that the third release is 1.0.0 - if (*changelog.Releases)[2].Tag != "1.0.0" { + if (*changelog.Releases)[2].Tag != "1.19.3" { t.Errorf("expected third release to be 1.0.0, got %s", (*changelog.Releases)[2].Tag) } }