From c8abfdba4c0f3d2d90b40de3c778fb333bb23fa8 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Tue, 5 Nov 2024 21:48:21 +0100 Subject: [PATCH 1/2] Added format detector --- CHANGELOG.md | 2 ++ changelog/format_detector.go | 60 +++++++++++++++++++++++++++++++ changelog/format_detector_test.go | 44 +++++++++++++++++++++++ cmd/changelog.go | 10 ++++-- 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 changelog/format_detector.go create mode 100644 changelog/format_detector_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9339bef..d22803b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/changelog/format_detector.go b/changelog/format_detector.go new file mode 100644 index 0000000..1b30ee7 --- /dev/null +++ b/changelog/format_detector.go @@ -0,0 +1,60 @@ +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 + } + } + + if len(templates) < 1 { + return "", fmt.Errorf("cannot detect pull request entry format") + } + + 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. + { + regexp.MustCompile("."), + `* [PR-{{ .Number }}]({{ .Url }}) + {{ .Title }}`, + }, + } +} diff --git a/changelog/format_detector_test.go b/changelog/format_detector_test.go new file mode 100644 index 0000000..85533ff --- /dev/null +++ b/changelog/format_detector_test.go @@ -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) + } +} diff --git a/cmd/changelog.go b/cmd/changelog.go index a9a28df..8e8b14f 100644 --- a/cmd/changelog.go +++ b/cmd/changelog.go @@ -2,9 +2,10 @@ package cmd import ( "fmt" + "os/exec" + "github.com/rimi-itk/gh-itkdev/changelog" "github.com/spf13/cobra" - "os/exec" ) // changelogCmd represents the changelog command @@ -12,8 +13,11 @@ 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 { From 2028694673adb102c7ffac1056fbd83f53447484 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 7 Nov 2024 09:05:49 +0100 Subject: [PATCH 2/2] Cleaned up --- changelog/format_detector.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/changelog/format_detector.go b/changelog/format_detector.go index 1b30ee7..bc50acd 100644 --- a/changelog/format_detector.go +++ b/changelog/format_detector.go @@ -23,10 +23,6 @@ func DetectPullRequestEntryFormat(changelog string) (string, error) { } } - if len(templates) < 1 { - return "", fmt.Errorf("cannot detect pull request entry format") - } - return templates[len(templates)-1].template, nil } @@ -52,6 +48,7 @@ func getPullRequestEntryTemplates() []struct { // The default – must come last. { + // Match any non-empty changelog and provide a default template. regexp.MustCompile("."), `* [PR-{{ .Number }}]({{ .Url }}) {{ .Title }}`,