-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslack.go
More file actions
178 lines (150 loc) · 4.77 KB
/
slack.go
File metadata and controls
178 lines (150 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
)
const SlackTriggerTechAllyProject = "trigger_tech_ally_project"
func connectToSlackViaSocketmode() (*socketmode.Client, *slack.Client, error) {
appToken := os.Getenv("SLACK_APP_TOKEN")
if appToken == "" {
return nil, nil, errors.New("SLACK_APP_TOKEN must be set")
}
if !strings.HasPrefix(appToken, "xapp-") {
return nil, nil, errors.New("SLACK_APP_TOKEN must have the prefix \"xapp-\".")
}
botToken := os.Getenv("SLACK_BOT_TOKEN")
if botToken == "" {
return nil, nil, errors.New("SLACK_BOT_TOKEN must be set.")
}
if !strings.HasPrefix(botToken, "xoxb-") {
return nil, nil, errors.New("SLACK_BOT_TOKEN must have the prefix \"xoxb-\".")
}
api := slack.New(
botToken,
slack.OptionDebug(debug()),
slack.OptionAppLevelToken(appToken),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
)
client := socketmode.New(
api,
socketmode.OptionDebug(debug()),
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
return client, api, nil
}
func listenToSlackEvents(client *socketmode.Client, api *slack.Client, config *c) {
for evt := range client.Events {
logger.Debugw("raw received", "type", evt.Type, "raw", evt)
switch evt.Type {
case socketmode.EventTypeSlashCommand:
cmd, ok := evt.Data.(slack.SlashCommand)
if !ok {
logger.Infow("event ignored", "type", evt.Type)
continue
}
logger.Infow("event received",
"type", evt.Type, "username", cmd.UserName,
"command", cmd.Command, "channel_name", cmd.ChannelName)
notifySlackChannel(api,
config.NotifySlackChannel,
fmt.Sprintf("User %s is preparing a release via `/release`", cmd.UserName),
)
client.Ack(*evt.Request, renderSlackCommandPayload(config))
case socketmode.EventTypeInteractive:
callback, ok := evt.Data.(slack.InteractionCallback)
if !ok {
logger.Infow("event ignored", "type", evt.Type)
continue
}
logger.Infow("event received",
"type", evt.Type, "response_url", callback.ResponseURL,
"value", callback.Value, "channel_name", callback.Channel.Name)
switch callback.Type {
case slack.InteractionTypeBlockActions:
go func() {
if err := runCodefreshPipeline(api, config, callback); err != nil {
logger.Errorw("unable to run codefresh pipeline",
"error", err, "raw", callback)
}
}()
default:
notifySlackChannel(api,
config.NotifySlackChannel,
fmt.Sprintf("Some weird type just showed up: *%s*", callback.Type),
)
}
var payload interface{}
client.Ack(*evt.Request, payload)
default:
logger.Warnw("unexpected event type received", "type", evt.Type, "raw", evt)
}
}
}
// createOptionBlockObjects - utility function for generating option block objects
func createOptionBlockObjects(options []string) []*slack.OptionBlockObject {
optionBlockObjects := make([]*slack.OptionBlockObject, 0, len(options))
for _, str := range options {
optionText := slack.NewTextBlockObject(slack.PlainTextType, str, false, false)
optionBlockObjects = append(optionBlockObjects, slack.NewOptionBlockObject(str, optionText, nil))
}
return optionBlockObjects
}
// Update message to Slack wrapper that log errors
func updateSlackMessage(api *slack.Client, channel string, timestamp string, options ...slack.MsgOption) {
_, _, _, err := api.UpdateMessage(channel, timestamp, options...)
if err != nil {
logger.Errorw("unable to update message to slack channel",
"channel", channel,
"error", err,
)
}
}
// Post message to Slack wrapper that log errors
func postSlackMessage(api *slack.Client, channel string, options ...slack.MsgOption) string {
_, timestamp, err := api.PostMessage(channel, options...)
if err != nil {
logger.Errorw("unable to post message to slack channel",
"channel", channel,
"error", err,
)
}
return timestamp
}
// Notify To Slack
func notifySlackChannel(api *slack.Client, channel, msg string) {
_, _, err := api.PostMessage(channel, slack.MsgOptionText(msg, false))
if err != nil {
logger.Errorw("unable to post message to slack channel",
"channel", channel,
"error", err,
)
}
}
func renderSlackCommandPayload(config *c) map[string]interface{} {
return map[string]interface{}{
"blocks": []slack.Block{
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: ":waving: Select the project to release",
},
nil,
slack.NewAccessory(
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
&slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "tech-ally projects",
},
SlackTriggerTechAllyProject,
createOptionBlockObjects(config.ListProjects())...,
),
),
),
}}
}