refactor: move things to packages (#39)

This commit is contained in:
Julian Tölle 2024-08-31 15:23:21 +02:00 committed by GitHub
parent 44184a77f9
commit a0a064d387
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 923 additions and 892 deletions

View file

@ -0,0 +1,54 @@
package changelog
import (
"bytes"
_ "embed"
"html/template"
"log"
"github.com/apricote/releaser-pleaser/internal/commitparser"
)
var (
changelogTemplate *template.Template
)
//go:embed changelog.md.tpl
var rawChangelogTemplate string
func init() {
var err error
changelogTemplate, err = template.New("changelog").Parse(rawChangelogTemplate)
if err != nil {
log.Fatalf("failed to parse changelog template: %v", err)
}
}
func NewChangelogEntry(commits []commitparser.AnalyzedCommit, version, link, prefix, suffix string) (string, error) {
features := make([]commitparser.AnalyzedCommit, 0)
fixes := make([]commitparser.AnalyzedCommit, 0)
for _, commit := range commits {
switch commit.Type {
case "feat":
features = append(features, commit)
case "fix":
fixes = append(fixes, commit)
}
}
var changelog bytes.Buffer
err := changelogTemplate.Execute(&changelog, map[string]any{
"Features": features,
"Fixes": fixes,
"Version": version,
"VersionLink": link,
"Prefix": prefix,
"Suffix": suffix,
})
if err != nil {
return "", err
}
return changelog.String(), nil
}

View file

@ -0,0 +1,22 @@
## [{{.Version}}]({{.VersionLink}})
{{- if .Prefix }}
{{ .Prefix }}
{{ end -}}
{{- if (gt (len .Features) 0) }}
### Features
{{ range .Features -}}
- {{ if .Scope }}**{{.Scope}}**: {{end}}{{.Description}}
{{ end -}}
{{- end -}}
{{- if (gt (len .Fixes) 0) }}
### Bug Fixes
{{ range .Fixes -}}
- {{ if .Scope }}**{{.Scope}}**: {{end}}{{.Description}}
{{ end -}}
{{- end -}}
{{- if .Suffix }}
{{ .Suffix }}
{{ end -}}

View file

@ -0,0 +1,174 @@
package changelog
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/apricote/releaser-pleaser/internal/commitparser"
"github.com/apricote/releaser-pleaser/internal/git"
)
func ptr[T any](input T) *T {
return &input
}
func Test_NewChangelogEntry(t *testing.T) {
type args struct {
analyzedCommits []commitparser.AnalyzedCommit
version string
link string
prefix string
suffix string
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "empty",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{},
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: "## [1.0.0](https://example.com/1.0.0)",
wantErr: assert.NoError,
},
{
name: "single feature",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "feat",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: "## [1.0.0](https://example.com/1.0.0)\n### Features\n\n- Foobar!\n",
wantErr: assert.NoError,
},
{
name: "single fix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: "## [1.0.0](https://example.com/1.0.0)\n### Bug Fixes\n\n- Foobar!\n",
wantErr: assert.NoError,
},
{
name: "multiple commits with scopes",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "feat",
Description: "Blabla!",
},
{
Commit: git.Commit{},
Type: "feat",
Description: "So awesome!",
Scope: ptr("awesome"),
},
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
{
Commit: git.Commit{},
Type: "fix",
Description: "So sad!",
Scope: ptr("sad"),
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
},
want: `## [1.0.0](https://example.com/1.0.0)
### Features
- Blabla!
- **awesome**: So awesome!
### Bug Fixes
- Foobar!
- **sad**: So sad!
`,
wantErr: assert.NoError,
},
{
name: "prefix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
prefix: "### Breaking Changes",
},
want: `## [1.0.0](https://example.com/1.0.0)
### Breaking Changes
### Bug Fixes
- Foobar!
`,
wantErr: assert.NoError,
},
{
name: "suffix",
args: args{
analyzedCommits: []commitparser.AnalyzedCommit{
{
Commit: git.Commit{},
Type: "fix",
Description: "Foobar!",
},
},
version: "1.0.0",
link: "https://example.com/1.0.0",
suffix: "### Compatibility\n\nThis version is compatible with flux-compensator v2.2 - v2.9.",
},
want: `## [1.0.0](https://example.com/1.0.0)
### Bug Fixes
- Foobar!
### Compatibility
This version is compatible with flux-compensator v2.2 - v2.9.
`,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewChangelogEntry(tt.args.analyzedCommits, tt.args.version, tt.args.link, tt.args.prefix, tt.args.suffix)
if !tt.wantErr(t, err) {
return
}
assert.Equal(t, tt.want, got)
})
}
}