Skip to content

Commit

Permalink
chore: bump golangci-lint and fix linting issues
Browse files Browse the repository at this point in the history
Nothing big, mostly `wsl`.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and talos-bot committed Apr 24, 2020
1 parent 6e0c294 commit cea1ee9
Show file tree
Hide file tree
Showing 21 changed files with 121 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Package cmd provides CLI commands.
package cmd

import (
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func PrintLongVersion() {
}

var wr bytes.Buffer

tmpl, err := template.New("version").Parse(versionTemplate)
if err != nil {
fmt.Println(err)
Expand All @@ -88,5 +89,5 @@ func PrintLongVersion() {

// PrintShortVersion prints the tag and sha.
func PrintShortVersion() {
fmt.Println(fmt.Sprintf("%s %s-%s", constants.AppName, Tag, SHA))
fmt.Printf("%s %s-%s\n", constants.AppName, Tag, SHA)
}
3 changes: 3 additions & 0 deletions hack/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ linters:
- typecheck
- gochecknoglobals
- gochecknoinits
- funlen
- gomnd
- gocognit
disable-all: false
fast: false

Expand Down
2 changes: 1 addition & 1 deletion hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CGO_ENABLED=1
lint_packages() {
if [ "${lint}" = true ]; then
echo "linting packages"
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.16.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.24.0
golangci-lint run --config "${BASH_SOURCE%/*}/golangci-lint.yaml"
fi
}
Expand Down
1 change: 1 addition & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Package constants defines common values.
package constants

const (
Expand Down
8 changes: 8 additions & 0 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Package enforcer defines policy enforcement.
package enforcer

import (
Expand Down Expand Up @@ -51,6 +52,7 @@ func New(r string) (*Conform, error) {
if err != nil {
return nil, err
}

c.reporter = s
default:
c.reporter = &reporter.Noop{}
Expand All @@ -60,6 +62,7 @@ func New(r string) (*Conform, error) {
if err != nil {
return nil, err
}

err = yaml.Unmarshal(configBytes, c)
if err != nil {
return nil, err
Expand All @@ -77,22 +80,27 @@ func (c *Conform) Enforce(setters ...policy.Option) error {
fmt.Fprintln(w, "POLICY\tCHECK\tSTATUS\tMESSAGE\t")

pass := true

for _, p := range c.Policies {
report, err := c.enforce(p, opts)
if err != nil {
log.Fatal(err)
}

for _, check := range report.Checks() {
if len(check.Errors()) != 0 {
for _, err := range check.Errors() {
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t\n", p.Type, check.Name(), "FAILED", err)
}

if err := c.reporter.SetStatus("failure", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: report failed: %+v", err)
}

pass = false
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", p.Type, check.Name(), "PASS", "<none>")

if err := c.reporter.SetStatus("success", p.Type, check.Name(), check.Message()); err != nil {
log.Printf("WARNING: report failed: %+v", err)
}
Expand Down
14 changes: 14 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Package git provides helpers for SCM.
package git

import (
Expand Down Expand Up @@ -36,10 +37,12 @@ func NewGit() (g *Git, err error) {
if err != nil {
return
}

repo, err := git.PlainOpen(path.Dir(p))
if err != nil {
return
}

g = &Git{repo: repo}

return g, err
Expand All @@ -52,18 +55,23 @@ func (g *Git) Message() (message string, err error) {
if err != nil {
return
}

commit, err := g.repo.CommitObject(ref.Hash())
if err != nil {
return
}

if commit.NumParents() > 1 {
parents := commit.Parents()

for i := 1; i <= commit.NumParents(); i++ {
var next *object.Commit

next, err = parents.Next()
if err != nil {
return
}

if i == commit.NumParents() {
message = next.Message
}
Expand All @@ -82,6 +90,7 @@ func (g *Git) HasGPGSignature() (ok bool, err error) {
if err != nil {
return false, err
}

commit, err := g.repo.CommitObject(ref.Hash())
if err != nil {
return false, err
Expand All @@ -100,6 +109,7 @@ func (g *Git) FetchPullRequest(remote string, number int) (err error) {
config.RefSpec(fmt.Sprintf("refs/pull/%d/head:pr/%d", number, number)),
},
}

if err = g.repo.Fetch(opts); err != nil {
return err
}
Expand Down Expand Up @@ -131,6 +141,7 @@ func (g *Git) SHA() (sha string, err error) {
if err != nil {
return sha, err
}

sha = ref.Hash().String()

return sha, nil
Expand All @@ -155,7 +166,9 @@ func (g *Git) AheadBehind(ref string) (ahead int, behind int, err error) {
}

var count int

iter := object.NewCommitPreorderIter(commit2, nil, nil)

err = iter.ForEach(func(comm *object.Commit) error {
if comm.Hash != ref1.Hash() {
count++
Expand All @@ -164,6 +177,7 @@ func (g *Git) AheadBehind(ref string) (ahead int, behind int, err error) {

return storer.ErrStop
})

if err != nil {
return 0, 0, nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/policy/commit/check_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (h Body) Message() string {
if len(h.errors) != 0 {
return h.errors[0].Error()
}

return "Commit body is valid"
}

Expand All @@ -45,10 +46,12 @@ func (c Commit) ValidateBody() policy.Check {

lines := strings.Split(strings.TrimPrefix(c.msg, "\n"), "\n")
valid := false

for _, line := range lines[1:] {
if DCORegex.MatchString(strings.TrimSpace(line)) {
continue
}

if line != "" {
valid = true
break
Expand Down
8 changes: 8 additions & 0 deletions internal/policy/commit/check_conventional_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (c ConventionalCommitCheck) Message() string {
if len(c.errors) != 0 {
return c.errors[0].Error()
}

return "Commit message is a valid conventional commit"
}

Expand All @@ -63,18 +64,21 @@ func (c ConventionalCommitCheck) Errors() []error {
func (c Commit) ValidateConventionalCommit() policy.Check {
check := &ConventionalCommitCheck{}
groups := parseHeader(c.msg)

if len(groups) != 6 {
check.errors = append(check.errors, errors.Errorf("Invalid conventional commits format: %q", c.msg))
return check
}

c.Conventional.Types = append(c.Conventional.Types, TypeFeat, TypeFix)
typeIsValid := false

for _, t := range c.Conventional.Types {
if t == groups[1] {
typeIsValid = true
}
}

if !typeIsValid {
check.errors = append(check.errors, errors.Errorf("Invalid type %q: allowed types are %v", groups[1], c.Conventional.Types))
return check
Expand All @@ -83,13 +87,15 @@ func (c Commit) ValidateConventionalCommit() policy.Check {
// Scope is optional.
if groups[3] != "" {
scopeIsValid := false

for _, scope := range c.Conventional.Scopes {
re := regexp.MustCompile(scope)
if re.Match([]byte(groups[3])) {
scopeIsValid = true
break
}
}

if !scopeIsValid {
check.errors = append(check.errors, errors.Errorf("Invalid scope %q: allowed scopes are %v", groups[3], c.Conventional.Scopes))
return check
Expand All @@ -100,9 +106,11 @@ func (c Commit) ValidateConventionalCommit() policy.Check {
if c.Conventional.DescriptionLength == 0 {
c.Conventional.DescriptionLength = 72
}

if len(groups[4]) <= c.Conventional.DescriptionLength && len(groups[4]) != 0 {
return check
}

check.errors = append(check.errors, errors.Errorf("Invalid description: %s", groups[4]))

return check
Expand Down
2 changes: 2 additions & 0 deletions internal/policy/commit/check_dco.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (d DCOCheck) Message() string {
if len(d.errors) != 0 {
return d.errors[0].Error()
}

return "Developer Certificate of Origin was found"
}

Expand All @@ -42,6 +43,7 @@ func (d DCOCheck) Errors() []error {
// ValidateDCO checks the commit message for a Developer Certificate of Origin.
func (c Commit) ValidateDCO() policy.Check {
check := &DCOCheck{}

for _, line := range strings.Split(c.msg, "\n") {
if DCORegex.MatchString(strings.TrimSpace(line)) {
return check
Expand Down
1 change: 1 addition & 0 deletions internal/policy/commit/check_gpg_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (g GPGCheck) Message() string {
if len(g.errors) != 0 {
return g.errors[0].Error()
}

return "GPG signature found"
}

Expand Down
4 changes: 4 additions & 0 deletions internal/policy/commit/check_header_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (h HeaderCaseCheck) Message() string {
if len(h.errors) != 0 {
return h.errors[0].Error()
}

return "Header case is valid"
}

Expand All @@ -53,6 +54,7 @@ func (c Commit) ValidateHeaderCase() policy.Check {
}

var valid bool

switch c.Header.Case {
case "upper":
valid = unicode.IsUpper(first)
Expand All @@ -62,8 +64,10 @@ func (c Commit) ValidateHeaderCase() policy.Check {
check.errors = append(check.errors, errors.Errorf("Invalid configured case %s", c.Header.Case))
return check
}

if !valid {
check.errors = append(check.errors, errors.Errorf("Commit header case is not %s", c.Header.Case))
}

return check
}
1 change: 1 addition & 0 deletions internal/policy/commit/check_header_last_character.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (h HeaderLastCharacterCheck) Message() string {
if len(h.errors) != 0 {
return h.errors[0].Error()
}

return "Header last character is valid"
}

Expand Down
7 changes: 7 additions & 0 deletions internal/policy/commit/check_imperative_verb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (i ImperativeCheck) Message() string {
if len(i.errors) != 0 {
return i.errors[0].Error()
}

return "Commit begins with imperative verb"
}

Expand All @@ -39,25 +40,31 @@ func (i ImperativeCheck) Errors() []error {
// ValidateImperative checks the commit message for a GPG signature.
func (c Commit) ValidateImperative() policy.Check {
check := &ImperativeCheck{}

var (
word string
err error
)

if word, err = c.firstWord(); err != nil {
check.errors = append(check.errors, err)
return check
}

doc, err := prose.NewDocument("I " + strings.ToLower(word))
if err != nil {
check.errors = append(check.errors, errors.Errorf("Failed to create document: %v", err))
return check
}

if len(doc.Tokens()) != 2 {
check.errors = append(check.errors, errors.Errorf("Expected 2 tokens, got %d", len(doc.Tokens())))
return check
}

tokens := doc.Tokens()
tok := tokens[1]

for _, tag := range []string{"VBD", "VBG", "VBZ"} {
if tok.Tag == tag {
check.errors = append(check.errors, errors.Errorf("First word of commit must be an imperative verb: %q is invalid", word))
Expand Down
2 changes: 2 additions & 0 deletions internal/policy/commit/check_number_of_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (h NumberOfCommits) Message() string {
if len(h.errors) != 0 {
return h.errors[0].Error()
}

return fmt.Sprintf("HEAD is %d commit(s) ahead of %s", h.ahead, h.ref)
}

Expand All @@ -46,6 +47,7 @@ func (c Commit) ValidateNumberOfCommits(g *git.Git, ref string) policy.Check {

var err error
check.ahead, _, err = g.AheadBehind(ref)

if err != nil {
check.errors = append(check.errors, err)
return check
Expand Down
Loading

0 comments on commit cea1ee9

Please sign in to comment.