Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get latest panic on single entry changelog #151

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ func parseChangelog(fileName string) (changelog.Changelog, error) {
func changelogWithSingleEntry(entry entry.Entry, repoName, repoOwner string) changelog.Changelog {
// Isolate the entry
entry.Next = nil
entry.PrevTag = entry.Previous.Tag
entry.Previous = nil
if entry.Previous != nil {
entry.PrevTag = entry.Previous.Tag
entry.Previous = nil
}

cl := changelog.NewChangelog(repoOwner, repoName)
cl.Insert(entry)
Expand Down
13 changes: 13 additions & 0 deletions internal/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

var fileName string = "CHANGELOG.md"
var singleEntryFileName string = "single_CHANGELOG.md"

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

func TestGetLatestWithNoPrevious(t *testing.T) {
cl, err := get.GetLatest(singleEntryFileName)

// Should not error
assert.Nil(t, err)

// Should have 1 entry
count := len(cl.GetEntries())
assert.Equal(t, 1, count)
assert.Equal(t, "", cl.GetEntries()[0].PrevTag)
}

func TestGetVersionWithAValidVersion(t *testing.T) {
// Should not error when version is found
cl, err := get.GetVersion(fileName, "v0.9.0")
Expand Down
10 changes: 10 additions & 0 deletions internal/get/single_CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- markdownlint-disable MD024 -->
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).

## [v0.1.0](https://github.com/chelnak/gh-changelog/tree/v0.1.0) - 2022-04-15

[Full Changelog](https://github.com/chelnak/gh-changelog/compare/42d4c93b23eaf307c5f9712f4c62014fe38332bd...v0.1.0)
Loading