Skip to content

Commit

Permalink
Only account for plan commands in limit bug. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishkrishnan authored and Nish Krishnan committed Jun 24, 2021
1 parent 0b379f6 commit 96e6dc3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/events/size_limited_project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ func (b *SizeLimitedProjectCommandBuilder) BuildPlanCommands(ctx *CommandContext
}

func (b *SizeLimitedProjectCommandBuilder) CheckAgainstLimit(projects []models.ProjectCommandContext) error {
if b.Limit != InfiniteProjectsPerPR && len(projects) > b.Limit {

var planCommands []models.ProjectCommandContext

for _, project := range projects {

if project.CommandName == models.PlanCommand {
planCommands = append(planCommands, project)
}
}

if b.Limit != InfiniteProjectsPerPR && len(planCommands) > b.Limit {
return errors.New(
fmt.Sprintf(
"Number of projects cannot exceed %d. This can either be caused by:\n"+
Expand Down
26 changes: 26 additions & 0 deletions server/events/size_limited_project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ func TestSizeLimitedProjectCommandBuilder_autoplan(t *testing.T) {

project1 := models.ProjectCommandContext{
ProjectName: "test1",
CommandName: models.PlanCommand,
}

project2 := models.ProjectCommandContext{
ProjectName: "test2",
CommandName: models.PlanCommand,
}

project3 := models.ProjectCommandContext{
ProjectName: "test1",
CommandName: models.PolicyCheckCommand,
}

expectedResult := []models.ProjectCommandContext{project1, project2}
Expand Down Expand Up @@ -73,6 +80,23 @@ Please break this pull request into smaller batches and try again.`, err)

Assert(t, len(result) == len(expectedResult), "size is expected")
})

t.Run("Only plan commands counted in limit", func(t *testing.T) {
subject := &events.SizeLimitedProjectCommandBuilder{
Limit: 2,
ProjectCommandBuilder: delegate,
}

resultWithPolicyCheckCommand := []models.ProjectCommandContext{project1, project2, project3}

When(delegate.BuildAutoplanCommands(ctx)).ThenReturn(resultWithPolicyCheckCommand, nil)

result, err := subject.BuildAutoplanCommands(ctx)

Ok(t, err)

Assert(t, len(result) == len(resultWithPolicyCheckCommand), "size is expected")
})
}

func TestSizeLimitedProjectCommandBuilder_planComment(t *testing.T) {
Expand All @@ -86,10 +110,12 @@ func TestSizeLimitedProjectCommandBuilder_planComment(t *testing.T) {

project1 := models.ProjectCommandContext{
ProjectName: "test1",
CommandName: models.PlanCommand,
}

project2 := models.ProjectCommandContext{
ProjectName: "test2",
CommandName: models.PlanCommand,
}

expectedResult := []models.ProjectCommandContext{project1, project2}
Expand Down

0 comments on commit 96e6dc3

Please sign in to comment.