diff --git a/.golangci.yml b/.golangci.yml index 14b06be..fc8ba72 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -68,6 +68,9 @@ issues: - source: "`json:" linters: - lll + - source: "`flag:" + linters: + - lll run: timeout: 2m diff --git a/main.go b/main.go index 958ba21..846764c 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( convcom "github.com/wfscheper/convcom" ) +//nolint: lll const changelogTemplate = ` ## {{ .Version }} ({{ simpleDate .LatestTagCommit.Committer.When }}) {{ range $type, $commits := .CommitsGrouped }} @@ -97,9 +98,13 @@ func autodetectBump(c *config) error { // find the latest tag var latestTagCommit *object.Commit tagRefs, err := repo.Tags() + if err != nil { + return err + } + err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error { rev := plumbing.Revision(tagRef.Name().String()) - tagCommitHash, err := repo.ResolveRevision(rev) + tagCommitHash, err := repo.ResolveRevision(rev) //nolint:govet if err != nil { return err } @@ -113,6 +118,7 @@ func autodetectBump(c *config) error { if commit.Committer.When.After(latestTagCommit.Committer.When) { latestTagCommit = commit } + return nil }) if err != nil && err != errDone { @@ -122,12 +128,17 @@ func autodetectBump(c *config) error { // find commits since the latest tag commitsSinceTag := []*object.Commit{} commitIter, err := repo.Log(&git.LogOptions{}) + if err != nil { + return err + } + err = commitIter.ForEach(func(commit *object.Commit) error { // once we reach the commit of the latest tag, we're done if commit.Hash == latestTagCommit.Hash { return errDone } commitsSinceTag = append(commitsSinceTag, commit) + return nil }) if err != nil && err != errDone { @@ -144,8 +155,7 @@ func autodetectBump(c *config) error { c.BumpMinor = false c.BumpPatch = false for _, commit := range commitsSinceTag { - switch { - case strings.Contains(commit.Message, "BREAKING"): + if strings.Contains(commit.Message, "BREAKING") { c.BumpMajor = true } } @@ -175,9 +185,13 @@ func gitTagUpdate(c *config) error { var latestTagCommit *object.Commit var latestTagName string tagRefs, err := repo.Tags() + if err != nil { + return err + } + err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error { rev := plumbing.Revision(tagRef.Name().String()) - tagCommitHash, err := repo.ResolveRevision(rev) + tagCommitHash, err := repo.ResolveRevision(rev) //nolint: govet if err != nil { return err } @@ -193,6 +207,7 @@ func gitTagUpdate(c *config) error { latestTagCommit = commit latestTagName = tagRef.Name().Short() } + return nil }) if err != nil && err != errDone { @@ -211,13 +226,17 @@ func gitTagUpdate(c *config) error { } // create new tag - repo.CreateTag(newVersion, head.Hash(), &git.CreateTagOptions{ + _, err = repo.CreateTag(newVersion, head.Hash(), &git.CreateTagOptions{ Message: "chore(version): bump version to " + newVersion, }) + if err != nil { + return err + } + return nil } -func changelogUpdate(c *config) error { +func changelogUpdate(c *config) error { //nolint: funlen,gocyclo // check if we are updating changelog if !c.ChangelogUpdate { return nil @@ -239,9 +258,13 @@ func changelogUpdate(c *config) error { var latestTagCommit *object.Commit var latestTagName string tagRefs, err := repo.Tags() + if err != nil { + return err + } + err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error { rev := plumbing.Revision(tagRef.Name().String()) - tagCommitHash, err := repo.ResolveRevision(rev) + tagCommitHash, err := repo.ResolveRevision(rev) //nolint: govet if err != nil { return err } @@ -257,6 +280,7 @@ func changelogUpdate(c *config) error { latestTagCommit = commit latestTagName = tagRef.Name().Short() } + return nil }) if err != nil && err != errDone { @@ -283,12 +307,16 @@ func changelogUpdate(c *config) error { commitsSinceTag := []*changelogCommit{} commitsSinceTagGrouped := map[string][]*changelogCommit{} commitIter, err := repo.Log(&git.LogOptions{}) + if err != nil { + return err + } + err = commitIter.ForEach(func(commit *object.Commit) error { // once we reach the commit of the latest tag, we're done if commit.Hash == latestTagCommit.Hash { return errDone } - convCommit, err := convComParser.Parse(commit.Message) + convCommit, err := convComParser.Parse(commit.Message) //nolint: govet if err != nil { return err } @@ -311,6 +339,7 @@ func changelogUpdate(c *config) error { commitsSinceTagGrouped[commitType], changelogEntry, ) + return nil }) if err != nil && err != errDone { @@ -366,12 +395,13 @@ func changelogUpdate(c *config) error { // render template var newBody bytes.Buffer - if err := tmpl.Execute(&newBody, &changelogEntry{ + err = tmpl.Execute(&newBody, &changelogEntry{ Version: newVersion, LatestTagCommit: latestTagCommit, Commits: commitsSinceTag, CommitsGrouped: commitsSinceTagGrouped, - }); err != nil { + }) + if err != nil { return err } @@ -385,19 +415,20 @@ func changelogUpdate(c *config) error { mergedBody := append(newBody.Bytes(), changelogBody...) // and update file - return ioutil.WriteFile(c.ChangelogPath, mergedBody, 0o644) + return ioutil.WriteFile(c.ChangelogPath, mergedBody, 0o644) //nolint: gosec } func fileUpdate(c *config) error { if !c.FileUpdate { return nil } - currentVersion := "0.0.0" + versionFileBody, err := ioutil.ReadFile(c.FilePath) if err != nil { return err } - currentVersion = string(versionFileBody) + + currentVersion := string(versionFileBody) if currentVersion == "" { currentVersion = c.VersionPrefix + "0.0.0" } @@ -405,6 +436,8 @@ func fileUpdate(c *config) error { if err != nil { return err } + + //nolint: gosec return ioutil.WriteFile(c.FilePath, []byte(newVersion), 0o644) } @@ -412,6 +445,7 @@ func bumpAtLeastMinor(c *config) error { if !c.BumpMajor && !c.BumpMinor && !c.BumpPatch { c.BumpPatch = true } + return nil } @@ -431,6 +465,7 @@ func bumpVersion(currentVersion string, c *config) (string, error) { if c.BumpPatch { cleanNewVersion = cleanNewVersion.IncPatch() } + return c.VersionPrefix + cleanNewVersion.String(), nil }