Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
merge: branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sascha-andres committed Nov 4, 2019
2 parents 42de064 + 58a3cac commit 9e7ac21
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 20 deletions.
8 changes: 5 additions & 3 deletions .commit-hook.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
body-required: true
body-required: false

lint_gitlab_ci: false

subject:
- severity: warning
expression: ^[0-9]+
name: useless warning for a test
expression: "^[a-z]{1,6}:"
name: prefixed line

external-tools:
- severity: error
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go

on: [push, pull_request]

jobs:
test:
strategy:
matrix:
go-version: [1.12.9, 1.13]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v1
- name: Test
run: go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ fabric.properties
.build/
dist/
coverage.txt
coverage.tmp
7 changes: 0 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
.PHONY: test

setup: ## Install all the build and lint dependencies
go get -u gopkg.in/alecthomas/gometalinter.v2
go get -u github.com/golang/dep/cmd/dep
go get -u golang.org/x/tools/cmd/goimports
dep ensure
gometalinter --install --update

test: ## Run all the tests
rm -f coverage.tmp && rm -f coverage.txt
echo 'mode: atomic' > coverage.txt && go list ./... | xargs -n1 -I{} sh -c 'go test -race -covermode=atomic -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> coverage.txt' && rm coverage.tmp
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Both files have the same structure.
enforce-body-line-length: true # Should too long body lines be treated as an error or as a warning
lint_gitlab_ci: true # If true this calls out to lint .gitlab-ci.yml files
gitlab_ci_file: relativePath # Set a different CI file location
confd-directories # Specify whether .commit-msg.d directory is used

ignore: # a list of regular expressions to ignore lines (no check)
- ^#.* # ignore comments
Expand Down Expand Up @@ -68,6 +69,7 @@ Essentially this just removes the hook, so it would remove any other hook also.

|Version|Description|
|---|---|
|1.1.0|Add support for conf.d directory|
|1.0.1|better gitlab ci handling|
|1.0.0|update to use go modules|
|0.9.0|add support for GitLab CI linter|
Expand Down
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ func validate() bool {
commitMessageFile := os.Args[1]
config, err := hook.NewForVersion(commitMessageFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return false
}

commitFileContent, err := loadCommitMessageFile(commitMessageFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return false
}

ok, err := config.Validate(commitFileContent)
if err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return false
}
Expand All @@ -98,12 +98,12 @@ func validate() bool {
func validateInput() int {
commitMessageFile := os.Args[1]
if commitMessageFile == "" {
fmt.Fprintln(os.Stderr, errors.New("no commit message file passed as parameter 1"))
_, _ = fmt.Fprintln(os.Stderr, errors.New("no commit message file passed as parameter 1"))
return 1
}

if !helper.FileExists(commitMessageFile) {
fmt.Fprintln(os.Stderr, errors.New("passed commit message file not found"))
_, _ = fmt.Fprintln(os.Stderr, errors.New("passed commit message file not found"))
return 1
}
return 0
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ go 1.12
require (
github.com/imdario/mergo v0.3.7
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/pty v1.1.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/pkg/errors v0.8.1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2
livingit.de/code/gitconfd v0.2.0
livingit.de/code/go-hookhelper v1.0.0
livingit.de/code/versioned v1.0.1
)
29 changes: 26 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
livingit.de/code/go-hookhelper v0.0.0-20180725060324-8271b504d319 h1:d3rzDWHLfy4IxOsgLLyJVcRR/5SLIJ6dO3tRfOSKtNg=
livingit.de/code/go-hookhelper v0.0.0-20180725060324-8271b504d319/go.mod h1:b0HDCpoQmdFIH7nYLCmI6ozW9ByeHU4onwV0nIYXLVo=
livingit.de/code/gitconfd v0.2.0 h1:c6YGogpVHr+adKViGB89mwvc1zTVgiTBwdXmMkkvzSs=
livingit.de/code/gitconfd v0.2.0/go.mod h1:pj3EfXnkWp1RJsPj8yuOW71XBAewl/6/MK6tUk8UNyc=
livingit.de/code/go-hookhelper v1.0.0 h1:TaD+88W4q+AqV0RqEj9BbCICR9cz21L5IG2K1Vi6eIs=
livingit.de/code/go-hookhelper v1.0.0/go.mod h1:L5lfocB/MU6W7GYmScDDk1weZpm+WoevZVvDOYq2fG4=
livingit.de/code/versioned v1.0.1 h1:24Ub5ASjRKZW5scJrtufmeIXYHqeOdbmyq/CAknBI68=
Expand Down
19 changes: 19 additions & 0 deletions hook/v2/execute_confd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package v2

import (
"os"
"path/filepath"

"livingit.de/code/gitconfd"
)

// ExecuteConfDDirectories run scrips in .commit-msg.d/ directory
func (cfg *Configuration) ExecuteConfDDirectories() bool {
if !cfg.ConfDDirectoriesEnabled {
return true
}

path := filepath.Dir(os.Args[1])

return gitconfd.Execute(path, os.Args[0])
}
2 changes: 1 addition & 1 deletion hook/v2/external_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ func printOutput(stdout, stderr bytes.Buffer) {

fmt.Println("error:")
fmt.Println()
fmt.Fprintln(os.Stderr, stderr.String())
_, _ = fmt.Fprintln(os.Stderr, stderr.String())
}
36 changes: 36 additions & 0 deletions hook/v2/external_tool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// +build !windows

package v2

import "testing"

func TestExternalTool(t *testing.T) {
tests := map[string]struct {
command string
severity string
result bool
}{
"success": {"/bin/true", ErrorSeverity, true},
"warning": {"/bin/false", "warning", false},
"failure": {"/bin/false", ErrorSeverity, false},
}

for k, v := range tests {
t.Run(k, func(t *testing.T) {
tools := make([]*Tool, 0)
commands := make([]string, 0)
commands = append(commands, v.command)
tools = append(tools, &Tool{
Name: v.command,
Severity: v.severity,
Command: commands,
})
cfg := &Configuration{
Externals: tools,
}
if result := cfg.runExternalTools(); result != v.result {
t.Logf("for %s expected %t, received %t", k, v.result, result)
}
})
}
}
27 changes: 27 additions & 0 deletions hook/v2/gitlab_ci_linter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v2

import (
"fmt"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func runLinting(cfg Configuration, name string, res bool) {
It(fmt.Sprintf("should return %v for \"%v\"", res, name), func() {
Expect(cfg.validateGitLabCI()).To(Equal(res))
})
}

var _ = Describe("GitLab CI linter", func() {
runLinting(Configuration{LintGitLabCI: false}, "linter disabled", true)
runLinting(Configuration{LintGitLabCI: true}, "linter enabled || not yet created", true)
// runLinting(Configuration{LintGitLabCI: true, GitLabCIFile: "test.yml"}, "linter enabled || other config file", false)
// runLinting(Configuration{LintGitLabCI: true, GitLabCIFile: "testdata/empty.yml"}, "linter enabled || empty file", true)
})

func TestGitLabCILinter(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "GitLab CI linter")
}
1 change: 1 addition & 0 deletions hook/v2/testdata/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions hook/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
ExternalChecks []string `yaml:"calls"` // ExternalChecks contains a list of commands to execute
LintGitLabCI bool `yaml:"lint_gitlab_ci"` // LintGitLabCI determines whether gitlab-ci-linter will be called
GitLabCIFile string `yaml:"gitlab_ci_file"` // GitLabCIFile use this to override .gitlab-ci.yml location
ConfDDirectoriesEnabled bool `yaml:"confd-directories"` // are .commit-msg.d directories enabled

ignoreCompiled []*regexp.Regexp
}
Expand Down
47 changes: 47 additions & 0 deletions hook/v2/validate_occurs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v2

import (
"strings"
"testing"
)

func TestTicket(t *testing.T) {
commitMessages := map[string]struct {
expected bool
value string
}{
"in-subject": {true, `this is a normal length line TICKET-1
Hello`},
"in-body": {true, `this is a normal length line
Hello TICKET-1`},
"not found": {false, "01234567890123456789012345678901234567890123456789012345678901234567890123456789"},
}

expr := make([]*ExpressionWithSeverity, 0)

expr = append(expr, &ExpressionWithSeverity{
Name: "find ticket",
Expression: "^TICKET-\\d{1,3}:.*",
})

cfg := &Configuration{
FindOccurrenceExpressions: expr,
}

err := cfg.setupRegularExpressions()
if err != nil {
t.Fatalf("error with regex: %s", err)
}

for k, v := range commitMessages {
msg := strings.Split(v.value, "\n")
t.Run(k, func(t *testing.T) {
if result := cfg.validateOccurs(msg); result != v.expected {
t.Logf("test %s failed: it is %t but should be %t", k, result, v.expected)
}
})
}

}
61 changes: 61 additions & 0 deletions hook/v2/validate_subject_line_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v2

import "testing"

func TestSubjectLineLength(t *testing.T) {
subjectLines := map[string]struct {
expected bool
value string
}{
"right-length": {true, "this is a normal length line"},
"too-long": {false, "01234567890123456789012345678901234567890123456789012345678901234567890123456789"},
"no-subject-line": {false, ""},
}

cfg := &Configuration{
SubjectLineLength: 50,
}

for k, v := range subjectLines {
t.Run(k, func(t *testing.T) {
if result := cfg.validateSubjectLine(v.value); result != v.expected {
t.Logf("test %s failed: it is %t but should be %t", k, result, v.expected)
}
})
}
}

func TestOccurs(t *testing.T) {
subjectLines := map[string]struct {
expected bool
value string
}{
"found ticket": {true, "TICKET-1: hello world"},
"no ticket number": {false, "hello world without ticket"},
}

expr := make([]*ExpressionWithSeverity, 0)

expr = append(expr, &ExpressionWithSeverity{
Name: "find ticket",
Expression: "^TICKET-\\d{1,3}:.*",
})

cfg := &Configuration{
SubjectExpressions: expr,
SubjectLineLength: 50,
}

err := cfg.setupRegularExpressions()
if err != nil {
t.Fatalf("error with regex: %s", err)
}

for k, v := range subjectLines {
t.Run(k, func(t *testing.T) {
if result := cfg.validateSubjectLine(v.value); result != v.expected {
t.Logf("test %s failed: it is %t but should be %t", k, result, v.expected)
}
})
}
}

0 comments on commit 9e7ac21

Please sign in to comment.