From b98977a422e87c3c47e5fdec30f776f57f481b5b Mon Sep 17 00:00:00 2001 From: "Steven E. Newton" Date: Fri, 15 Feb 2019 10:52:55 -0800 Subject: [PATCH] Allow for posting to multiple slack channels --- ankh/execute.go | 2 +- ankh/main.go | 6 +++--- context/context.go | 5 +++-- go.sum | 2 +- slack/slack.go | 26 ++++++++++++++++---------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/ankh/execute.go b/ankh/execute.go index 5046ad18..5e5a1ecd 100644 --- a/ankh/execute.go +++ b/ankh/execute.go @@ -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) } diff --git a/ankh/main.go b/ankh/main.go index c26c7f2a..1156a7e5 100644 --- a/ankh/main.go +++ b/ankh/main.go @@ -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{} @@ -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{} @@ -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 diff --git a/context/context.go b/context/context.go index 32621875..2960b031 100644 --- a/context/context.go +++ b/context/context.go @@ -55,10 +55,11 @@ type ExecutionContext struct { DeploymentTag string - SlackChannel string SlackMessageOverride string - CreateJiraTicket bool + CreateJiraTicket bool + SlackChannels []string + SlackDeploymentVersion string Filters []string diff --git a/go.sum b/go.sum index db49fe2b..2545aad9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/slack/slack.go b/slack/slack.go index 85f5763a..8510d62b 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -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 }