File tree Expand file tree Collapse file tree 4 files changed +110
-3
lines changed Expand file tree Collapse file tree 4 files changed +110
-3
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
8
8
## [ Unreleased]
9
9
10
+ * [ PR-11] ( https://github.com/rimi-itk/gh-itkdev/pull/11 )
11
+ Added format detector
10
12
* [ PR-9] ( https://github.com/rimi-itk/gh-itkdev/pull/9 )
11
13
Added alternative base branches
12
14
* [ PR-7] ( https://github.com/rimi-itk/gh-itkdev/pull/7 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,18 +2,22 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "os/exec"
6
+
5
7
"github.com/rimi-itk/gh-itkdev/changelog"
6
8
"github.com/spf13/cobra"
7
- "os/exec"
8
9
)
9
10
10
11
// changelogCmd represents the changelog command
11
12
var (
12
13
create bool
13
14
14
15
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
+ }()
17
21
18
22
release string
19
23
baseBranch string = func () string {
You can’t perform that action at this time.
0 commit comments