diff --git a/server/events/markdown_renderer.go b/server/events/markdown_renderer.go index 47b6309ea4..4aba2f210b 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -23,11 +23,11 @@ import ( "github.com/runatlantis/atlantis/server/events/models" ) -const ( - planCommandTitle = "Plan" - applyCommandTitle = "Apply" - policyCheckCommandTitle = "Policy Check" - approvePoliciesCommandTitle = "Approve Policies" +var ( + planCommandTitle = models.PlanCommand.TitleString() + applyCommandTitle = models.ApplyCommand.TitleString() + policyCheckCommandTitle = models.PolicyCheckCommand.TitleString() + approvePoliciesCommandTitle = models.ApprovePoliciesCommand.TitleString() // maxUnwrappedLines is the maximum number of lines the Terraform output // can be before we wrap it in an expandable template. maxUnwrappedLines = 12 diff --git a/server/events/models/models.go b/server/events/models/models.go index 817d4d1f90..04cf1b8a5a 100644 --- a/server/events/models/models.go +++ b/server/events/models/models.go @@ -624,6 +624,12 @@ const ( // Adding more? Don't forget to update String() below ) +// TitleString returns the string representation in title form. +// ie. policy_check becomes Policy Check +func (c CommandName) TitleString() string { + return strings.Title(strings.ReplaceAll(strings.ToLower(c.String()), "_", " ")) +} + // String returns the string representation of c. func (c CommandName) String() string { switch c { diff --git a/server/events/pull_updater.go b/server/events/pull_updater.go index bab9938771..3cc69f310e 100644 --- a/server/events/pull_updater.go +++ b/server/events/pull_updater.go @@ -16,11 +16,11 @@ func (c *PullUpdater) updatePull(ctx *CommandContext, command PullCommand, res C ctx.Log.Warn(res.Failure) } - // HidePrevPlanComments will hide old comments left from previous plan runs to reduce + // HidePrevCommandComments will hide old comments left from previous runs to reduce // clutter in a pull/merge request. This will not delete the comment, since the // comment trail may be useful in auditing or backtracing problems. if c.HidePrevPlanComments { - if err := c.VCSClient.HidePrevPlanComments(ctx.Pull.BaseRepo, ctx.Pull.Num); err != nil { + if err := c.VCSClient.HidePrevCommandComments(ctx.Pull.BaseRepo, ctx.Pull.Num, command.CommandName().TitleString()); err != nil { ctx.Log.Err("unable to hide old comments: %s", err) } } diff --git a/server/events/vcs/azuredevops_client.go b/server/events/vcs/azuredevops_client.go index beaf847921..2a166f8ce2 100644 --- a/server/events/vcs/azuredevops_client.go +++ b/server/events/vcs/azuredevops_client.go @@ -126,7 +126,7 @@ func (g *AzureDevopsClient) CreateComment(repo models.Repo, pullNum int, comment return nil } -func (g *AzureDevopsClient) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (g *AzureDevopsClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { return nil } diff --git a/server/events/vcs/bitbucketcloud/client.go b/server/events/vcs/bitbucketcloud/client.go index edf4786926..9ba1ca41ee 100644 --- a/server/events/vcs/bitbucketcloud/client.go +++ b/server/events/vcs/bitbucketcloud/client.go @@ -100,7 +100,7 @@ func (b *Client) CreateComment(repo models.Repo, pullNum int, comment string, co return err } -func (b *Client) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (b *Client) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { return nil } diff --git a/server/events/vcs/bitbucketserver/client.go b/server/events/vcs/bitbucketserver/client.go index a30ccd6f70..27af85354d 100644 --- a/server/events/vcs/bitbucketserver/client.go +++ b/server/events/vcs/bitbucketserver/client.go @@ -141,7 +141,7 @@ func (b *Client) CreateComment(repo models.Repo, pullNum int, comment string, co return nil } -func (b *Client) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (b *Client) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { return nil } diff --git a/server/events/vcs/client.go b/server/events/vcs/client.go index e5902aafbd..0f49cc89de 100644 --- a/server/events/vcs/client.go +++ b/server/events/vcs/client.go @@ -25,7 +25,7 @@ type Client interface { // relative to the repo root, e.g. parent/child/file.txt. GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) CreateComment(repo models.Repo, pullNum int, comment string, command string) error - HidePrevPlanComments(repo models.Repo, pullNum int) error + HidePrevCommandComments(repo models.Repo, pullNum int, command string) error PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error) PullIsMergeable(repo models.Repo, pull models.PullRequest) (bool, error) // UpdateStatus updates the commit status to state for pull. src is the diff --git a/server/events/vcs/github_client.go b/server/events/vcs/github_client.go index 9dd8180b07..b14f589ed2 100644 --- a/server/events/vcs/github_client.go +++ b/server/events/vcs/github_client.go @@ -169,7 +169,7 @@ func (g *GithubClient) CreateComment(repo models.Repo, pullNum int, comment stri return nil } -func (g *GithubClient) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (g *GithubClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { var allComments []*github.IssueComment nextPage := 0 for { @@ -205,7 +205,7 @@ func (g *GithubClient) HidePrevPlanComments(repo models.Repo, pullNum int) error continue } firstLine := strings.ToLower(body[0]) - if !strings.Contains(firstLine, models.PlanCommand.String()) { + if !strings.Contains(firstLine, strings.ToLower(command)) { continue } var m struct { diff --git a/server/events/vcs/github_client_test.go b/server/events/vcs/github_client_test.go index 0987a95cfd..115902dd88 100644 --- a/server/events/vcs/github_client_test.go +++ b/server/events/vcs/github_client_test.go @@ -214,7 +214,7 @@ func TestGithubClient_PaginatesComments(t *testing.T) { Ok(t, err) defer disableSSLVerification()() - err = client.HidePrevPlanComments( + err = client.HidePrevCommandComments( models.Repo{ FullName: "owner/repo", Owner: "owner", @@ -227,6 +227,7 @@ func TestGithubClient_PaginatesComments(t *testing.T) { }, }, 123, + models.PlanCommand.TitleString(), ) Ok(t, err) Equals(t, 2, len(gotMinimizeCalls)) @@ -302,7 +303,7 @@ func TestGithubClient_HideOldComments(t *testing.T) { Ok(t, err) defer disableSSLVerification()() - err = client.HidePrevPlanComments( + err = client.HidePrevCommandComments( models.Repo{ FullName: "owner/repo", Owner: "owner", @@ -315,6 +316,7 @@ func TestGithubClient_HideOldComments(t *testing.T) { }, }, 123, + models.PlanCommand.TitleString(), ) Ok(t, err) Equals(t, 3, len(gotMinimizeCalls)) diff --git a/server/events/vcs/gitlab_client.go b/server/events/vcs/gitlab_client.go index 9c3a2a4716..dffab011a5 100644 --- a/server/events/vcs/gitlab_client.go +++ b/server/events/vcs/gitlab_client.go @@ -148,7 +148,7 @@ func (g *GitlabClient) CreateComment(repo models.Repo, pullNum int, comment stri return err } -func (g *GitlabClient) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (g *GitlabClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { return nil } diff --git a/server/events/vcs/mocks/mock_client.go b/server/events/vcs/mocks/mock_client.go index b3e5c3a9f9..a39dd61e45 100644 --- a/server/events/vcs/mocks/mock_client.go +++ b/server/events/vcs/mocks/mock_client.go @@ -59,12 +59,12 @@ func (mock *MockClient) CreateComment(repo models.Repo, pullNum int, comment str return ret0 } -func (mock *MockClient) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (mock *MockClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { if mock == nil { panic("mock must not be nil. Use myMock := NewMockClient().") } - params := []pegomock.Param{repo, pullNum} - result := pegomock.GetGenericMockFrom(mock).Invoke("HidePrevPlanComments", params, []reflect.Type{reflect.TypeOf((*error)(nil)).Elem()}) + params := []pegomock.Param{repo, pullNum, command} + result := pegomock.GetGenericMockFrom(mock).Invoke("HidePrevCommandComments", params, []reflect.Type{reflect.TypeOf((*error)(nil)).Elem()}) var ret0 error if len(result) != 0 { if result[0] != nil { @@ -306,23 +306,23 @@ func (c *MockClient_CreateComment_OngoingVerification) GetAllCapturedArguments() return } -func (verifier *VerifierMockClient) HidePrevPlanComments(repo models.Repo, pullNum int) *MockClient_HidePrevPlanComments_OngoingVerification { - params := []pegomock.Param{repo, pullNum} - methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "HidePrevPlanComments", params, verifier.timeout) - return &MockClient_HidePrevPlanComments_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations} +func (verifier *VerifierMockClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) *MockClient_HidePrevCommandComments_OngoingVerification { + params := []pegomock.Param{repo, pullNum, command} + methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "HidePrevCommandComments", params, verifier.timeout) + return &MockClient_HidePrevCommandComments_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations} } -type MockClient_HidePrevPlanComments_OngoingVerification struct { +type MockClient_HidePrevCommandComments_OngoingVerification struct { mock *MockClient methodInvocations []pegomock.MethodInvocation } -func (c *MockClient_HidePrevPlanComments_OngoingVerification) GetCapturedArguments() (models.Repo, int) { - repo, pullNum := c.GetAllCapturedArguments() - return repo[len(repo)-1], pullNum[len(pullNum)-1] +func (c *MockClient_HidePrevCommandComments_OngoingVerification) GetCapturedArguments() (models.Repo, int, string) { + repo, pullNum, command := c.GetAllCapturedArguments() + return repo[len(repo)-1], pullNum[len(pullNum)-1], command[len(command)-1] } -func (c *MockClient_HidePrevPlanComments_OngoingVerification) GetAllCapturedArguments() (_param0 []models.Repo, _param1 []int) { +func (c *MockClient_HidePrevCommandComments_OngoingVerification) GetAllCapturedArguments() (_param0 []models.Repo, _param1 []int, _param2 []string) { params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations) if len(params) > 0 { _param0 = make([]models.Repo, len(c.methodInvocations)) @@ -333,6 +333,10 @@ func (c *MockClient_HidePrevPlanComments_OngoingVerification) GetAllCapturedArgu for u, param := range params[1] { _param1[u] = param.(int) } + _param2 = make([]string, len(c.methodInvocations)) + for u, param := range params[2] { + _param2[u] = param.(string) + } } return } diff --git a/server/events/vcs/not_configured_vcs_client.go b/server/events/vcs/not_configured_vcs_client.go index c73a6a22e2..3f8556765f 100644 --- a/server/events/vcs/not_configured_vcs_client.go +++ b/server/events/vcs/not_configured_vcs_client.go @@ -32,7 +32,7 @@ func (a *NotConfiguredVCSClient) GetModifiedFiles(repo models.Repo, pull models. func (a *NotConfiguredVCSClient) CreateComment(repo models.Repo, pullNum int, comment string, command string) error { return a.err() } -func (a *NotConfiguredVCSClient) HidePrevPlanComments(repo models.Repo, pullNum int) error { +func (a *NotConfiguredVCSClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { return nil } func (a *NotConfiguredVCSClient) PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error) { diff --git a/server/events/vcs/proxy.go b/server/events/vcs/proxy.go index a49895c0e2..c14563a467 100644 --- a/server/events/vcs/proxy.go +++ b/server/events/vcs/proxy.go @@ -60,8 +60,8 @@ func (d *ClientProxy) CreateComment(repo models.Repo, pullNum int, comment strin return d.clients[repo.VCSHost.Type].CreateComment(repo, pullNum, comment, command) } -func (d *ClientProxy) HidePrevPlanComments(repo models.Repo, pullNum int) error { - return d.clients[repo.VCSHost.Type].HidePrevPlanComments(repo, pullNum) +func (d *ClientProxy) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error { + return d.clients[repo.VCSHost.Type].HidePrevCommandComments(repo, pullNum, command) } func (d *ClientProxy) PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error) {