diff --git a/CHANGELOG.md b/CHANGELOG.md index 49bdd9ce..adb81e5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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, diff --git a/generators/charts/changelog.go b/generators/charts/changelog.go index e2638480..438dc3ae 100644 --- a/generators/charts/changelog.go +++ b/generators/charts/changelog.go @@ -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 @@ -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] @@ -103,9 +108,11 @@ 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": @@ -113,7 +120,8 @@ func cmpSchemas(kind, parent string, wasSpec, hasSpec *schema) []string { 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}) } } } @@ -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 @@ -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") @@ -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 { diff --git a/generators/charts/changelog_test.go b/generators/charts/changelog_test.go index c5f6075f..380972cb 100644 --- a/generators/charts/changelog_test.go +++ b/generators/charts/changelog_test.go @@ -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 @@ -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 `, @@ -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 `,