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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* [PR-15](https://github.com/itk-dev/gh-itkdev/pull/15)
Added “Under udvikling” as allowed “Unreleased” header

## [v1.0.0] - 2024-11-12

* [PR-11](https://github.com/itk-dev/gh-itkdev/pull/11)
Expand Down
22 changes: 20 additions & 2 deletions changelog/fuckingchangelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package changelog
import (
"bufio"
"fmt"
"iter"
"log"
"os"
"regexp"
Expand All @@ -11,8 +12,25 @@ import (
"text/template"
)

// https://stackoverflow.com/a/71624929
func Map[T, U any](seq iter.Seq[T], f func(T) U) iter.Seq[U] {
return func(yield func(U) bool) {
for a := range seq {
if !yield(f(a)) {
return
}
}
}
}

func addPullRequest(changelog string, pr pullRequest, itemTemplate string) (string, error) {
headerPattern := regexp.MustCompile(`(?i)^\#+ +\[unreleased\]`)
unreleasedHeaders := []string{"Unreleased", "Under udvikling"}
var quotedHeaders []string
for header := range Map(slices.Values(unreleasedHeaders), regexp.QuoteMeta) {
quotedHeaders = append(quotedHeaders, header)
}
headerPattern := regexp.MustCompile(fmt.Sprintf(`(?i)^\#+ +\[(%s)\]`, strings.Join(quotedHeaders, "|")))

unreleasedHeaderIndex := -1
var lines []string
scanner := bufio.NewScanner(strings.NewReader(changelog))
Expand All @@ -28,7 +46,7 @@ func addPullRequest(changelog string, pr pullRequest, itemTemplate string) (stri
}

if unreleasedHeaderIndex < 0 {
return "", fmt.Errorf("cannot find \"Unreleased\" header")
return "", fmt.Errorf("cannot find %s header", strings.Join(unreleasedHeaders, "/"))
}

// Make sure that we have a blank line after the header
Expand Down
65 changes: 62 additions & 3 deletions changelog/fuckingchangelog_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package changelog

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,6 +15,7 @@ func TestFuckingChangelog(t *testing.T) {
pullRequest pullRequest
itemTemplate string
expected string
expectedErr error
}{
{
`## [Unreleased]
Expand All @@ -29,6 +31,7 @@ func TestFuckingChangelog(t *testing.T) {
* [PR-87](https://example.com/pr/87)
Test
`,
nil,
},

{
Expand All @@ -49,6 +52,7 @@ func TestFuckingChangelog(t *testing.T) {

[Unreleased]: https://example.com/
`,
nil,
},

{
Expand All @@ -70,6 +74,7 @@ func TestFuckingChangelog(t *testing.T) {
* [PR-42](https://example.com/pr/42)
Added the meaning
`,
nil,
},

{
Expand All @@ -88,6 +93,7 @@ func TestFuckingChangelog(t *testing.T) {
- [#87](https://example.com/pr/87): Test
- [#42](https://example.com/pr/42): Added the meaning
`,
nil,
},

{
Expand All @@ -111,12 +117,65 @@ func TestFuckingChangelog(t *testing.T) {

- [#42](https://example.com/pr/42): Added the meaning
`,
nil,
},

{
`## [Under udvikling]
`,
pullRequest{
Number: 87,
Title: "Test",
Url: "https://example.com/pr/87",
},
defaultItemTemplate,
`## [Under udvikling]

* [PR-87](https://example.com/pr/87)
Test
`,
nil,
},

{
`## [under udviklinG]
`,
pullRequest{
Number: 87,
Title: "Test",
Url: "https://example.com/pr/87",
},
defaultItemTemplate,
`## [under udviklinG]

* [PR-87](https://example.com/pr/87)
Test
`,
nil,
},

{
`## [We're still working on it]
`,
pullRequest{
Number: 87,
Title: "Test",
Url: "https://example.com/pr/87",
},
defaultItemTemplate,
"",
fmt.Errorf("cannot find Unreleased/Under udvikling header"),
},
}

for _, testCase := range testCases {
actual, _ := addPullRequest(testCase.changelog, testCase.pullRequest, testCase.itemTemplate)

assert.Equal(t, testCase.expected, actual)
actual, err := addPullRequest(testCase.changelog, testCase.pullRequest, testCase.itemTemplate)
if err != nil {
if assert.NotNil(t, testCase.expectedErr) {
assert.Equal(t, testCase.expectedErr.Error(), err.Error())
}
} else {
assert.Equal(t, testCase.expected, actual)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/itk-dev/gh-itkdev

go 1.22
go 1.23

require (
github.com/cli/go-gh v1.2.1
Expand Down