1
0
Fork 0

refactor: add versioning, tag and release notes config

This commit is contained in:
Beatriz Vieira 2021-02-14 02:32:23 -03:00
parent f6debee45e
commit 9b63aacd8d
7 changed files with 54 additions and 18 deletions

View file

@ -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()

View file

@ -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
}

View file

@ -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)

View file

@ -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}

View file

@ -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)
}

View file

@ -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),
}
}

View file

@ -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)
}