Skip to content

Commit

Permalink
First version that sorts the versions correctly without duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed May 13, 2024
1 parent 8f12b86 commit b32413b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 12 additions & 10 deletions changelog/output/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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 {
Expand All @@ -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{},
})
}
Expand Down
18 changes: 9 additions & 9 deletions changelog/output/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
}
}

0 comments on commit b32413b

Please sign in to comment.