From 07cd943fdad8f2e8842ab3352ee383ad591c0f59 Mon Sep 17 00:00:00 2001 From: ashing Date: Sat, 2 Sep 2023 14:07:44 +0800 Subject: [PATCH] chore: go imports and add github workflow for lint Signed-off-by: ashing --- .github/workflows/lint.yaml | 51 ++++++++++++++++++++++++++++++++++++ Makefile | 4 +++ chatbot/chatbot_handler.go | 1 + client/client.go | 10 ++++--- client/config_test.go | 3 ++- client/option_test.go | 6 +++-- event/event.go | 3 ++- event/event_handler.go | 1 + event/event_handler_test.go | 3 ++- event/event_test.go | 4 ++- event/frame_handler.go | 1 + event/frame_handler_test.go | 6 +++-- example/example.go | 1 + handler/ihandler.go | 1 + logger/logger_test.go | 3 ++- payload/connection_test.go | 3 ++- payload/data_frame_test.go | 3 ++- payload/utils.go | 1 + payload/utils_test.go | 3 ++- plugin/plugin_handler.go | 1 + scripts/goimports-reviser.sh | 11 ++++++++ utils/errors_test.go | 3 ++- utils/net_test.go | 3 ++- 23 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/lint.yaml create mode 100755 scripts/goimports-reviser.sh diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..11bcc7a --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,51 @@ +name: Lint Check + +on: + push: + branches: + - main + - release/** + pull_request: + branches: + - main + - release/** +permissions: read-all +jobs: + gofmt: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v3 + - name: Setup Go Environment + uses: actions/setup-go@v3 + with: + go-version: '1.20.3' + - name: Run gofmt Check + working-directory: ./ + run: | + diffs=`gofmt -l .` + if [[ -n $diffs ]]; then + echo "Files are not formatted by gofmt:" + echo $diffs + exit 1 + fi + golint: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - name: Setup Go Environment + uses: actions/setup-go@v3 + with: + go-version: '1.20.3' + - name: Install jq + run: sudo apt install -y jq + - uses: actions/setup-node@v3 + with: + node-version: '16' + - name: Download golangci-lint + run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2 + - name: Run Golang Linters + working-directory: ./ + run: | + PATH=${PATH}:$(go env GOPATH)/bin make lint diff --git a/Makefile b/Makefile index d3d9dbc..dd17349 100644 --- a/Makefile +++ b/Makefile @@ -22,3 +22,7 @@ test: go tool cover -html=./sdk.cover -o ./sdk.html fmt: find ./ -name "*.go" | xargs gofmt -w + +lint: ## Apply go lint check + @golangci-lint run --timeout 10m ./... +.PHONY: lint diff --git a/chatbot/chatbot_handler.go b/chatbot/chatbot_handler.go index b3be28f..6400162 100644 --- a/chatbot/chatbot_handler.go +++ b/chatbot/chatbot_handler.go @@ -3,6 +3,7 @@ package chatbot import ( "context" "encoding/json" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) diff --git a/client/client.go b/client/client.go index 52c7136..5f4f213 100644 --- a/client/client.go +++ b/client/client.go @@ -6,17 +6,19 @@ import ( "encoding/json" "errors" "fmt" + "io" + "net/http" + "sync" + "time" + "github.com/gorilla/websocket" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" "github.com/open-dingtalk/dingtalk-stream-sdk-go/handler" "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger" "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" "github.com/open-dingtalk/dingtalk-stream-sdk-go/plugin" "github.com/open-dingtalk/dingtalk-stream-sdk-go/utils" - "io" - "net/http" - "sync" - "time" ) /** diff --git a/client/config_test.go b/client/config_test.go index 3eade8b..03c6a9d 100644 --- a/client/config_test.go +++ b/client/config_test.go @@ -1,8 +1,9 @@ package client import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/client/option_test.go b/client/option_test.go index ac07abf..f9ab979 100644 --- a/client/option_test.go +++ b/client/option_test.go @@ -2,9 +2,11 @@ package client import ( "context" - "github.com/stretchr/testify/assert" - "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" "testing" + + "github.com/stretchr/testify/assert" + + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) /** diff --git a/event/event.go b/event/event.go index bb2165b..b68c0bf 100644 --- a/event/event.go +++ b/event/event.go @@ -1,8 +1,9 @@ package event import ( - "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" "strconv" + + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) /** diff --git a/event/event_handler.go b/event/event_handler.go index ecd6d7e..84123c1 100644 --- a/event/event_handler.go +++ b/event/event_handler.go @@ -2,6 +2,7 @@ package event import ( "context" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger" ) diff --git a/event/event_handler_test.go b/event/event_handler_test.go index 7c06fc8..3522f0b 100644 --- a/event/event_handler_test.go +++ b/event/event_handler_test.go @@ -2,8 +2,9 @@ package event import ( "context" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/event/event_test.go b/event/event_test.go index 072c883..fd01cb5 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -1,10 +1,12 @@ package event import ( + "testing" + "github.com/stretchr/testify/assert" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" "github.com/open-dingtalk/dingtalk-stream-sdk-go/utils" - "testing" ) /** diff --git a/event/frame_handler.go b/event/frame_handler.go index 9884aac..e95e241 100644 --- a/event/frame_handler.go +++ b/event/frame_handler.go @@ -3,6 +3,7 @@ package event import ( "context" "encoding/json" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger" "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) diff --git a/event/frame_handler_test.go b/event/frame_handler_test.go index ff27dbb..086d9f5 100644 --- a/event/frame_handler_test.go +++ b/event/frame_handler_test.go @@ -3,9 +3,11 @@ package event import ( "context" "errors" - "github.com/stretchr/testify/assert" - "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" "testing" + + "github.com/stretchr/testify/assert" + + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) /** diff --git a/example/example.go b/example/example.go index 0af836f..ca1b563 100644 --- a/example/example.go +++ b/example/example.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot" "github.com/open-dingtalk/dingtalk-stream-sdk-go/client" "github.com/open-dingtalk/dingtalk-stream-sdk-go/event" diff --git a/handler/ihandler.go b/handler/ihandler.go index 4cf5a69..0420c32 100644 --- a/handler/ihandler.go +++ b/handler/ihandler.go @@ -2,6 +2,7 @@ package handler import ( "context" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) diff --git a/logger/logger_test.go b/logger/logger_test.go index 95e5270..6091c12 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -1,8 +1,9 @@ package logger import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/payload/connection_test.go b/payload/connection_test.go index 0876747..c189edd 100644 --- a/payload/connection_test.go +++ b/payload/connection_test.go @@ -1,8 +1,9 @@ package payload import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/payload/data_frame_test.go b/payload/data_frame_test.go index 7066200..dbdfc12 100644 --- a/payload/data_frame_test.go +++ b/payload/data_frame_test.go @@ -2,8 +2,9 @@ package payload import ( "errors" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/payload/utils.go b/payload/utils.go index 0e5f5c9..39ee57e 100644 --- a/payload/utils.go +++ b/payload/utils.go @@ -2,6 +2,7 @@ package payload import ( "fmt" + "github.com/google/uuid" ) diff --git a/payload/utils_test.go b/payload/utils_test.go index 4babd02..46b6a8d 100644 --- a/payload/utils_test.go +++ b/payload/utils_test.go @@ -1,9 +1,10 @@ package payload import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/plugin/plugin_handler.go b/plugin/plugin_handler.go index 339d4f9..fd78435 100644 --- a/plugin/plugin_handler.go +++ b/plugin/plugin_handler.go @@ -3,6 +3,7 @@ package plugin import ( "context" "encoding/json" + "github.com/open-dingtalk/dingtalk-stream-sdk-go/payload" ) diff --git a/scripts/goimports-reviser.sh b/scripts/goimports-reviser.sh new file mode 100755 index 0000000..eb5fec9 --- /dev/null +++ b/scripts/goimports-reviser.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +go install github.com/incu6us/goimports-reviser/v2@latest + +PROJECT_NAME=github.com/open-dingtalk/dingtalk-stream-sdk-go + +find . -name '*.go' -print0 | while IFS= read -r -d '' file; do + goimports-reviser -file-path "$file" -project-name "$PROJECT_NAME" +done diff --git a/utils/errors_test.go b/utils/errors_test.go index 3265810..0e5e42c 100644 --- a/utils/errors_test.go +++ b/utils/errors_test.go @@ -1,11 +1,12 @@ package utils import ( - "github.com/stretchr/testify/assert" "io" "net/http" "strings" "testing" + + "github.com/stretchr/testify/assert" ) /** diff --git a/utils/net_test.go b/utils/net_test.go index 1bb5d6a..5b5620c 100644 --- a/utils/net_test.go +++ b/utils/net_test.go @@ -1,8 +1,9 @@ package utils import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetFirstLanIP(t *testing.T) {