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

Fix some linter warnings and deprecations #464

Merged
merged 1 commit into from
Aug 30, 2023
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
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tmp_dir = "tmp"
bin = "./tmp/main"
cmd = "go build -o ./tmp/main cmd/bot/main.go"
delay = 2500
exclude_dir = ["storage", "tmp", "vendor"]
exclude_dir = ["storage", "tmp", "vendor", "build", "docs", "examples"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ issues:

linters:
enable:
- asasalint
- asciicheck
- bodyclose
- dogsled
Expand All @@ -16,6 +17,7 @@ linters:
- exhaustive
- exportloopref
- gochecknoinits
- gocheckcompilerdirectives
- gocritic
- gocyclo
- gofumpt
Expand All @@ -25,6 +27,8 @@ linters:
- govet
- importas
- ineffassign
- loggercheck
- mirror
- makezero
- megacheck
- misspell
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ mocks: dep
go generate ./...

# live reload, see https://github.com/cosmtrek/air
air:
run-live-reload:
command -v air || go install github.com/cosmtrek/air@latest
air
4 changes: 2 additions & 2 deletions bot/storage/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package storage

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -21,7 +21,7 @@ type fileStorage struct {

func (s *fileStorage) GetKeys(collection string) ([]string, error) {
dir := filepath.Join(s.dir, collection)
files, _ := ioutil.ReadDir(dir)
files, _ := os.ReadDir(dir)

keys := make([]string, 0, len(files))

Expand Down
18 changes: 7 additions & 11 deletions bot/storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,30 @@ type redisStorage struct {
client *redis.Client
}

var redisCtx = context.Background()

func (s redisStorage) Write(collection, key string, v any) error {
data, err := json.Marshal(v)
if err != nil {
return err
}

ctx := context.Background()
s.client.HSet(ctx, collection, key, string(data))
s.client.HSet(redisCtx, collection, key, data)

return nil
}

func (s redisStorage) Read(collection, key string, v any) error {
ctx := context.Background()
res, err := s.client.HGet(ctx, collection, key).Result()
res, err := s.client.HGet(redisCtx, collection, key).Bytes()
if err != nil {
return err
}

return json.Unmarshal([]byte(res), v)
return json.Unmarshal(res, v)
}

func (s redisStorage) GetKeys(collection string) ([]string, error) {
ctx := context.Background()
res, err := s.client.HKeys(ctx, collection).Result()
res, err := s.client.HKeys(redisCtx, collection).Result()
if err != nil {
return nil, err
}
Expand All @@ -51,8 +50,5 @@ func (s redisStorage) GetKeys(collection string) ([]string, error) {
}

func (s redisStorage) Delete(collection, key string) error {
ctx := context.Background()
_, err := s.client.HDel(ctx, collection, key).Result()

return err
return s.client.HDel(redisCtx, collection, key).Err()
}
5 changes: 3 additions & 2 deletions command/clouds/aws/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func ListServices(cluster string) ([]string, error) {

func assertECS() *ecs.ECS {
if _ecs == nil {
_ecs = ecs.New(session.New(getServiceConfiguration()))
sess, _ := session.NewSession(getServiceConfiguration())
_ecs = ecs.New(sess)
}
return _ecs
}
Expand Down Expand Up @@ -144,7 +145,7 @@ func ForceNewDeployment(clusterName string, serviceName string) error {
return err
}
if len(result.Services) == 0 {
return fmt.Errorf("Could not find service %s in cluster %s", serviceName, clusterName)
return fmt.Errorf("could not find service %s in cluster %s", serviceName, clusterName)
}

// Update Service
Expand Down
16 changes: 12 additions & 4 deletions command/games/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"math/rand"
"net/http"
"strconv"

"github.com/slack-go/slack"
"time"

"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/client"
"github.com/pkg/errors"
"github.com/slack-go/slack"
)

const (
Expand All @@ -25,13 +25,21 @@ const (

// NewQuizCommand returns a new quizCommand which is a small quiz game
func NewQuizCommand(base bot.BaseCommand) bot.Command {
return &quizCommand{base, quiz{}, apiURL}
random := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec

return &quizCommand{
base,
quiz{},
apiURL,
random,
}
}

type quizCommand struct {
bot.BaseCommand
quiz quiz
apiURL string
rand *rand.Rand
}

type question struct {
Expand Down Expand Up @@ -125,7 +133,7 @@ func (c *quizCommand) parseAnswers() {
answers := question.IncorrectAnswers
answers = append(answers, question.CorrectAnswer)

rand.Shuffle(len(answers), func(i, j int) {
c.rand.Shuffle(len(answers), func(i, j int) {
answers[i], answers[j] = answers[j], answers[i]
})

Expand Down
5 changes: 2 additions & 3 deletions command/games/quiz_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package games

import (
"math/rand"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -17,8 +16,6 @@ func TestQuiz(t *testing.T) {
slackClient := &mocks.SlackClient{}
base := bot.BaseCommand{SlackClient: slackClient}

// mock test data
rand.Seed(2)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
file, _ := os.ReadFile("./quiz_example.json")
w.Write(file)
Expand All @@ -28,6 +25,8 @@ func TestQuiz(t *testing.T) {

command := NewQuizCommand(base).(*quizCommand)
command.apiURL = ts.URL
command.rand.Seed(2) // we want always the same random in our test

commands := bot.Commands{}
commands.AddCommand(command)

Expand Down
2 changes: 1 addition & 1 deletion command/pool/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func GetCommands(cfg *config.Pool, slackClient client.SlackClient) bot.Commands
return commands
}

p := GetNewPool(cfg)
p := getNewPool(cfg)

commands.AddCommand(
newPoolCommands(slackClient, cfg, p),
Expand Down
8 changes: 3 additions & 5 deletions command/pool/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@ func TestPools(t *testing.T) {
commands := GetCommands(cfg, base)
assert.Equal(t, 1, len(commands.GetCommandNames()))

runCommand := func(message msg.Message) msg.Message {
runCommand := func(message msg.Message) {
actual := commands.Run(message)
assert.True(t, actual)

return message
}

// list
message := msg.Message{}
message.Text = "pool list"
mocks.AssertSlackMessage(slackClient, message, "*Available:*\n`server1`, `server2`\n\n*Used/Locked:*")
message = runCommand(message)
runCommand(message)

// lock
message = msg.Message{}
message.Text = "pool lock server1"
mocks.AssertSlackMessageRegexp(slackClient, message, "^`server1` is locked for you until")
message = runCommand(message)
runCommand(message)

// extend
message = msg.Message{}
Expand Down
8 changes: 2 additions & 6 deletions command/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type pool struct {
mu sync.RWMutex
}

// GetNewPool create a new pool and initialize it by the local storage
func GetNewPool(cfg *config.Pool) *pool {
// getNewPool create a new pool and initialize it by the local storage
func getNewPool(cfg *config.Pool) *pool {
var p pool

p.lockDuration = cfg.LockDuration
Expand All @@ -51,10 +51,6 @@ func GetNewPool(cfg *config.Pool) *pool {
}

keys, _ := storage.GetKeys(storageKey)
if len(keys) == 0 {
return &p
}

for _, key := range keys {
var lock ResourceLock
if err := storage.Read(storageKey, key, &lock); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions command/pool/pool_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (c *poolCommands) IsEnabled() bool {
return c.config.IsEnabled()
}

// Matcher to handle commands
// GetMatcher to handle commands
func (c *poolCommands) GetMatcher() matcher.Matcher {
var resources []string
resources := make([]string, 0, len(c.config.Resources))
for _, res := range c.config.Resources {
resources = append(resources, res.Name)
}
Expand Down
7 changes: 4 additions & 3 deletions command/pullrequest/pull_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pullrequest

import (
"errors"
"fmt"
"net"
"text/template"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/innogames/slack-bot/v2/bot/util"
"github.com/innogames/slack-bot/v2/client"
"github.com/innogames/slack-bot/v2/command/queue"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
Expand Down Expand Up @@ -117,7 +117,8 @@ func (c command) watch(match matcher.Result, message msg.Message) {

// something failed while loading the PR data...retry if it was temporary, else quit watching
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
var nerr net.Error
if errors.As(err, &nerr) && nerr.Timeout() {
time.Sleep(maxCheckInterval)
continue
}
Expand All @@ -127,7 +128,7 @@ func (c command) watch(match matcher.Result, message msg.Message) {
// reply error in new thread
c.ReplyError(
message,
errors.Wrapf(err, "Error while fetching PR data %d times in a row", currentErrorCount),
fmt.Errorf("error while fetching PR data %d times in a row: %w", currentErrorCount, err),
)
c.AddReaction(c.cfg.Reactions.Error, message)
return
Expand Down
3 changes: 1 addition & 2 deletions command/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"text/template"
"time"

"github.com/innogames/slack-bot/v2/bot/util"

"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/bot/util"
)

// NewRandomCommand will reply a random entry
Expand Down
3 changes: 0 additions & 3 deletions command/random_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package command

import (
"math/rand"
"testing"

"github.com/innogames/slack-bot/v2/bot"
Expand Down Expand Up @@ -49,8 +48,6 @@ func TestRandom(t *testing.T) {
})

t.Run("pick random entry", func(t *testing.T) {
rand.Seed(1) // we want always the same random

message := msg.Message{}
message.Text = "random 4 5 6"

Expand Down
4 changes: 2 additions & 2 deletions mocks/testing.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mocks

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -160,7 +160,7 @@ func AssertContainsSlackBlocks(t *testing.T, slackClient *SlackClient, message m
expectedJSONBlock, err := json.Marshal(block)
assert.Nil(t, err)

if strings.Contains(string(givenJSON), string(expectedJSONBlock)) {
if bytes.Contains(givenJSON, expectedJSONBlock) {
// all good!
return true
}
Expand Down
Loading