Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for @bot describe locks command #1128

Merged
merged 1 commit into from
Aug 30, 2024
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
29 changes: 29 additions & 0 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func (s *SlackListener) runCommand(cmd slackcmd.Command, triggeredBy string, rep
msgOpt = s.lock(cmd, user, replyIn)
case *slackcmd.Unlock:
msgOpt = s.unlock(cmd, user, replyIn)
case *slackcmd.DescribeLocks:
msgOpt = s.describeLocks()
default:
panic("unreachable")
}
Expand Down Expand Up @@ -303,6 +305,33 @@ func (s *SlackListener) unlock(cmd *slackcmd.Unlock, triggeredBy User, replyIn s
return s.infoMessage(fmt.Sprintf("Unlocked %s %s", cmd.Project, cmd.Env))
}

// describeLocks describes the locks of all projects and environments, and replies to the given channel.
func (s *SlackListener) describeLocks() slack.MsgOption {
locks, err := s.getOrCreateCoordinator().DescribeLocks(context.Background())
if err != nil {
return s.errorMessage(err.Error())
}

var buf strings.Builder
for project, envs := range locks {
buf.WriteString(project)
buf.WriteString("\n")
for env, lock := range envs {
buf.WriteString(" ")
buf.WriteString(env)
buf.WriteString(": ")
if lock.Locked {
buf.WriteString("Locked")
} else {
buf.WriteString("Unlocked")
}
buf.WriteString("\n")
}
}

return s.infoMessage(buf.String())
}

func (s *SlackListener) validateProjectEnvUser(projectID, env string, user User, replyIn string) error {
pj, err := s.projectList.FindByAlias(projectID)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ func TestSlackLockUnlock(t *testing.T) {
}))
require.Equal(t, "Locked myproject1 production", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
}))
require.Equal(t, `myproject1
production: Locked
`, nextMessage().Text())

// User 1 is a developer so cannot unlock the project forcefully
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Expand All @@ -278,6 +288,16 @@ func TestSlackLockUnlock(t *testing.T) {
Text: "unlock myproject1 production",
}))
require.Equal(t, "Unlocked myproject1 production", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
}))
require.Equal(t, `myproject1
production: Unlocked
`, nextMessage().Text())
}

// Message is a message posted to the fake Slack API's chat.postMessage endpoint
Expand Down
Loading