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

Commit

Permalink
Post to multiple slack channels
Browse files Browse the repository at this point in the history
Per a request from Donald Johnson, added support to ankh for
posting to multiple slack channels. Also added channels as an
array to the slack section in ankh config
  • Loading branch information
Steven E. Newton committed Feb 19, 2019
1 parent 6a45ebc commit 99fa233
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ When invoked, Ankh will operate over both the `haste-server` and `myservice` cha
| format | string | Optional. Format of slack message that will be used. See available variables below. |
| rollbackFormat | string | Optional. Format of message for rollbacks that will be used. See available variables below. |
| pretext | string | Optional. Pretext for slack message. Default is `A new release notification has been received`. |
| channels | []string | Optional. Channel[s} to post the message to. |

#### `JiraConfig`
| Field | Type | Description |
Expand Down
9 changes: 6 additions & 3 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ func execute(ctx *ankh.ExecutionContext) {
executeContext(ctx, &rootAnkhFile)
}

if len(ctx.SlackChannels) > 0 {
if len(ctx.AnkhConfig.Slack.Channels) > 0 {
ctx.Logger.Debugf("Pinging %d slack channels %+v", len(ctx.AnkhConfig.Slack.Channels), ctx.AnkhConfig.Slack.Channels)
if ctx.Mode == ankh.Rollback {
ctx.SlackDeploymentVersion = "rollback"
}
Expand Down Expand Up @@ -912,7 +913,9 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Apply
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
if *slackChannel != "" {
ctx.AnkhConfig.Slack.Channels = []string{*slackChannel}
}
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
filters := []string{}
Expand Down Expand Up @@ -946,7 +949,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Rollback
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.AnkhConfig.Slack.Channels = append(ctx.AnkhConfig.Slack.Channels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
ctx.Filters = []string{"deployment", "statfulset"}
Expand Down
13 changes: 7 additions & 6 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ type DockerConfig struct {
}

type SlackConfig struct {
Token string `yaml:"token"`
Icon string `yaml:"icon-url"`
Username string `yaml:"username"`
Format string `yaml:"format"`
RollbackFormat string `yaml:"rollbackFormat"`
Pretext string `yaml:"pretext"`
Token string `yaml:"token"`
Icon string `yaml:"icon-url"`
Username string `yaml:"username"`
Format string `yaml:"format"`
RollbackFormat string `yaml:"rollbackFormat"`
Pretext string `yaml:"pretext"`
Channels []string `yaml:"channels"`
}

type JiraConfig struct {
Expand Down
26 changes: 13 additions & 13 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ func PingSlackChannel(ctx *ankh.ExecutionContext) error {
Username: username,
}

channels := make(map[string]string, len(ctx.SlackChannels))
for _, ch := range ctx.SlackChannels {
channels := make(map[string]string, len(ctx.AnkhConfig.Slack.Channels))
for _, ch := range ctx.AnkhConfig.Slack.Channels {
channelId, err := getSlackChannelIDByName(api, ch)
if err != nil {
return err
}
channels[ch] = channelId
}
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)
}
}
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 99fa233

Please sign in to comment.