Skip to content

Commit

Permalink
chore: move CommandContext and CommandResult to models (#193) (#2093)
Browse files Browse the repository at this point in the history
* Moved CommandContext and CommandResult to models (#193)

* Moved CommandContext and CommandResult to models

* move from models to command

rename CommandContext -> Context
rename CommandResult -> Result

* moved command related helpers into command package

* move ProjectCommandContext and ProjectResult to command/project package

* move project command context and project result

* revert unrelated code

* move tests

* fix left over

* fix linting

* fix tests

* remove unused import

* fix project context dependencies

* fix depenedecies

* fix typo
  • Loading branch information
msarvar authored Mar 21, 2022
1 parent d1d1539 commit 90e92e3
Show file tree
Hide file tree
Showing 254 changed files with 2,190 additions and 1,967 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Each interface that is mocked has a `go:generate` command above it, e.g.
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_project_command_builder.go ProjectCommandBuilder
type ProjectCommandBuilder interface {
BuildAutoplanCommands(ctx *CommandContext) ([]models.ProjectCommandContext, error)
BuildAutoplanCommands(ctx *command.Context) ([]command.ProjectContext, error)
}
```

Expand Down
13 changes: 7 additions & 6 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/runatlantis/atlantis/server/core/runtime/policy"
"github.com/runatlantis/atlantis/server/core/terraform"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"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 @@ -1068,12 +1069,12 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
silenceNoProjects,
)

commentCommandRunnerByCmd := map[models.CommandName]events.CommentCommandRunner{
models.PlanCommand: planCommandRunner,
models.ApplyCommand: applyCommandRunner,
models.ApprovePoliciesCommand: approvePoliciesCommandRunner,
models.UnlockCommand: unlockCommandRunner,
models.VersionCommand: versionCommandRunner,
commentCommandRunnerByCmd := map[command.Name]events.CommentCommandRunner{
command.Plan: planCommandRunner,
command.Apply: applyCommandRunner,
command.ApprovePolicies: approvePoliciesCommandRunner,
command.Unlock: unlockCommandRunner,
command.Version: versionCommandRunner,
}

commandRunner := &events.DefaultCommandRunner{
Expand Down

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

3 changes: 2 additions & 1 deletion server/controllers/events/mocks/matchers/slice_of_byte.go

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

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

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

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

5 changes: 3 additions & 2 deletions server/controllers/locks_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/runatlantis/atlantis/server/events"

"github.com/runatlantis/atlantis/server/core/locking/mocks"
"github.com/runatlantis/atlantis/server/events/command"
mocks2 "github.com/runatlantis/atlantis/server/events/mocks"
"github.com/runatlantis/atlantis/server/events/models"
vcsmocks "github.com/runatlantis/atlantis/server/events/vcs/mocks"
Expand Down Expand Up @@ -302,9 +303,9 @@ func TestDeleteLock_UpdateProjectStatus(t *testing.T) {
db, err := db.New(tmp)
Ok(t, err)
// Seed the DB with a successful plan for that project (that is later discarded).
_, err = db.UpdatePullWithResults(pull, []models.ProjectResult{
_, err = db.UpdatePullWithResults(pull, []command.ProjectResult{
{
Command: models.PlanCommand,
Command: command.Plan,
RepoRelDir: projectPath,
Workspace: workspaceName,
PlanSuccess: &models.PlanSuccess{
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/templates/mocks/matchers/io_writer.go

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

3 changes: 2 additions & 1 deletion server/controllers/templates/mocks/mock_template_writer.go

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

19 changes: 10 additions & 9 deletions server/core/db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
bolt "go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -173,10 +174,10 @@ func (b *BoltDB) List() ([]models.ProjectLock, error) {
// LockCommand attempts to create a new lock for a CommandName.
// If the lock doesn't exists, it will create a lock and return a pointer to it.
// If the lock already exists, it will return an "lock already exists" error
func (b *BoltDB) LockCommand(cmdName models.CommandName, lockTime time.Time) (*models.CommandLock, error) {
lock := models.CommandLock{
func (b *BoltDB) LockCommand(cmdName command.Name, lockTime time.Time) (*command.Lock, error) {
lock := command.Lock{
CommandName: cmdName,
LockMetadata: models.LockMetadata{
LockMetadata: command.LockMetadata{
UnixTime: lockTime.Unix(),
},
}
Expand Down Expand Up @@ -204,7 +205,7 @@ func (b *BoltDB) LockCommand(cmdName models.CommandName, lockTime time.Time) (*m

// UnlockCommand removes CommandName lock if present.
// If there are no lock it returns an error.
func (b *BoltDB) UnlockCommand(cmdName models.CommandName) error {
func (b *BoltDB) UnlockCommand(cmdName command.Name) error {
transactionErr := b.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.globalLocksBucketName)

Expand All @@ -224,8 +225,8 @@ func (b *BoltDB) UnlockCommand(cmdName models.CommandName) error {

// CheckCommandLock checks if CommandName lock was set.
// If the lock exists return the pointer to the lock object, otherwise return nil
func (b *BoltDB) CheckCommandLock(cmdName models.CommandName) (*models.CommandLock, error) {
cmdLock := models.CommandLock{}
func (b *BoltDB) CheckCommandLock(cmdName command.Name) (*command.Lock, error) {
cmdLock := command.Lock{}

found := false

Expand Down Expand Up @@ -312,7 +313,7 @@ func (b *BoltDB) GetLock(p models.Project, workspace string) (*models.ProjectLoc

// UpdatePullWithResults updates pull's status with the latest project results.
// It returns the new PullStatus object.
func (b *BoltDB) UpdatePullWithResults(pull models.PullRequest, newResults []models.ProjectResult) (models.PullStatus, error) {
func (b *BoltDB) UpdatePullWithResults(pull models.PullRequest, newResults []command.ProjectResult) (models.PullStatus, error) {
key, err := b.pullKey(pull)
if err != nil {
return models.PullStatus{}, err
Expand Down Expand Up @@ -451,7 +452,7 @@ func (b *BoltDB) pullKey(pull models.PullRequest) ([]byte, error) {
nil
}

func (b *BoltDB) commandLockKey(cmdName models.CommandName) string {
func (b *BoltDB) commandLockKey(cmdName command.Name) string {
return fmt.Sprintf("%s/lock", cmdName)
}

Expand Down Expand Up @@ -480,7 +481,7 @@ func (b *BoltDB) writePullToBucket(bucket *bolt.Bucket, key []byte, pull models.
return bucket.Put(key, serialized)
}

func (b *BoltDB) projectResultToProject(p models.ProjectResult) models.ProjectStatus {
func (b *BoltDB) projectResultToProject(p command.ProjectResult) models.ProjectStatus {
return models.ProjectStatus{
Workspace: p.Workspace,
RepoRelDir: p.RepoRelDir,
Expand Down
51 changes: 26 additions & 25 deletions server/core/db/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/runatlantis/atlantis/server/core/db"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
. "github.com/runatlantis/atlantis/testing"
bolt "go.etcd.io/bbolt"
Expand All @@ -47,7 +48,7 @@ func TestLockCommandNotSet(t *testing.T) {
t.Log("retrieving apply lock when there are none should return empty LockCommand")
db, b := newTestDB()
defer cleanupDB(db)
exists, err := b.CheckCommandLock(models.ApplyCommand)
exists, err := b.CheckCommandLock(command.Apply)
Ok(t, err)
Assert(t, exists == nil, "exp nil")
}
Expand All @@ -57,10 +58,10 @@ func TestLockCommandEnabled(t *testing.T) {
db, b := newTestDB()
defer cleanupDB(db)
timeNow := time.Now()
_, err := b.LockCommand(models.ApplyCommand, timeNow)
_, err := b.LockCommand(command.Apply, timeNow)
Ok(t, err)

config, err := b.CheckCommandLock(models.ApplyCommand)
config, err := b.CheckCommandLock(command.Apply)
Ok(t, err)
Equals(t, true, config.IsLocked())
}
Expand All @@ -70,10 +71,10 @@ func TestLockCommandFail(t *testing.T) {
db, b := newTestDB()
defer cleanupDB(db)
timeNow := time.Now()
_, err := b.LockCommand(models.ApplyCommand, timeNow)
_, err := b.LockCommand(command.Apply, timeNow)
Ok(t, err)

_, err = b.LockCommand(models.ApplyCommand, timeNow)
_, err = b.LockCommand(command.Apply, timeNow)
ErrEquals(t, "db transaction failed: lock already exists", err)
}

Expand All @@ -82,17 +83,17 @@ func TestUnlockCommandDisabled(t *testing.T) {
db, b := newTestDB()
defer cleanupDB(db)
timeNow := time.Now()
_, err := b.LockCommand(models.ApplyCommand, timeNow)
_, err := b.LockCommand(command.Apply, timeNow)
Ok(t, err)

config, err := b.CheckCommandLock(models.ApplyCommand)
config, err := b.CheckCommandLock(command.Apply)
Ok(t, err)
Equals(t, true, config.IsLocked())

err = b.UnlockCommand(models.ApplyCommand)
err = b.UnlockCommand(command.Apply)
Ok(t, err)

config, err = b.CheckCommandLock(models.ApplyCommand)
config, err = b.CheckCommandLock(command.Apply)
Ok(t, err)
Assert(t, config == nil, "exp nil object")
}
Expand All @@ -101,15 +102,15 @@ func TestUnlockCommandFail(t *testing.T) {
t.Log("setting the apply lock")
db, b := newTestDB()
defer cleanupDB(db)
err := b.UnlockCommand(models.ApplyCommand)
err := b.UnlockCommand(command.Apply)
ErrEquals(t, "db transaction failed: no lock exists", err)
}

func TestMixedLocksPresent(t *testing.T) {
db, b := newTestDB()
defer cleanupDB(db)
timeNow := time.Now()
_, err := b.LockCommand(models.ApplyCommand, timeNow)
_, err := b.LockCommand(command.Apply, timeNow)
Ok(t, err)

_, _, err = b.TryLock(lock)
Expand Down Expand Up @@ -456,9 +457,9 @@ func TestPullStatus_UpdateGet(t *testing.T) {
}
status, err := b.UpdatePullWithResults(
pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
Command: models.PlanCommand,
Command: command.Plan,
RepoRelDir: ".",
Workspace: "default",
Failure: "failure",
Expand Down Expand Up @@ -507,7 +508,7 @@ func TestPullStatus_UpdateDeleteGet(t *testing.T) {
}
_, err := b.UpdatePullWithResults(
pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
RepoRelDir: ".",
Workspace: "default",
Expand Down Expand Up @@ -553,7 +554,7 @@ func TestPullStatus_UpdateProject(t *testing.T) {
}
_, err := b.UpdatePullWithResults(
pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
RepoRelDir: ".",
Workspace: "default",
Expand Down Expand Up @@ -617,7 +618,7 @@ func TestPullStatus_UpdateNewCommit(t *testing.T) {
}
_, err := b.UpdatePullWithResults(
pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
RepoRelDir: ".",
Workspace: "default",
Expand All @@ -628,7 +629,7 @@ func TestPullStatus_UpdateNewCommit(t *testing.T) {

pull.HeadCommit = "newsha"
status, err := b.UpdatePullWithResults(pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
RepoRelDir: ".",
Workspace: "staging",
Expand Down Expand Up @@ -680,22 +681,22 @@ func TestPullStatus_UpdateMerge(t *testing.T) {
}
_, err := b.UpdatePullWithResults(
pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
Command: models.PlanCommand,
Command: command.Plan,
RepoRelDir: "mergeme",
Workspace: "default",
Failure: "failure",
},
{
Command: models.PlanCommand,
Command: command.Plan,
RepoRelDir: "projectname",
Workspace: "default",
ProjectName: "projectname",
Failure: "failure",
},
{
Command: models.PlanCommand,
Command: command.Plan,
RepoRelDir: "staythesame",
Workspace: "default",
PlanSuccess: &models.PlanSuccess{
Expand All @@ -709,22 +710,22 @@ func TestPullStatus_UpdateMerge(t *testing.T) {
Ok(t, err)

updateStatus, err := b.UpdatePullWithResults(pull,
[]models.ProjectResult{
[]command.ProjectResult{
{
Command: models.ApplyCommand,
Command: command.Apply,
RepoRelDir: "mergeme",
Workspace: "default",
ApplySuccess: "applied!",
},
{
Command: models.ApplyCommand,
Command: command.Apply,
RepoRelDir: "projectname",
Workspace: "default",
ProjectName: "projectname",
Error: errors.New("apply error"),
},
{
Command: models.ApplyCommand,
Command: command.Apply,
RepoRelDir: "newresult",
Workspace: "default",
ApplySuccess: "success!",
Expand Down
Loading

0 comments on commit 90e92e3

Please sign in to comment.