Files
atlantis/server/command_handler.go
Luke Kysow 906033e99e Implement local plan storage. Refactor S3 storage (#33)
* added new flags `plan-backend`, `plan-s3-bucket`, `plan-s3-prefix` and deleted `s3-bucket`
* interface `plan.Backend` that is implemented by `file` and `s3`
* simplified s3 code
* didn't end up following my suggestions in #30 since storing stuff in metadata requires you to `Get` the object *first* and then use the metadata. By parsing the `Key` to get repo, pull, path, and env, it skips an initial `Get` step, and I can download directly to the correct directory
* allow users to specify `application/json` or `application/x-www-form-urlencoded` for the webhook delivery type
* remove sending of special header for pull request api (fixes #11)

Closes #30 and #17 and #11
2017-06-20 00:22:42 -07:00

75 lines
1.9 KiB
Go

package server
import (
"fmt"
"github.com/hootsuite/atlantis/logging"
"github.com/hootsuite/atlantis/recovery"
)
type CommandHandler struct {
planExecutor *PlanExecutor
applyExecutor *ApplyExecutor
helpExecutor *HelpExecutor
githubClient *GithubClient
eventParser *EventParser
logger *logging.SimpleLogger
}
type CommandType int
const (
Apply CommandType = iota
Plan
Help
)
type Command struct {
verbose bool
environment string
commandType CommandType
}
func (s *CommandHandler) ExecuteCommand(ctx *CommandContext) {
src := fmt.Sprintf("%s/pull/%d", ctx.Repo.FullName, ctx.Pull.Num)
// it'e safe to reuse the underlying logger e.logger.Log
ctx.Log = logging.NewSimpleLogger(src, s.logger.Log, true, s.logger.Level)
defer s.recover(ctx)
// need to get additional data from the PR
ghPull, _, err := s.githubClient.GetPullRequest(ctx.Repo, ctx.Pull.Num)
if err != nil {
ctx.Log.Err("pull request data api call failed: %v", err)
return
}
pull, err := s.eventParser.ExtractPullData(ghPull)
if err != nil {
ctx.Log.Err("failed to extract required fields from comment data: %v", err)
return
}
ctx.Pull = pull
switch ctx.Command.commandType {
case Plan:
s.planExecutor.execute(ctx, s.githubClient)
case Apply:
s.applyExecutor.execute(ctx, s.githubClient)
case Help:
s.helpExecutor.execute(ctx, s.githubClient)
default:
ctx.Log.Err("failed to determine desired command, neither plan nor apply")
}
}
func (s *CommandHandler) SetDeleteLockURL(f func(id string) (url string)) {
s.planExecutor.DeleteLockURL = f
}
// recover logs and creates a comment on the pull request for panics
func (s *CommandHandler) recover(ctx *CommandContext) {
if err := recover(); err != nil {
stack := recovery.Stack(3)
s.githubClient.CreateComment(ctx, fmt.Sprintf("**Error: goroutine panic. This is a bug.**\n```\n%s\n%s```", err, stack))
ctx.Log.Err("PANIC: %s\n%s", err, stack)
}
}