From 8f12b8657dc32536055b5b200e48ebbba32e6a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Mon, 13 May 2024 15:56:40 +0200 Subject: [PATCH] It basically works, but has problems with the correct order --- changelog/generate.go | 17 +-------- changelog/output/format.go | 65 ++++++++++++++++++++++++++------- changelog/output/format_test.go | 10 ++--- go.mod | 5 +-- go.sum | 12 +----- 5 files changed, 63 insertions(+), 46 deletions(-) diff --git a/changelog/generate.go b/changelog/generate.go index 5b8ecef..ca8cadb 100644 --- a/changelog/generate.go +++ b/changelog/generate.go @@ -2,7 +2,6 @@ package changelog import ( "context" - "fmt" "github.com/prodyna/changelog-json/changelog/output" "github.com/shurcooL/githubv4" "golang.org/x/oauth2" @@ -37,12 +36,7 @@ func New(config Config) (*ChangelogGenerator, error) { func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.Changelog, err error) { slog.Info("Generating changelog", "repositories", clg.config.Repositories, "organization", clg.config.Organization) changelog = &output.Changelog{ - Releases: &[]output.Release{ - { - Tag: "0.0.0", - Components: &[]output.Component{}, - }, - }, + Releases: &[]output.Release{}, } src := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: clg.config.GitHubToken}, @@ -97,17 +91,10 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output. Component: repository, Description: release.Description, } - slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description", entry.Description) + slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description.len", len(entry.Description)) changelog.AddEntry(entry) } } - output, err := changelog.RenderJSON() - if err != nil { - slog.Error("unable to render changelog", "error", err) - return nil, err - } - fmt.Printf("%s", output) - return changelog, nil } diff --git a/changelog/output/format.go b/changelog/output/format.go index c686ff5..0c53461 100644 --- a/changelog/output/format.go +++ b/changelog/output/format.go @@ -2,7 +2,9 @@ package output import ( "encoding/json" - "github.com/coreos/go-semver/semver" + "github.com/Masterminds/semver/v3" + "log/slog" + "slices" ) type Entry struct { @@ -10,15 +12,18 @@ type Entry struct { Name string Component string Description string + Date string } type Component struct { Name string `json:"name"` + Title string `json:"title"` Description string `json:"description"` } type Release struct { Tag string `json:"tag"` + Date string `json:"date"` Components *[]Component `json:"components"` } @@ -26,7 +31,7 @@ type Changelog struct { Releases *[]Release `json:"releases"` } -func (c *Changelog) AddEntry(entry Entry) { +func (c *Changelog) AddEntry(entry Entry) error { if c.Releases == nil { c.Releases = &[]Release{} } @@ -34,39 +39,73 @@ func (c *Changelog) AddEntry(entry Entry) { // create a new release and insert it into the changelog in the right order if len(*c.Releases) == 0 { // no releaes, create it anyways + slog.Info("Inserting first release", "entry", entry) *c.Releases = append(*c.Releases, Release{ Tag: entry.Tag, + Date: entry.Date, Components: &[]Component{}}) } else { - // find the right place to insert the release + // check if this release exist already + found := false for _, r := range *c.Releases { if r.Tag == entry.Tag { - // release already exists, break here + found = true + slog.Info("Release already exists", "tag", entry.Tag) break } - v0 := semver.New(r.Tag) - v1 := semver.New(entry.Tag) - if v0.LessThan(*v1) { - // add release before the current release + } + + if !found { + // find the right place to insert the release + added := false + for i, r := range *c.Releases { + v0, err := semver.NewVersion(r.Tag) + if err != nil { + slog.Error("unable to parse version", "tag", r.Tag, "error", err) + return err + } + v1, err := semver.NewVersion(entry.Tag) + if err != nil { + slog.Error("unable to parse version", "tag", entry.Tag, "error", err) + return err + } + 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 + } + + if !added { + // add release at the end + slog.Info("Adding release at the end", "tag", entry.Tag, "at the end") *c.Releases = append(*c.Releases, Release{ Tag: entry.Tag, - Components: &[]Component{}}) + Date: entry.Date, + Components: &[]Component{}, + }) } - break } } // find the right release and add the component - for i, release := range *c.Releases { + for _, release := range *c.Releases { if release.Tag == entry.Tag { - *(*c.Releases)[i].Components = append(*(*c.Releases)[i].Components, Component{ + *release.Components = append(*release.Components, Component{ Name: entry.Component, + Title: entry.Name, Description: entry.Description, }) break } } - + return nil } func (c *Changelog) RenderJSON() (output []byte, err error) { diff --git a/changelog/output/format_test.go b/changelog/output/format_test.go index 6633f73..4a22c8d 100644 --- a/changelog/output/format_test.go +++ b/changelog/output/format_test.go @@ -9,31 +9,31 @@ import ( func TestChangelog_AddEntry(t *testing.T) { changelog := &Changelog{} changelog.AddEntry(Entry{ - Tag: "1.0.0", + Tag: "1.19.3", Name: "Initial Release", Component: "frontend", Description: "Initial frontend version", }) changelog.AddEntry(Entry{ - Tag: "1.0.0", + Tag: "1.19.3", Name: "Initial Release", Component: "backend", Description: "Initial backend version", }) changelog.AddEntry(Entry{ - Tag: "1.1.0", + Tag: "1.22.1", Name: "Feature 1", Component: "frontend", Description: "This cool frontend feature", }) changelog.AddEntry(Entry{ - Tag: "1.1.0", + Tag: "1.22.1", Name: "Feature 1", Component: "backend", Description: "This cool backend feature", }) changelog.AddEntry(Entry{ - Tag: "1.2.0", + Tag: "1.25.0", Name: "Feature 2", Component: "frontend", Description: "More cool frontend feature", diff --git a/go.mod b/go.mod index 85dad29..a709257 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,12 @@ module github.com/prodyna/changelog-json go 1.22.3 require ( - github.com/google/go-github/v61 v61.0.0 + github.com/Masterminds/semver/v3 v3.2.1 github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064 golang.org/x/oauth2 v0.20.0 ) require ( - github.com/coreos/go-semver v0.3.1 // indirect - github.com/google/go-querystring v1.1.0 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect ) diff --git a/go.sum b/go.sum index 078ee57..fff58c8 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,10 @@ -github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= -github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= +github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5pNlu1go= -github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY= -github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= -github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064 h1:RCQBSFx5JrsbHltqTtJ+kN3U0Y3a/N/GlVdmRSoxzyE= github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8= github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0= github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=