Skip to content

Commit cf08c07

Browse files
committed
allow lagacy json key for changlelog gen
1 parent d00768c commit cf08c07

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

pkg/changelog/changed_data.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ type ChangedData struct {
1919
Apps map[string]*App `json:"apps"`
2020
}
2121

22+
// UnmarshalJSON supports loading both the current "apps" key and the legacy
23+
// "charts" key so that cached changelog.json files produced by older versions
24+
// continue to work without a cache bust.
25+
func (c *ChangedData) UnmarshalJSON(data []byte) error {
26+
type Alias ChangedData
27+
aux := &struct {
28+
*Alias
29+
Charts map[string]*App `json:"charts"`
30+
}{
31+
Alias: (*Alias)(c),
32+
}
33+
if err := json.Unmarshal(data, aux); err != nil {
34+
return err
35+
}
36+
// Merge legacy "charts" entries into Apps.
37+
if len(aux.Charts) > 0 && len(c.Apps) == 0 {
38+
c.Apps = aux.Charts
39+
} else {
40+
for k, v := range aux.Charts {
41+
if _, exists := c.Apps[k]; !exists {
42+
c.Apps[k] = v
43+
}
44+
}
45+
}
46+
return nil
47+
}
48+
2249
var readChangedDataFileFunc = os.ReadFile
2350
var marshalChangedDataFunc = json.MarshalIndent
2451

pkg/changelog/changed_data_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,79 @@ func TestWriteToFileAndRoundTrip(t *testing.T) {
212212
t.Fatalf("expected commit message preserved")
213213
}
214214
}
215+
216+
func TestLoadFromFileLegacyChartsKey(t *testing.T) {
217+
tmp := filepath.Join(t.TempDir(), "legacy.json")
218+
legacy := []byte(`{
219+
"last_commit": "legacy123",
220+
"charts": {
221+
"myapp": {
222+
"versions": {
223+
"2.0.0": {
224+
"version": "2.0.0",
225+
"train": "stable",
226+
"commits": {
227+
"h1": {
228+
"commit_hash": "h1",
229+
"parent_hash": "p1",
230+
"author": {"name": "dev", "date": "2024-06-01"},
231+
"kind": "fix",
232+
"message": "legacy commit"
233+
}
234+
}
235+
}
236+
}
237+
}
238+
}
239+
}`)
240+
if err := os.WriteFile(tmp, legacy, 0644); err != nil {
241+
t.Fatalf("write legacy file: %v", err)
242+
}
243+
244+
var cd ChangedData
245+
cd.mu = &sync.RWMutex{}
246+
if err := cd.LoadFromFile(tmp); err != nil {
247+
t.Fatalf("LoadFromFile legacy: %v", err)
248+
}
249+
if cd.LastCommit != "legacy123" {
250+
t.Fatalf("expected LastCommit legacy123, got %s", cd.LastCommit)
251+
}
252+
if cd.Apps["myapp"] == nil {
253+
t.Fatalf("expected myapp to be loaded from legacy charts key")
254+
}
255+
if cd.Apps["myapp"].Versions["2.0.0"].Commits["h1"].Message != "legacy commit" {
256+
t.Fatalf("expected legacy commit message preserved")
257+
}
258+
}
259+
260+
func TestLoadFromFileWritesAppsKey(t *testing.T) {
261+
tmp := filepath.Join(t.TempDir(), "roundtrip.json")
262+
cd := ChangedData{
263+
mu: &sync.RWMutex{},
264+
LastCommit: "rt1",
265+
Apps: map[string]*App{"testapp": {Versions: map[string]*Version{}}},
266+
}
267+
if err := cd.WriteToFile(tmp); err != nil {
268+
t.Fatalf("WriteToFile: %v", err)
269+
}
270+
raw, _ := os.ReadFile(tmp)
271+
if !contains(string(raw), `"apps"`) {
272+
t.Fatalf("expected written JSON to use \"apps\" key, got: %s", string(raw))
273+
}
274+
if contains(string(raw), `"charts"`) {
275+
t.Fatalf("written JSON must not contain legacy \"charts\" key")
276+
}
277+
}
278+
279+
func contains(s, substr string) bool {
280+
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsHelper(s, substr))
281+
}
282+
283+
func containsHelper(s, substr string) bool {
284+
for i := 0; i <= len(s)-len(substr); i++ {
285+
if s[i:i+len(substr)] == substr {
286+
return true
287+
}
288+
}
289+
return false
290+
}

0 commit comments

Comments
 (0)