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

Update Socket Slack app upload API #1480

Merged
merged 1 commit into from
Nov 11, 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/sha1sum/aws_signing_client v0.0.0-20200229211254-f7815c59d5c1
github.com/sirupsen/logrus v1.9.3
github.com/slack-go/slack v0.12.2
github.com/slack-go/slack v0.15.0
github.com/sourcegraph/conc v0.3.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -154,7 +154,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/graph-gophers/graphql-go v1.4.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
Expand Down Expand Up @@ -1068,8 +1068,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ=
github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
Expand Down
236 changes: 120 additions & 116 deletions helm/botkube/README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,10 @@ executors:
# # Configures which K8s resource are displayed in resources dropdown.
# resources: [ "deployments", "pods", "namespaces", "daemonsets", "statefulsets", "storageclasses", "nodes", "configmaps", "services", "ingresses", "replicasets", "secrets", "cronjobs", "jobs" ]
context: *default-plugin-context
botkubeExtra/helm:
displayName: "Helm"
enabled: true
context: *default-plugin-context

# -- Custom aliases for given commands.
# The aliases are replaced with the underlying command before executing it.
Expand Down Expand Up @@ -909,6 +913,8 @@ plugins:
botkube:
url: https://storage.googleapis.com/botkube-plugins-latest/plugins-index.yaml
# headers: {} # optional headers for plugins repository.
botkubeExtra:
url: https://github.com/kubeshop/botkube-plugins/releases/download/v1.14.0/plugins-index.yaml

# -- Configure Incoming webhook for source plugins.
incomingWebhook:
Expand Down
1 change: 1 addition & 0 deletions pkg/bot/slack_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ func (b *CloudSlack) uploadFileToSlack(ctx context.Context, event slackMessage,
ThreadTimestamp: b.resolveMessageTimestamp(resp, event),
}

//nolint:staticcheck
file, err := b.client.UploadFileContext(ctx, params)
if err != nil {
return nil, fmt.Errorf("while uploading file: %w", err)
Expand Down
64 changes: 51 additions & 13 deletions pkg/bot/slack_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"strings"
"sync"

"github.com/avast/retry-go/v4"
"github.com/google/uuid"
"github.com/sanity-io/litter"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
Expand Down Expand Up @@ -531,10 +533,10 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
}

// Upload message as a file if too long
var file *slack.File
var file *slack.FileSummary
var err error
if len(markdown) >= slackMaxMessageSize {
file, err = uploadFileToSlack(ctx, event.Channel, resp, b.client, event.ThreadTimeStamp)
file, err = b.uploadFileToSlack(ctx, event.Channel, resp, event.ThreadTimeStamp)
if err != nil {
return err
}
Expand Down Expand Up @@ -564,7 +566,7 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
// if the message should be sent in thread, but thread is not yet started, then use the root message timestamp
event.ThreadTimeStamp = event.RootMessageTimeStamp
}
if ts := b.getThreadOptionIfNeeded(event, file); ts != nil {
if ts := b.getThreadOptionIfNeeded(ctx, event, file); ts != nil {
options = append(options, ts)
}

Expand Down Expand Up @@ -707,7 +709,7 @@ func resolveBlockActionCommand(act slack.BlockAction) (string, command.Origin) {
return cmd, cmdOrigin
}

func (b *SocketSlack) getThreadOptionIfNeeded(event slackMessage, file *slack.File) slack.MsgOption {
func (b *SocketSlack) getThreadOptionIfNeeded(ctx context.Context, event slackMessage, file *slack.FileSummary) slack.MsgOption {
//if the message is from thread then add an option to return the response to the thread
if event.ThreadTimeStamp != "" {
return slack.MsgOptionTS(event.ThreadTimeStamp)
Expand All @@ -717,11 +719,45 @@ func (b *SocketSlack) getThreadOptionIfNeeded(event slackMessage, file *slack.Fi
return nil
}

// If the message was already as a file attachment, reply it a given thread
for _, share := range file.Shares.Public {
if len(share) >= 1 && share[0].Ts != "" {
return slack.MsgOptionTS(share[0].Ts)
var ts string
err := b.withRetry(ctx, func() error {
info, _, _, err := b.client.GetFileInfoContext(ctx, file.ID, 100, 1)
if err != nil {
fmt.Println(err)
return err
}

for _, share := range info.Shares.Public {
if len(share) >= 1 && share[0].Ts != "" {
ts = share[0].Ts
return nil
}
}

litter.Dump(info)
return fmt.Errorf("file was not yet processed")
})
if err != nil {
b.log.WithError(err).Error("Cannot get file share info. Sending message without thread option.")
return nil
}

if ts != "" {
return slack.MsgOptionTS(ts)
}

return nil
}

func (b *SocketSlack) withRetry(ctx context.Context, fn func() error) error {
err := retry.Do(
fn,
retry.Attempts(maxRetries),
retry.LastErrorOnly(true),
retry.Context(ctx),
)
if err != nil {
return fmt.Errorf("while retrying: %w", err)
}

return nil
Expand Down Expand Up @@ -766,17 +802,19 @@ func (b *SocketSlack) GetStatus() health.PlatformStatus {
}
}

func uploadFileToSlack(ctx context.Context, channel string, resp interactive.CoreMessage, client *slack.Client, ts string) (*slack.File, error) {
params := slack.FileUploadParameters{
func (b *SocketSlack) uploadFileToSlack(ctx context.Context, channel string, resp interactive.CoreMessage, ts string) (*slack.FileSummary, error) {
content := interactive.MessageToPlaintext(resp, interactive.NewlineFormatter)
params := slack.UploadFileV2Parameters{
Filename: "Response.txt",
Title: "Response.txt",
InitialComment: resp.Description,
Content: interactive.MessageToPlaintext(resp, interactive.NewlineFormatter),
Channels: []string{channel},
Content: content,
FileSize: len(content),
Channel: channel,
ThreadTimestamp: ts,
}

file, err := client.UploadFileContext(ctx, params)
file, err := b.client.UploadFileV2Context(ctx, params)
if err != nil {
return nil, fmt.Errorf("while uploading file: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/sanity-io/litter v1.5.5
github.com/slack-go/slack v0.12.3
github.com/slack-go/slack v0.15.0
github.com/stretchr/testify v1.9.0
github.com/vrischmann/envconfig v1.3.0
golang.org/x/oauth2 v0.16.0
Expand Down Expand Up @@ -105,7 +105,7 @@ require (
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a h1:i0+Se9S+2zL5CBxJouqn2Ej6UQMwH1c57ZB6DVnqck4=
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo=
Expand Down Expand Up @@ -1161,8 +1161,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/slack-go/slack v0.12.3 h1:92/dfFU8Q5XP6Wp5rr5/T5JHLM5c5Smtn53fhToAP88=
github.com/slack-go/slack v0.12.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down
Loading