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

## [Unreleased]

* [PR-11](https://github.com/rimi-itk/gh-itkdev/pull/11)
Added format detector
* [PR-9](https://github.com/rimi-itk/gh-itkdev/pull/9)
Added alternative base branches
* [PR-7](https://github.com/rimi-itk/gh-itkdev/pull/7)
Expand Down
57 changes: 57 additions & 0 deletions changelog/format_detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package changelog

import (
"fmt"
"os"
"regexp"
)

func DetectPullRequestEntryFormat(changelog string) (string, error) {
if _, err := os.Stat(changelog); err == nil {
b, err := os.ReadFile(changelog)
if err != nil {
return "", fmt.Errorf("cannot read file %s", changelog)
}
changelog = string(b)
}

templates := getPullRequestEntryTemplates()

for _, template := range templates {
if template.pattern.MatchString(changelog) {
return template.template, nil
}
}

return templates[len(templates)-1].template, nil
}

func getPullRequestEntryTemplates() []struct {
pattern *regexp.Regexp
template string
} {
return []struct {
pattern *regexp.Regexp
template string
}{
{
regexp.MustCompile("(?m)^- \\[PR-[0-9]+\\]\\([^)]+\\)\n .+$"),
`- [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},

{
regexp.MustCompile("(?m)^- \\[#[0-9]+\\]\\([^)]+\\)\n .+$"),
`- [#{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},

// The default – must come last.
{
// Match any non-empty changelog and provide a default template.
regexp.MustCompile("."),
`* [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},
}
}
44 changes: 44 additions & 0 deletions changelog/format_detector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package changelog

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPullRequestTemplateDetector(t *testing.T) {
testCases := []struct {
changelog string
expected string
}{
{
`- [PR-87](https://example.com/pr/87)
Test
`,
`- [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},

{
`- [#87](https://example.com/pr/87)
Test
`,
`- [#{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},

{
`* [PR-87](https://example.com/pr/87)
Test
`,
`* [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`,
},
}

for _, testCase := range testCases {
actual, _ := DetectPullRequestEntryFormat(testCase.changelog)

assert.Equal(t, testCase.expected, actual)
}
}
10 changes: 7 additions & 3 deletions cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package cmd

import (
"fmt"
"os/exec"

"github.com/rimi-itk/gh-itkdev/changelog"
"github.com/spf13/cobra"
"os/exec"
)

// changelogCmd represents the changelog command
var (
create bool

fuckingChangelog bool
pullRequestItemTemplate string = `* [PR-{{ .Number }}]({{ .Url }})
{{ .Title }}`
pullRequestItemTemplate string = func() string {
format, _ := changelog.DetectPullRequestEntryFormat(changelogName)

return format
}()

release string
baseBranch string = func() string {
Expand Down
Loading