Skip to content

Commit fd40093

Browse files
authored
Merge pull request #11 from rimi-itk/feature/changelog-format-detection
Added format detector
2 parents e6330f1 + 2028694 commit fd40093

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
* [PR-11](https://github.com/rimi-itk/gh-itkdev/pull/11)
11+
Added format detector
1012
* [PR-9](https://github.com/rimi-itk/gh-itkdev/pull/9)
1113
Added alternative base branches
1214
* [PR-7](https://github.com/rimi-itk/gh-itkdev/pull/7)

changelog/format_detector.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package changelog
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
)
8+
9+
func DetectPullRequestEntryFormat(changelog string) (string, error) {
10+
if _, err := os.Stat(changelog); err == nil {
11+
b, err := os.ReadFile(changelog)
12+
if err != nil {
13+
return "", fmt.Errorf("cannot read file %s", changelog)
14+
}
15+
changelog = string(b)
16+
}
17+
18+
templates := getPullRequestEntryTemplates()
19+
20+
for _, template := range templates {
21+
if template.pattern.MatchString(changelog) {
22+
return template.template, nil
23+
}
24+
}
25+
26+
return templates[len(templates)-1].template, nil
27+
}
28+
29+
func getPullRequestEntryTemplates() []struct {
30+
pattern *regexp.Regexp
31+
template string
32+
} {
33+
return []struct {
34+
pattern *regexp.Regexp
35+
template string
36+
}{
37+
{
38+
regexp.MustCompile("(?m)^- \\[PR-[0-9]+\\]\\([^)]+\\)\n .+$"),
39+
`- [PR-{{ .Number }}]({{ .Url }})
40+
{{ .Title }}`,
41+
},
42+
43+
{
44+
regexp.MustCompile("(?m)^- \\[#[0-9]+\\]\\([^)]+\\)\n .+$"),
45+
`- [#{{ .Number }}]({{ .Url }})
46+
{{ .Title }}`,
47+
},
48+
49+
// The default – must come last.
50+
{
51+
// Match any non-empty changelog and provide a default template.
52+
regexp.MustCompile("."),
53+
`* [PR-{{ .Number }}]({{ .Url }})
54+
{{ .Title }}`,
55+
},
56+
}
57+
}

changelog/format_detector_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package changelog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPullRequestTemplateDetector(t *testing.T) {
10+
testCases := []struct {
11+
changelog string
12+
expected string
13+
}{
14+
{
15+
`- [PR-87](https://example.com/pr/87)
16+
Test
17+
`,
18+
`- [PR-{{ .Number }}]({{ .Url }})
19+
{{ .Title }}`,
20+
},
21+
22+
{
23+
`- [#87](https://example.com/pr/87)
24+
Test
25+
`,
26+
`- [#{{ .Number }}]({{ .Url }})
27+
{{ .Title }}`,
28+
},
29+
30+
{
31+
`* [PR-87](https://example.com/pr/87)
32+
Test
33+
`,
34+
`* [PR-{{ .Number }}]({{ .Url }})
35+
{{ .Title }}`,
36+
},
37+
}
38+
39+
for _, testCase := range testCases {
40+
actual, _ := DetectPullRequestEntryFormat(testCase.changelog)
41+
42+
assert.Equal(t, testCase.expected, actual)
43+
}
44+
}

cmd/changelog.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package cmd
22

33
import (
44
"fmt"
5+
"os/exec"
6+
57
"github.com/rimi-itk/gh-itkdev/changelog"
68
"github.com/spf13/cobra"
7-
"os/exec"
89
)
910

1011
// changelogCmd represents the changelog command
1112
var (
1213
create bool
1314

1415
fuckingChangelog bool
15-
pullRequestItemTemplate string = `* [PR-{{ .Number }}]({{ .Url }})
16-
{{ .Title }}`
16+
pullRequestItemTemplate string = func() string {
17+
format, _ := changelog.DetectPullRequestEntryFormat(changelogName)
18+
19+
return format
20+
}()
1721

1822
release string
1923
baseBranch string = func() string {

0 commit comments

Comments
 (0)