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 Feb 19, 2019
1 parent 4352a87 commit 6a45ebc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
12 changes: 8 additions & 4 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,12 @@ func execute(ctx *ankh.ExecutionContext) {
executeContext(ctx, &rootAnkhFile)
}

if ctx.SlackChannel != "" {
if err := slack.PingSlackChannel(ctx); err != nil {
if len(ctx.SlackChannels) > 0 {
if ctx.Mode == ankh.Rollback {
ctx.SlackDeploymentVersion = "rollback"
}
err := slack.PingSlackChannel(ctx)
if err != nil {
ctx.Logger.Errorf("Slack message failed with error: %v", err)
}
}
Expand Down Expand Up @@ -908,7 +912,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Apply
ctx.SlackChannel = *slackChannel
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
filters := []string{}
Expand Down Expand Up @@ -942,7 +946,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
ctx.Filters = []string{"deployment", "statfulset"}
Expand Down
7 changes: 4 additions & 3 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 Expand Up @@ -201,7 +202,7 @@ func (ctx *ExecutionContext) DetermineHelmRepository(preferredRepository *string
return repository
}

ctx.Logger.Fatalf("No helm repository configured. "+
ctx.Logger.Fatalf("No helm repository configured. " +
"Set `helm.repository` globally, pass it as an argument, or see README.md")
return ""
}
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 6a45ebc

Please sign in to comment.