Skip to content

Commit

Permalink
moving things to command package (#218)
Browse files Browse the repository at this point in the history
* moving things to command package

* remove unnecessary change
  • Loading branch information
msarvar authored Mar 23, 2022
1 parent 5fb8bdb commit 8d6c504
Show file tree
Hide file tree
Showing 36 changed files with 376 additions and 250 deletions.
2 changes: 1 addition & 1 deletion server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
silenceNoProjects,
)

commentCommandRunnerByCmd := map[command.Name]events.CommentCommandRunner{
commentCommandRunnerByCmd := map[command.Name]command.Runner{
command.Plan: planCommandRunner,
command.Apply: applyCommandRunner,
command.ApprovePolicies: approvePoliciesCommandRunner,
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/events/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
events_controllers "github.com/runatlantis/atlantis/server/controllers/events"
"github.com/runatlantis/atlantis/server/controllers/events/mocks"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
emocks "github.com/runatlantis/atlantis/server/events/mocks"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
Expand Down Expand Up @@ -375,7 +376,7 @@ func TestPost_GithubCommentSuccess(t *testing.T) {
When(v.Validate(req, secret)).ThenReturn([]byte(event), nil)
baseRepo := models.Repo{}
user := models.User{}
cmd := events.CommentCommand{}
cmd := command.Comment{}
When(p.ParseGithubIssueCommentEvent(matchers.AnyPtrToGithubIssueCommentEvent())).ThenReturn(baseRepo, user, 1, nil)
When(cp.Parse("", models.Github)).ThenReturn(events.CommentParseResult{Command: &cmd})
w := httptest.NewRecorder()
Expand Down
2 changes: 1 addition & 1 deletion server/events/apply_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type ApplyCommandRunner struct {
silenceVCSStatusNoProjects bool
}

func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) {
func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *command.Comment) {
var err error
baseRepo := ctx.Pull.BaseRepo
pull := ctx.Pull
Expand Down
3 changes: 1 addition & 2 deletions server/events/apply_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/google/go-github/v31/github"
. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/core/locking"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/models/fixtures"
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestApplyCommandRunner_IsLocked(t *testing.T) {
}

When(applyLockChecker.CheckApplyLock()).ThenReturn(locking.ApplyCommandLock{Locked: c.ApplyLocked}, c.ApplyLockError)
applyCommandRunner.Run(ctx, &events.CommentCommand{Name: command.Apply})
applyCommandRunner.Run(ctx, &command.Comment{Name: command.Apply})

vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, c.ExpComment, "apply")
})
Expand Down
2 changes: 1 addition & 1 deletion server/events/approve_policies_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ApprovePoliciesCommandRunner struct {
silenceVCSStatusNoProjects bool
}

func (a *ApprovePoliciesCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) {
func (a *ApprovePoliciesCommandRunner) Run(ctx *command.Context, cmd *command.Comment) {
baseRepo := ctx.Pull.BaseRepo
pull := ctx.Pull

Expand Down
2 changes: 1 addition & 1 deletion server/events/automerger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *AutoMerger) automerge(ctx *command.Context, pullStatus models.PullStatu
}
}

// automergeEnabled returns true if automerging is enabled in this context.
// AutomergeEnabled returns true if automerging is enabled in this context.
func (c *AutoMerger) automergeEnabled(projectCmds []command.ProjectContext) bool {
// If the global automerge is set, we always automerge.
return c.GlobalAutomerge ||
Expand Down
3 changes: 1 addition & 2 deletions server/events/command/apply/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apply

import (
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/vcs"
)
Expand All @@ -16,7 +15,7 @@ type Runner struct {
vcsClient vcs.Client
}

func (r *Runner) Run(ctx *command.Context, cmd *events.CommentCommand) {
func (r *Runner) Run(ctx *command.Context, cmd *command.Comment) {
if err := r.vcsClient.CreateComment(ctx.Pull.BaseRepo, ctx.Pull.Num, "I'm a platform mode apply runner", command.Apply.String()); err != nil {
ctx.Log.Err("unable to comment: %s", err)
}
Expand Down
81 changes: 81 additions & 0 deletions server/events/command/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package command

import (
"fmt"
"path"
"strings"
)

// NewComment constructs a Command, setting all missing fields to defaults.
func NewComment(repoRelDir string, flags []string, name Name, verbose, forceApply, autoMergeDisabled bool, workspace string, project string) *Comment {
// If repoRelDir was empty we want to keep it that way to indicate that it
// wasn't specified in the comment.
if repoRelDir != "" {
repoRelDir = path.Clean(repoRelDir)
if repoRelDir == "/" {
repoRelDir = "."
}
}
return &Comment{
RepoRelDir: repoRelDir,
Flags: flags,
Name: name,
Verbose: verbose,
Workspace: workspace,
AutoMergeDisabled: autoMergeDisabled,
ProjectName: project,
ForceApply: forceApply,
}
}

// Comment is a command that was triggered by a pull request comment.
type Comment struct {
// RepoRelDir is the path relative to the repo root to run the command in.
// Will never end in "/". If empty then the comment specified no directory.
RepoRelDir string
// Flags are the extra arguments appended to the comment,
// ex. atlantis plan -- -target=resource
Flags []string
// Name is the name of the command the comment specified.
Name Name
// AutoMergeDisabled is true if the command should not automerge after apply.
AutoMergeDisabled bool
// Verbose is true if the command should output verbosely.
Verbose bool
//ForceApply is true of the command should ignore apply_requirments.
ForceApply bool
// Workspace is the name of the Terraform workspace to run the command in.
// If empty then the comment specified no workspace.
Workspace string
// ProjectName is the name of a project to run the command on. It refers to a
// project specified in an atlantis.yaml file.
// If empty then the comment specified no project.
ProjectName string
}

// IsForSpecificProject returns true if the command is for a specific dir, workspace
// or project name. Otherwise it's a command like "atlantis plan" or "atlantis
// apply".
func (c Comment) IsForSpecificProject() bool {
return c.RepoRelDir != "" || c.Workspace != "" || c.ProjectName != ""
}

// CommandName returns the name of this command.
func (c Comment) CommandName() Name {
return c.Name
}

// IsVerbose is true if the command should give verbose output.
func (c Comment) IsVerbose() bool {
return c.Verbose
}

// IsAutoplan will be false for comment commands.
func (c Comment) IsAutoplan() bool {
return false
}

// String returns a string representation of the command.
func (c Comment) String() string {
return fmt.Sprintf("command=%q verbose=%t dir=%q workspace=%q project=%q flags=%q", c.Name.String(), c.Verbose, c.RepoRelDir, c.Workspace, c.ProjectName, strings.Join(c.Flags, ","))
}
20 changes: 20 additions & 0 deletions server/events/command/mocks/matchers/ptr_to_command_comment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions server/events/command/mocks/matchers/ptr_to_command_context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions server/events/command/mocks/mock_runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions server/events/command/plan/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plan

import (
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/vcs"
)
Expand All @@ -16,7 +15,7 @@ type Runner struct {
vcsClient vcs.Client
}

func (r *Runner) Run(ctx *command.Context, cmd *events.CommentCommand) {
func (r *Runner) Run(ctx *command.Context, cmd *command.Comment) {
if err := r.vcsClient.CreateComment(ctx.Pull.BaseRepo, ctx.Pull.Num, "I'm a platform mode plan runner", command.Plan.String()); err != nil {
ctx.Log.Err("unable to comment: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions server/events/command/policies/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package policies

import (
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/vcs"
)
Expand All @@ -16,7 +15,7 @@ type Runner struct {
vcsClient vcs.Client
}

func (r *Runner) Run(ctx *command.Context, cmd *events.CommentCommand) {
func (r *Runner) Run(ctx *command.Context, cmd *command.Comment) {
if err := r.vcsClient.CreateComment(ctx.Pull.BaseRepo, ctx.Pull.Num, "I'm a platform mode approve_policies runner", command.ApprovePolicies.String()); err != nil {
ctx.Log.Err("unable to comment: %s", err)
}
Expand Down
8 changes: 8 additions & 0 deletions server/events/command/runners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package command

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_runner.go Runner

// Runner runs individual command workflows.
type Runner interface {
Run(ctx *Context, cmd *Comment)
}
Loading

0 comments on commit 8d6c504

Please sign in to comment.