-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ORCA-554] Add support for limiting number of projects per PR. (#41)
- Loading branch information
1 parent
ee2e5ed
commit 0b379f6
Showing
6 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/runatlantis/atlantis/server/events/models" | ||
) | ||
|
||
type SizeLimitedProjectCommandBuilder struct { | ||
Limit int | ||
ProjectCommandBuilder | ||
} | ||
|
||
func (b *SizeLimitedProjectCommandBuilder) BuildAutoplanCommands(ctx *CommandContext) ([]models.ProjectCommandContext, error) { | ||
projects, err := b.ProjectCommandBuilder.BuildAutoplanCommands(ctx) | ||
|
||
if err != nil { | ||
return projects, err | ||
} | ||
|
||
return projects, b.CheckAgainstLimit(projects) | ||
} | ||
|
||
func (b *SizeLimitedProjectCommandBuilder) BuildPlanCommands(ctx *CommandContext, comment *CommentCommand) ([]models.ProjectCommandContext, error) { | ||
projects, err := b.ProjectCommandBuilder.BuildPlanCommands(ctx, comment) | ||
|
||
if err != nil { | ||
return projects, err | ||
} | ||
|
||
return projects, b.CheckAgainstLimit(projects) | ||
} | ||
|
||
func (b *SizeLimitedProjectCommandBuilder) CheckAgainstLimit(projects []models.ProjectCommandContext) error { | ||
if b.Limit != InfiniteProjectsPerPR && len(projects) > b.Limit { | ||
return errors.New( | ||
fmt.Sprintf( | ||
"Number of projects cannot exceed %d. This can either be caused by:\n"+ | ||
"1) GH failure in recognizing the diff\n"+ | ||
"2) Pull Request batch is too large for the given Atlantis instance\n\n"+ | ||
"Please break this pull request into smaller batches and try again.", | ||
b.Limit, | ||
), | ||
) | ||
} | ||
return nil | ||
} |
143 changes: 143 additions & 0 deletions
143
server/events/size_limited_project_command_builder_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package events_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/petergtz/pegomock" | ||
"github.com/runatlantis/atlantis/server/events" | ||
"github.com/runatlantis/atlantis/server/events/mocks" | ||
"github.com/runatlantis/atlantis/server/events/models" | ||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
func TestSizeLimitedProjectCommandBuilder_autoplan(t *testing.T) { | ||
RegisterMockTestingT(t) | ||
|
||
delegate := mocks.NewMockProjectCommandBuilder() | ||
|
||
ctx := &events.CommandContext{} | ||
|
||
project1 := models.ProjectCommandContext{ | ||
ProjectName: "test1", | ||
} | ||
|
||
project2 := models.ProjectCommandContext{ | ||
ProjectName: "test2", | ||
} | ||
|
||
expectedResult := []models.ProjectCommandContext{project1, project2} | ||
|
||
t.Run("Limit Defined and Breached", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: 1, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildAutoplanCommands(ctx)).ThenReturn(expectedResult, nil) | ||
|
||
_, err := subject.BuildAutoplanCommands(ctx) | ||
|
||
ErrEquals(t, `Number of projects cannot exceed 1. This can either be caused by: | ||
1) GH failure in recognizing the diff | ||
2) Pull Request batch is too large for the given Atlantis instance | ||
Please break this pull request into smaller batches and try again.`, err) | ||
}) | ||
|
||
t.Run("Limit defined and not breached", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: 2, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildAutoplanCommands(ctx)).ThenReturn(expectedResult, nil) | ||
|
||
result, err := subject.BuildAutoplanCommands(ctx) | ||
|
||
Ok(t, err) | ||
|
||
Assert(t, len(result) == len(expectedResult), "size is expected") | ||
}) | ||
|
||
t.Run("Limit not defined", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: events.InfiniteProjectsPerPR, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildAutoplanCommands(ctx)).ThenReturn(expectedResult, nil) | ||
|
||
result, err := subject.BuildAutoplanCommands(ctx) | ||
|
||
Ok(t, err) | ||
|
||
Assert(t, len(result) == len(expectedResult), "size is expected") | ||
}) | ||
} | ||
|
||
func TestSizeLimitedProjectCommandBuilder_planComment(t *testing.T) { | ||
RegisterMockTestingT(t) | ||
|
||
delegate := mocks.NewMockProjectCommandBuilder() | ||
|
||
ctx := &events.CommandContext{} | ||
|
||
comment := &events.CommentCommand{} | ||
|
||
project1 := models.ProjectCommandContext{ | ||
ProjectName: "test1", | ||
} | ||
|
||
project2 := models.ProjectCommandContext{ | ||
ProjectName: "test2", | ||
} | ||
|
||
expectedResult := []models.ProjectCommandContext{project1, project2} | ||
|
||
t.Run("Limit Defined and Breached", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: 1, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildPlanCommands(ctx, comment)).ThenReturn(expectedResult, nil) | ||
|
||
_, err := subject.BuildPlanCommands(ctx, comment) | ||
|
||
ErrEquals(t, `Number of projects cannot exceed 1. This can either be caused by: | ||
1) GH failure in recognizing the diff | ||
2) Pull Request batch is too large for the given Atlantis instance | ||
Please break this pull request into smaller batches and try again.`, err) | ||
}) | ||
|
||
t.Run("Limit defined and not breached", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: 2, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildPlanCommands(ctx, comment)).ThenReturn(expectedResult, nil) | ||
|
||
result, err := subject.BuildPlanCommands(ctx, comment) | ||
|
||
Ok(t, err) | ||
|
||
Assert(t, len(result) == len(expectedResult), "size is expected") | ||
}) | ||
|
||
t.Run("Limit not defined", func(t *testing.T) { | ||
subject := &events.SizeLimitedProjectCommandBuilder{ | ||
Limit: events.InfiniteProjectsPerPR, | ||
ProjectCommandBuilder: delegate, | ||
} | ||
|
||
When(delegate.BuildPlanCommands(ctx, comment)).ThenReturn(expectedResult, nil) | ||
|
||
result, err := subject.BuildPlanCommands(ctx, comment) | ||
|
||
Ok(t, err) | ||
|
||
Assert(t, len(result) == len(expectedResult), "size is expected") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters