Skip to content

Commit

Permalink
feat(parser): add RelatedItems property
Browse files Browse the repository at this point in the history
removes PR property in favour of an array of issues/PRs
  • Loading branch information
scottmckendry committed Jan 12, 2025
1 parent 7b6cf30 commit fed2a7b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
49 changes: 37 additions & 12 deletions changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"regexp"
"slices"
"strings"
"time"

Expand All @@ -13,7 +14,7 @@ import (
const (
dateFormat = "2006-01-02"
versionPattern = `## (?:\[)?(?:v)?([\d.]+(?:-[a-zA-Z0-9]+(?:\.[0-9]+)?)?)\]?(?:\((.*?)\))? \((\d{4}-\d{2}-\d{2})\)`
changePattern = `\* (?:\*\*(.*?)\*\*: )?(.+?)\s*(?:\((?:#(\d+))?\)?)?\s*(?:\((.*?)\))?(?:,\s*closes.*)?$`
changePattern = `\* (?:\*\*(.*?)\*\*: )?(.+?)\s*(?:\((.*?)\))?(?:,\s*closes.*)?$`
)

type ChangelogEntry struct {
Expand All @@ -24,11 +25,11 @@ type ChangelogEntry struct {
}

type Change struct {
Scope string `json:"scope,omitempty" yaml:"scope,omitempty" toml:"scope,omitempty"`
Description string `json:"description" yaml:"description" toml:"description"`
PR string `json:"pr,omitempty" yaml:"pr,omitempty" toml:"pr,omitempty"`
Commit string `json:"commit,omitempty" yaml:"commit,omitempty" toml:"commit,omitempty"`
CommitBody string `json:"commitBody,omitempty" yaml:"commitBody,omitempty" toml:"commitBody,omitempty"`
Scope string `json:"scope,omitempty" yaml:"scope,omitempty" toml:"scope,omitempty"`
Description string `json:"description" yaml:"description" toml:"description"`
Commit string `json:"commit,omitempty" yaml:"commit,omitempty" toml:"commit,omitempty"`
CommitBody string `json:"commitBody,omitempty" yaml:"commitBody,omitempty" toml:"commitBody,omitempty"`
RelatedItems []string `json:"relatedItems,omitempty" yaml:"relatedItems,omitempty" toml:"relatedItems,omitempty"`
}

type Parser struct {
Expand Down Expand Up @@ -126,18 +127,25 @@ func (p *Parser) parseChange(line string, changeRegex *regexp.Regexp, currentSec
}

change := Change{
Scope: matches[1],
Description: matches[2],
Scope: matches[1],
Description: matches[2],
RelatedItems: extractRelatedItems(matches[2]), // Extract from description
}

if matches[3] != "" {
change.PR = matches[3]
}
if matches[4] != "" {
change.Commit = parseCommitHashFromLink(matches[4])
change.Commit = parseCommitHashFromLink(matches[3])
if err := p.addCommitBody(&change); err != nil {
return err
}
// Extract related items from commit body if available
if change.CommitBody != "" {
bodyItems := extractRelatedItems(change.CommitBody)
for _, item := range bodyItems {
if !slices.Contains(change.RelatedItems, item) {
change.RelatedItems = append(change.RelatedItems, item)
}
}
}
}

if currentSection != "" {
Expand Down Expand Up @@ -176,3 +184,20 @@ func parseCommitHashFromLink(link string) string {

return ""
}

func extractRelatedItems(text string) []string {
regex := regexp.MustCompile(`#(\d+)`)
matches := regex.FindAllStringSubmatch(text, -1)

seen := make(map[string]bool)
var items []string

for _, match := range matches {
if !seen[match[1]] {
items = append(items, match[1])
seen[match[1]] = true
}
}

return items
}
34 changes: 17 additions & 17 deletions changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func createTestEntry(version, date string, compareURL string, changes map[string
}
}

func createTestChange(description string, scope string, pr string, commit string) Change {
func createTestChange(description string, scope string, commit string, relatedItems []string) Change {
return Change{
Description: description,
Scope: scope,
PR: pr,
Commit: commit,
Description: description,
Scope: scope,
Commit: commit,
RelatedItems: relatedItems,
}
}

Expand All @@ -40,7 +40,7 @@ func TestParse(t *testing.T) {
### Features
* **api**: add new endpoint (#123)
* **api**: add new endpoint
* basic feature (1a196c09283903991da080552e3aa980ac64fec9)
### Bug Fixes
Expand All @@ -50,11 +50,11 @@ func TestParse(t *testing.T) {
want: []ChangelogEntry{
createTestEntry("1.0.0", "2025-01-01", "https://github.com/user/repo/compare/v0.1.0...v1.0.0", map[string][]Change{
"Features": {
createTestChange("add new endpoint", "api", "123", ""),
createTestChange("basic feature", "", "", "1a196c09283903991da080552e3aa980ac64fec9"),
createTestChange("add new endpoint", "api", "", nil),
createTestChange("basic feature", "", "1a196c09283903991da080552e3aa980ac64fec9", nil),
},
"Bug Fixes": {
createTestChange("fix button alignment", "ui", "", ""),
createTestChange("fix button alignment", "ui", "", nil),
},
}),
},
Expand All @@ -71,7 +71,7 @@ func TestParse(t *testing.T) {
want: []ChangelogEntry{
createTestEntry("1.0.0-alpha.1", "2025-01-01", "https://github.com/user/repo/compare/v0.1.0...v1.0.0-alpha.1", map[string][]Change{
"Features": {
createTestChange("basic feature", "", "", ""),
createTestChange("basic feature", "", "", nil),
},
}),
},
Expand All @@ -83,12 +83,12 @@ func TestParse(t *testing.T) {
### Features
* basic feature
* basic feature #456
`,
want: []ChangelogEntry{
createTestEntry("1.0.0", "2025-01-01", "", map[string][]Change{
"Features": {
createTestChange("basic feature", "", "", ""),
createTestChange("basic feature #456", "", "", []string{"456"}),
},
}),
},
Expand All @@ -107,9 +107,9 @@ func TestParse(t *testing.T) {
want: []ChangelogEntry{
createTestEntry("1.0.0", "2025-01-01", "https://github.com/user/repo/compare/v0.1.0...v1.0.0", map[string][]Change{
"Features": {
createTestChange("basic feature", "", "", "8f5b75c6ba6c525e29463e2a96fec119e426e283"),
createTestChange("another feature", "", "", "22822a9f19442b51d952b550e73ad3c229583371"),
createTestChange("some docs", "", "", ""),
createTestChange("basic feature", "", "8f5b75c6ba6c525e29463e2a96fec119e426e283", nil),
createTestChange("another feature", "", "22822a9f19442b51d952b550e73ad3c229583371", nil),
createTestChange("some docs", "", "", nil),
},
}),
},
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestGetLatest(t *testing.T) {
## [v2.0.0](https://github.com/user/repo/compare/v1.0.0...v2.0.0) (2025-02-01)
### Features
* new feature
* new feature #123
## [v1.0.0](https://github.com/user/repo/compare/v0.1.0...v1.0.0) (2025-01-01)
### Features
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestGetVersion(t *testing.T) {
## [v2.0.0](https://github.com/user/repo/compare/v1.0.0...v2.0.0) (2025-02-01)
### Features
* new feature
* new feature #123
## [v1.0.0](https://github.com/user/repo/compare/v0.1.0...v1.0.0) (2025-01-01)
### Features
Expand Down

0 comments on commit fed2a7b

Please sign in to comment.