Skip to content

Commit

Permalink
Creates a full list, returns all parameters, but not fully complete
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed May 13, 2024
1 parent 6b67fbb commit 7283458
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
33 changes: 23 additions & 10 deletions changelog/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package changelog

import (
"context"
"fmt"
"github.com/prodyna/changelog-json/changelog/output"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -35,7 +36,14 @@ 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{}
changelog = &output.Changelog{
Releases: &[]output.Release{
{
Tag: "0.0.0",
Components: []output.Component{},
},
},
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: clg.config.GitHubToken},
)
Expand Down Expand Up @@ -83,18 +91,23 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.

for _, release := range query.Organization.Repository.Releases.Nodes {
slog.Debug("Release", "tag", release.Tag.Name, "date", release.CreatedAt, "name", release.Name, "description.len", len(release.Description))
for _, pr := range release.Tag.AssociatedPullRequests.Nodes {
slog.Debug("PR", "number", pr.Number, "author", pr.Author.Login)
entry := output.Entry{
Tag: release.Tag.Name,
Name: release.Name,
Component: repository,
Description: release.Description,
}
changelog.AddEntry(entry)
entry := output.Entry{
Tag: release.Tag.Name,
Name: release.Name,
Component: repository,
Description: release.Description,
}
slog.Info("Adding entry", "tag", entry.Tag, "name", entry.Name, "component", entry.Component, "description", 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
}
20 changes: 11 additions & 9 deletions changelog/output/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ type Release struct {
}

type Changelog struct {
Releases []Release `json:"releases"`
Releases *[]Release `json:"releases"`
}

func (c *Changelog) AddEntry(entry Entry) {
if c.Releases == nil {
c.Releases = []Release{}
c.Releases = &[]Release{}
}

// create a new release and insert it into the changelog in the right order
if len(c.Releases) == 0 {
if len(*c.Releases) == 0 {
// no releaes, create it anyways
c.Releases = append(c.Releases, Release{
*c.Releases = append(*c.Releases, Release{
Tag: entry.Tag,
Components: []Component{}})
} else {
// find the right place to insert the release
for i, r := range c.Releases {
for _, r := range *c.Releases {
if r.Tag == entry.Tag {
// release already exists, break here
break
Expand All @@ -48,16 +48,18 @@ func (c *Changelog) AddEntry(entry Entry) {
v1 := semver.New(entry.Tag)
if v0.LessThan(*v1) {
// add release before the current release
c.Releases = append(c.Releases[:i], append([]Release{{Tag: entry.Tag, Components: []Component{}}}, c.Releases[i:]...)...)
break
*c.Releases = append(*c.Releases, Release{
Tag: entry.Tag,
Components: []Component{}})
}
break
}
}

// find the right release and add the component
for i, release := range c.Releases {
for i, release := range *c.Releases {
if release.Tag == entry.Tag {
c.Releases[i].Components = append(c.Releases[i].Components, Component{
(*c.Releases)[i].Components = append((*c.Releases)[i].Components, Component{
Name: entry.Component,
Description: entry.Description,
})
Expand Down
28 changes: 14 additions & 14 deletions changelog/output/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,34 @@ func TestChangelog_AddEntry(t *testing.T) {
}
fmt.Printf("%s", output)

if len(changelog.Releases) != 3 {
t.Errorf("expected 3 releases, got %d", len(changelog.Releases))
if len(*changelog.Releases) != 3 {
t.Errorf("expected 3 releases, got %d", len(*changelog.Releases))
}

if len(changelog.Releases[0].Components) != 1 {
t.Errorf("expected 2 components, got %d", len(changelog.Releases[0].Components))
if len((*changelog.Releases)[0].Components) != 1 {
t.Errorf("expected 2 components, got %d", len((*changelog.Releases)[0].Components))
}

if len(changelog.Releases[1].Components) != 2 {
t.Errorf("expected 2 components, got %d", len(changelog.Releases[1].Components))
if len((*changelog.Releases)[1].Components) != 2 {
t.Errorf("expected 2 components, got %d", len((*changelog.Releases)[1].Components))
}

if len(changelog.Releases[2].Components) != 2 {
t.Errorf("expected 1 component, got %d", len(changelog.Releases[2].Components))
if len((*changelog.Releases)[2].Components) != 2 {
t.Errorf("expected 1 component, got %d", len((*changelog.Releases)[2].Components))
}

// expect that the first release is 1.2.0
if changelog.Releases[0].Tag != "1.2.0" {
t.Errorf("expected first release to be 1.2.0, got %s", changelog.Releases[0].Tag)
if (*changelog.Releases)[0].Tag != "1.2.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" {
t.Errorf("expected second release to be 1.1.0, got %s", changelog.Releases[1].Tag)
if (*changelog.Releases)[1].Tag != "1.1.0" {
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" {
t.Errorf("expected third release to be 1.0.0, got %s", changelog.Releases[2].Tag)
if (*changelog.Releases)[2].Tag != "1.0.0" {
t.Errorf("expected third release to be 1.0.0, got %s", (*changelog.Releases)[2].Tag)
}
}

0 comments on commit 7283458

Please sign in to comment.