From 96e6dc385e4b5494a1f0ec963b4d0916fe887947 Mon Sep 17 00:00:00 2001 From: Nish Krishnan Date: Thu, 25 Feb 2021 12:04:14 -0800 Subject: [PATCH] Only account for plan commands in limit bug. (#47) --- .../size_limited_project_command_builder.go | 12 ++++++++- ...ze_limited_project_command_builder_test.go | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/server/events/size_limited_project_command_builder.go b/server/events/size_limited_project_command_builder.go index 984a9ecf41..909dbf425e 100644 --- a/server/events/size_limited_project_command_builder.go +++ b/server/events/size_limited_project_command_builder.go @@ -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"+ diff --git a/server/events/size_limited_project_command_builder_test.go b/server/events/size_limited_project_command_builder_test.go index 36f58a7107..7f77a64df5 100644 --- a/server/events/size_limited_project_command_builder_test.go +++ b/server/events/size_limited_project_command_builder_test.go @@ -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} @@ -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) { @@ -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}