From 9b63aacd8dfd2e14112bc687618ad54adcb7a6fb Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sun, 14 Feb 2021 02:32:23 -0300 Subject: [PATCH] refactor: add versioning, tag and release notes config --- cmd/git-sv/main.go | 14 +++++++++++--- sv/config.go | 28 ++++++++++++++++++++++++++++ sv/git.go | 8 ++++---- sv/releasenotes.go | 8 ++++---- sv/releasenotes_test.go | 2 +- sv/semver.go | 10 +++++----- sv/semver_test.go | 2 +- 7 files changed, 54 insertions(+), 18 deletions(-) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 5819bfa..6f05d80 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -32,12 +32,20 @@ func main() { PrefixRegex: cfg.BranchIssuePrefixRegex, SuffixRegex: cfg.BranchIssueSuffixRegex, } + versioningConfig := sv.VersioningConfig{ + UpdateMajor: cfg.MajorVersionTypes, + UpdateMinor: cfg.MinorVersionTypes, + UpdatePatch: cfg.PatchVersionTypes, + UnknownTypeAsPatch: cfg.IncludeUnknownTypeAsPatch, + } + tagConfig := sv.TagConfig{Pattern: cfg.TagPattern} + releaseNotesConfig := sv.ReleaseNotesConfig{Headers: cfg.ReleaseNotesTags} //// messageProcessor := sv.NewMessageProcessor(commitMessageCfg, branchesConfig) - git := sv.NewGit(messageProcessor, cfg.TagPattern) - semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes) - releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags) + git := sv.NewGit(messageProcessor, tagConfig) + semverProcessor := sv.NewSemVerCommitsProcessor(versioningConfig) + releasenotesProcessor := sv.NewReleaseNoteProcessor(releaseNotesConfig) outputFormatter := sv.NewOutputFormatter() app := cli.NewApp() diff --git a/sv/config.go b/sv/config.go index 9f3fb03..74e8c79 100644 --- a/sv/config.go +++ b/sv/config.go @@ -1,5 +1,7 @@ package sv +// ==== Message ==== + // CommitMessageConfig config a commit message. type CommitMessageConfig struct { Types []string @@ -42,6 +44,8 @@ type CommitMessageIssueConfig struct { Regex string } +// ==== Branches ==== + // BranchesConfig branches preferences. type BranchesConfig struct { PrefixRegex string @@ -49,3 +53,27 @@ type BranchesConfig struct { ExpectIssue bool Skip []string } + +// ==== Versioning ==== + +// VersioningConfig versioning preferences. +type VersioningConfig struct { + UpdateMajor []string + UpdateMinor []string + UpdatePatch []string + UnknownTypeAsPatch bool +} + +// ==== Tag ==== + +// TagConfig tag preferences. +type TagConfig struct { + Pattern string +} + +// ==== Release Notes ==== + +// ReleaseNotesConfig release notes preferences. +type ReleaseNotesConfig struct { + Headers map[string]string +} diff --git a/sv/git.go b/sv/git.go index 3a75c94..0a130c7 100644 --- a/sv/git.go +++ b/sv/git.go @@ -65,14 +65,14 @@ func NewLogRange(t LogRangeType, start, end string) LogRange { // GitImpl git command implementation type GitImpl struct { messageProcessor MessageProcessor - tagPattern string + tagCfg TagConfig } // NewGit constructor -func NewGit(messageProcessor MessageProcessor, tagPattern string) *GitImpl { +func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl { return &GitImpl{ messageProcessor: messageProcessor, - tagPattern: tagPattern, + tagCfg: cfg, } } @@ -122,7 +122,7 @@ func (g GitImpl) Commit(header, body, footer string) error { // Tag create a git tag func (g GitImpl) Tag(version semver.Version) error { - tag := fmt.Sprintf(g.tagPattern, version.Major(), version.Minor(), version.Patch()) + tag := fmt.Sprintf(g.tagCfg.Pattern, version.Major(), version.Minor(), version.Patch()) tagMsg := fmt.Sprintf("Version %d.%d.%d", version.Major(), version.Minor(), version.Patch()) tagCommand := exec.Command("git", "tag", "-a", tag, "-m", tagMsg) diff --git a/sv/releasenotes.go b/sv/releasenotes.go index 1ea13d9..86b196d 100644 --- a/sv/releasenotes.go +++ b/sv/releasenotes.go @@ -13,12 +13,12 @@ type ReleaseNoteProcessor interface { // ReleaseNoteProcessorImpl release note based on commit log. type ReleaseNoteProcessorImpl struct { - tags map[string]string + cfg ReleaseNotesConfig } // NewReleaseNoteProcessor ReleaseNoteProcessor constructor. -func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl { - return &ReleaseNoteProcessorImpl{tags: tags} +func NewReleaseNoteProcessor(cfg ReleaseNotesConfig) *ReleaseNoteProcessorImpl { + return &ReleaseNoteProcessorImpl{cfg: cfg} } // Create create a release note based on commits. @@ -26,7 +26,7 @@ func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, date time.Time sections := make(map[string]ReleaseNoteSection) var breakingChanges []string for _, commit := range commits { - if name, exists := p.tags[commit.Message.Type]; exists { + if name, exists := p.cfg.Headers[commit.Message.Type]; exists { section, sexists := sections[commit.Message.Type] if !sexists { section = ReleaseNoteSection{Name: name} diff --git a/sv/releasenotes_test.go b/sv/releasenotes_test.go index 6aba9a8..aa09df0 100644 --- a/sv/releasenotes_test.go +++ b/sv/releasenotes_test.go @@ -42,7 +42,7 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p := NewReleaseNoteProcessor(map[string]string{"t1": "Tag 1", "t2": "Tag 2"}) + p := NewReleaseNoteProcessor(ReleaseNotesConfig{Headers: map[string]string{"t1": "Tag 1", "t2": "Tag 2"}}) if got := p.Create(tt.version, tt.date, tt.commits); !reflect.DeepEqual(got, tt.want) { t.Errorf("ReleaseNoteProcessorImpl.Create() = %v, want %v", got, tt.want) } diff --git a/sv/semver.go b/sv/semver.go index 132b5b3..08bf072 100644 --- a/sv/semver.go +++ b/sv/semver.go @@ -38,12 +38,12 @@ type SemVerCommitsProcessorImpl struct { } // NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor -func NewSemVerCommitsProcessor(unknownAsPatch bool, majorTypes, minorTypes, patchTypes []string) *SemVerCommitsProcessorImpl { +func NewSemVerCommitsProcessor(cfg VersioningConfig) *SemVerCommitsProcessorImpl { return &SemVerCommitsProcessorImpl{ - IncludeUnknownTypeAsPatch: unknownAsPatch, - MajorVersionTypes: toMap(majorTypes), - MinorVersionTypes: toMap(minorTypes), - PatchVersionTypes: toMap(patchTypes), + IncludeUnknownTypeAsPatch: cfg.UnknownTypeAsPatch, + MajorVersionTypes: toMap(cfg.UpdateMajor), + MinorVersionTypes: toMap(cfg.UpdateMinor), + PatchVersionTypes: toMap(cfg.UpdatePatch), } } diff --git a/sv/semver_test.go b/sv/semver_test.go index f3ea960..5d3f1d6 100644 --- a/sv/semver_test.go +++ b/sv/semver_test.go @@ -25,7 +25,7 @@ func TestSemVerCommitsProcessorImpl_NextVersion(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p := NewSemVerCommitsProcessor(tt.unknownAsPatch, []string{"major"}, []string{"minor"}, []string{"patch"}) + p := NewSemVerCommitsProcessor(VersioningConfig{UpdateMajor: []string{"major"}, UpdateMinor: []string{"minor"}, UpdatePatch: []string{"patch"}, UnknownTypeAsPatch: tt.unknownAsPatch}) if got := p.NextVersion(tt.version, tt.commits); !reflect.DeepEqual(got, tt.want) { t.Errorf("SemVerCommitsProcessorImpl.NextVersion() = %v, want %v", got, tt.want) }