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 Apr 24, 2019
1 parent b98977a commit f95b462
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 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
4 changes: 2 additions & 2 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func main() {
ctx.Chart = *chartPath
ctx.LocalChart = true
}
ctx.Mode = ankh.Deploy
ctx.Mode = ankh.Apply
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.CreateJiraTicket = *createJiraTicket
Expand Down Expand Up @@ -389,7 +389,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

Expand Down
13 changes: 7 additions & 6 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,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 f95b462

Please sign in to comment.