This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlock.go
More file actions
327 lines (276 loc) · 9.04 KB
/
lock.go
File metadata and controls
327 lines (276 loc) · 9.04 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package slack
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/nleeper/goment"
"github.com/slack-go/slack"
"go.uber.org/zap"
"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/ent/callback"
"github.com/gitploy-io/gitploy/ent/perm"
"github.com/gitploy-io/gitploy/vo"
)
type (
lockViewSubmission struct {
Env string
}
)
func (s *Slack) handleLockCmd(c *gin.Context) {
ctx := c.Request.Context()
av, _ := c.Get(KeyCmd)
cmd := av.(slack.SlashCommand)
bv, _ := c.Get(KeyChatUser)
cu := bv.(*ent.ChatUser)
s.log.Debug("Processing lock command.", zap.String("command", cmd.Text))
ns, n := parseCmd(cmd.Text)
r, err := s.i.FindRepoOfUserByNamespaceName(ctx, cu.Edges.User, ns, n)
if ent.IsNotFound(err) {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, fmt.Sprintf("The `%s/%s` repository is not found.", ns, n))
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to get the repo.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
// Validate the perm for the repo.
if p, err := s.i.FindPermOfRepo(ctx, r, cu.Edges.User); !(p.RepoPerm == perm.RepoPermWrite || p.RepoPerm == perm.RepoPermAdmin) {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, "Write perm is required to lock the environment.")
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to get the perm.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
// Build the modal with unlocked envs.
config, err := s.i.GetConfig(ctx, cu.Edges.User, r)
if vo.IsConfigNotFoundError(err) || vo.IsConfigParseError(err) {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, "The config is invalid.")
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to get the config file.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
locks, err := s.i.ListLocksOfRepo(ctx, r)
if err != nil {
s.log.Error("It has failed to list locks.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
cb, err := s.i.CreateCallback(ctx, &ent.Callback{
Type: callback.TypeLock,
RepoID: r.ID,
})
if err != nil {
s.log.Error("It has failed to create a new callback.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
_, err = slack.New(cu.BotToken).
OpenViewContext(ctx, cmd.TriggerID, buildLockView(cb.Hash, config, locks))
if err != nil {
s.log.Error("It has failed to open a new view.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
}
func buildLockView(callbackID string, c *vo.Config, locks []*ent.Lock) slack.ModalViewRequest {
hasLocked := func(env string) bool {
for _, lock := range locks {
if lock.Env == env {
return true
}
}
return false
}
envs := []*slack.OptionBlockObject{}
for _, env := range c.Envs {
if hasLocked(env.Name) {
continue
}
envs = append(envs, &slack.OptionBlockObject{
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: env.Name,
},
Value: env.Name,
})
}
return slack.ModalViewRequest{
Type: slack.VTModal,
CallbackID: callbackID,
Title: slack.NewTextBlockObject(slack.PlainTextType, "Lock", false, false),
Submit: slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false),
Close: slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false),
Blocks: slack.Blocks{
BlockSet: []slack.Block{
slack.NewInputBlock(
blockEnv,
slack.NewTextBlockObject(slack.PlainTextType, "Environment", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select the environment", false, false),
actionEnv,
envs...,
),
),
},
},
}
}
func (s *Slack) handleUnlockCmd(c *gin.Context) {
ctx := c.Request.Context()
av, _ := c.Get(KeyCmd)
cmd := av.(slack.SlashCommand)
bv, _ := c.Get(KeyChatUser)
cu := bv.(*ent.ChatUser)
s.log.Debug("Processing lock command.", zap.String("command", cmd.Text))
ns, n := parseCmd(cmd.Text)
r, err := s.i.FindRepoOfUserByNamespaceName(ctx, cu.Edges.User, ns, n)
if ent.IsNotFound(err) {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, fmt.Sprintf("The `%s/%s` repository is not found.", ns, n))
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to get the repo.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
// Validate the perm for the repo.
if p, err := s.i.FindPermOfRepo(ctx, r, cu.Edges.User); !(p.RepoPerm == perm.RepoPermWrite || p.RepoPerm == perm.RepoPermAdmin) {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, "Write perm is required to lock the environment.")
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to get the perm.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
// Build the modal with unlocked envs.
locks, err := s.i.ListLocksOfRepo(ctx, r)
if len(locks) == 0 {
postResponseMessage(cmd.ChannelID, cmd.ResponseURL, fmt.Sprintf("There is no locked environments in the `%s/%s` repository.", ns, n))
c.Status(http.StatusOK)
return
} else if err != nil {
s.log.Error("It has failed to list locks.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
cb, err := s.i.CreateCallback(ctx, &ent.Callback{
Type: callback.TypeUnlock,
RepoID: r.ID,
})
if err != nil {
s.log.Error("It has failed to create a new callback.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
_, err = slack.New(cu.BotToken).
OpenViewContext(ctx, cmd.TriggerID, buildUnlockView(cb.Hash, locks))
if err != nil {
s.log.Error("It has failed to open a new view.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
}
func buildUnlockView(callbackID string, locks []*ent.Lock) slack.ModalViewRequest {
envs := []*slack.OptionBlockObject{}
for _, lock := range locks {
var txt string
if lock.Edges.User != nil {
ca, _ := goment.New(lock.CreatedAt)
txt = fmt.Sprintf("%s - Locked by %s %s", lock.Env, lock.Edges.User.Login, ca.FromNow())
} else {
ca, _ := goment.New(lock.CreatedAt)
txt = fmt.Sprintf("%s - Locked %s", lock.Env, ca.FromNow())
}
envs = append(envs, &slack.OptionBlockObject{
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: txt,
},
Value: lock.Env,
})
}
return slack.ModalViewRequest{
Type: slack.VTModal,
CallbackID: callbackID,
Title: slack.NewTextBlockObject(slack.PlainTextType, "Unlock", false, false),
Submit: slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false),
Close: slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false),
Blocks: slack.Blocks{
BlockSet: []slack.Block{
slack.NewInputBlock(
blockEnv,
slack.NewTextBlockObject(slack.PlainTextType, "Environment", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select the environment", false, false),
actionEnv,
envs...,
),
),
},
},
}
}
func (s *Slack) interactLock(c *gin.Context) {
ctx := c.Request.Context()
iv, _ := c.Get(KeyIntr)
itr := iv.(slack.InteractionCallback)
cv, _ := c.Get(KeyChatUser)
cu := cv.(*ent.ChatUser)
cb, _ := s.i.FindCallbackByHash(ctx, itr.View.CallbackID)
sm := parseLockViewSubmissions(itr)
if _, err := s.i.CreateLock(ctx, &ent.Lock{
Env: sm.Env,
UserID: cu.Edges.User.ID,
RepoID: cb.Edges.Repo.ID,
}); err != nil {
s.log.Error("It has failed to lock the environment.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
postBotMessage(cu, fmt.Sprintf("Success to lock the `%s` environment of the `%s` repository.", sm.Env, cb.Edges.Repo.GetFullName()))
c.Status(http.StatusOK)
}
func (s *Slack) interactUnlock(c *gin.Context) {
ctx := c.Request.Context()
iv, _ := c.Get(KeyIntr)
itr := iv.(slack.InteractionCallback)
cv, _ := c.Get(KeyChatUser)
cu := cv.(*ent.ChatUser)
cb, _ := s.i.FindCallbackByHash(ctx, itr.View.CallbackID)
sm := parseLockViewSubmissions(itr)
lock, err := s.i.FindLockOfRepoByEnv(ctx, cb.Edges.Repo, sm.Env)
if ent.IsNotFound(err) {
postBotMessage(cu, fmt.Sprintf("The `%s` environment is not locked.", sm.Env))
c.Status(http.StatusOK)
} else if err != nil {
s.log.Error("It has failed to find the lock.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
if err := s.i.DeleteLock(ctx, lock); err != nil {
s.log.Error("It has failed to unlock the environment.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
postBotMessage(cu, fmt.Sprintf("Success to unlock the `%s` environment of the `%s` repository.", sm.Env, cb.Edges.Repo.GetFullName()))
c.Status(http.StatusOK)
}
func parseLockViewSubmissions(itr slack.InteractionCallback) *lockViewSubmission {
sm := &lockViewSubmission{}
values := itr.View.State.Values
if v, ok := values[blockEnv][actionEnv]; ok {
sm.Env = v.SelectedOption.Value
}
return sm
}