diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..45a3b9b --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,87 @@ +linters: + enable: + - dogsled + - dupl + - gocognit + - goconst + - gocritic + - gocyclo + - gofmt + - golint + - gosec + - maligned + - nakedret + - prealloc + - scopelint + - unconvert + - unparam + - whitespace + +linters-settings: + funlen: + lines: 60 + statements: 40 + + gocognit: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + + gocyclo: + # minimal code complexity to report, 30 by default (but we recommend 10-20) + min-complexity: 10 + + golint: + # minimal confidence for issues, default is 0.8 + min-confidence: 0.3 + + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true + + nakedret: + # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 + max-func-lines: 2 + + unparam: + # Inspect exported functions, default is false. Set to true if no external program/library imports your code. + # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find external interfaces. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: true + + unused: + # treat code as a program (not a library) and report unused exported identifiers; default is false. + # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: + # if it's called for subdir of a project it can't find funcs usages. All text editor integrations + # with golangci-lint call it on a directory with the changed file. + check-exported: false + + whitespace: + # Enforces newlines (or comments) after every multi-line if statement + multi-if: true + # Enforces newlines (or comments) after every multi-line function signature + multi-func: true + +issues: + # Not using default exclusions because we want to require comments on public + # functions and types. + exclude-use-default: false + +# options for analysis running +run: + # include test files or not, default is true + tests: false + + # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": + # If invoked with -mod=readonly, the go command is disallowed from the implicit + # automatic updating of go.mod described above. Instead, it fails when any changes + # to go.mod are needed. This setting is most useful to check that go.mod does + # not need updates, such as in a continuous integration and testing system. + # If invoked with -mod=vendor, the go command assumes that the vendor + # directory holds the correct copies of dependencies and ignores + # the dependency descriptions in go.mod. + modules-download-mode: vendor + +service: + # use the fixed version to not introduce new linters unexpectedly + golangci-lint-version: 1.20.x \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index be0a5c7..e30c0a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,25 +1,24 @@ -# This is a weird way of telling Travis to use the fast container-based test -# runner instead of the slow VM-based runner. -sudo: false +dist: bionic language: go go: - 1.12.x + - 1.13.x + - 1.14.x before_script: - - go get -u golang.org/x/lint/golint - - go get -u github.com/golang/dep/cmd/dep + - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $GOPATH/bin - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls script: + - make install - make lint - make test - - make race - make coverage - go test $(shell go list ./... | grep -v /vendor/) -race -coverprofile=coverage.txt -covermode=atomic after_success: - - $GOPATH/bin/goveralls -service=travis-ci - - bash <(curl -s https://codecov.io/bash) + - if if [[ $TRAVIS_GO_VERSION == 1.14* ]]; $GOPATH/bin/goveralls -service=travis-ci ; fi + - if if [[ $TRAVIS_GO_VERSION == 1.14* ]]; bash <(curl -s https://codecov.io/bash) ; fi diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 6836dc2..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,64 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:c5e95872057b00cec8cfe365d65087f7a7b268ad1b1ad9700a8c50c53bc29955" - name = "github.com/dasrick/go-teams-notify" - packages = ["."] - pruneopts = "UT" - revision = "97487a69edb5826fa9aca369fc12bbc6605294f6" - version = "v1.3.0" - -[[projects]] - digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec" - name = "github.com/davecgh/go-spew" - packages = ["spew"] - pruneopts = "UT" - revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" - version = "v1.1.1" - -[[projects]] - digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" - name = "github.com/pmezard/go-difflib" - packages = ["difflib"] - pruneopts = "UT" - revision = "792786c7400a136282c1664665ae0a8db921c6c2" - version = "v1.0.0" - -[[projects]] - digest = "1:23472f69a3ee7225de2f104aa48c6456959d1c155e45035c9845806d83109190" - name = "github.com/stretchr/objx" - packages = ["."] - pruneopts = "UT" - revision = "ea4fe68685ee0d3cee7032121851b57e7494e8ea" - version = "v0.2.0" - -[[projects]] - digest = "1:d43ea99f6c448b0660a10e93a259ed2988893d2ffa4eb4684014bf4fc4b64e04" - name = "github.com/stretchr/testify" - packages = [ - "assert", - "mock", - ] - pruneopts = "UT" - revision = "3ebf1ddaeb260c4b1ae502a01c7844fa8c1fa0e9" - version = "v1.5.1" - -[[projects]] - digest = "1:55b110c99c5fdc4f14930747326acce56b52cfce60b24b1c03ef686ac0e46bb1" - name = "gopkg.in/yaml.v2" - packages = ["."] - pruneopts = "UT" - revision = "53403b58ad1b561927d19068c655246f2db79d48" - version = "v2.2.8" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/dasrick/go-teams-notify", - "github.com/stretchr/testify/assert", - "github.com/stretchr/testify/mock", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index d7072c2..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,30 +0,0 @@ -# Gopkg.toml example -# -# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" -# -# [prune] -# non-go = false -# go-tests = true -# unused-packages = true - - -[prune] - go-tests = true - unused-packages = true diff --git a/Makefile b/Makefile index a4fed4b..2c90e95 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,43 @@ LIST_ALL := $(shell go list ./... | grep -v /vendor/) +# Force using Go Modules and always read the dependencies from +# the `vendor` folder. +export GO111MODULE = on +#export GOFLAGS = -mod=vendor -.PHONY: all lint test race coverage report dep help +all: lint test +.PHONY: install +install: ## Install the dependencies + @go mod vendor -all: lint test +.PHONY: update +update: ## Update the dependencies + @go mod tidy +.PHONY: clean +clean: ## Remove binaries and ZIP files based on directory `./cmd/` + @rm -rf "$(go env GOCACHE)" + @rm -f coverage.out -lint: ## Lint all files +.PHONY: lint +lint: ## Lint all files (via golangci-lint) @go fmt ${LIST_ALL} - @golint -set_exit_status ${LIST_ALL} + @golangci-lint run -test: dep ## Run unittests - @go test -short ${LIST_ALL} +.PHONY: test +test: clean ## Run unit tests (incl. race and coverprofile) + @go test -race -cover -short -timeout=90s -coverprofile=coverage.out ${LIST_ALL} -race: dep ## Run data race detector - @go test -race -short ${LIST_ALL} - -coverage: dep # Generate coverage report - @go test ${LIST_ALL} -coverprofile coverage.out +.PHONY: coverage +coverage: test ## Generate coverage report @go tool cover -func coverage.out -report: coverage # Open the coverage report in browser +.PHONY: report +report: coverage ## Open the coverage report in browser @go tool cover -html=coverage.out - -dep: ## Get the dependencies - @dep ensure +# ---------------------------------------------------------------------------------------------------------------------- help: ## Display this help screen @grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/README.md b/README.md index ce7f98e..c2d8745 100644 --- a/README.md +++ b/README.md @@ -22,20 +22,20 @@ To get the package, execute: ``` -go get gopkg.in/dasrick/go-teams-notify.v1 +go get https://github.com/dasrick/go-teams-notify/v2 ``` To import this package, add the following line to your code: ``` -import "gopkg.in/dasrick/go-teams-notify.v1" +import "github.com/dasrick/go-teams-notify/v2" ``` And this is an example of a simple implementation ... ``` import ( - "gopkg.in/dasrick/go-teams-notify.v1" + "github.com/dasrick/go-teams-notify/v2" ) func main() { @@ -44,18 +44,18 @@ func main() { func sendTheMessage() error { // init the client - mstClient, err := goteamsnotify.NewClient() - if err != nil { - return err - } + mstClient := goteamsnotify.NewClient() + // setup webhook url webhookUrl := "https://outlook.office.com/webhook/YOUR_WEBHOOK_URL_OF_TEAMS_CHANNEL" + // setup message card msgCard := goteamsnotify.NewMessageCard() msgCard.Title = "Hello world" msgCard.Text = "Here are some examples of formatted stuff like
* this list itself
* **bold**
* *italic*
* ***bolditalic***" msgCard.ThemeColor = "#DF813D" - // firestarter + + // send return mstClient.Send(webhookUrl, msgCard) } ``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3f7580b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/dasrick/go-teams-notify/v2 + +go 1.14 + +require github.com/stretchr/testify v1.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c0565d7 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/mocks/API.go b/mocks/API.go deleted file mode 100644 index a3736f7..0000000 --- a/mocks/API.go +++ /dev/null @@ -1,27 +0,0 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. - -package mocks - -import ( - goteamsnotify "github.com/dasrick/go-teams-notify" - mock "github.com/stretchr/testify/mock" -) - -// API is an autogenerated mock type for the API type -type API struct { - mock.Mock -} - -// Send provides a mock function with given fields: webhookURL, webhookMessage -func (_m *API) Send(webhookURL string, webhookMessage goteamsnotify.MessageCard) error { - ret := _m.Called(webhookURL, webhookMessage) - - var r0 error - if rf, ok := ret.Get(0).(func(string, goteamsnotify.MessageCard) error); ok { - r0 = rf(webhookURL, webhookMessage) - } else { - r0 = ret.Error(0) - } - - return r0 -} diff --git a/send.go b/send.go index 828bdef..90cc137 100644 --- a/send.go +++ b/send.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "errors" - "log" "net/http" "net/url" "strings" @@ -21,13 +20,13 @@ type teamsClient struct { } // NewClient - create a brand new client for MS Teams notify -func NewClient() (API, error) { +func NewClient() API { client := teamsClient{ httpClient: &http.Client{ Timeout: 5 * time.Second, }, } - return &client, nil + return &client } // Send - will post a notification to MS Teams incomingWebhookURL @@ -52,7 +51,6 @@ func (c teamsClient) Send(webhookURL string, webhookMessage MessageCard) error { } if res.StatusCode >= 299 { err = errors.New("error on notification: " + res.Status) - log.Println(err) return err } diff --git a/send_test.go b/send_test.go index c049b98..d0426c8 100644 --- a/send_test.go +++ b/send_test.go @@ -10,8 +10,7 @@ import ( ) func TestNewClient(t *testing.T) { - client, err := NewClient() - assert.IsType(t, nil, err) + client := NewClient() assert.IsType(t, &teamsClient{}, client) } @@ -96,8 +95,6 @@ func TestTeamsClientSend(t *testing.T) { assert.Equal(t, req.URL.String(), test.reqURL) return &http.Response{ StatusCode: test.resStatus, - // Send response to be tested - //Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)), // Must be set to non-nil value or it panics Header: make(http.Header), }, test.resError @@ -119,7 +116,7 @@ func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } -//NewTestClient returns *http.API with Transport replaced to avoid making real calls +// NewTestClient returns *http.API with Transport replaced to avoid making real calls func NewTestClient(fn RoundTripFunc) *http.Client { return &http.Client{ Transport: RoundTripFunc(fn),