Skip to content

Commit ad313c8

Browse files
authored
Update Socket Slack app upload API (#1480)
1 parent 712e327 commit ad313c8

File tree

8 files changed

+190
-141
lines changed

8 files changed

+190
-141
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ require (
5050
github.com/segmentio/analytics-go v3.1.0+incompatible
5151
github.com/sha1sum/aws_signing_client v0.0.0-20200229211254-f7815c59d5c1
5252
github.com/sirupsen/logrus v1.9.3
53-
github.com/slack-go/slack v0.12.2
53+
github.com/slack-go/slack v0.15.0
5454
github.com/sourcegraph/conc v0.3.0
5555
github.com/spf13/cobra v1.8.0
5656
github.com/spf13/pflag v1.0.5
@@ -154,7 +154,7 @@ require (
154154
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
155155
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
156156
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
157-
github.com/gorilla/websocket v1.5.0 // indirect
157+
github.com/gorilla/websocket v1.5.3 // indirect
158158
github.com/gosuri/uitable v0.0.4 // indirect
159159
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a // indirect
160160
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
645645
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
646646
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
647647
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
648-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
649-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
648+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
649+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
650650
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
651651
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
652652
github.com/graph-gophers/graphql-go v1.4.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
@@ -1068,8 +1068,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
10681068
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
10691069
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
10701070
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1071-
github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ=
1072-
github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
1071+
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
1072+
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
10731073
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE=
10741074
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
10751075
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=

helm/botkube/README.md

+120-116
Large diffs are not rendered by default.

helm/botkube/values.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ executors:
487487
# # Configures which K8s resource are displayed in resources dropdown.
488488
# resources: [ "deployments", "pods", "namespaces", "daemonsets", "statefulsets", "storageclasses", "nodes", "configmaps", "services", "ingresses", "replicasets", "secrets", "cronjobs", "jobs" ]
489489
context: *default-plugin-context
490+
botkubeExtra/helm:
491+
displayName: "Helm"
492+
enabled: true
493+
context: *default-plugin-context
490494

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

913919
# -- Configure Incoming webhook for source plugins.
914920
incomingWebhook:

pkg/bot/slack_cloud.go

+1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ func (b *CloudSlack) uploadFileToSlack(ctx context.Context, event slackMessage,
621621
ThreadTimestamp: b.resolveMessageTimestamp(resp, event),
622622
}
623623

624+
//nolint:staticcheck
624625
file, err := b.client.UploadFileContext(ctx, params)
625626
if err != nil {
626627
return nil, fmt.Errorf("while uploading file: %w", err)

pkg/bot/slack_socket.go

+51-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"strings"
99
"sync"
1010

11+
"github.com/avast/retry-go/v4"
1112
"github.com/google/uuid"
13+
"github.com/sanity-io/litter"
1214
"github.com/sirupsen/logrus"
1315
"github.com/slack-go/slack"
1416
"github.com/slack-go/slack/slackevents"
@@ -531,10 +533,10 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
531533
}
532534

533535
// Upload message as a file if too long
534-
var file *slack.File
536+
var file *slack.FileSummary
535537
var err error
536538
if len(markdown) >= slackMaxMessageSize {
537-
file, err = uploadFileToSlack(ctx, event.Channel, resp, b.client, event.ThreadTimeStamp)
539+
file, err = b.uploadFileToSlack(ctx, event.Channel, resp, event.ThreadTimeStamp)
538540
if err != nil {
539541
return err
540542
}
@@ -564,7 +566,7 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
564566
// if the message should be sent in thread, but thread is not yet started, then use the root message timestamp
565567
event.ThreadTimeStamp = event.RootMessageTimeStamp
566568
}
567-
if ts := b.getThreadOptionIfNeeded(event, file); ts != nil {
569+
if ts := b.getThreadOptionIfNeeded(ctx, event, file); ts != nil {
568570
options = append(options, ts)
569571
}
570572

@@ -707,7 +709,7 @@ func resolveBlockActionCommand(act slack.BlockAction) (string, command.Origin) {
707709
return cmd, cmdOrigin
708710
}
709711

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

720-
// If the message was already as a file attachment, reply it a given thread
721-
for _, share := range file.Shares.Public {
722-
if len(share) >= 1 && share[0].Ts != "" {
723-
return slack.MsgOptionTS(share[0].Ts)
722+
var ts string
723+
err := b.withRetry(ctx, func() error {
724+
info, _, _, err := b.client.GetFileInfoContext(ctx, file.ID, 100, 1)
725+
if err != nil {
726+
fmt.Println(err)
727+
return err
728+
}
729+
730+
for _, share := range info.Shares.Public {
731+
if len(share) >= 1 && share[0].Ts != "" {
732+
ts = share[0].Ts
733+
return nil
734+
}
724735
}
736+
737+
litter.Dump(info)
738+
return fmt.Errorf("file was not yet processed")
739+
})
740+
if err != nil {
741+
b.log.WithError(err).Error("Cannot get file share info. Sending message without thread option.")
742+
return nil
743+
}
744+
745+
if ts != "" {
746+
return slack.MsgOptionTS(ts)
747+
}
748+
749+
return nil
750+
}
751+
752+
func (b *SocketSlack) withRetry(ctx context.Context, fn func() error) error {
753+
err := retry.Do(
754+
fn,
755+
retry.Attempts(maxRetries),
756+
retry.LastErrorOnly(true),
757+
retry.Context(ctx),
758+
)
759+
if err != nil {
760+
return fmt.Errorf("while retrying: %w", err)
725761
}
726762

727763
return nil
@@ -766,17 +802,19 @@ func (b *SocketSlack) GetStatus() health.PlatformStatus {
766802
}
767803
}
768804

769-
func uploadFileToSlack(ctx context.Context, channel string, resp interactive.CoreMessage, client *slack.Client, ts string) (*slack.File, error) {
770-
params := slack.FileUploadParameters{
805+
func (b *SocketSlack) uploadFileToSlack(ctx context.Context, channel string, resp interactive.CoreMessage, ts string) (*slack.FileSummary, error) {
806+
content := interactive.MessageToPlaintext(resp, interactive.NewlineFormatter)
807+
params := slack.UploadFileV2Parameters{
771808
Filename: "Response.txt",
772809
Title: "Response.txt",
773810
InitialComment: resp.Description,
774-
Content: interactive.MessageToPlaintext(resp, interactive.NewlineFormatter),
775-
Channels: []string{channel},
811+
Content: content,
812+
FileSize: len(content),
813+
Channel: channel,
776814
ThreadTimestamp: ts,
777815
}
778816

779-
file, err := client.UploadFileContext(ctx, params)
817+
file, err := b.client.UploadFileV2Context(ctx, params)
780818
if err != nil {
781819
return nil, fmt.Errorf("while uploading file: %w", err)
782820
}

test/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/pkg/errors v0.9.1
2323
github.com/pmezard/go-difflib v1.0.0
2424
github.com/sanity-io/litter v1.5.5
25-
github.com/slack-go/slack v0.12.3
25+
github.com/slack-go/slack v0.15.0
2626
github.com/stretchr/testify v1.9.0
2727
github.com/vrischmann/envconfig v1.3.0
2828
golang.org/x/oauth2 v0.16.0
@@ -105,7 +105,7 @@ require (
105105
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
106106
github.com/gookit/color v1.5.2 // indirect
107107
github.com/gorilla/mux v1.8.0 // indirect
108-
github.com/gorilla/websocket v1.5.1 // indirect
108+
github.com/gorilla/websocket v1.5.3 // indirect
109109
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a // indirect
110110
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
111111
github.com/hashicorp/errwrap v1.1.0 // indirect

test/go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
687687
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
688688
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
689689
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
690-
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
691-
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
690+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
691+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
692692
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a h1:i0+Se9S+2zL5CBxJouqn2Ej6UQMwH1c57ZB6DVnqck4=
693693
github.com/graph-gophers/graphql-go v1.5.1-0.20230110080634-edea822f558a/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
694694
github.com/graph-gophers/graphql-transport-ws v0.0.2 h1:DbmSkbIGzj8SvHei6n8Mh9eLQin8PtA8xY9eCzjRpvo=
@@ -1161,8 +1161,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
11611161
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
11621162
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
11631163
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1164-
github.com/slack-go/slack v0.12.3 h1:92/dfFU8Q5XP6Wp5rr5/T5JHLM5c5Smtn53fhToAP88=
1165-
github.com/slack-go/slack v0.12.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
1164+
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
1165+
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
11661166
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
11671167
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
11681168
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=

0 commit comments

Comments
 (0)