From b83c6e335d23810d25e2c87bb0fb8ad46247f3c8 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:03:58 -0300 Subject: [PATCH 01/14] chore: run lint autofix --- cmd/git-sv/config.go | 5 ++--- cmd/git-sv/handlers.go | 3 +-- cmd/git-sv/main.go | 3 +-- sv/formatter.go | 6 +++--- sv/formatter_test.go | 3 +++ sv/git.go | 30 +++++++++++++++--------------- sv/message.go | 6 +++--- sv/message_test.go | 8 ++++++-- sv/releasenotes.go | 2 +- sv/semver.go | 12 ++++++------ 10 files changed, 41 insertions(+), 37 deletions(-) diff --git a/cmd/git-sv/config.go b/cmd/git-sv/config.go index dfb00dc..9e34d6b 100644 --- a/cmd/git-sv/config.go +++ b/cmd/git-sv/config.go @@ -10,13 +10,12 @@ import ( "strings" "github.com/bvieira/sv4git/sv" - "github.com/imdario/mergo" "github.com/kelseyhightower/envconfig" "gopkg.in/yaml.v3" ) -// EnvConfig env vars for cli configuration +// EnvConfig env vars for cli configuration. type EnvConfig struct { Home string `envconfig:"SV4GIT_HOME" default:""` } @@ -30,7 +29,7 @@ func loadEnvConfig() EnvConfig { return c } -// Config cli yaml config +// Config cli yaml config. type Config struct { Version string `yaml:"version"` Versioning sv.VersioningConfig `yaml:"versioning"` diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index 64a8564..cd8c79b 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -10,9 +10,8 @@ import ( "strings" "time" - "github.com/bvieira/sv4git/sv" - "github.com/Masterminds/semver/v3" + "github.com/bvieira/sv4git/sv" "github.com/urfave/cli/v2" "gopkg.in/yaml.v3" ) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 12c5525..670ff92 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -6,11 +6,10 @@ import ( "path/filepath" "github.com/bvieira/sv4git/sv" - "github.com/urfave/cli/v2" ) -// Version for git-sv +// Version for git-sv. var Version = "" const ( diff --git a/sv/formatter.go b/sv/formatter.go index abb9316..6509e8b 100644 --- a/sv/formatter.go +++ b/sv/formatter.go @@ -78,7 +78,7 @@ func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string { return b.String() } -// FormatChangelog format a changelog +// FormatChangelog format a changelog. func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string { var templateVars []releaseNoteTemplateVariables for _, v := range releasenotes { @@ -91,12 +91,12 @@ func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string } func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables { - var date = "" + date := "" if !releasenote.Date.IsZero() { date = releasenote.Date.Format("2006-01-02") } - var version = "" + version := "" if releasenote.Version != nil { version = releasenote.Version.String() } diff --git a/sv/formatter_test.go b/sv/formatter_test.go index 263e6d7..d460605 100644 --- a/sv/formatter_test.go +++ b/sv/formatter_test.go @@ -9,10 +9,13 @@ import ( var dateChangelog = `## v1.0.0 (2020-05-01) ` + var emptyDateChangelog = `## v1.0.0 ` + var emptyVersionChangelog = `## 2020-05-01 ` + var fullChangeLog = `## v1.0.0 (2020-05-01) ### Features diff --git a/sv/git.go b/sv/git.go index dde3242..7d873c9 100644 --- a/sv/git.go +++ b/sv/git.go @@ -18,7 +18,7 @@ const ( endLine = "~~" ) -// Git commands +// Git commands. type Git interface { LastTag() string Log(lr LogRange) ([]GitCommitLog, error) @@ -29,48 +29,48 @@ type Git interface { IsDetached() (bool, error) } -// GitCommitLog description of a single commit log +// GitCommitLog description of a single commit log. type GitCommitLog struct { Date string `json:"date,omitempty"` Hash string `json:"hash,omitempty"` Message CommitMessage `json:"message,omitempty"` } -// GitTag git tag info +// GitTag git tag info. type GitTag struct { Name string Date time.Time } -// LogRangeType type of log range +// LogRangeType type of log range. type LogRangeType string -// constants for log range type +// constants for log range type. const ( TagRange LogRangeType = "tag" DateRange = "date" HashRange = "hash" ) -// LogRange git log range +// LogRange git log range. type LogRange struct { rangeType LogRangeType start string end string } -// NewLogRange LogRange constructor +// NewLogRange LogRange constructor. func NewLogRange(t LogRangeType, start, end string) LogRange { return LogRange{rangeType: t, start: start, end: end} } -// GitImpl git command implementation +// GitImpl git command implementation. type GitImpl struct { messageProcessor MessageProcessor tagCfg TagConfig } -// NewGit constructor +// NewGit constructor. func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl { return &GitImpl{ messageProcessor: messageProcessor, @@ -78,7 +78,7 @@ func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl { } } -// LastTag get last tag, if no tag found, return empty +// LastTag get last tag, if no tag found, return empty. func (GitImpl) LastTag() string { cmd := exec.Command("git", "for-each-ref", "refs/tags", "--sort", "-creatordate", "--format", "%(refname:short)", "--count", "1") out, err := cmd.CombinedOutput() @@ -88,7 +88,7 @@ func (GitImpl) LastTag() string { return strings.TrimSpace(strings.Trim(string(out), "\n")) } -// Log return git log +// Log return git log. func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) { format := "--pretty=format:\"%ad" + logSeparator + "%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\"" params := []string{"log", "--date=short", format} @@ -114,7 +114,7 @@ func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) { return parseLogOutput(g.messageProcessor, string(out)), nil } -// Commit runs git commit +// Commit runs git commit. func (g GitImpl) Commit(header, body, footer string) error { cmd := exec.Command("git", "commit", "-m", header, "-m", "", "-m", body, "-m", "", "-m", footer) cmd.Stdout = os.Stdout @@ -122,7 +122,7 @@ func (g GitImpl) Commit(header, body, footer string) error { return cmd.Run() } -// Tag create a git tag +// Tag create a git tag. func (g GitImpl) Tag(version semver.Version) error { 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()) @@ -136,7 +136,7 @@ func (g GitImpl) Tag(version semver.Version) error { return pushCommand.Run() } -// Tags list repository tags +// Tags list repository tags. func (g GitImpl) Tags() ([]GitTag, error) { cmd := exec.Command("git", "for-each-ref", "--sort", "creatordate", "--format", "%(creatordate:iso8601)#%(refname:short)", "refs/tags") out, err := cmd.CombinedOutput() @@ -146,7 +146,7 @@ func (g GitImpl) Tags() ([]GitTag, error) { return parseTagsOutput(string(out)) } -// Branch get git branch +// Branch get git branch. func (GitImpl) Branch() string { cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD") out, err := cmd.CombinedOutput() diff --git a/sv/message.go b/sv/message.go index cc5e354..fd08ef1 100644 --- a/sv/message.go +++ b/sv/message.go @@ -23,7 +23,7 @@ type CommitMessage struct { Metadata map[string]string `json:"metadata,omitempty"` } -// NewCommitMessage commit message constructor +// NewCommitMessage commit message constructor. func NewCommitMessage(ctype, scope, description, body, issue, breakingChanges string) CommitMessage { metadata := make(map[string]string) if issue != "" { @@ -58,7 +58,7 @@ type MessageProcessor interface { Parse(subject, body string) CommitMessage } -// NewMessageProcessor MessageProcessorImpl constructor +// NewMessageProcessor MessageProcessorImpl constructor. func NewMessageProcessor(mcfg CommitMessageConfig, bcfg BranchesConfig) *MessageProcessorImpl { return &MessageProcessorImpl{ messageCfg: mcfg, @@ -125,7 +125,7 @@ func (p MessageProcessorImpl) ValidateDescription(description string) error { // Enhance add metadata on commit message. func (p MessageProcessorImpl) Enhance(branch string, message string) (string, error) { if p.branchesCfg.DisableIssue || p.messageCfg.IssueFooterConfig().Key == "" || hasIssueID(message, p.messageCfg.IssueFooterConfig()) { - return "", nil //enhance disabled + return "", nil // enhance disabled } issue, err := p.IssueID(branch) diff --git a/sv/message_test.go b/sv/message_test.go index 4d3c43f..1992d1b 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -62,7 +62,7 @@ func newBranchCfg(skipDetached bool) BranchesConfig { } } -// messages samples start +// messages samples start. var fullMessage = `fix: correct minor typos in code see the issue for details @@ -71,6 +71,7 @@ on typos fixed. Reviewed-by: Z Refs #133` + var fullMessageWithJira = `fix: correct minor typos in code see the issue for details @@ -80,6 +81,7 @@ on typos fixed. Reviewed-by: Z Refs #133 jira: JIRA-456` + var fullMessageRefs = `fix: correct minor typos in code see the issue for details @@ -87,11 +89,13 @@ see the issue for details on typos fixed. Refs #133` + var subjectAndBodyMessage = `fix: correct minor typos in code see the issue for details on typos fixed.` + var subjectAndFooterMessage = `refactor!: drop support for Node 6 BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.` @@ -470,7 +474,7 @@ func Test_splitCommitMessageContent(t *testing.T) { } } -//commitType, scope, description, hasBreakingChange +//commitType, scope, description, hasBreakingChange. func Test_parseSubjectMessage(t *testing.T) { tests := []struct { name string diff --git a/sv/releasenotes.go b/sv/releasenotes.go index e02c291..7fafeb7 100644 --- a/sv/releasenotes.go +++ b/sv/releasenotes.go @@ -55,7 +55,7 @@ type ReleaseNote struct { BreakingChanges BreakingChangeSection } -// BreakingChangeSection breaking change section +// BreakingChangeSection breaking change section. type BreakingChangeSection struct { Name string Messages []string diff --git a/sv/semver.go b/sv/semver.go index 06d76d1..b648da6 100644 --- a/sv/semver.go +++ b/sv/semver.go @@ -11,7 +11,7 @@ const ( major ) -// ToVersion parse string to semver.Version +// ToVersion parse string to semver.Version. func ToVersion(value string) (semver.Version, error) { version := value if version == "" { @@ -24,12 +24,12 @@ func ToVersion(value string) (semver.Version, error) { return *v, nil } -// SemVerCommitsProcessor interface +// SemVerCommitsProcessor interface. type SemVerCommitsProcessor interface { NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool) } -// SemVerCommitsProcessorImpl process versions using commit log +// SemVerCommitsProcessorImpl process versions using commit log. type SemVerCommitsProcessorImpl struct { MajorVersionTypes map[string]struct{} MinorVersionTypes map[string]struct{} @@ -38,7 +38,7 @@ type SemVerCommitsProcessorImpl struct { IncludeUnknownTypeAsPatch bool } -// NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor +// NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor. func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig) *SemVerCommitsProcessorImpl { return &SemVerCommitsProcessorImpl{ IncludeUnknownTypeAsPatch: !vcfg.IgnoreUnknown, @@ -49,9 +49,9 @@ func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig) } } -// NextVersion calculates next version based on commit log +// NextVersion calculates next version based on commit log. func (p SemVerCommitsProcessorImpl) NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool) { - var versionToUpdate = none + versionToUpdate := none for _, commit := range commits { if v := p.versionTypeToUpdate(commit); v > versionToUpdate { versionToUpdate = v From 162ce50ae599d6c3316917f5b54aad67ea421133 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:25:55 -0300 Subject: [PATCH 02/14] refactor: remove uneeded comment --- sv/message_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/sv/message_test.go b/sv/message_test.go index 1992d1b..84068d5 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -474,7 +474,6 @@ func Test_splitCommitMessageContent(t *testing.T) { } } -//commitType, scope, description, hasBreakingChange. func Test_parseSubjectMessage(t *testing.T) { tests := []struct { name string From a96ffa51ce0bafbbf1fc1b09faf0bbfe2464ef80 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:35:28 -0300 Subject: [PATCH 03/14] refactor: fix prealloc lint --- sv/formatter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sv/formatter.go b/sv/formatter.go index 6509e8b..2fe78f2 100644 --- a/sv/formatter.go +++ b/sv/formatter.go @@ -80,9 +80,9 @@ func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string { // FormatChangelog format a changelog. func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string { - var templateVars []releaseNoteTemplateVariables - for _, v := range releasenotes { - templateVars = append(templateVars, releaseNoteVariables(v)) + templateVars := make([]releaseNoteTemplateVariables, len(releasenotes)) + for i, v := range releasenotes { + templateVars[i] = releaseNoteVariables(v) } var b bytes.Buffer From dcba2569034c9144bc5944ad606e771cb1c91169 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:35:54 -0300 Subject: [PATCH 04/14] refactor: fix ifshort lint --- cmd/git-sv/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 670ff92..bb57b38 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -164,8 +164,7 @@ func main() { }, } - apperr := app.Run(os.Args) - if apperr != nil { + if apperr := app.Run(os.Args); apperr != nil { log.Fatal(apperr) } } From 77152fcd52c72578a032f6d48a6ded9e82dffe55 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:37:15 -0300 Subject: [PATCH 05/14] refactor: fix printf-like lint --- cmd/git-sv/handlers.go | 6 +++--- cmd/git-sv/log.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index cd8c79b..90290eb 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -454,12 +454,12 @@ func validateCommitMessageHandler(git sv.Git, messageProcessor sv.MessageProcess detached, derr := git.IsDetached() if messageProcessor.SkipBranch(branch, derr == nil && detached) { - warn("commit message validation skipped, branch in ignore list or detached...") + warnf("commit message validation skipped, branch in ignore list or detached...") return nil } if source := c.String("source"); source == "merge" { - warn("commit message validation skipped, ignoring source: %s...", source) + warnf("commit message validation skipped, ignoring source: %s...", source) return nil } @@ -476,7 +476,7 @@ func validateCommitMessageHandler(git sv.Git, messageProcessor sv.MessageProcess msg, err := messageProcessor.Enhance(branch, commitMessage) if err != nil { - warn("could not enhance commit message, %s", err.Error()) + warnf("could not enhance commit message, %s", err.Error()) return nil } if msg == "" { diff --git a/cmd/git-sv/log.go b/cmd/git-sv/log.go index 932f7fb..69b0feb 100644 --- a/cmd/git-sv/log.go +++ b/cmd/git-sv/log.go @@ -2,6 +2,6 @@ package main import "fmt" -func warn(format string, values ...interface{}) { +func warnf(format string, values ...interface{}) { fmt.Printf("WARN: "+format+"\n", values...) } From 6184ac3ad44d575fc417dc05436cfd090f616003 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:39:38 -0300 Subject: [PATCH 06/14] refactor: fix tagliatelle yaml lint --- cmd/git-sv/config.go | 4 ++-- sv/config.go | 4 ++-- sv/message.go | 2 +- sv/message_test.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/git-sv/config.go b/cmd/git-sv/config.go index 9e34d6b..b9d9b26 100644 --- a/cmd/git-sv/config.go +++ b/cmd/git-sv/config.go @@ -76,8 +76,8 @@ func defaultConfig() Config { Tag: sv.TagConfig{Pattern: "%d.%d.%d"}, ReleaseNotes: sv.ReleaseNotesConfig{Headers: map[string]string{"fix": "Bug Fixes", "feat": "Features", "breaking-change": "Breaking Changes"}}, Branches: sv.BranchesConfig{ - PrefixRegex: "([a-z]+\\/)?", - SuffixRegex: "(-.*)?", + Prefix: "([a-z]+\\/)?", + Suffix: "(-.*)?", DisableIssue: false, Skip: []string{"master", "main", "developer"}, SkipDetached: &skipDetached, diff --git a/sv/config.go b/sv/config.go index 7342436..eb894e1 100644 --- a/sv/config.go +++ b/sv/config.go @@ -40,8 +40,8 @@ type CommitMessageIssueConfig struct { // BranchesConfig branches preferences. type BranchesConfig struct { - PrefixRegex string `yaml:"prefix"` - SuffixRegex string `yaml:"suffix"` + Prefix string `yaml:"prefix"` + Suffix string `yaml:"suffix"` DisableIssue bool `yaml:"disable-issue"` Skip []string `yaml:"skip,flow"` SkipDetached *bool `yaml:"skip-detached"` diff --git a/sv/message.go b/sv/message.go index fd08ef1..050ecf9 100644 --- a/sv/message.go +++ b/sv/message.go @@ -160,7 +160,7 @@ func (p MessageProcessorImpl) IssueID(branch string) (string, error) { return "", nil } - rstr := fmt.Sprintf("^%s(%s)%s$", p.branchesCfg.PrefixRegex, p.messageCfg.Issue.Regex, p.branchesCfg.SuffixRegex) + rstr := fmt.Sprintf("^%s(%s)%s$", p.branchesCfg.Prefix, p.messageCfg.Issue.Regex, p.branchesCfg.Suffix) r, err := regexp.Compile(rstr) if err != nil { return "", fmt.Errorf("could not compile issue regex: %s, error: %v", rstr, err.Error()) diff --git a/sv/message_test.go b/sv/message_test.go index 84068d5..e1f95d9 100644 --- a/sv/message_test.go +++ b/sv/message_test.go @@ -55,8 +55,8 @@ var ccfgWithScope = CommitMessageConfig{ func newBranchCfg(skipDetached bool) BranchesConfig { return BranchesConfig{ - PrefixRegex: "([a-z]+\\/)?", - SuffixRegex: "(-.*)?", + Prefix: "([a-z]+\\/)?", + Suffix: "(-.*)?", Skip: []string{"develop", "master"}, SkipDetached: &skipDetached, } From 18b4d78bd801b783c41fcc1e89b4dcf5593248e4 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:40:18 -0300 Subject: [PATCH 07/14] chore: config golangci lint --- .golangci.yml | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2d0a9ae..965f5e3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,26 +1,45 @@ linters: - # disable-all: true - # enable: - # - megacheck - # - govet enable-all: true disable: - scopelint - paralleltest - - staticcheck - noctx + - godox - wsl - lll - forbidigo - # - prealloc - # presets: - # - bugs - # - unused - - # bugs|comment|complexity|error|format|import|metalinter|module|performance|sql|style|test|unused + - nlreturn + - gomnd fast: true run: skip-dirs: - build - artifacts + +linters-settings: + tagliatelle: + case: + use-field-name: true + rules: + json: camel + yaml: kebab + xml: camel + bson: camel + avro: snake + mapstructure: kebab + +issues: + exclude-rules: + - path: _test\.go + linters: + - gocyclo + - errcheck + - dupl + - gosec + - gochecknoglobals + - testpackage + - path: cmd/git-sv/main.go + linters: + - gochecknoglobals + - funlen \ No newline at end of file From 077fcbf717748679f6f6a06177bdefa675f2cccf Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:53:00 -0300 Subject: [PATCH 08/14] ci: add golangci to github actions --- .github/workflows/ci.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f832e4..1da25a8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,16 @@ on: jobs: + golangci: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: latest + build: name: Build runs-on: ubuntu-latest @@ -16,7 +26,7 @@ jobs: - name: Set up Go 1.x uses: actions/setup-go@v2 with: - go-version: ^1.13 + go-version: ^1.16 id: go - name: Check out code into the Go module directory From 11580133b0d78255ab7686cf93f021d05de416ba Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 16:58:28 -0300 Subject: [PATCH 09/14] style: add newline to .golangci.yml --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 965f5e3..cd8cd4f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -42,4 +42,4 @@ issues: - path: cmd/git-sv/main.go linters: - gochecknoglobals - - funlen \ No newline at end of file + - funlen From 7b3df4e24531987f4b4e3fb954be10c196dfa0fc Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 17:09:25 -0300 Subject: [PATCH 10/14] refactor: remove unused variables --- cmd/git-sv/handlers.go | 10 +++++----- cmd/git-sv/main.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index 90290eb..67808ad 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -72,7 +72,7 @@ func nextVersionHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) f } } -func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *cli.Context) error { +func commitLogHandler(git sv.Git) func(c *cli.Context) error { return func(c *cli.Context) error { var commits []sv.GitCommitLog var err error @@ -162,7 +162,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, var err error if tag := c.String("t"); tag != "" { - rnVersion, date, commits, err = getTagVersionInfo(git, semverProcessor, tag) + rnVersion, date, commits, err = getTagVersionInfo(git, tag) } else { // TODO: should generate release notes if version was not updated? rnVersion, _, date, commits, err = getNextVersionInfo(git, semverProcessor) @@ -178,7 +178,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, } } -func getTagVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, tag string) (semver.Version, time.Time, []sv.GitCommitLog, error) { +func getTagVersionInfo(git sv.Git, tag string) (semver.Version, time.Time, []sv.GitCommitLog, error) { tagVersion, err := sv.ToVersion(tag) if err != nil { return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from tag, message: %v", tag, err) @@ -280,7 +280,7 @@ func getCommitScope(cfg Config, p sv.MessageProcessor, input string, noScope boo return input, p.ValidateScope(input) } -func getCommitDescription(cfg Config, p sv.MessageProcessor, input string) (string, error) { +func getCommitDescription(p sv.MessageProcessor, input string) (string, error) { if input == "" { return promptSubject() } @@ -365,7 +365,7 @@ func commitHandler(cfg Config, git sv.Git, messageProcessor sv.MessageProcessor) return err } - subject, err := getCommitDescription(cfg, messageProcessor, inputDescription) + subject, err := getCommitDescription(messageProcessor, inputDescription) if err != nil { return err } diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index bb57b38..a78fded 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -91,7 +91,7 @@ func main() { Aliases: []string{"cl"}, Usage: "list all commit logs according to range as jsons", Description: "The range filter is used based on git log filters, check https://git-scm.com/docs/git-log for more info. When flag range is \"tag\" and start is empty, last tag created will be used instead. When flag range is \"date\", if \"end\" is YYYY-MM-DD the range will be inclusive.", - Action: commitLogHandler(git, semverProcessor), + Action: commitLogHandler(git), Flags: []cli.Flag{ &cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"}, &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Value: string(sv.TagRange)}, From fec91a19472f98ee08c34898f24a0bac56965f72 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 17:34:40 -0300 Subject: [PATCH 11/14] refactor: fix errcheck lint --- cmd/git-sv/handlers.go | 19 +++++++++++++++---- sv/formatter.go | 20 ++++++++++++-------- sv/formatter_test.go | 22 ++++++++++++++-------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index 67808ad..68b6443 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -148,8 +148,11 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF date, _ = time.Parse("2006-01-02", commits[0].Date) } - releasenote := rnProcessor.Create(nil, date, commits) - fmt.Println(outputFormatter.FormatReleaseNote(releasenote)) + output, err := outputFormatter.FormatReleaseNote(rnProcessor.Create(nil, date, commits)) + if err != nil { + return fmt.Errorf("could not format release notes, message: %v", err) + } + fmt.Println(output) return nil } } @@ -173,7 +176,11 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, } releasenote := rnProcessor.Create(&rnVersion, date, commits) - fmt.Println(outputFormatter.FormatReleaseNote(releasenote)) + output, err := outputFormatter.FormatReleaseNote(releasenote) + if err != nil { + return fmt.Errorf("could not format release notes, message: %v", err) + } + fmt.Println(output) return nil } } @@ -442,7 +449,11 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP releaseNotes = append(releaseNotes, rnProcessor.Create(¤tVer, tag.Date, commits)) } - fmt.Println(formatter.FormatChangelog(releaseNotes)) + output, err := formatter.FormatChangelog(releaseNotes) + if err != nil { + return fmt.Errorf("could not format changelog, message: %v", err) + } + fmt.Println(output) return nil } diff --git a/sv/formatter.go b/sv/formatter.go index 2fe78f2..fc5a534 100644 --- a/sv/formatter.go +++ b/sv/formatter.go @@ -51,8 +51,8 @@ const ( // OutputFormatter output formatter interface. type OutputFormatter interface { - FormatReleaseNote(releasenote ReleaseNote) string - FormatChangelog(releasenotes []ReleaseNote) string + FormatReleaseNote(releasenote ReleaseNote) (string, error) + FormatChangelog(releasenotes []ReleaseNote) (string, error) } // OutputFormatterImpl formater for release note and changelog. @@ -72,22 +72,26 @@ func NewOutputFormatter() *OutputFormatterImpl { } // FormatReleaseNote format a release note. -func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string { +func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) { var b bytes.Buffer - p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)) - return b.String() + if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil { + return "", err + } + return b.String(), nil } // FormatChangelog format a changelog. -func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string { +func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string, error) { templateVars := make([]releaseNoteTemplateVariables, len(releasenotes)) for i, v := range releasenotes { templateVars[i] = releaseNoteVariables(v) } var b bytes.Buffer - p.changelogTemplate.Execute(&b, templateVars) - return b.String() + if err := p.changelogTemplate.Execute(&b, templateVars); err != nil { + return "", err + } + return b.String(), nil } func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables { diff --git a/sv/formatter_test.go b/sv/formatter_test.go index d460605..faa7968 100644 --- a/sv/formatter_test.go +++ b/sv/formatter_test.go @@ -39,20 +39,26 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) { date, _ := time.Parse("2006-01-02", "2020-05-01") tests := []struct { - name string - input ReleaseNote - want string + name string + input ReleaseNote + want string + wantErr bool }{ - {"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog}, - {"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog}, - {"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog}, - {"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog}, + {"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog, false}, + {"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, false}, + {"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog, false}, + {"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NewOutputFormatter().FormatReleaseNote(tt.input); got != tt.want { + got, err := NewOutputFormatter().FormatReleaseNote(tt.input) + if got != tt.want { t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want) } + + if (err != nil) != tt.wantErr { + t.Errorf("OutputFormatterImpl.FormatReleaseNote() error = %v, wantErr %v", err, tt.wantErr) + } }) } } From 5993f00757f3ac534ce767c2c48acbc75c8a28fe Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 17:35:15 -0300 Subject: [PATCH 12/14] ci: remove fast from golangci --- .golangci.yml | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index cd8cd4f..4db1c3e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,16 +1,6 @@ linters: - enable-all: true - disable: - - scopelint - - paralleltest - - noctx - - godox - - wsl - - lll - - forbidigo - - nlreturn - - gomnd - fast: true + enable: + - tagliatelle run: skip-dirs: From 6fd31b9aa039eaa622367b05a26a8fec94e26625 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 17:44:42 -0300 Subject: [PATCH 13/14] refactor: fix lint staticcheck SA9004 --- sv/git.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sv/git.go b/sv/git.go index 7d873c9..4290310 100644 --- a/sv/git.go +++ b/sv/git.go @@ -48,8 +48,8 @@ type LogRangeType string // constants for log range type. const ( TagRange LogRangeType = "tag" - DateRange = "date" - HashRange = "hash" + DateRange LogRangeType = "date" + HashRange LogRangeType = "hash" ) // LogRange git log range. From 178239a666b6d020212706d1e944e5c15ca37d17 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 17:52:25 -0300 Subject: [PATCH 14/14] refactor: fix lint gosimple S1007 --- sv/message.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sv/message.go b/sv/message.go index 050ecf9..6def635 100644 --- a/sv/message.go +++ b/sv/message.go @@ -82,7 +82,7 @@ func (p MessageProcessorImpl) Validate(message string) error { subject, body := splitCommitMessageContent(message) msg := p.Parse(subject, body) - if !regexp.MustCompile("^[a-z+]+(\\(.+\\))?!?: .+$").MatchString(subject) { + if !regexp.MustCompile(`^[a-z+]+(\(.+\))?!?: .+$`).MatchString(subject) { return fmt.Errorf("subject [%s] should be valid according with conventional commits", subject) } @@ -229,7 +229,7 @@ func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage { } func parseSubjectMessage(message string) (string, string, string, bool) { - regex := regexp.MustCompile("([a-z]+)(\\((.*)\\))?(!)?: (.*)") + regex := regexp.MustCompile(`([a-z]+)(\((.*)\))?(!)?: (.*)`) result := regex.FindStringSubmatch(message) if len(result) != 6 { return "", "", message, false