From 6e3bdfaebe0df945f4c198631b4bef7174b1e03c Mon Sep 17 00:00:00 2001 From: NeoVg Date: Sun, 28 May 2023 07:38:27 +0200 Subject: [PATCH 1/2] Update GitHub actions --- .github/workflows/build.yml | 23 +++++++++++++++++ .github/workflows/golangci-lint.yml | 39 ----------------------------- .github/workflows/lint.yml | 20 +++++++++++++++ .github/workflows/staticcheck.yml | 23 +++++++++++++++++ .github/workflows/test.yml | 23 +++++++++++++++++ .github/workflows/vet.yml | 20 +++++++++++++++ 6 files changed, 109 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/staticcheck.yml create mode 100644 .github/workflows/test.yml create mode 100644 .github/workflows/vet.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b85748a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,23 @@ +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + name: Check Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.19.0' + + - name: Verify dependencies + run: go mod verify + + - name: Build + run: go build -v ./... diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml deleted file mode 100644 index dd1ab07..0000000 --- a/.github/workflows/golangci-lint.yml +++ /dev/null @@ -1,39 +0,0 @@ ---- -name: golangci-lint -on: - push: - tags: - - v* - branches: - - master - - main - pull_request: -jobs: - golangci: - name: lint - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: golangci-lint - uses: golangci/golangci-lint-action@v3.4.0 - with: - # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version - version: latest - - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - args: --timeout 5m - - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true - - # Optional: if set to true then the action will use pre-installed Go. - # skip-go-installation: true - - # Optional: if set to true then the action don't cache or restore ~/go/pkg. - # skip-pkg-cache: true - - # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. - # skip-build-cache: true diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..2a25a3b --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,20 @@ +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.19.0' + + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v3 diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml new file mode 100644 index 0000000..810e293 --- /dev/null +++ b/.github/workflows/staticcheck.yml @@ -0,0 +1,23 @@ +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + staticcheck: + name: Staticcheck + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.19.0' + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run staticcheck + run: staticcheck ./... diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..d52369e --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.19.0' + + - name: Verify dependencies + run: go mod verify + + - name: Run tests + run: go test -race -vet=off ./... diff --git a/.github/workflows/vet.yml b/.github/workflows/vet.yml new file mode 100644 index 0000000..8672c49 --- /dev/null +++ b/.github/workflows/vet.yml @@ -0,0 +1,20 @@ +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + vet: + name: Go Vet + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.19.0' + + - name: Run go vet + run: go vet ./... From 542431bb44b72b7a4126e6cc3bb61b12c9dc4bd5 Mon Sep 17 00:00:00 2001 From: NeoVg Date: Sun, 28 May 2023 07:47:33 +0200 Subject: [PATCH 2/2] Fix build and staticcheck --- cmd/db_connect/db_connect.go | 1 - cmd/test_logger/test_logger.go | 2 +- container/container.go | 1 - pkg/webhook/handler.go | 12 ++++++------ 4 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 cmd/db_connect/db_connect.go diff --git a/cmd/db_connect/db_connect.go b/cmd/db_connect/db_connect.go deleted file mode 100644 index 06ab7d0..0000000 --- a/cmd/db_connect/db_connect.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/cmd/test_logger/test_logger.go b/cmd/test_logger/test_logger.go index 1576950..0c94298 100644 --- a/cmd/test_logger/test_logger.go +++ b/cmd/test_logger/test_logger.go @@ -15,5 +15,5 @@ func main() { logger.Warning("warning") logger.Error("error") //logger.Panic("panic") - logger.Fatal("fatal") + //logger.Fatal("fatal") } diff --git a/container/container.go b/container/container.go index 8c39d22..6b32d35 100644 --- a/container/container.go +++ b/container/container.go @@ -5,7 +5,6 @@ import ( logger "github.com/br0-space/bot-logger" "github.com/br0-space/bot/interfaces" "github.com/br0-space/bot/pkg/config" - _ "github.com/br0-space/bot/pkg/config" "github.com/br0-space/bot/pkg/db" "github.com/br0-space/bot/pkg/fortune" "github.com/br0-space/bot/pkg/matcher" diff --git a/pkg/webhook/handler.go b/pkg/webhook/handler.go index b2fb076..554399d 100644 --- a/pkg/webhook/handler.go +++ b/pkg/webhook/handler.go @@ -35,7 +35,7 @@ func (h *Handler) InitMatchers() { func (h *Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) { h.log.Debugf("%s %s %s from %s", req.Method, req.URL, req.Proto, req.RemoteAddr) - messageIn, err, status := h.parseRequest(req) + messageIn, status, err := h.parseRequest(req) if err != nil { h.log.Error(err) http.Error(res, err.Error(), status) @@ -45,21 +45,21 @@ func (h *Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) { h.processRequest(*messageIn) } -func (h *Handler) parseRequest(req *http.Request) (*interfaces.TelegramWebhookMessageStruct, error, int) { +func (h *Handler) parseRequest(req *http.Request) (*interfaces.TelegramWebhookMessageStruct, int, error) { if req.Method != http.MethodPost { - return nil, fmt.Errorf("method not allowed: %s (actual) != POST (expected)", req.Method), http.StatusMethodNotAllowed + return nil, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s (actual) != POST (expected)", req.Method) } body := &interfaces.TelegramWebhookBodyStruct{} if err := json.NewDecoder(req.Body).Decode(body); err != nil { - return nil, fmt.Errorf("unable to decode request body: %s", err.Error()), http.StatusBadRequest + return nil, http.StatusBadRequest, fmt.Errorf("unable to decode request body: %s", err.Error()) } if body.Message.Chat.ID != h.cfg.Telegram.ChatID { - return nil, fmt.Errorf("chat id mismatch: %d (actual) != %d (expected)", body.Message.Chat.ID, h.cfg.Telegram.ChatID), http.StatusOK + return nil, http.StatusOK, fmt.Errorf("chat id mismatch: %d (actual) != %d (expected)", body.Message.Chat.ID, h.cfg.Telegram.ChatID) } - return &body.Message, nil, 0 + return &body.Message, 0, nil } func (h *Handler) processRequest(messageIn interfaces.TelegramWebhookMessageStruct) {