Skip to content

Commit

Permalink
It basically works, but has problems with the correct order
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed May 13, 2024
1 parent a3866ce commit 8f12b86
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
17 changes: 2 additions & 15 deletions changelog/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package changelog

import (
"context"
"fmt"
"github.com/prodyna/changelog-json/changelog/output"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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
}
65 changes: 52 additions & 13 deletions changelog/output/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,110 @@ package output

import (
"encoding/json"
"github.com/coreos/go-semver/semver"
"github.com/Masterminds/semver/v3"
"log/slog"
"slices"
)

type Entry struct {
Tag string
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"`
}

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{}
}

// 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) {
Expand Down
10 changes: 5 additions & 5 deletions changelog/output/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 8f12b86

Please sign in to comment.