Skip to content

Commit 8f12b86

Browse files
committed
It basically works, but has problems with the correct order
1 parent a3866ce commit 8f12b86

File tree

5 files changed

+63
-46
lines changed

5 files changed

+63
-46
lines changed

changelog/generate.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package changelog
22

33
import (
44
"context"
5-
"fmt"
65
"github.com/prodyna/changelog-json/changelog/output"
76
"github.com/shurcooL/githubv4"
87
"golang.org/x/oauth2"
@@ -37,12 +36,7 @@ func New(config Config) (*ChangelogGenerator, error) {
3736
func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.Changelog, err error) {
3837
slog.Info("Generating changelog", "repositories", clg.config.Repositories, "organization", clg.config.Organization)
3938
changelog = &output.Changelog{
40-
Releases: &[]output.Release{
41-
{
42-
Tag: "0.0.0",
43-
Components: &[]output.Component{},
44-
},
45-
},
39+
Releases: &[]output.Release{},
4640
}
4741
src := oauth2.StaticTokenSource(
4842
&oauth2.Token{AccessToken: clg.config.GitHubToken},
@@ -97,17 +91,10 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.
9791
Component: repository,
9892
Description: release.Description,
9993
}
100-
slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description", entry.Description)
94+
slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description.len", len(entry.Description))
10195
changelog.AddEntry(entry)
10296
}
10397
}
10498

105-
output, err := changelog.RenderJSON()
106-
if err != nil {
107-
slog.Error("unable to render changelog", "error", err)
108-
return nil, err
109-
}
110-
fmt.Printf("%s", output)
111-
11299
return changelog, nil
113100
}

changelog/output/format.go

+52-13
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,110 @@ package output
22

33
import (
44
"encoding/json"
5-
"github.com/coreos/go-semver/semver"
5+
"github.com/Masterminds/semver/v3"
6+
"log/slog"
7+
"slices"
68
)
79

810
type Entry struct {
911
Tag string
1012
Name string
1113
Component string
1214
Description string
15+
Date string
1316
}
1417

1518
type Component struct {
1619
Name string `json:"name"`
20+
Title string `json:"title"`
1721
Description string `json:"description"`
1822
}
1923

2024
type Release struct {
2125
Tag string `json:"tag"`
26+
Date string `json:"date"`
2227
Components *[]Component `json:"components"`
2328
}
2429

2530
type Changelog struct {
2631
Releases *[]Release `json:"releases"`
2732
}
2833

29-
func (c *Changelog) AddEntry(entry Entry) {
34+
func (c *Changelog) AddEntry(entry Entry) error {
3035
if c.Releases == nil {
3136
c.Releases = &[]Release{}
3237
}
3338

3439
// create a new release and insert it into the changelog in the right order
3540
if len(*c.Releases) == 0 {
3641
// no releaes, create it anyways
42+
slog.Info("Inserting first release", "entry", entry)
3743
*c.Releases = append(*c.Releases, Release{
3844
Tag: entry.Tag,
45+
Date: entry.Date,
3946
Components: &[]Component{}})
4047
} else {
41-
// find the right place to insert the release
48+
// check if this release exist already
49+
found := false
4250
for _, r := range *c.Releases {
4351
if r.Tag == entry.Tag {
44-
// release already exists, break here
52+
found = true
53+
slog.Info("Release already exists", "tag", entry.Tag)
4554
break
4655
}
47-
v0 := semver.New(r.Tag)
48-
v1 := semver.New(entry.Tag)
49-
if v0.LessThan(*v1) {
50-
// add release before the current release
56+
}
57+
58+
if !found {
59+
// find the right place to insert the release
60+
added := false
61+
for i, r := range *c.Releases {
62+
v0, err := semver.NewVersion(r.Tag)
63+
if err != nil {
64+
slog.Error("unable to parse version", "tag", r.Tag, "error", err)
65+
return err
66+
}
67+
v1, err := semver.NewVersion(entry.Tag)
68+
if err != nil {
69+
slog.Error("unable to parse version", "tag", entry.Tag, "error", err)
70+
return err
71+
}
72+
if v0.LessThan(v1) {
73+
slog.Info("Inserting release", "tag", entry.Tag, "before", r.Tag)
74+
// add release before the current release
75+
*c.Releases = slices.Insert(*c.Releases, i, Release{
76+
Tag: entry.Tag,
77+
Date: entry.Date,
78+
Components: &[]Component{},
79+
})
80+
added = true
81+
}
82+
break
83+
}
84+
85+
if !added {
86+
// add release at the end
87+
slog.Info("Adding release at the end", "tag", entry.Tag, "at the end")
5188
*c.Releases = append(*c.Releases, Release{
5289
Tag: entry.Tag,
53-
Components: &[]Component{}})
90+
Date: entry.Date,
91+
Components: &[]Component{},
92+
})
5493
}
55-
break
5694
}
5795
}
5896

5997
// find the right release and add the component
60-
for i, release := range *c.Releases {
98+
for _, release := range *c.Releases {
6199
if release.Tag == entry.Tag {
62-
*(*c.Releases)[i].Components = append(*(*c.Releases)[i].Components, Component{
100+
*release.Components = append(*release.Components, Component{
63101
Name: entry.Component,
102+
Title: entry.Name,
64103
Description: entry.Description,
65104
})
66105
break
67106
}
68107
}
69-
108+
return nil
70109
}
71110

72111
func (c *Changelog) RenderJSON() (output []byte, err error) {

changelog/output/format_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestChangelog_AddEntry(t *testing.T) {
1010
changelog := &Changelog{}
1111
changelog.AddEntry(Entry{
12-
Tag: "1.0.0",
12+
Tag: "1.19.3",
1313
Name: "Initial Release",
1414
Component: "frontend",
1515
Description: "Initial frontend version",
1616
})
1717
changelog.AddEntry(Entry{
18-
Tag: "1.0.0",
18+
Tag: "1.19.3",
1919
Name: "Initial Release",
2020
Component: "backend",
2121
Description: "Initial backend version",
2222
})
2323
changelog.AddEntry(Entry{
24-
Tag: "1.1.0",
24+
Tag: "1.22.1",
2525
Name: "Feature 1",
2626
Component: "frontend",
2727
Description: "This cool frontend feature",
2828
})
2929
changelog.AddEntry(Entry{
30-
Tag: "1.1.0",
30+
Tag: "1.22.1",
3131
Name: "Feature 1",
3232
Component: "backend",
3333
Description: "This cool backend feature",
3434
})
3535
changelog.AddEntry(Entry{
36-
Tag: "1.2.0",
36+
Tag: "1.25.0",
3737
Name: "Feature 2",
3838
Component: "frontend",
3939
Description: "More cool frontend feature",

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ module github.com/prodyna/changelog-json
33
go 1.22.3
44

55
require (
6-
github.com/google/go-github/v61 v61.0.0
6+
github.com/Masterminds/semver/v3 v3.2.1
77
github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064
88
golang.org/x/oauth2 v0.20.0
99
)
1010

1111
require (
12-
github.com/coreos/go-semver v0.3.1 // indirect
13-
github.com/google/go-querystring v1.1.0 // indirect
12+
github.com/google/go-cmp v0.6.0 // indirect
1413
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
1514
)

go.sum

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
2-
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
3-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1+
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
2+
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
43
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
54
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6-
github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5pNlu1go=
7-
github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY=
8-
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
9-
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
105
github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064 h1:RCQBSFx5JrsbHltqTtJ+kN3U0Y3a/N/GlVdmRSoxzyE=
116
github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064/go.mod h1:zqMwyHmnN/eDOZOdiTohqIUKUrTFX62PNlu7IJdu0q8=
127
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=
138
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE=
149
golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo=
1510
golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
16-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
17-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)