Skip to content

Commit 619d8e8

Browse files
committed
Fix get latest panic on single entry changelog
1 parent 74c5093 commit 619d8e8

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

internal/get/get.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ func parseChangelog(fileName string) (changelog.Changelog, error) {
2828
func changelogWithSingleEntry(entry entry.Entry, repoName, repoOwner string) changelog.Changelog {
2929
// Isolate the entry
3030
entry.Next = nil
31-
entry.PrevTag = entry.Previous.Tag
32-
entry.Previous = nil
31+
if entry.Previous != nil {
32+
entry.PrevTag = entry.Previous.Tag
33+
entry.Previous = nil
34+
}
3335

3436
cl := changelog.NewChangelog(repoOwner, repoName)
3537
cl.Insert(entry)

internal/get/get_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
var fileName string = "CHANGELOG.md"
11+
var singleEntryFileName string = "single_CHANGELOG.md"
1112

1213
func TestGetAll(t *testing.T) {
1314
cl, err := get.GetAll(fileName)
@@ -32,6 +33,18 @@ func TestGetLatest(t *testing.T) {
3233
assert.Equal(t, "v0.13.0", cl.GetEntries()[0].PrevTag)
3334
}
3435

36+
func TestGetLatestWithNoPrevious(t *testing.T) {
37+
cl, err := get.GetLatest(singleEntryFileName)
38+
39+
// Should not error
40+
assert.Nil(t, err)
41+
42+
// Should have 1 entry
43+
count := len(cl.GetEntries())
44+
assert.Equal(t, 1, count)
45+
assert.Equal(t, "", cl.GetEntries()[0].PrevTag)
46+
}
47+
3548
func TestGetVersionWithAValidVersion(t *testing.T) {
3649
// Should not error when version is found
3750
cl, err := get.GetVersion(fileName, "v0.9.0")

0 commit comments

Comments
 (0)