Skip to content
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
7 changes: 4 additions & 3 deletions changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ func (p *Parser) Between(oldVersion, newVersion string) (string, bool) {
var start, end int
found := false

if oldLine >= 0 && newLine >= 0 {
switch {
case oldLine >= 0 && newLine >= 0:
if oldLine < newLine {
// Ascending: old appears first, take from old line to end
start = oldLine
Expand All @@ -258,14 +259,14 @@ func (p *Parser) Between(oldVersion, newVersion string) (string, bool) {
end = oldLine
}
found = true
} else if oldLine >= 0 {
case oldLine >= 0:
if oldLine == 0 {
return "", false
}
start = 0
end = oldLine
found = true
} else if newLine >= 0 {
case newLine >= 0:
start = newLine
end = len(lines)
found = true
Expand Down
18 changes: 11 additions & 7 deletions changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func TestKeepAChangelogFormat(t *testing.T) {
}

entry, _ = p.Entry("1.1.0")
assertDate(t, entry.Date, 2024, time.March, 15)
assertDate(t, entry.Date, time.March, 15)

entry, _ = p.Entry("1.0.1")
assertDate(t, entry.Date, 2024, time.February, 1)
assertDate(t, entry.Date, time.February, 1)

entry, _ = p.Entry("1.0.0")
assertDate(t, entry.Date, 2024, time.January, 15)
assertDate(t, entry.Date, time.January, 15)
})

t.Run("extracts content", func(t *testing.T) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestMarkdownHeaderFormat(t *testing.T) {
if !ok {
t.Fatal("2.0.0 not found")
}
assertDate(t, entry.Date, 2024, time.March, 1)
assertDate(t, entry.Date, time.March, 1)
})

t.Run("parses versions without dates", func(t *testing.T) {
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestCustomPattern(t *testing.T) {
}

entry, _ := p.Entry("1.2.0")
assertDate(t, entry.Date, 2024, time.January, 1)
assertDate(t, entry.Date, time.January, 1)
})

t.Run("custom match group", func(t *testing.T) {
Expand Down Expand Up @@ -477,6 +477,9 @@ func TestEdgeCases(t *testing.T) {
}
})

}

func TestEdgeCasesContent(t *testing.T) {
t.Run("preserves markdown links", func(t *testing.T) {
p := Parse("## [1.0.0] - 2024-01-01\n\n- Added [feature](https://example.com)\n- See [docs](https://docs.example.com) for details\n")
entry, _ := p.Entry("1.0.0")
Expand Down Expand Up @@ -552,7 +555,7 @@ func TestEdgeCases(t *testing.T) {
if !ok {
t.Fatal("1.0.0 not found")
}
assertDate(t, entry.Date, 2024, time.January, 1)
assertDate(t, entry.Date, time.January, 1)
})
}

Expand All @@ -576,11 +579,12 @@ func TestComprehensiveFixture(t *testing.T) {
}
}

func assertDate(t *testing.T, got *time.Time, year int, month time.Month, day int) {
func assertDate(t *testing.T, got *time.Time, month time.Month, day int) {
t.Helper()
if got == nil {
t.Fatal("expected non-nil date")
}
const year = 2024
if got.Year() != year || got.Month() != month || got.Day() != day {
t.Errorf("date = %v, want %d-%02d-%02d", got, year, month, day)
}
Expand Down
6 changes: 4 additions & 2 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func RawContentURL(repoURL, filename string) (string, error) {
return "", fmt.Errorf("parsing repository URL: %w", err)
}

parts := strings.SplitN(strings.TrimPrefix(parsed.Path, "/"), "/", 3)
if len(parts) < 2 {
const maxURLParts = 3
const minURLParts = 2
parts := strings.SplitN(strings.TrimPrefix(parsed.Path, "/"), "/", maxURLParts)
if len(parts) < minURLParts {
return "", fmt.Errorf("cannot parse owner/repo from %s", repoURL)
}
owner := parts[0]
Expand Down
Loading