Skip to content

Commit

Permalink
Parse slack config, add slack client to apply_executor
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-wu-hs committed Nov 2, 2017
1 parent d5d8e12 commit 5491a30
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions server/events/apply_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
12 changes: 12 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -86,6 +97,7 @@ func NewServer(config Config) (*Server, error) {
}
applyExecutor := &events.ApplyExecutor{
Github: githubClient,
Slack: slackClient,
Terraform: terraformClient,
RequireApproval: config.RequireApproval,
Run: run,
Expand Down

0 comments on commit 5491a30

Please sign in to comment.