Skip to content

Commit

Permalink
Fix a parser bug for Unreleased entries
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed May 3, 2024
1 parent a46b55e commit 9b58248
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
5 changes: 3 additions & 2 deletions internal/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ 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).
{{- $unreleased := .GetUnreleased }}
{{if $unreleased }}
{{- if $unreleased }}
## Unreleased
{{range $unreleased }}
- {{.}}
{{- end}}
{{- end -}}
{{- end }}
{{range .GetEntries}}
## [{{.Tag}}](https://github.com/{{$.GetRepoOwner}}/{{$.GetRepoName}}/tree/{{.Tag}}) - {{.Date.Format "2006-01-02"}}
{{ if .Previous }}
Expand Down
26 changes: 23 additions & 3 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (p *parser) Parse() (changelog.Changelog, error) {
markdownParser := mdparser.New()
output := markdownParser.Parse(data)

var tagIndex []string // This is a list of tags in order
var tagIndex []string // This is a list of tags in order
var unreleased []string
var entries = map[string]*entry.Entry{} // Maintain a map of tag to entry
var currentTag string
var currentSection string
Expand All @@ -68,10 +69,14 @@ func (p *parser) Parse() (changelog.Changelog, error) {
case *ast.Heading:
if isHeading(child, 2) {
currentTag = getTagFromHeadingLink(child)
if currentTag == "" && isHeadingUnreleased(child) {
currentTag = "Unreleased"
continue
}
date := getDateFromHeading(child)
if _, ok := entries[currentTag]; !ok {
entry := entry.NewEntry(currentTag, date)
entries[currentTag] = &entry
e := entry.NewEntry(currentTag, date)
entries[currentTag] = &e
tagIndex = append(tagIndex, currentTag)
}
}
Expand All @@ -81,6 +86,13 @@ func (p *parser) Parse() (changelog.Changelog, error) {
}
case *ast.List:
items := getItemsFromList(child)
if currentTag == "Unreleased" {
for _, item := range items {
unreleased = append(unreleased, getTextFromChildNodes(item))
}
continue
}

for _, item := range items {
err := entries[currentTag].Append(currentSection, getTextFromChildNodes(item))
if err != nil {
Expand All @@ -97,6 +109,10 @@ func (p *parser) Parse() (changelog.Changelog, error) {

cl := changelog.NewChangelog(p.repoOwner, p.repoName)

if len(unreleased) > 0 {
cl.AddUnreleased(unreleased)
}

for _, tag := range tagIndex {
cl.Insert(*entries[tag])
}
Expand Down Expand Up @@ -186,3 +202,7 @@ func getDateFromHeading(node ast.Node) time.Time {
}
return date
}

func isHeadingUnreleased(node ast.Node) bool {
return strings.Contains(getTextFromChildNodes(node), "Unreleased")
}
25 changes: 24 additions & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
package parser_test

// TODO: Write tests for parser.go
import (
"testing"

"github.com/chelnak/gh-changelog/pkg/parser"
"github.com/stretchr/testify/require"
)

func TestParser(t *testing.T) {
t.Run("can parse a changelog with an unreleased section", func(t *testing.T) {
p := parser.NewParser("./testdata/unreleased.md", "chelnak", "gh-changelog")
c, err := p.Parse()
require.NoError(t, err)
require.Len(t, c.GetUnreleased(), 2)
require.Len(t, c.GetEntries(), 3)
})

t.Run("can parse a changelog without an unreleased section", func(t *testing.T) {
p := parser.NewParser("./testdata/no_unreleased.md", "chelnak", "gh-changelog")
c, err := p.Parse()
require.NoError(t, err)
require.Len(t, c.GetUnreleased(), 0)
require.Len(t, c.GetEntries(), 3)
})
}
26 changes: 26 additions & 0 deletions pkg/parser/testdata/no_unreleased.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- 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.15.1](https://github.com/chelnak/gh-changelog/tree/v0.15.1) - 2023-10-09

[Full Changelog](https://github.com/chelnak/gh-changelog/compare/v0.15.0...v0.15.1)

### Fixed

- bugfix: Release creation toggling RepoName & RepoOwner [#142](https://github.com/chelnak/gh-changelog/pull/142) ([Ramesh7](https://github.com/Ramesh7))

## [v0.15.0](https://github.com/chelnak/gh-changelog/tree/v0.15.0) - 2023-10-01

[Full Changelog](https://github.com/chelnak/gh-changelog/compare/v0.14.0...v0.15.0)

### Added

- Improve sections ordering [#139](https://github.com/chelnak/gh-changelog/pull/139) ([smortex](https://github.com/smortex))

## [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)
31 changes: 31 additions & 0 deletions pkg/parser/testdata/unreleased.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!-- 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).

## Unreleased

- Fix no previous tag when using get cmd [#148](https://github.com/chelnak/gh-changelog/pull/148) ([h0tw1r3](https://github.com/h0tw1r3))
- Add missing line between "Changed" title and list [#146](https://github.com/chelnak/gh-changelog/pull/146) ([smortex](https://github.com/smortex))

## [v0.15.1](https://github.com/chelnak/gh-changelog/tree/v0.15.1) - 2023-10-09

[Full Changelog](https://github.com/chelnak/gh-changelog/compare/v0.15.0...v0.15.1)

### Fixed

- bugfix: Release creation toggling RepoName & RepoOwner [#142](https://github.com/chelnak/gh-changelog/pull/142) ([Ramesh7](https://github.com/Ramesh7))

## [v0.15.0](https://github.com/chelnak/gh-changelog/tree/v0.15.0) - 2023-10-01

[Full Changelog](https://github.com/chelnak/gh-changelog/compare/v0.14.0...v0.15.0)

### Added

- Improve sections ordering [#139](https://github.com/chelnak/gh-changelog/pull/139) ([smortex](https://github.com/smortex))

## [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)

0 comments on commit 9b58248

Please sign in to comment.