diff --git a/server/events/apply_executor.go b/server/events/apply_executor.go index 309eab26..3911b52a 100644 --- a/server/events/apply_executor.go +++ b/server/events/apply_executor.go @@ -11,11 +11,13 @@ import ( "github.com/hootsuite/atlantis/server/events/github" "github.com/hootsuite/atlantis/server/events/models" "github.com/hootsuite/atlantis/server/events/run" + "github.com/hootsuite/atlantis/server/events/slack" "github.com/hootsuite/atlantis/server/events/terraform" ) type ApplyExecutor struct { Github github.Client + Slack slack.Client Terraform *terraform.Client RequireApproval bool Run *run.Run @@ -93,8 +95,14 @@ func (a *ApplyExecutor) apply(ctx *CommandContext, repoDir string, plan models.P tfApplyCmd := append(append(append([]string{"apply", "-no-color"}, applyExtraArgs...), ctx.Command.Flags...), plan.LocalPath) output, err := a.Terraform.RunCommandWithVersion(ctx.Log, absolutePath, tfApplyCmd, terraformVersion, env) if err != nil { + if a.Slack != nil { + a.Slack.PostMessage(createSlackMessage(ctx, false)) + } return ProjectResult{Error: fmt.Errorf("%s\n%s", err.Error(), output)} } + if a.Slack != nil { + a.Slack.PostMessage(createSlackMessage(ctx, true)) + } ctx.Log.Info("apply succeeded") if len(config.PostApply) > 0 { @@ -106,3 +114,19 @@ func (a *ApplyExecutor) apply(ctx *CommandContext, repoDir string, plan models.P return ProjectResult{ApplySuccess: output} } + +func createSlackMessage(ctx *CommandContext, success bool) string { + var status string + if success { + status = ":white_check_mark:" + } else { + status = ":x:" + } + + return fmt.Sprintf("%s *%s* %s in <%s|%s>.", + status, + ctx.User.Username, + ctx.Command.Name.String()+" "+ctx.Command.Environment, + ctx.Pull.URL, + ctx.BaseRepo.Name) +} diff --git a/server/server.go b/server/server.go index f53555e9..7bd76a2c 100644 --- a/server/server.go +++ b/server/server.go @@ -18,6 +18,7 @@ import ( "github.com/hootsuite/atlantis/server/events/locking" "github.com/hootsuite/atlantis/server/events/locking/boltdb" "github.com/hootsuite/atlantis/server/events/run" + "github.com/hootsuite/atlantis/server/events/slack" "github.com/hootsuite/atlantis/server/events/terraform" "github.com/hootsuite/atlantis/server/logging" "github.com/hootsuite/atlantis/server/static" @@ -53,6 +54,8 @@ type Config struct { LogLevel string `mapstructure:"log-level"` Port int `mapstructure:"port"` RequireApproval bool `mapstructure:"require-approval"` + SlackToken string `mapstructure:"slack-token"` + SlackChannel string `mapstructure:"slack-channel"` } func NewServer(config Config) (*Server, error) { @@ -61,6 +64,14 @@ func NewServer(config Config) (*Server, error) { return nil, err } githubStatus := &events.GithubStatus{Client: githubClient} + // nil slackClient unless token and channel was specified + var slackClient slack.Client + if config.SlackToken != "" && config.SlackChannel != "" { + slackClient, err = slack.NewClient(config.SlackToken, config.SlackChannel) + if err != nil { + return nil, errors.Wrap(err, "initializing slack client") + } + } terraformClient, err := terraform.NewClient() if err != nil { return nil, errors.Wrap(err, "initializing terraform") @@ -86,6 +97,7 @@ func NewServer(config Config) (*Server, error) { } applyExecutor := &events.ApplyExecutor{ Github: githubClient, + Slack: slackClient, Terraform: terraformClient, RequireApproval: config.RequireApproval, Run: run,