Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix lint issues, update workflows, fix lint workflow #112

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions .github/workflows/core-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Code Check

on:
push:
branches:
- main
pull_request:

jobs:
Expand All @@ -10,13 +12,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.18
go-version: ^1.22
id: go

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Get dependencies
run: |
Expand All @@ -26,10 +28,10 @@ jobs:
run: go test -v -covermode=count -coverprofile=coverage.out ./...

- name: Convert coverage.out to coverage.lcov
uses: jandelgado/gcov2lcov-action@v1.0.7
uses: jandelgado/gcov2lcov-action@v1.0.9

- name: Coveralls
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: coverage.lcov
Expand All @@ -39,13 +41,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ^1.18
go-version: ^1.22
id: go

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Get dependencies
run: |
Expand All @@ -62,10 +64,15 @@ jobs:
name: golangci lint check
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: ^1.22

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v4
with:
version: v1.51.0
version: v1.57
118 changes: 62 additions & 56 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,76 @@ run:
timeout: 5m
issues-exit-code: 1
tests: true
skip-dirs: []
skip-files: []
modules-download-mode: readonly

linters-settings:
gci:
local-prefix: github.com/yitsushi/go-misskey
goimports:
local-prefix: github.com/yitsushi/go-misskey
misspell:
locale: US
nolintlint:
allow-leading-space: false
allow-unused: false
require-explanation: true
require-specific: false
gocognit:
min-complexity: 40
varnamelen:
ignore-names:
- err
- ok
- wg

issues:
exclude-rules:
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- exhaustruct
- goconst
- gocyclo
- goerr113
- gosec
- lll
- paralleltest
- wastedassign
- wrapcheck
- text: "sig: func github.com/yitsushi/go-misskey/"
linters:
- wrapcheck
- text: "sig: func \\(\\*?github.com/yitsushi/go-misskey/"
linters:
- wrapcheck

linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- bodyclose
enable-all: true
disable:
# Deprecated
- golint
- varcheck
- interfacer
- ifshort
- scopelint
- maligned
- structcheck
- deadcode
- nosnakecase
- exhaustivestruct

# Disabled for now
- depguard
- dogsled
- dupl
- exhaustive
- exportloopref
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
# - golint # Replaced by revive.
- revive
- gomnd
- gomodguard
- goprintffuncname
- gosec
# - interfacer # The repository of the linter has been archived by the owner.
- lll
# - maligned # Replaced by govet 'fieldalignment'.
- misspell
- nakedret
- nestif
- nlreturn
- noctx
- nolintlint
- prealloc
- rowserrcheck
# - scopelint # Replaced by exportloopref.
- exportloopref
- sqlclosecheck
- stylecheck
- testpackage
- unconvert
- unparam
- whitespace
- wsl
- testableexamples
- testifylint

# We have no control over JSON keys in Misskey
- tagliatelle

# I don't agree with these
- perfsprint

# Mostly just annoying
- exhaustruct
18 changes: 10 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -81,12 +81,12 @@ func (c Client) sendRequest(request core.Request, response interface{}) error {

req, err := http.NewRequestWithContext(
context.Background(),
"POST",
http.MethodPost,
c.url(request.EndpointPath()),
bytes.NewBuffer(requestBody),
)
if err != nil {
return err
return fmt.Errorf("unable to create new request: %w", err)
}

req.Header.Set("Content-Type", contentType)
Expand All @@ -101,7 +101,7 @@ func (c Client) sendRequest(request core.Request, response interface{}) error {

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return core.RequestError{Message: core.ResponseReadBodyError, Origin: err}
}
Expand All @@ -113,9 +113,11 @@ func (c Client) sendRequest(request core.Request, response interface{}) error {
}).Debugf("%s", body)

if resp.StatusCode == http.StatusOK {
err = json.Unmarshal(body, response)
if err := json.Unmarshal(body, response); err != nil {
return fmt.Errorf("unable to parse response: %w", err)
}

return err
return nil
}

if resp.StatusCode == http.StatusNoContent {
Expand All @@ -126,7 +128,7 @@ func (c Client) sendRequest(request core.Request, response interface{}) error {
}

if resp.StatusCode == http.StatusNotFound {
return core.EndpointNotFound{
return core.EndpointNotFoundError{
Endpoint: request.EndpointPath(),
}
}
Expand All @@ -135,7 +137,7 @@ func (c Client) sendRequest(request core.Request, response interface{}) error {
}

func unwrapError(body []byte) error {
var errorWrapper core.ErrorResponseWrapper
var errorWrapper core.ErrorResponseWrapperError

err := json.Unmarshal(body, &errorWrapper)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package misskey_test
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -17,7 +17,7 @@ func TestNewClient_NormalRequestContent(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
defer request.Body.Close()
body, _ := ioutil.ReadAll(request.Body)
body, _ := io.ReadAll(request.Body)

var statsRequest map[string]interface{}

Expand Down Expand Up @@ -92,8 +92,8 @@ func TestNewClient_createDefaultHTTPClient(t *testing.T) {

func TestNewClient_RequestError(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
return test.NewMockResponse(http.StatusNotImplemented, []byte{}, errors.New("bad")) //nolint:goerr113
mockClient.MockRequest("/api/stats", func(_ *http.Request) (*http.Response, error) {
return test.NewMockResponse(http.StatusNotImplemented, []byte{}, errors.New("bad"))
})

client, _ := misskey.NewClientWithOptions(
Expand All @@ -110,7 +110,7 @@ func TestNewClient_RequestError(t *testing.T) {

expected := core.RequestError{
Message: core.ResponseReadError,
Origin: errors.New("bad"), //nolint:goerr113
Origin: errors.New("bad"),
}
if err.Error() != expected.Error() {
t.Errorf("Expected error = %s, got = %s", expected.Error(), err.Error())
Expand All @@ -119,7 +119,7 @@ func TestNewClient_RequestError(t *testing.T) {

func TestNewClient_ReadError(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
mockClient.MockRequest("/api/stats", func(_ *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: test.BadReadCloser{},
Expand All @@ -141,7 +141,7 @@ func TestNewClient_ReadError(t *testing.T) {

expected := core.RequestError{
Message: core.ResponseReadBodyError,
Origin: errors.New("Read error"), //nolint:goerr113
Origin: errors.New("read error"),
}
if err.Error() != expected.Error() {
t.Errorf("Expected error = %s, got = %s", expected.Error(), err.Error())
Expand All @@ -150,7 +150,7 @@ func TestNewClient_ReadError(t *testing.T) {

func TestNewClient_ErrorResponseWrapper_Error(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
mockClient.MockRequest("/api/stats", func(_ *http.Request) (*http.Response, error) {
content := []byte("something")

return test.NewMockResponse(http.StatusInternalServerError, content, nil)
Expand All @@ -171,7 +171,7 @@ func TestNewClient_ErrorResponseWrapper_Error(t *testing.T) {

expected := core.RequestError{
Message: core.ErrorResponseParseError,
Origin: errors.New("invalid character 's' looking for beginning of value"), //nolint:goerr113
Origin: errors.New("invalid character 's' looking for beginning of value"),
}
if err.Error() != expected.Error() {
t.Errorf("Expected error = %s, got = %s", expected.Error(), err.Error())
Expand All @@ -180,7 +180,7 @@ func TestNewClient_ErrorResponseWrapper_Error(t *testing.T) {

func TestNewClient_ErrorResponseParse_Error(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
mockClient.MockRequest("/api/stats", func(_ *http.Request) (*http.Response, error) {
content := []byte("{\"error\": true}")

return test.NewMockResponse(http.StatusInternalServerError, content, nil)
Expand All @@ -201,7 +201,7 @@ func TestNewClient_ErrorResponseParse_Error(t *testing.T) {

expected := core.RequestError{
Message: core.ErrorResponseParseError,
Origin: errors.New("json: cannot unmarshal bool into Go value of type core.ErrorResponse"), //nolint:goerr113
Origin: errors.New("json: cannot unmarshal bool into Go value of type core.ErrorResponse"),
}
if err.Error() != expected.Error() {
t.Errorf("Expected error = %s, got = %s", expected.Error(), err.Error())
Expand All @@ -210,7 +210,7 @@ func TestNewClient_ErrorResponseParse_Error(t *testing.T) {

func TestNewClient_ValidErrorResponse(t *testing.T) {
mockClient := test.NewMockHTTPClient()
mockClient.MockRequest("/api/stats", func(request *http.Request) (*http.Response, error) {
mockClient.MockRequest("/api/stats", func(_ *http.Request) (*http.Response, error) {
content := []byte(`{
"error": {
"info": {
Expand Down
Loading
Loading