-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
284 lines (242 loc) · 9.28 KB
/
slack.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"bytes"
slack "ciderbot/slack"
"ciderbot/types"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
var ValidSlackCommands = map[string][2]string{
"help": {":eyes:", "Get the usage guide for App Store SlackBot"},
"app_info": {":information_source:", "Get some basic information about your app to verify you are working with the correct app"},
"beta_groups": {":test_tube:", "List all the beta groups present in TestFlight"},
"overall_status": {":convenience_store:", "Get an overall store status for your app, what builds are distributed to which channels (TestFlight and AppStore)"},
"inflight_release": {":airplane_departure:", "Get the current inflight release in the App Store"},
"live_release": {":iphone:", "Get the current live release in the App Store"},
"pause_live_release": {":double_vertical_bar:", "Pause the phased release of the current live release in the App Store"},
"resume_live_release": {":arrow_forward:", "Resume the phased release of the current live release in the App Store"},
"release_to_all": {":roller_coaster:", "Release the current live release in the App Store to all users"},
}
func handleSlackCommand(form types.SlackFormData, user *types.User) types.SlackResponse {
if !user.AppStoreBundleID.Valid {
return slack.EphemeralMessage{Msg: "No iOS app registered. Please add ASC details to use appstoreslackbot."}.Render()
}
command := strings.Split(form.Text, " ")[0]
if _, ok := ValidSlackCommands[command]; ok {
user.CommandCount += 1
db.Save(&user)
go handleValidSlackCommand(command, form.ResponseUrl, user)
return slack.EphemeralMessage{Msg: fmt.Sprintf("Got the `%s` command, working on it.", command)}.Render()
}
return slack.EphemeralMessage{Msg: "Please input a valid command. Use the `help` command to see all the valid commands."}.Render()
}
func handleValidSlackCommand(command string, responseURL string, user *types.User) {
slackResponse := processValidSlackCommand(command, user)
sendResponseToSlack(responseURL, slackResponse)
}
func processValidSlackCommand(command string, user *types.User) types.SlackResponse {
switch command {
case "help":
return handleHelpCommand(user)
case "app_info":
return handleInfoCommand(user)
case "live_release":
return handleLiveReleaseCommand(user)
case "overall_status":
return handleCurrentStatusCommand(user)
case "beta_groups":
return handleBetaGroupsCommand(user)
case "inflight_release":
return handleInflightReleaseCommand(user)
case "pause_live_release":
return handlePauseReleaseCommand(user)
case "resume_live_release":
return handleResumeReleaseCommand(user)
case "release_to_all":
return handleReleaseToAllCommand(user)
default:
return slack.EphemeralMessage{Msg: "Please input a valid command!"}.Render()
}
}
func handleHelpCommand(_user *types.User) types.SlackResponse {
return slack.HelpText{Commands: ValidSlackCommands}.Render()
}
func handleInfoCommand(user *types.User) types.SlackResponse {
appMetadata, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find an app."}.Render()
}
return slack.AppInfo{
Name: appMetadata.Name,
Sku: appMetadata.Sku,
BundleId: appMetadata.BundleId,
Id: appMetadata.Id,
}.Render()
}
func handleLiveReleaseCommand(user *types.User) types.SlackResponse {
appInfo, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find your app."}.Render()
}
liveRelease, err := getLiveRelease(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find a live release for your app."}.Render()
}
return slack.LiveRelease{
AppId: appInfo.Id,
Version: liveRelease.VersionName,
BuildNumber: liveRelease.BuildNumber,
PhasedReleaseStatus: liveRelease.PhasedRelease.CurrentDayNumber,
ReleaseStatus: liveRelease.PhasedRelease.PhasedReleaseState,
}.Render()
}
func handleCurrentStatusCommand(user *types.User) types.SlackResponse {
appCurrentStatuses, err := getAppCurrentStatus(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find an app."}.Render()
}
storeStatus := slack.CurrentStoreStatus{}
for _, channelStatus := range appCurrentStatuses {
channel := struct {
Name string `json:"name"`
Builds []struct {
Id string `json:"id"`
BuildNumber string `json:"build_number"`
Status string `json:"status"`
VersionString string `json:"version_string"`
ReleaseDate time.Time `json:"release_date"`
} `json:"builds"`
}{
Name: channelStatus.Name,
Builds: channelStatus.Builds,
}
storeStatus.Channels = append(storeStatus.Channels, channel)
}
return storeStatus.Render()
}
func handleBetaGroupsCommand(user *types.User) types.SlackResponse {
betaGroups, err := getBetaGroups(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find an app."}.Render()
}
groups := slack.BetaGroups{}
for _, betaGroup := range betaGroups {
group := struct {
Name string `json:"name"`
Internal bool `json:"internal"`
TesterCount int `json:"testers"`
}{
Name: betaGroup.Name,
Internal: betaGroup.Internal,
TesterCount: len(betaGroup.Testers),
}
groups.Groups = append(groups.Groups, group)
}
return groups.Render()
}
func handleInflightReleaseCommand(user *types.User) types.SlackResponse {
appInfo, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find your app."}.Render()
}
inflightRelease, err := getInflightRelease(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find an inflight release for your app."}.Render()
}
phasedReleaseEnabled := true
if inflightRelease.PhasedRelease.Id == "" {
phasedReleaseEnabled = false
}
return slack.InflightRelease{
VersionString: inflightRelease.VersionName,
BuildNumber: inflightRelease.BuildNumber,
StoreStatus: inflightRelease.AppStoreState,
ReleaseType: inflightRelease.ReleaseType,
PhasedRelease: phasedReleaseEnabled,
AppId: appInfo.Id,
}.Render()
}
func handlePauseReleaseCommand(user *types.User) types.SlackResponse {
appInfo, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find your app."}.Render()
}
liveRelease, err := pauseLiveRelease(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find a live release to pause."}.Render()
}
return slack.LiveRelease{
AppId: appInfo.Id,
Version: liveRelease.VersionName,
BuildNumber: liveRelease.BuildNumber,
PhasedReleaseStatus: liveRelease.PhasedRelease.CurrentDayNumber,
ReleaseStatus: liveRelease.PhasedRelease.PhasedReleaseState,
}.Render()
}
func handleResumeReleaseCommand(user *types.User) types.SlackResponse {
appInfo, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find your app."}.Render()
}
liveRelease, err := resumeLiveRelease(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find a paused release to resume."}.Render()
}
return slack.LiveRelease{
AppId: appInfo.Id,
Version: liveRelease.VersionName,
BuildNumber: liveRelease.BuildNumber,
PhasedReleaseStatus: liveRelease.PhasedRelease.CurrentDayNumber,
ReleaseStatus: liveRelease.PhasedRelease.PhasedReleaseState,
}.Render()
}
func handleReleaseToAllCommand(user *types.User) types.SlackResponse {
appInfo, err := getAppMetadata(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find your app."}.Render()
}
liveRelease, err := releaseToAll(userAppleCredentials(user))
if err != nil {
return slack.EphemeralMessage{Msg: "Could not find a live release."}.Render()
}
return slack.LiveRelease{
AppId: appInfo.Id,
Version: liveRelease.VersionName,
BuildNumber: liveRelease.BuildNumber,
PhasedReleaseStatus: liveRelease.PhasedRelease.CurrentDayNumber,
ReleaseStatus: liveRelease.PhasedRelease.PhasedReleaseState,
}.Render()
}
func sendResponseToSlack(requestURL string, slackResponse types.SlackResponse) error {
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(slackResponse)
if err != nil {
fmt.Printf("slack: could not encode the response: %s\n", err)
return err
}
req, err := http.NewRequest(http.MethodPost, requestURL, &body)
if err != nil {
fmt.Printf("slack: could not create request: %s\n", err)
return err
}
req.Header.Set("Content-Type", "application/json")
_, err = http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("slack: request failed: %s\n", err)
return err
}
fmt.Printf("slack: response was delivered!")
return nil
}
func userAppleCredentials(user *types.User) *types.AppleCredentials {
appleCredentials := types.AppleCredentials{
BundleID: user.AppStoreBundleID.String,
IssuerID: user.AppStoreIssuerID.String,
KeyID: user.AppStoreKeyID.String,
P8File: decrypt(user.AppStoreP8File, []byte(encryptionKey), user.AppStoreP8FileIV),
}
return &appleCredentials
}