Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Fix not to display the deployer for the approvers select in Slack #151

Merged
merged 3 commits into from
Oct 7, 2021
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
142 changes: 86 additions & 56 deletions internal/server/slack/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (s *Slack) handleDeployCmd(c *gin.Context) {
return
}

perms = s.filterPerms(perms, cu)

// Create a new callback to interact with submissions.
cb, err := s.i.CreateCallback(ctx, &ent.Callback{
Type: callback.TypeDeploy,
Expand Down Expand Up @@ -122,6 +124,29 @@ func parseCmd(cmd string) (string, string) {
return nn[0], nn[1]
}

// filterPerms returns permissions except the deployer.
func (s *Slack) filterPerms(perms []*ent.Perm, cu *ent.ChatUser) []*ent.Perm {
ret := []*ent.Perm{}

for _, p := range perms {
if p.Edges.User == nil {
s.log.Warn("The user edge of perm is not found.", zap.Int("perm_id", p.ID))
continue
}

if cu.Edges.User == nil {
s.log.Warn("The user edge of chat-user is not found.", zap.String("chat_user_id", cu.ID))
continue
}

if p.Edges.User.ID != cu.Edges.User.ID {
ret = append(ret, p)
}
}

return ret
}

func buildDeployView(callbackID string, c *vo.Config, perms []*ent.Perm) slack.ModalViewRequest {
envs := []*slack.OptionBlockObject{}
for _, env := range c.Envs {
Expand Down Expand Up @@ -150,69 +175,74 @@ func buildDeployView(callbackID string, c *vo.Config, perms []*ent.Perm) slack.M
})
}

set := []slack.Block{
slack.NewInputBlock(
blockEnv,
slack.NewTextBlockObject(slack.PlainTextType, "Environment", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select target environment", false, false),
actionEnv,
envs...,
),
),
slack.NewInputBlock(
blockType,
slack.NewTextBlockObject(slack.PlainTextType, "Type", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select your ref type", false, false),
actionType,
slack.NewOptionBlockObject(
"commit",
slack.NewTextBlockObject(slack.PlainTextType, "Commit", false, false),
nil,
),
slack.NewOptionBlockObject(
"branch",
slack.NewTextBlockObject(slack.PlainTextType, "Branch", false, false),
nil,
),
slack.NewOptionBlockObject(
"tag",
slack.NewTextBlockObject(slack.PlainTextType, "Tag", false, false),
nil,
),
),
),
slack.NewInputBlock(
blockRef,
slack.NewTextBlockObject(slack.PlainTextType, "Ref", false, false),
slack.NewPlainTextInputBlockElement(
slack.NewTextBlockObject(slack.PlainTextType, "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2", false, false),
actionRef,
),
),
}

if len(approvers) > 0 {
set = append(set, slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockApprovers,
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
Optional: true,
Element: slack.NewOptionsSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
actionApprovers,
approvers...,
),
})
}

return slack.ModalViewRequest{
Type: slack.VTModal,
CallbackID: callbackID,
Title: slack.NewTextBlockObject(slack.PlainTextType, "Deploy", 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 target environment", false, false),
actionEnv,
envs...,
),
),
slack.NewInputBlock(
blockType,
slack.NewTextBlockObject(slack.PlainTextType, "Type", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select your ref type", false, false),
actionType,
slack.NewOptionBlockObject(
"commit",
slack.NewTextBlockObject(slack.PlainTextType, "Commit", false, false),
nil,
),
slack.NewOptionBlockObject(
"branch",
slack.NewTextBlockObject(slack.PlainTextType, "Branch", false, false),
nil,
),
slack.NewOptionBlockObject(
"tag",
slack.NewTextBlockObject(slack.PlainTextType, "Tag", false, false),
nil,
),
),
),
slack.NewInputBlock(
blockRef,
slack.NewTextBlockObject(slack.PlainTextType, "Ref", false, false),
slack.NewPlainTextInputBlockElement(
slack.NewTextBlockObject(slack.PlainTextType, "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2", false, false),
actionRef,
),
),
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockApprovers,
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
Optional: true,
Element: slack.NewOptionsSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
actionApprovers,
approvers...,
),
},
},
BlockSet: set,
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/server/slack/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (s *Slack) handleUnlockCmd(c *gin.Context) {
// 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 envs in the `%s/%s` repository.", ns, n))
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 {
Expand Down Expand Up @@ -273,7 +273,7 @@ func (s *Slack) interactLock(c *gin.Context) {
UserID: cu.Edges.User.ID,
RepoID: cb.Edges.Repo.ID,
}); err != nil {
s.log.Error("It has failed to lock the env.", zap.Error(err))
s.log.Error("It has failed to lock the environment.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func (s *Slack) interactUnlock(c *gin.Context) {
}

if err := s.i.DeleteLock(ctx, lock); err != nil {
s.log.Error("It has failed to unlock the env.", zap.Error(err))
s.log.Error("It has failed to unlock the environment.", zap.Error(err))
c.Status(http.StatusInternalServerError)
return
}
Expand Down
55 changes: 31 additions & 24 deletions internal/server/slack/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (s *Slack) handleRollbackCmd(c *gin.Context) {
return
}

perms = s.filterPerms(perms, cu)

cb, err := s.i.CreateCallback(ctx, &ent.Callback{
Type: callback.TypeRollback,
RepoID: r.ID,
Expand Down Expand Up @@ -136,37 +138,42 @@ func buildRollbackView(callbackID string, as []*deploymentAggregation, perms []*
nil))
}

sets := []slack.Block{
slack.NewInputBlock(
blockDeployment,
slack.NewTextBlockObject(slack.PlainTextType, "Deployments", false, false),
slack.NewOptionsGroupSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select target deployment", false, false),
actionDeployment,
groups...,
),
),
}

if len(approvers) > 0 {
sets = append(sets, slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockApprovers,
Optional: true,
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
Element: slack.NewOptionsSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
actionApprovers,
approvers...,
),
})
}

return slack.ModalViewRequest{
Type: slack.VTModal,
CallbackID: callbackID,
Title: slack.NewTextBlockObject(slack.PlainTextType, "Rollback", 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(
blockDeployment,
slack.NewTextBlockObject(slack.PlainTextType, "Deployments", false, false),
slack.NewOptionsGroupSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select target deployment", false, false),
actionDeployment,
groups...,
),
),
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockApprovers,
Optional: true,
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
Element: slack.NewOptionsSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
actionApprovers,
approvers...,
),
},
},
BlockSet: sets,
},
}
}
Expand Down