Skip to content

Commit

Permalink
feat(changelog): override entries with same titles (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Jan 26, 2024
1 parent 0376caa commit 7a1b13a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
- Change `KafkaConnect` field `userConfig.additional_backup_regions`: deprecated
- Change `Kafka` field `userConfig.additional_backup_regions`: deprecated
- Change `OpenSearch` field `userConfig.additional_backup_regions`: deprecated
- Change `PostgreSQL` field `userConfig.pg_version`: enum ~~`[11, 12, 13, 14, 15]`~~ → `[11, 12, 13,
14, 15, 16]`
- Change `Redis` field `userConfig.additional_backup_regions`: deprecated
- Change `Cassandra` field `userConfig.cassandra_version`: enum ~~`[3, 4]`~~`[3, 4, 4.1]`
- Change `Cassandra` field `userConfig.cassandra_version`: enum ~~`[3, 4, 4.1]`~~`[4, 4.1]`
- Change `Kafka` field `userConfig.kafka_version`: enum ~~`[3.1, 3.3, 3.4, 3.5, 3.6]`~~`[3.4, 3.5, 3.6]`
- Change `PostgreSQL` field `userConfig.pg_version`: enum ~~`[11, 12, 13, 14, 15, 16]`~~ → `[12, 13,
Expand Down
40 changes: 29 additions & 11 deletions generators/charts/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func loadSchema(b []byte) (*schema, error) {
}

// genChangelog generates changelog for given yamls
func genChangelog(wasBytes, hasBytes []byte) ([]string, error) {
func genChangelog(wasBytes, hasBytes []byte) ([]changelog, error) {
wasSchema, err := loadSchema(wasBytes)
if err != nil {
return nil, err
Expand All @@ -80,18 +80,23 @@ func genChangelog(wasBytes, hasBytes []byte) ([]string, error) {

changes := cmpSchemas(hasSchema.Kind, "", wasSchema, hasSchema)
sort.Slice(changes, func(i, j int) bool {
return changes[i][0] < changes[j][0]
return changes[i].title < changes[j].title
})

return changes, nil
}

func cmpSchemas(kind, parent string, wasSpec, hasSpec *schema) []string {
type changelog struct {
title string
value string
}

func cmpSchemas(kind, parent string, wasSpec, hasSpec *schema) []changelog {
if cmp.Equal(wasSpec, hasSpec) {
return nil
}

changes := make([]string, 0)
changes := make([]changelog, 0)
for _, k := range mergedKeys(wasSpec.Properties, hasSpec.Properties) {
ov, oOk := wasSpec.Properties[k]
nv, nOk := hasSpec.Properties[k]
Expand All @@ -103,17 +108,20 @@ func cmpSchemas(kind, parent string, wasSpec, hasSpec *schema) []string {

switch {
case !nOk:
changes = append(changes, fmt.Sprintf("Remove `%s` field `%s`, type `%s`: %s", kind, fieldPath, ov.Type, shortDescription(ov.Description)))
title := fmt.Sprintf("Remove `%s` field `%s`, type `%s`", kind, fieldPath, ov.Type)
changes = append(changes, changelog{title: title, value: shortDescription(ov.Description)})
case !oOk:
changes = append(changes, fmt.Sprintf("Add `%s` field `%s`, type `%s`: %s", kind, fieldPath, nv.Type, shortDescription(nv.Description)))
title := fmt.Sprintf("Add `%s` field `%s`, type `%s`", kind, fieldPath, nv.Type)
changes = append(changes, changelog{title: title, value: shortDescription(nv.Description)})
case !cmp.Equal(ov, nv):
switch ov.Type {
case "object":
changes = append(changes, cmpSchemas(kind, fieldPath, ov, nv)...)
default:
c := fmtChanges(ov, nv)
if c != "" {
changes = append(changes, fmt.Sprintf("Change `%s` field `%s`: %s", kind, fieldPath, c))
title := fmt.Sprintf("Change `%s` field `%s`", kind, fieldPath)
changes = append(changes, changelog{title: title, value: c})
}
}
}
Expand Down Expand Up @@ -241,8 +249,9 @@ func softWrapLine(src, linebreak string, n int) string {
return src
}

func addChanges(body []byte, changes []string) string {
lines := strings.Split(string(body), "\n")
func addChanges(body []byte, changes []changelog) string {
strBody := string(body)
lines := strings.Split(strBody, "\n")

i := 0
headerAt := -1
Expand All @@ -262,8 +271,17 @@ func addChanges(body []byte, changes []string) string {
// adds extra line between headers (no changes in the changelog)
items = append(items, "")
}
outer:
for _, s := range changes {
items = append(items, softWrapLine("- "+s, "\n ", lineWidth))
line := softWrapLine(fmt.Sprintf("- %s: %s", s.title, s.value), "\n ", lineWidth)
// Replaces a change with the same title to store latest change only
for index, old := range items {
if strings.Contains(old, s.title) {
items[index] = line
continue outer
}
}
items = append(items, line)
}
items = append(items, lines[i:]...)
return strings.Join(items, "\n")
Expand All @@ -286,7 +304,7 @@ func updateChangelog(operatorPath, crdCharts string) (func() error, error) {
}

// Finds changes per Kind
changes := make([]string, 0)
changes := make([]changelog, 0)
for _, k := range sortedKeys(hasFiles) {
kindChanges, err := genChangelog(wasFiles[k], hasFiles[k])
if err != nil {
Expand Down
41 changes: 31 additions & 10 deletions generators/charts/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ func TestGenChangelog(t *testing.T) {
changes, err := genChangelog([]byte(testOldCRD), []byte(testNewCRD))
require.NoError(t, err)

expect := []string{
"Add `Kafka` field `disk_space`, type `string`: The disk space of the service",
"Change `Kafka` field `cloudName`: enum ~~`[bar, foo]`~~ → `[bar, baz, foo]`, maxLength ~~`120`~~ → `256`, maximum ~~`2`~~ → `4`, minimum ~~`1`~~ → `3`",
"Change `Kafka` field `topic_name`: enum `[bar, baz, foo]`, format ~~`^[1-9]*(GiB|G)*`~~ → `^[1-9][0-9]*(GiB|G)*`, maxLength ~~`111`~~ → `249`, maximum `1000000`, minLength `1`, minimum `1`",
"Remove `Kafka` field `karapace`, type `boolean`: Switch the service to use Karapace for schema registry and REST proxy",
expect := []changelog{
{title: "Add `Kafka` field `disk_space`, type `string`", value: "The disk space of the service"},
{title: "Change `Kafka` field `cloudName`", value: "enum ~~`[bar, foo]`~~ → `[bar, baz, foo]`, maxLength ~~`120`~~ → `256`, maximum ~~`2`~~ → `4`, minimum ~~`1`~~ → `3`"},
{title: "Change `Kafka` field `topic_name`", value: "enum `[bar, baz, foo]`, format ~~`^[1-9]*(GiB|G)*`~~ → `^[1-9][0-9]*(GiB|G)*`, maxLength ~~`111`~~ → `249`, maximum `1000000`, minLength `1`, minimum `1`"},
{title: "Remove `Kafka` field `karapace`, type `boolean`", value: "Switch the service to use Karapace for schema registry and REST proxy"},
}

assert.Equal(t, expect, changes)
}

func TestAddChanges(t *testing.T) {
changes := []string{"a change", "another change"}
changes := []changelog{
{title: "a change", value: "wow!"},
{title: "another change", value: "great!"},
}
cases := []struct {
name string
source string
Expand All @@ -99,8 +102,8 @@ func TestAddChanges(t *testing.T) {
`,
expect: `## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
- a change
- another change
- a change: wow!
- another change: great!
## v0.13.0 - 2023-08-18
`,
Expand All @@ -116,8 +119,26 @@ func TestAddChanges(t *testing.T) {
expect: `## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
- new go version
- a change
- another change
- a change: wow!
- another change: great!
## v0.13.0 - 2023-08-18
`,
},
{
name: "overrides existing change: old one! -> wow!",
source: `## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
- a change: old one!
- new go version
## v0.13.0 - 2023-08-18
`,
expect: `## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
- a change: wow!
- new go version
- another change: great!
## v0.13.0 - 2023-08-18
`,
Expand Down

0 comments on commit 7a1b13a

Please sign in to comment.