Skip to content

Commit 9bd3fb1

Browse files
authored
run go fix ./... (#836)
1 parent d08051b commit 9bd3fb1

File tree

35 files changed

+173
-160
lines changed

35 files changed

+173
-160
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ linters:
5858
- linters:
5959
- tagalign
6060
path: bot/config/
61+
- linters:
62+
- gochecknoinits
63+
path: plugins/
6164
paths:
6265
- third_party$
6366
- builtin$

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This is a **Slack Bot** written in **Go** that improves development team workflows with integrations for Jenkins, GitHub, GitLab, and Jira. The bot supports custom commands, macros, cron jobs, and flexible project-specific functionality.
66

7-
- **Language**: Go (requires Go 1.24+)
7+
- **Language**: Go (requires Go 1.25+)
88
- **Type**: Slack application with Socket Mode support
99
- **Size**: ~50+ Go packages across bot/, command/, client/, and cmd/ directories
1010
- **Architecture**: Modular command-based bot with plugin system
@@ -13,7 +13,7 @@ This is a **Slack Bot** written in **Go** that improves development team workflo
1313
## Build and Validation Instructions
1414

1515
### Prerequisites
16-
- **Go 1.24 or later**
16+
- **Go 1.25 or later**
1717
- **Make** (for build targets)
1818
- **Docker** (optional, for containerized builds)
1919
- **golangci-lint** (for linting)

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dep:
3131
@go mod vendor
3232

3333
lint:
34+
go fix ./...
3435
golangci-lint run --fix
3536

3637
docker-build:

bot/bot_handle.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,16 @@ func (b *Bot) ProcessMessage(message msg.Message, fromUserContext bool) {
101101

102102
logger.Errorf("user %s (%s) is not allowed to execute message (authentication is active and user is missing in 'allowed_users' section): %s", userName, message.User, message.Text)
103103

104-
errorMessage := "Sorry, you are not whitelisted in the config yet."
104+
var errorMessage strings.Builder
105+
errorMessage.WriteString("Sorry, you are not whitelisted in the config yet.")
105106
if len(b.config.AdminUsers) > 0 {
106-
errorMessage += " Please ask a slack-bot admin to get access: "
107+
errorMessage.WriteString(" Please ask a slack-bot admin to get access: ")
107108
for _, admin := range b.config.AdminUsers {
108109
adminID, _ := client.GetUserIDAndName(admin)
109-
errorMessage += fmt.Sprintf(
110-
" <@%s>",
111-
adminID,
112-
)
110+
fmt.Fprintf(&errorMessage, " <@%s>", adminID)
113111
}
114112
}
115-
b.slackClient.SendEphemeralMessage(message, errorMessage)
113+
b.slackClient.SendEphemeralMessage(message, errorMessage.String())
116114
b.slackClient.AddReaction("❌", message)
117115

118116
stats.IncreaseOne(stats.UnauthorizedCommands)

bot/config/config.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package config
44

55
import (
6+
"slices"
7+
68
"github.com/brainexe/viper"
79
)
810

@@ -129,13 +131,7 @@ type UserList []string
129131

130132
// Contains checks if the given user is in the UserList
131133
func (l UserList) Contains(givenUserID string) bool {
132-
for _, userID := range l {
133-
if userID == givenUserID {
134-
return true
135-
}
136-
}
137-
138-
return false
134+
return slices.Contains(l, givenUserID)
139135
}
140136

141137
// UserMap indexed by user id, value is the user name

bot/interaction.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (b *Bot) handleEvent(eventsAPIEvent slackevents.EventsAPIEvent) {
6060
}
6161

6262
func (b *Bot) loadFileContent(event *slackevents.MessageEvent) string {
63-
response := ""
63+
var response strings.Builder
6464

6565
for _, file := range event.Message.Files {
6666
if !strings.HasPrefix(file.Mimetype, "text/") {
@@ -77,10 +77,10 @@ func (b *Bot) loadFileContent(event *slackevents.MessageEvent) string {
7777
continue
7878
}
7979

80-
response += "\n" + downloadedText.String()
80+
response.WriteString("\n" + downloadedText.String())
8181
}
8282

83-
return response
83+
return response.String()
8484
}
8585

8686
func (b *Bot) handleInteraction(payload slack.InteractionCallback) bool {
@@ -91,7 +91,7 @@ func (b *Bot) handleInteraction(payload slack.InteractionCallback) bool {
9191
stats.IncreaseOne(stats.Interactions)
9292

9393
switch payload.Type {
94-
case "block_actions":
94+
case slack.InteractionTypeBlockActions:
9595
// user clicked on one of our interactive buttons
9696
action := payload.ActionCallback.BlockActions[0]
9797
command := action.Value
@@ -133,10 +133,25 @@ func (b *Bot) handleInteraction(payload slack.InteractionCallback) bool {
133133

134134
// execute the command which is stored for this interaction
135135
go b.ProcessMessage(ref.WithText(command), true)
136-
case "message_action":
136+
case slack.InteractionTypeMessageAction:
137137
// todo implement interactive slack messages (right click menu)
138138
log.Warnf("Received unhandled message action: %+v", payload)
139139
return true
140+
case slack.InteractionTypeDialogCancellation,
141+
slack.InteractionTypeDialogSubmission,
142+
slack.InteractionTypeDialogSuggestion,
143+
slack.InteractionTypeInteractionMessage,
144+
slack.InteractionTypeBlockSuggestion,
145+
slack.InteractionTypeViewSubmission,
146+
slack.InteractionTypeViewClosed,
147+
slack.InteractionTypeShortcut,
148+
slack.InteractionTypeWorkflowStepEdit:
149+
// These interaction types are not currently handled by this bot
150+
log.Infof("Received unhandled interaction type: %s", payload.Type)
151+
return false
152+
default:
153+
log.Warnf("Received unknown interaction type: %s", payload.Type)
154+
return false
140155
}
141156

142157
return true

bot/tester/tester.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"strings"
1011

1112
"github.com/gookit/color"
1213
"github.com/innogames/slack-bot/v2/bot"
@@ -80,32 +81,40 @@ func messageHandler(w http.ResponseWriter, r *http.Request, output io.Writer) {
8081
var blocks []map[string]any
8182
_ = json.Unmarshal([]byte(blockJSON), &blocks)
8283

84+
var blockText strings.Builder
8385
for _, block := range blocks {
84-
text += formatBlock(block) + "\n"
86+
blockText.WriteString(formatBlock(block) + "\n")
8587
}
88+
text += blockText.String()
8689
} else if text == "" && query.Get("attachments") != "" {
8790
attachmentJSON := query.Get("attachments")
8891
var attachments []map[string]any
8992
_ = json.Unmarshal([]byte(attachmentJSON), &attachments)
9093

94+
var attachmentTitles strings.Builder
95+
var attachmentActions strings.Builder
9196
for _, attachment := range attachments {
9297
if txt, ok := attachment["title"].(string); ok {
93-
text += txt + "\n"
98+
attachmentTitles.WriteString(txt + "\n")
9499
}
100+
var actionText strings.Builder
95101
for _, action := range attachment["actions"].([]any) {
96102
actionMap := action.(map[string]any)
97103

98104
if actionMap["type"] == "button" {
99-
text += fmt.Sprintf(
105+
fmt.Fprintf(&actionText,
100106
"<%s|%s>\n",
101107
actionMap["url"],
102108
actionMap["text"],
103109
)
104110
} else {
105-
text += fmt.Sprintf("Attachment-actions are not supported yet:\n%v\n", action)
111+
fmt.Fprintf(&actionText, "Attachment-actions are not supported yet:\n%v\n", action)
106112
}
107113
}
114+
attachmentActions.WriteString(actionText.String())
108115
}
116+
text += attachmentActions.String()
117+
text += attachmentTitles.String()
109118
}
110119

111120
_, _ = fmt.Fprint(output, formatSlackMessage(text)+"\n")
@@ -139,11 +148,11 @@ func formatBlock(block map[string]any) string {
139148
// bit hacky way to extract the text from some kind of block element
140149
func extractText(block map[string]any) string {
141150
if fields, ok := block["fields"]; ok {
142-
result := ""
151+
var result strings.Builder
143152
for _, field := range fields.([]any) {
144-
result += extractText(field.(map[string]any))
153+
result.WriteString(extractText(field.(map[string]any)))
145154
}
146-
return result
155+
return result.String()
147156
}
148157

149158
// contains "text" element

bot/util/template.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"maps"
78
"text/template"
89
"time"
910
)
@@ -60,9 +61,7 @@ func GetTemplateFunctions() template.FuncMap {
6061

6162
// RegisterFunctions will add a function to any template renderer
6263
func RegisterFunctions(funcMap template.FuncMap) {
63-
for name, function := range funcMap {
64-
functions[name] = function
65-
}
64+
maps.Copy(functions, funcMap)
6665
}
6766

6867
// CompileTemplate pre compiles a template and returns an error if an function is not available etc

cmd/bot/pprof.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build pprof
2-
// +build pprof
32

43
package main
54

command/admin/bot_log.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ func (c *botLogCommand) readFile(filename string, chars int64) []byte {
7070
defer file.Close()
7171

7272
stat, _ := os.Stat(filename)
73-
start := stat.Size() - chars
74-
if start < 0 {
75-
start = 0
76-
}
73+
start := max(stat.Size()-chars, 0)
7774

7875
_, err = file.ReadAt(buf, start)
7976
if err != nil && err.Error() != "EOF" {

0 commit comments

Comments
 (0)