Skip to content

Commit

Permalink
chore: added golangci-lint (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww authored May 5, 2023
1 parent 89128a4 commit a8b41dd
Show file tree
Hide file tree
Showing 41 changed files with 464 additions and 262 deletions.
91 changes: 51 additions & 40 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,49 @@ on:
- "**/*.md"

jobs:
scan:
runs-on: ubuntu-latest
steps:
# 代码签出
- uses: actions/checkout@v3

# 设定 Go 环境
- uses: actions/setup-go@v3
with:
go-version: "^1.20.0"
cache: true
# scan:
# runs-on: ubuntu-latest
# steps:
# # 代码签出
# - uses: actions/checkout@v3

# Get values for cache paths to be used in later steps
- name: Setup Go Cache PATH
id: go-cache-paths
run: |
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT
# # 设定 Go 环境
# - uses: actions/setup-go@v3
# with:
# go-version: "^1.20.0"
# cache: true

# Cache go build cache, used to speedup go test
- name: Go Build Cache
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}
# # Get values for cache paths to be used in later steps
# - name: Setup Go Cache PATH
# id: go-cache-paths
# run: |
# echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT
# echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT

# Cache go mod cache, used to speedup builds
- name: Go Mod Cache
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
# # Cache go build cache, used to speedup go test
# - name: Go Build Cache
# uses: actions/cache@v2
# with:
# path: ${{ steps.go-cache-paths.outputs.go-build }}
# key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}

- name: Setup govulncheck
run: go install golang.org/x/vuln/cmd/govulncheck@latest
# # Cache go mod cache, used to speedup builds
# - name: Go Mod Cache
# uses: actions/cache@v2
# with:
# path: ${{ steps.go-cache-paths.outputs.go-mod }}
# key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}

# 代码检查潜在错误
- name: Vet (Scan for potential mistakes)
run: |
go vet ./internal/...
go vet ./pkg/...
go vet ./cmd/...
# - name: Setup govulncheck
# run: go install golang.org/x/vuln/cmd/govulncheck@latest

govulncheck ./internal/...
govulncheck ./pkg/...
govulncheck ./cmd/...
# # 代码检查潜在错误
# - name: Vet (Scan for potential mistakes)
# run: |
# go vet ./...
# govulncheck ./...
buildtest:
name: Build Test
runs-on: ubuntu-latest
steps:
# 代码签出
Expand Down Expand Up @@ -93,7 +89,9 @@ jobs:
# 测试构建
- name: Test Build
run: go build -a -o "release/insights-bot" "github.com/nekomeowww/insights-bot/cmd/insights-bot"

ent_check:
name: Check entgo.io codegen
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -102,6 +100,19 @@ jobs:
go-version: "^1.20"
- uses: ent/contrib/ci@master

lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: "^1.20.0"
cache: true

- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/[email protected]

# unittest:
# # 运行目标
# runs-on: ubuntu-latest
Expand Down
57 changes: 57 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
linters:
enable:
# - contextcheck # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
- errcheck
- gosimple
# - govet # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
- ineffassign
- staticcheck
- typecheck
- unused

# - bodyclose # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
- containedctx
- depguard
- dupl
- durationcheck
- errname
- exhaustive
- exportloopref
- forcetypeassert
- godot
- gofmt
- goheader
- goprintffuncname
- gosec
- musttag
- nestif
# - nilerr # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
# - noctx # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- reassign
- revive
- tenv
- testableexamples
- unconvert
# - unparam # due to https://github.com/golangci/golangci-lint/issues/3086#issuecomment-1475232706
- usestdlibvars
- whitespace
- wsl

linters-settings:
wsl:
allow-assign-and-call: false
revive:
rules:
- name: blank-imports
disabled: true

issues:
exclude:
- "if statements should only be cuddled with assignments" # from wsl
- "if statements should only be cuddled with assignments used in the if statement itself" # from wsl
- "assignments should only be cuddled with other assignments" # from wsl. false positive case: var a bool\nb := true

16 changes: 6 additions & 10 deletions cmd/insights-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"context"
"log"
"net/http"
_ "net/http/pprof"
"time"

"go.uber.org/fx"
Expand All @@ -16,7 +14,8 @@ import (
"github.com/nekomeowww/insights-bot/internal/lib"
"github.com/nekomeowww/insights-bot/internal/models"
"github.com/nekomeowww/insights-bot/internal/services"
"github.com/nekomeowww/insights-bot/internal/services/chat_history_recap"
"github.com/nekomeowww/insights-bot/internal/services/autorecap"
"github.com/nekomeowww/insights-bot/internal/services/pprof"
"github.com/nekomeowww/insights-bot/internal/thirdparty"
)

Expand All @@ -31,19 +30,16 @@ func main() {
fx.Options(telegram.NewModules()),
fx.Options(slack.NewModules()),
fx.Invoke(telegram.Run()),
fx.Invoke(chat_history_recap.Run()),
fx.Invoke(autorecap.Run()),
fx.Invoke(slack.Run()),
fx.Invoke(func() {
err := http.ListenAndServe(":6060", nil)
if err != nil {
log.Println(err)
}
}),
fx.Invoke(pprof.Run()),
))

app.Run()

stopCtx, stopCtxCancel := context.WithTimeout(context.Background(), time.Second*15)
defer stopCtxCancel()

if err := app.Stop(stopCtx); err != nil {
log.Fatal(err)
}
Expand Down
34 changes: 17 additions & 17 deletions internal/bots/telegram/handlers/recap/recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ func NewModules() fx.Option {
}

var (
_ tgbot.CommandHandler = (*RecapCommandHandler)(nil)
_ tgbot.CommandHandler = (*CommandHandler)(nil)
)

type NewHandlersParams struct {
fx.In

RecapCommand *RecapCommandHandler
RecapCallbackQuery *RecapCallbackQueryHandler
RecapCommand *CommandHandler
RecapCallbackQuery *CallbackQueryHandler
EnableRecapCommand *EnableRecapCommandHandler
DisableRecapCommand *DisableRecapCommandHandler
}

type Handlers struct {
recapCommand *RecapCommandHandler
recapCallbackQuery *RecapCallbackQueryHandler
recapCommand *CommandHandler
recapCallbackQuery *CallbackQueryHandler
enableRecapCommand *EnableRecapCommandHandler
disableRecapCommand *DisableRecapCommandHandler
}
Expand All @@ -61,44 +61,44 @@ func (h *Handlers) Install(dispatcher *tgbot.Dispatcher) {
}

var (
RecapSelectHourAvailables = []int64{
RecapSelectHourAvailable = []int64{
1, 2, 4, 6, 12,
}
RecapSelectHourAvailableText = lo.SliceToMap(RecapSelectHourAvailables, func(item int64) (int64, string) {
RecapSelectHourAvailableText = lo.SliceToMap(RecapSelectHourAvailable, func(item int64) (int64, string) {
return item, fmt.Sprintf("%d 小时", item)
})
RecapSelectHourAvailableValues = lo.SliceToMap(RecapSelectHourAvailables, func(item int64) (int64, string) {
RecapSelectHourAvailableValues = lo.SliceToMap(RecapSelectHourAvailable, func(item int64) (int64, string) {
return item, fmt.Sprintf("%d", item)
})
)

type NewRecapCommandHandlerParams struct {
type NewCommandHandlerParams struct {
fx.In

TgChats *tgchats.Model
}

type RecapCommandHandler struct {
type CommandHandler struct {
tgchats *tgchats.Model
}

func NewRecapCommandHandler() func(NewRecapCommandHandlerParams) *RecapCommandHandler {
return func(param NewRecapCommandHandlerParams) *RecapCommandHandler {
return &RecapCommandHandler{
func NewRecapCommandHandler() func(NewCommandHandlerParams) *CommandHandler {
return func(param NewCommandHandlerParams) *CommandHandler {
return &CommandHandler{
tgchats: param.TgChats,
}
}
}

func (h RecapCommandHandler) Command() string {
func (h CommandHandler) Command() string {
return "recap"
}

func (h RecapCommandHandler) CommandHelp() string {
func (h CommandHandler) CommandHelp() string {
return "总结过去的聊天记录并生成回顾快报"
}

func (h *RecapCommandHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {
func (h *CommandHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {
enabled, err := h.tgchats.HasChatHistoriesRecapEnabled(c.Update.Message.Chat.ID, telegram.ChatType(c.Update.Message.Chat.Type))
if err != nil {
return nil, tgbot.NewExceptionError(err).WithMessage("生成失败,请稍后再试。").WithReply(c.Update.Message)
Expand All @@ -109,7 +109,7 @@ func (h *RecapCommandHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {

replyMarkupKeyboard := tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
lo.Map(RecapSelectHourAvailables, func(item int64, _ int) tgbotapi.InlineKeyboardButton {
lo.Map(RecapSelectHourAvailable, func(item int64, _ int) tgbotapi.InlineKeyboardButton {
return tgbotapi.NewInlineKeyboardButtonData(
RecapSelectHourAvailableText[item],
tgbot.NewCallbackQueryData("recap", "select_hour", url.Values{"hour": []string{RecapSelectHourAvailableValues[item]}}),
Expand Down
28 changes: 15 additions & 13 deletions internal/bots/telegram/handlers/recap/recap_callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,55 @@ import (
"github.com/samber/lo"
"go.uber.org/fx"

"github.com/nekomeowww/insights-bot/internal/models/chat_histories"
"github.com/nekomeowww/insights-bot/internal/models/chathistories"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/logger"
)

var (
_ tgbot.CallbackQueryHandler = (*RecapCallbackQueryHandler)(nil)
_ tgbot.CallbackQueryHandler = (*CallbackQueryHandler)(nil)
)

type NewRecapCallbackQueryHandlerParams struct {
fx.In

Logger *logger.Logger
ChatHistories *chat_histories.Model
ChatHistories *chathistories.Model
}

type RecapCallbackQueryHandler struct {
type CallbackQueryHandler struct {
logger *logger.Logger
chatHistories *chat_histories.Model
chatHistories *chathistories.Model
}

func NewRecapCallbackQueryHandler() func(NewRecapCallbackQueryHandlerParams) *RecapCallbackQueryHandler {
return func(param NewRecapCallbackQueryHandlerParams) *RecapCallbackQueryHandler {
return &RecapCallbackQueryHandler{
func NewRecapCallbackQueryHandler() func(NewRecapCallbackQueryHandlerParams) *CallbackQueryHandler {
return func(param NewRecapCallbackQueryHandlerParams) *CallbackQueryHandler {
return &CallbackQueryHandler{
logger: param.Logger,
chatHistories: param.ChatHistories,
}
}
}

type RecapSelectHourCallbackQueryData struct {
type SelectHourCallbackQueryData struct {
Hour int64 `schema:"hour" json:"hour"`
}

func (h RecapCallbackQueryHandler) CallbackQueryRoute() string {
func (h CallbackQueryHandler) CallbackQueryRoute() string {
return "recap/select_hour"
}

func (h *RecapCallbackQueryHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {
func (h *CallbackQueryHandler) Handle(c *tgbot.Context) (tgbot.Response, error) {
chatID := c.Update.CallbackQuery.Message.Chat.ID
messageID := c.Update.CallbackQuery.Message.MessageID

var data RecapSelectHourCallbackQueryData
var data SelectHourCallbackQueryData

err := c.CallbackQueryDataBindQuery(&data)
if err != nil {
return nil, tgbot.NewExceptionError(err)
}
if !lo.Contains(RecapSelectHourAvailables, data.Hour) {
if !lo.Contains(RecapSelectHourAvailable, data.Hour) {
return nil, tgbot.NewExceptionError(fmt.Errorf("invalid hour: %d", data.Hour)).WithReply(c.Update.CallbackQuery.Message.ReplyToMessage)
}

Expand Down Expand Up @@ -92,5 +93,6 @@ func (h *RecapCallbackQueryHandler) Handle(c *tgbot.Context) (tgbot.Response, er
}

h.logger.Infof("sending chat histories recap for chat %d: %s", chatID, summarization)

return c.NewMessageReplyTo(fmt.Sprintf("%s\n\n#recap\n<em>🤖️ Generated by chatGPT</em>", summarization), c.Update.CallbackQuery.Message.ReplyToMessage.MessageID).WithParseModeHTML(), nil
}
Loading

0 comments on commit a8b41dd

Please sign in to comment.