Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Allow for posting to multiple slack channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven E. Newton committed Apr 24, 2019
1 parent 94cc341 commit b98977a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ankh/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func execute(ctx *ankh.ExecutionContext) {
executeContext(ctx, &rootAnkhFile)
}

if ctx.SlackChannel != "" {
if len(ctx.SlackChannels) != 0 {
if err := slack.PingSlackChannel(ctx); err != nil {
ctx.Logger.Errorf("Slack message failed with error: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Apply
ctx.SlackChannel = *slackChannel
ctx.SlackChannels = strings.Split(*slackChannel, ",");
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
filters := []string{}
Expand Down Expand Up @@ -355,7 +355,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Deploy
ctx.SlackChannel = *slackChannel
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
filters := []string{}
Expand Down Expand Up @@ -389,7 +389,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Rollback
ctx.SlackChannel = *slackChannel
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket

Expand Down
5 changes: 3 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ type ExecutionContext struct {

DeploymentTag string

SlackChannel string
SlackMessageOverride string

CreateJiraTicket bool
CreateJiraTicket bool
SlackChannels []string
SlackDeploymentVersion string

Filters []string

Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ github.com/docker/distribution v0.0.0-20180920194744-16128bbac47f/go.mod h1:J2gT
github.com/docker/distribution v2.7.0-rc.0+incompatible h1:Nw9tozLpkMnG3IA1zLzsCuwKizII6havt4iIXWWzU2s=
github.com/docker/distribution v2.7.0-rc.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v0.0.0-20180924202107-a9c061deec0f/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v1.13.1 h1:5VBhsO6ckUxB0A8CE5LlUJdXzik9cbEbBTQ/ggeml7M=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-ce v0.0.0-20180924210327-f53bd8bb8e43/go.mod h1:l1FUGRYBvbjnZ8MS6A2xOji4aZFlY/Qmgz7p4oXH7ac=
github.com/docker/docker-credential-helpers v0.6.1/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y=
Expand Down
26 changes: 16 additions & 10 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ func PingSlackChannel(ctx *ankh.ExecutionContext) error {
Username: username,
}

channelId, err := getSlackChannelIDByName(api, ctx.SlackChannel)
if err != nil {
return err
}

if !ctx.DryRun {
_, _, err = api.PostMessage(channelId, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams))
} else {
ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, ctx.SlackChannel)
}
channels := make(map[string]string, len(ctx.SlackChannels))
for _, ch := range ctx.SlackChannels {
channelId, err := getSlackChannelIDByName(api, ch)
if err != nil {
return err
}
channels[ch] = channelId
}

for name, id := range channels {
if !ctx.DryRun {
_, _, err = api.PostMessage(id, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams))
} else {
ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, name)
}
}

return err
}
Expand Down

0 comments on commit b98977a

Please sign in to comment.