From f5e50c1e176a6c3a398f992d998012751e377bd7 Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Thu, 2 Apr 2020 20:01:00 +0200 Subject: [PATCH] Integrate GolangCI-Lint (#90) * Use plugin orb for ci * Add golangci-lint config and fix issues * Simplify circleci config * Drop unparam * Drop maligned * Ignore varcheck in server/manifest.go --- .circleci/config.yml | 27 +++++-------------- .golangci.yml | 60 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 52 +++++++++---------------------------- build/deploy/main.go | 4 +-- server/plugin_test.go | 1 + 5 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 .golangci.yml diff --git a/.circleci/config.yml b/.circleci/config.yml index 2ffb702..d469528 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,27 +1,12 @@ version: 2.1 -executors: - default: - docker: - - image: circleci/golang:1.14-node -jobs: - lint: - executor: - name: default - steps: - - checkout - - run: make check-style - - test: - executor: - name: default - steps: - - checkout - - run: make test +orbs: + plugin-ci: mattermost/plugin-ci@0.1.0 workflows: version: 2 - untagged-build: + ci: jobs: - - lint - - test + - plugin-ci/lint + - plugin-ci/test + - plugin-ci/build diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..e36fed3 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,60 @@ +run: + timeout: 5m + modules-download-mode: readonly + +linters-settings: + goconst: + min-len: 2 + min-occurrences: 2 + gofmt: + simplify: true + goimports: + local-prefixes: github.com/mattermost/mattermost-starter-template + golint: + min-confidence: 0 + govet: + check-shadowing: true + enable-all: true + misspell: + locale: US + +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - errcheck + - goconst + - gocritic + - gofmt + - goimports + - golint + - gosec + - gosimple + - govet + - ineffassign + - interfacer + - misspell + - nakedret + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unused + - varcheck + - whitespace + +issues: + exclude-rules: + - path: server/manifest.go + linters: + - unused + - varcheck + - path: server/configuration.go + linters: + - unused + - path: _test\.go + linters: + - goconst + - scopelint # https://github.com/kyoh86/scopelint/issues/4 diff --git a/Makefile b/Makefile index 9e80d25..ca7f0bf 100644 --- a/Makefile +++ b/Makefile @@ -30,54 +30,25 @@ all: check-style test dist apply: ./build/bin/manifest apply -## Runs govet and gofmt against all packages. +## Runs golangci-lint and eslint. .PHONY: check-style -check-style: webapp/.npminstall gofmt govet golint +check-style: webapp/.npminstall golangci-lint @echo Checking for style guide compliance ifneq ($(HAS_WEBAPP),) cd webapp && npm run lint endif -## Runs gofmt against all packages. -.PHONY: gofmt -gofmt: -ifneq ($(HAS_SERVER),) - @echo Running gofmt - @for package in $$(go list ./...); do \ - echo "Checking "$$package; \ - files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \ - if [ "$$files" ]; then \ - gofmt_output=$$(gofmt -d -s $$files 2>&1); \ - if [ "$$gofmt_output" ]; then \ - echo "$$gofmt_output"; \ - echo "Gofmt failure"; \ - exit 1; \ - fi; \ - fi; \ - done - @echo Gofmt success -endif +## Run golangci-lint on codebase. +.PHONY: golangci-lint +golangci-lint: + @if ! [ -x "$$(command -v golangci-lint)" ]; then \ + echo "golangci-lint is not installed. Please see https://github.com/golangci/golangci-lint#install for installation instructions."; \ + exit 1; \ + fi; \ -## Runs govet against all packages. -.PHONY: govet -govet: -ifneq ($(HAS_SERVER),) - @echo Running govet - @# Workaround because you can't install binaries without adding them to go.mod - env GO111MODULE=off $(GO) get golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow - $(GO) vet ./... - $(GO) vet -vettool=$(GOPATH)/bin/shadow ./... - @echo Govet success -endif - -## Runs golint against all packages. -.PHONY: golint -golint: - @echo Running lint - env GO111MODULE=off $(GO) get golang.org/x/lint/golint - $(GOPATH)/bin/golint -set_exit_status ./... - @echo lint success + @echo Running golangci-lint + golangci-lint run ./... ## Builds the server, if it exists, including support for multiple architectures. .PHONY: server @@ -104,6 +75,7 @@ ifneq ($(HAS_WEBAPP),) endif ## Builds the webapp in debug mode, if it exists. +.PHONY: webapp-debug webapp-debug: webapp/.npminstall ifneq ($(HAS_WEBAPP),) cd webapp && \ diff --git a/build/deploy/main.go b/build/deploy/main.go index e01e2a8..186bb49 100644 --- a/build/deploy/main.go +++ b/build/deploy/main.go @@ -85,13 +85,13 @@ func uploadPlugin(client *model.Client4, pluginID, bundlePath string) error { log.Print("Uploading plugin via API.") _, resp := client.UploadPluginForced(pluginBundle) if resp.Error != nil { - return fmt.Errorf("Failed to upload plugin bundle: %s", resp.Error.Error()) + return errors.Wrap(resp.Error, "failed to upload plugin bundle") } log.Print("Enabling plugin.") _, resp = client.EnablePlugin(pluginID) if resp.Error != nil { - return fmt.Errorf("Failed to enable plugin: %s", resp.Error.Error()) + return errors.Wrap(resp.Error, "Failed to enable plugin") } return nil diff --git a/server/plugin_test.go b/server/plugin_test.go index 9b7aa9c..1d3a474 100644 --- a/server/plugin_test.go +++ b/server/plugin_test.go @@ -19,6 +19,7 @@ func TestServeHTTP(t *testing.T) { result := w.Result() assert.NotNil(result) + defer result.Body.Close() bodyBytes, err := ioutil.ReadAll(result.Body) assert.Nil(err) bodyString := string(bodyBytes)