From d376d24a5618b8516069a355528b19f08fc19d6e Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 05:43:28 +0900 Subject: [PATCH 1/6] make MarkdownRenderer private --- cmd/server_test.go | 2 +- .../events/events_controller_e2e_test.go | 4 +- server/core/locking/apply_locking.go | 6 +- server/core/locking/locking_test.go | 8 +- server/events/apply_command_runner.go | 2 +- server/events/apply_command_runner_test.go | 4 +- server/events/command_runner_test.go | 2 +- server/events/comment_parser.go | 12 +- server/events/markdown_renderer.go | 80 ++++---- server/events/markdown_renderer_test.go | 178 +++++++++--------- server/events/pull_updater.go | 2 +- server/server.go | 2 +- 12 files changed, 151 insertions(+), 151 deletions(-) diff --git a/cmd/server_test.go b/cmd/server_test.go index 572242b7e8..989766d2a3 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -57,7 +57,7 @@ var testFlags = map[string]interface{}{ ADWebhookPasswordFlag: "ad-wh-pass", ADWebhookUserFlag: "ad-wh-user", AtlantisURLFlag: "url", - AllowCommandsFlag: "version,plan,unlock,import,approve_policies", // apply is disabled by DisableApply + AllowCommandsFlag: "version,plan,unlock,import,approve_policies", // apply is disabled by disableApply AllowForkPRsFlag: true, AllowRepoConfigFlag: true, AutomergeFlag: true, diff --git a/server/controllers/events/events_controller_e2e_test.go b/server/controllers/events/events_controller_e2e_test.go index 5729c83060..1b230b568d 100644 --- a/server/controllers/events/events_controller_e2e_test.go +++ b/server/controllers/events/events_controller_e2e_test.go @@ -90,7 +90,7 @@ func TestGitHubWorkflow(t *testing.T) { ModifiedFiles []string // Comments are what our mock user writes to the pull request. Comments []string - // DisableApply flag used by userConfig object when initializing atlantis server. + // disableApply flag used by userConfig object when initializing atlantis server. DisableApply bool // ApplyLock creates an apply lock that temporarily disables apply command ApplyLock bool @@ -1157,7 +1157,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers pullUpdater := &events.PullUpdater{ HidePrevPlanComments: false, VCSClient: e2eVCSClient, - MarkdownRenderer: events.GetMarkdownRenderer(false, false, false, false, false, false, ""), + MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, ""), } autoMerger := &events.AutoMerger{ diff --git a/server/core/locking/apply_locking.go b/server/core/locking/apply_locking.go index d071ca8669..0adbc314f6 100644 --- a/server/core/locking/apply_locking.go +++ b/server/core/locking/apply_locking.go @@ -31,8 +31,8 @@ type ApplyLocker interface { // ApplyCommandLock contains information about apply command lock status. type ApplyCommandLock struct { // Locked is true is when apply commands are locked - // Either by using DisableApply flag or creating a global ApplyCommandLock - // DisableApply lock take precedence when set + // Either by using disableApply flag or creating a global ApplyCommandLock + // disableApply lock take precedence when set Locked bool Time time.Time Failure string @@ -77,7 +77,7 @@ func (c *ApplyClient) LockApply() (ApplyCommandLock, error) { // this function returns an error func (c *ApplyClient) UnlockApply() error { if c.disableApplyFlag { - return errors.New("apply commands are disabled until DisableApply flag is unset") + return errors.New("apply commands are disabled until disableApply flag is unset") } err := c.backend.UnlockCommand(command.Apply) diff --git a/server/core/locking/locking_test.go b/server/core/locking/locking_test.go index 0d7de72cb9..1cb50fa7d4 100644 --- a/server/core/locking/locking_test.go +++ b/server/core/locking/locking_test.go @@ -203,7 +203,7 @@ func TestApplyLocker(t *testing.T) { Assert(t, !lock.Locked, "exp false") }) - t.Run("can't lock if userConfig.DisableApply is set", func(t *testing.T) { + t.Run("can't lock if userConfig.disableApply is set", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) @@ -233,12 +233,12 @@ func TestApplyLocker(t *testing.T) { Equals(t, errExpected, err) }) - t.Run("can't unlock if userConfig.DisableApply is set", func(t *testing.T) { + t.Run("can't unlock if userConfig.disableApply is set", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) err := l.UnlockApply() - ErrEquals(t, "apply commands are disabled until DisableApply flag is unset", err) + ErrEquals(t, "apply commands are disabled until disableApply flag is unset", err) backend.VerifyWasCalled(Never()).UnlockCommand(matchers.AnyCommandName()) }) @@ -265,7 +265,7 @@ func TestApplyLocker(t *testing.T) { Equals(t, lock.Locked, false) }) - t.Run("when DisableApply flag is set always return a lock", func(t *testing.T) { + t.Run("when disableApply flag is set always return a lock", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) diff --git a/server/events/apply_command_runner.go b/server/events/apply_command_runner.go index eae9ec8593..5094f40d76 100644 --- a/server/events/apply_command_runner.go +++ b/server/events/apply_command_runner.go @@ -71,7 +71,7 @@ func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) { pull := ctx.Pull locked, err := a.IsLocked() - // CheckApplyLock falls back to DisableApply flag if fetching the lock + // CheckApplyLock falls back to disableApply flag if fetching the lock // raises an error // We will log failure as warning if err != nil { diff --git a/server/events/apply_command_runner_test.go b/server/events/apply_command_runner_test.go index 514ca6f5c6..c79f2233cd 100644 --- a/server/events/apply_command_runner_test.go +++ b/server/events/apply_command_runner_test.go @@ -31,13 +31,13 @@ func TestApplyCommandRunner_IsLocked(t *testing.T) { ExpComment: "**Error:** Running `atlantis apply` is disabled.", }, { - Description: "When no global apply lock is present and DisableApply flag is false IsDisabled returns false", + Description: "When no global apply lock is present and disableApply flag is false IsDisabled returns false", ApplyLocked: false, ApplyLockError: nil, ExpComment: "Ran Apply for 0 projects:\n\n\n\n", }, { - Description: "If ApplyLockChecker returns an error IsDisabled return value of DisableApply flag", + Description: "If ApplyLockChecker returns an error IsDisabled return value of disableApply flag", ApplyLockError: errors.New("error"), ApplyLocked: false, ExpComment: "Ran Apply for 0 projects:\n\n\n\n", diff --git a/server/events/command_runner_test.go b/server/events/command_runner_test.go index 1dd74be6a7..b3d5f85bf8 100644 --- a/server/events/command_runner_test.go +++ b/server/events/command_runner_test.go @@ -118,7 +118,7 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock pullUpdater = &events.PullUpdater{ HidePrevPlanComments: false, VCSClient: vcsClient, - MarkdownRenderer: events.GetMarkdownRenderer(false, false, false, false, false, false, ""), + MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, ""), } autoMerger = &events.AutoMerger{ diff --git a/server/events/comment_parser.go b/server/events/comment_parser.go index 3c64cc93f8..e22338bdd3 100644 --- a/server/events/comment_parser.go +++ b/server/events/comment_parser.go @@ -443,23 +443,23 @@ var helpCommentTemplate = "```cmake\n" + Terraform Pull Request Automation Usage: - {{ .ExecutableName }} [options] -- [terraform options] + {{ .executableName }} [options] -- [terraform options] Examples: # show atlantis help - {{ .ExecutableName }} help + {{ .executableName }} help {{- if .AllowPlan }} # run plan in the root directory passing the -target flag to terraform - {{ .ExecutableName }} plan -d . -- -target=resource + {{ .executableName }} plan -d . -- -target=resource {{- end }} {{- if .AllowApply }} # apply all unapplied plans from this pull request - {{ .ExecutableName }} apply + {{ .executableName }} apply # apply the plan for the root directory and staging workspace - {{ .ExecutableName }} apply -d . -w staging + {{ .executableName }} apply -d . -w staging {{- end }} Commands: @@ -491,7 +491,7 @@ Commands: Flags: -h, --help help for atlantis -Use "{{ .ExecutableName }} [command] --help" for more information about a command.` + +Use "{{ .executableName }} [command] --help" for more information about a command.` + "\n```" // DidYouMeanAtlantisComment is the comment we add to the pull request when diff --git a/server/events/markdown_renderer.go b/server/events/markdown_renderer.go index 7708adf3ef..d9e4866cb4 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -42,18 +42,18 @@ var ( templatesFS embed.FS ) -// MarkdownRenderer renders responses as markdown. -type MarkdownRenderer struct { - // GitlabSupportsCommonMark is true if the version of GitLab we're +// markdownRenderer renders responses as markdown. +type markdownRenderer struct { + // gitlabSupportsCommonMark is true if the version of GitLab we're // using supports the CommonMark markdown format. // If we're not configured with a GitLab client, this will be false. - GitlabSupportsCommonMark bool - DisableApplyAll bool - DisableApply bool - DisableMarkdownFolding bool - DisableRepoLocking bool - EnableDiffMarkdownFormat bool - MarkdownTemplates *template.Template + gitlabSupportsCommonMark bool + disableApplyAll bool + disableApply bool + disableMarkdownFolding bool + disableRepoLocking bool + enableDiffMarkdownFormat bool + markdownTemplates *template.Template } // commonData is data that all responses have. @@ -108,48 +108,48 @@ type projectResultTmplData struct { } // Initialize templates -func GetMarkdownRenderer( - GitlabSupportsCommonMark bool, - DisableApplyAll bool, - DisableApply bool, - DisableMarkdownFolding bool, - DisableRepoLocking bool, - EnableDiffMarkdownFormat bool, - MarkdownTemplateOverridesDir string, -) *MarkdownRenderer { +func NewMarkdownRenderer( + gitlabSupportsCommonMark bool, + disableApplyAll bool, + disableApply bool, + disableMarkdownFolding bool, + disableRepoLocking bool, + enableDiffMarkdownFormat bool, + markdownTemplateOverridesDir string, +) *markdownRenderer { var templates *template.Template templates, _ = template.New("").Funcs(sprig.TxtFuncMap()).ParseFS(templatesFS, "templates/*.tmpl") - if overrides, err := templates.ParseGlob(fmt.Sprintf("%s/*.tmpl", MarkdownTemplateOverridesDir)); err == nil { + if overrides, err := templates.ParseGlob(fmt.Sprintf("%s/*.tmpl", markdownTemplateOverridesDir)); err == nil { // doesn't override if templates directory doesn't exist templates = overrides } - return &MarkdownRenderer{ - GitlabSupportsCommonMark: GitlabSupportsCommonMark, - DisableApplyAll: DisableApplyAll, - DisableMarkdownFolding: DisableMarkdownFolding, - DisableApply: DisableApply, - DisableRepoLocking: DisableRepoLocking, - EnableDiffMarkdownFormat: EnableDiffMarkdownFormat, - MarkdownTemplates: templates, + return &markdownRenderer{ + gitlabSupportsCommonMark: gitlabSupportsCommonMark, + disableApplyAll: disableApplyAll, + disableMarkdownFolding: disableMarkdownFolding, + disableApply: disableApply, + disableRepoLocking: disableRepoLocking, + enableDiffMarkdownFormat: enableDiffMarkdownFormat, + markdownTemplates: templates, } } // Render formats the data into a markdown string. // nolint: interfacer -func (m *MarkdownRenderer) Render(res command.Result, cmdName command.Name, log string, verbose bool, vcsHost models.VCSHostType) string { +func (m *markdownRenderer) Render(res command.Result, cmdName command.Name, log string, verbose bool, vcsHost models.VCSHostType) string { commandStr := cases.Title(language.English).String(strings.Replace(cmdName.String(), "_", " ", -1)) common := commonData{ Command: commandStr, Verbose: verbose, Log: log, PlansDeleted: res.PlansDeleted, - DisableApplyAll: m.DisableApplyAll || m.DisableApply, - DisableApply: m.DisableApply, - DisableRepoLocking: m.DisableRepoLocking, - EnableDiffMarkdownFormat: m.EnableDiffMarkdownFormat, + DisableApplyAll: m.disableApplyAll || m.disableApply, + DisableApply: m.disableApply, + DisableRepoLocking: m.disableRepoLocking, + EnableDiffMarkdownFormat: m.enableDiffMarkdownFormat, } - templates := m.MarkdownTemplates + templates := m.markdownTemplates if res.Error != nil { return m.renderTemplate(templates.Lookup("unwrappedErrWithLog"), errData{res.Error.Error(), common}) @@ -160,13 +160,13 @@ func (m *MarkdownRenderer) Render(res command.Result, cmdName command.Name, log return m.renderProjectResults(res.ProjectResults, common, vcsHost) } -func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, common commonData, vcsHost models.VCSHostType) string { +func (m *markdownRenderer) renderProjectResults(results []command.ProjectResult, common commonData, vcsHost models.VCSHostType) string { var resultsTmplData []projectResultTmplData numPlanSuccesses := 0 numPolicyCheckSuccesses := 0 numVersionSuccesses := 0 - templates := m.MarkdownTemplates + templates := m.markdownTemplates for _, result := range results { resultData := projectResultTmplData{ @@ -272,8 +272,8 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, // templates that collapse the output to make the comment smaller on initial // load. Some VCS providers or versions of VCS providers don't support this // syntax. -func (m *MarkdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, output string) bool { - if m.DisableMarkdownFolding { +func (m *markdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, output string) bool { + if m.disableMarkdownFolding { return false } @@ -282,14 +282,14 @@ func (m *MarkdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, outp return false } - if vcsHost == models.Gitlab && !m.GitlabSupportsCommonMark { + if vcsHost == models.Gitlab && !m.gitlabSupportsCommonMark { return false } return strings.Count(output, "\n") > maxUnwrappedLines } -func (m *MarkdownRenderer) renderTemplate(tmpl *template.Template, data interface{}) string { +func (m *markdownRenderer) renderTemplate(tmpl *template.Template, data interface{}) string { buf := &bytes.Buffer{} if err := tmpl.Execute(buf, data); err != nil { return fmt.Sprintf("Failed to render template, this is a bug: %v", err) diff --git a/server/events/markdown_renderer_test.go b/server/events/markdown_renderer_test.go index 6a9151a3cd..507e7e51d7 100644 --- a/server/events/markdown_renderer_test.go +++ b/server/events/markdown_renderer_test.go @@ -57,7 +57,7 @@ func TestRenderErr(t *testing.T) { }, } - r := events.GetMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") for _, c := range cases { res := command.Result{ Error: c.Error, @@ -102,7 +102,7 @@ func TestRenderFailure(t *testing.T) { }, } - r := events.GetMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") for _, c := range cases { res := command.Result{ Failure: c.Failure, @@ -121,7 +121,7 @@ func TestRenderFailure(t *testing.T) { } func TestRenderErrAndFailure(t *testing.T) { - r := events.GetMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") res := command.Result{ Error: errors.New("error"), Failure: "failure", @@ -791,7 +791,7 @@ $$$ }, } - r := events.GetMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") for _, c := range cases { t.Run(c.Description, func(t *testing.T) { res := command.Result{ @@ -942,13 +942,13 @@ $$$ `, }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) for _, c := range cases { @@ -1094,13 +1094,13 @@ $$$ }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - true, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) for _, c := range cases { @@ -1131,13 +1131,13 @@ func TestRenderCustomPolicyCheckTemplate_DisableApplyAll(t *testing.T) { Ok(t, err) err = os.WriteFile(filePath, []byte("{{ define \"policyCheckSuccessUnwrapped\" -}}somecustometext{{- end}}\n"), 0600) Ok(t, err) - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - true, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat tmpDir, // MarkdownTemplateOverridesDir ) @@ -1162,13 +1162,13 @@ func TestRenderCustomPolicyCheckTemplate_DisableApplyAll(t *testing.T) { // Test that if folding is disabled that it's not used. func TestRenderProjectResults_DisableFolding(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - true, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + true, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) @@ -1252,13 +1252,13 @@ func TestRenderProjectResults_WrappedErr(t *testing.T) { for _, c := range cases { t.Run(fmt.Sprintf("%s_%v", c.VCSHost.String(), c.ShouldWrap), func(t *testing.T) { - mr := events.GetMarkdownRenderer( - c.GitlabCommonMarkSupport, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + c.GitlabCommonMarkSupport, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) @@ -1370,13 +1370,13 @@ func TestRenderProjectResults_WrapSingleProject(t *testing.T) { for _, cmd := range []command.Name{command.Plan, command.Apply} { t.Run(fmt.Sprintf("%s_%s_%v", c.VCSHost.String(), cmd.String(), c.ShouldWrap), func(t *testing.T) { - mr := events.GetMarkdownRenderer( - c.GitlabCommonMarkSupport, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + c.GitlabCommonMarkSupport, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) var pr command.ProjectResult @@ -1481,13 +1481,13 @@ $$$ } func TestRenderProjectResults_MultiProjectApplyWrapped(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) tfOut := strings.Repeat("line\n", 13) @@ -1535,13 +1535,13 @@ $$$ } func TestRenderProjectResults_MultiProjectPlanWrapped(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) tfOut := strings.Repeat("line\n", 13) + "Plan: 1 to add, 0 to change, 0 to destroy." @@ -1716,13 +1716,13 @@ This plan was not saved because one or more projects failed and automerge requir for name, c := range cases { t.Run(name, func(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + mr := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) rendered := mr.Render(c.cr, command.Plan, "log", false, models.Github) @@ -2184,16 +2184,16 @@ $$$ }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) - r.DisableRepoLocking = true + r.disableRepoLocking = true for _, c := range cases { t.Run(c.Description, func(t *testing.T) { res := command.Result{ @@ -2623,13 +2623,13 @@ Plan: 1 to add, 2 to change, 1 to destroy. } func TestRenderProjectResultsWithEnableDiffMarkdownFormat(t *testing.T) { - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - true, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - true, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) @@ -2658,13 +2658,13 @@ var Render string func BenchmarkRenderProjectResultsWithEnableDiffMarkdownFormat(b *testing.B) { var render string - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - true, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - true, // EnableDiffMarkdownFormat + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir ) diff --git a/server/events/pull_updater.go b/server/events/pull_updater.go index 3c83550974..1166d142e6 100644 --- a/server/events/pull_updater.go +++ b/server/events/pull_updater.go @@ -8,7 +8,7 @@ import ( type PullUpdater struct { HidePrevPlanComments bool VCSClient vcs.Client - MarkdownRenderer *MarkdownRenderer + MarkdownRenderer *markdownRenderer } func (c *PullUpdater) updatePull(ctx *command.Context, cmd PullCommand, res command.Result) { diff --git a/server/server.go b/server/server.go index 4d87277bbb..baf76c4907 100644 --- a/server/server.go +++ b/server/server.go @@ -412,7 +412,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { if err != nil && flag.Lookup("test.v") == nil { return nil, errors.Wrap(err, "initializing terraform") } - markdownRenderer := events.GetMarkdownRenderer( + markdownRenderer := events.NewMarkdownRenderer( gitlabClient.SupportsCommonMark(), userConfig.DisableApplyAll, userConfig.DisableMarkdownFolding, From 382604de6d6467e652f57b1490b3b307eb4a47c3 Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 05:51:07 +0900 Subject: [PATCH 2/6] ExecutableName is available on markdown renderer common data --- .../events/events_controller_e2e_test.go | 2 +- server/events/command_runner_test.go | 2 +- server/events/markdown_renderer.go | 5 + server/events/markdown_renderer_test.go | 161 ++++++++++-------- server/server.go | 1 + 5 files changed, 94 insertions(+), 77 deletions(-) diff --git a/server/controllers/events/events_controller_e2e_test.go b/server/controllers/events/events_controller_e2e_test.go index 1b230b568d..5401e0b594 100644 --- a/server/controllers/events/events_controller_e2e_test.go +++ b/server/controllers/events/events_controller_e2e_test.go @@ -1157,7 +1157,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers pullUpdater := &events.PullUpdater{ HidePrevPlanComments: false, VCSClient: e2eVCSClient, - MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, ""), + MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis"), } autoMerger := &events.AutoMerger{ diff --git a/server/events/command_runner_test.go b/server/events/command_runner_test.go index b3d5f85bf8..e65cdf5620 100644 --- a/server/events/command_runner_test.go +++ b/server/events/command_runner_test.go @@ -118,7 +118,7 @@ func setup(t *testing.T, options ...func(testConfig *TestConfig)) *vcsmocks.Mock pullUpdater = &events.PullUpdater{ HidePrevPlanComments: false, VCSClient: vcsClient, - MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, ""), + MarkdownRenderer: events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis"), } autoMerger = &events.AutoMerger{ diff --git a/server/events/markdown_renderer.go b/server/events/markdown_renderer.go index d9e4866cb4..3819436434 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -54,6 +54,7 @@ type markdownRenderer struct { disableRepoLocking bool enableDiffMarkdownFormat bool markdownTemplates *template.Template + executableName string } // commonData is data that all responses have. @@ -66,6 +67,7 @@ type commonData struct { DisableApply bool DisableRepoLocking bool EnableDiffMarkdownFormat bool + ExecutableName string } // errData is data about an error response. @@ -116,6 +118,7 @@ func NewMarkdownRenderer( disableRepoLocking bool, enableDiffMarkdownFormat bool, markdownTemplateOverridesDir string, + executableName string, ) *markdownRenderer { var templates *template.Template templates, _ = template.New("").Funcs(sprig.TxtFuncMap()).ParseFS(templatesFS, "templates/*.tmpl") @@ -131,6 +134,7 @@ func NewMarkdownRenderer( disableRepoLocking: disableRepoLocking, enableDiffMarkdownFormat: enableDiffMarkdownFormat, markdownTemplates: templates, + executableName: executableName, } } @@ -147,6 +151,7 @@ func (m *markdownRenderer) Render(res command.Result, cmdName command.Name, log DisableApply: m.disableApply, DisableRepoLocking: m.disableRepoLocking, EnableDiffMarkdownFormat: m.enableDiffMarkdownFormat, + ExecutableName: m.executableName, } templates := m.markdownTemplates diff --git a/server/events/markdown_renderer_test.go b/server/events/markdown_renderer_test.go index 507e7e51d7..f0615574cf 100644 --- a/server/events/markdown_renderer_test.go +++ b/server/events/markdown_renderer_test.go @@ -57,7 +57,7 @@ func TestRenderErr(t *testing.T) { }, } - r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis") for _, c := range cases { res := command.Result{ Error: c.Error, @@ -102,7 +102,7 @@ func TestRenderFailure(t *testing.T) { }, } - r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis") for _, c := range cases { res := command.Result{ Failure: c.Failure, @@ -121,7 +121,7 @@ func TestRenderFailure(t *testing.T) { } func TestRenderErrAndFailure(t *testing.T) { - r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis") res := command.Result{ Error: errors.New("error"), Failure: "failure", @@ -791,7 +791,7 @@ $$$ }, } - r := events.NewMarkdownRenderer(false, false, false, false, false, false, "") + r := events.NewMarkdownRenderer(false, false, false, false, false, false, "", "atlantis") for _, c := range cases { t.Run(c.Description, func(t *testing.T) { res := command.Result{ @@ -943,13 +943,14 @@ $$$ }, } r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - true, // disableApplyAll - false, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + true, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { t.Run(c.Description, func(t *testing.T) { @@ -1095,13 +1096,14 @@ $$$ } r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - true, // disableApplyAll - true, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { t.Run(c.Description, func(t *testing.T) { @@ -1132,13 +1134,14 @@ func TestRenderCustomPolicyCheckTemplate_DisableApplyAll(t *testing.T) { err = os.WriteFile(filePath, []byte("{{ define \"policyCheckSuccessUnwrapped\" -}}somecustometext{{- end}}\n"), 0600) Ok(t, err) r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - true, // disableApplyAll - true, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - tmpDir, // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + tmpDir, // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := r.Render(command.Result{ @@ -1163,13 +1166,14 @@ func TestRenderCustomPolicyCheckTemplate_DisableApplyAll(t *testing.T) { // Test that if folding is disabled that it's not used. func TestRenderProjectResults_DisableFolding(t *testing.T) { mr := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - false, // disableApplyAll - false, // disableApply - true, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + true, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := mr.Render(command.Result{ @@ -1260,6 +1264,7 @@ func TestRenderProjectResults_WrappedErr(t *testing.T) { false, // disableRepoLocking false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := mr.Render(command.Result{ @@ -1378,6 +1383,7 @@ func TestRenderProjectResults_WrapSingleProject(t *testing.T) { false, // disableRepoLocking false, // enableDiffMarkdownFormat "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) var pr command.ProjectResult switch cmd { @@ -1482,13 +1488,14 @@ $$$ func TestRenderProjectResults_MultiProjectApplyWrapped(t *testing.T) { mr := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - false, // disableApplyAll - false, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) tfOut := strings.Repeat("line\n", 13) rendered := mr.Render(command.Result{ @@ -1536,13 +1543,14 @@ $$$ func TestRenderProjectResults_MultiProjectPlanWrapped(t *testing.T) { mr := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - false, // disableApplyAll - false, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) tfOut := strings.Repeat("line\n", 13) + "Plan: 1 to add, 0 to change, 0 to destroy." rendered := mr.Render(command.Result{ @@ -1717,13 +1725,14 @@ This plan was not saved because one or more projects failed and automerge requir for name, c := range cases { t.Run(name, func(t *testing.T) { mr := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - false, // disableApplyAll - false, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := mr.Render(c.cr, command.Plan, "log", false, models.Github) expWithBackticks := strings.Replace(c.exp, "$", "`", -1) @@ -2185,15 +2194,15 @@ $$$ } r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - false, // disableApplyAll - false, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - false, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + false, // disableMarkdownFolding + true, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) - r.disableRepoLocking = true for _, c := range cases { t.Run(c.Description, func(t *testing.T) { res := command.Result{ @@ -2624,13 +2633,14 @@ Plan: 1 to add, 2 to change, 1 to destroy. func TestRenderProjectResultsWithEnableDiffMarkdownFormat(t *testing.T) { r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - true, // disableApplyAll - true, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - true, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { @@ -2659,13 +2669,14 @@ func BenchmarkRenderProjectResultsWithEnableDiffMarkdownFormat(b *testing.B) { var render string r := events.NewMarkdownRenderer( - false, // gitlabSupportsCommonMark - true, // disableApplyAll - true, // disableApply - false, // disableMarkdownFolding - false, // disableRepoLocking - true, // enableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { diff --git a/server/server.go b/server/server.go index baf76c4907..61e00afde6 100644 --- a/server/server.go +++ b/server/server.go @@ -420,6 +420,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { userConfig.DisableRepoLocking, userConfig.EnableDiffMarkdownFormat, userConfig.MarkdownTemplateOverridesDir, + userConfig.ExecutableName, ) var lockingClient locking.Locker From c1b896370aef16643c73f2d5c3aeaa61432a9367 Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 05:52:27 +0900 Subject: [PATCH 3/6] replace template atlantis name with .ExecutableName --- server/events/templates/multi_project_plan.tmpl | 4 ++-- server/events/templates/single_project_plan_success.tmpl | 4 ++-- server/events/templates/unwrapped_err.tmpl | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/events/templates/multi_project_plan.tmpl b/server/events/templates/multi_project_plan.tmpl index 9ee33c590f..2686e43fdf 100644 --- a/server/events/templates/multi_project_plan.tmpl +++ b/server/events/templates/multi_project_plan.tmpl @@ -5,7 +5,7 @@ {{ if ne $disableApplyAll true }}--- {{end}}{{end}}{{ if ne .DisableApplyAll true }}{{ if and (gt (len .Results) 0) (not .PlansDeleted) }}* :fast_forward: To **apply** all unapplied plans from this pull request, comment: - * `atlantis apply` + * `{{ .ExecutableName }} apply` * :put_litter_in_its_place: To delete all plans and locks for the PR, comment: - * `atlantis unlock`{{end}}{{end}}{{ template "log" . }} + * `{{ .ExecutableName }} unlock`{{end}}{{end}}{{ template "log" . }} {{ end }} diff --git a/server/events/templates/single_project_plan_success.tmpl b/server/events/templates/single_project_plan_success.tmpl index 08c1624037..499fa8aa26 100644 --- a/server/events/templates/single_project_plan_success.tmpl +++ b/server/events/templates/single_project_plan_success.tmpl @@ -5,7 +5,7 @@ {{ if ne .DisableApplyAll true }}--- * :fast_forward: To **apply** all unapplied plans from this pull request, comment: - * `atlantis apply` + * `{{ .ExecutableName }} apply` * :put_litter_in_its_place: To delete all plans and locks for the PR, comment: - * `atlantis unlock`{{ end }}{{ template "log" . }} + * `{{ .ExecutableName }} unlock`{{ end }}{{ template "log" . }} {{ end }} diff --git a/server/events/templates/unwrapped_err.tmpl b/server/events/templates/unwrapped_err.tmpl index 3d732e4ae6..2a33873eae 100644 --- a/server/events/templates/unwrapped_err.tmpl +++ b/server/events/templates/unwrapped_err.tmpl @@ -4,7 +4,7 @@ {{.Error}} ```{{ if eq .Command "Policy Check" }} * :heavy_check_mark: To **approve** failing policies an authorized approver can comment: - * `atlantis approve_policies` + * `{{ .ExecutableName }} approve_policies` * :repeat: Or, address the policy failure by modifying the codebase and re-planning. {{ end }} {{- end }} From d7956241cc11c3cc245a72c05ed7fdfc6c9e660c Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 05:59:44 +0900 Subject: [PATCH 4/6] MarkdownRenderer fields can be private, but itself needs public --- server/events/markdown_renderer.go | 16 ++++++++-------- server/events/pull_updater.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/server/events/markdown_renderer.go b/server/events/markdown_renderer.go index 3819436434..01e6994208 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -42,8 +42,8 @@ var ( templatesFS embed.FS ) -// markdownRenderer renders responses as markdown. -type markdownRenderer struct { +// MarkdownRenderer renders responses as markdown. +type MarkdownRenderer struct { // gitlabSupportsCommonMark is true if the version of GitLab we're // using supports the CommonMark markdown format. // If we're not configured with a GitLab client, this will be false. @@ -119,14 +119,14 @@ func NewMarkdownRenderer( enableDiffMarkdownFormat bool, markdownTemplateOverridesDir string, executableName string, -) *markdownRenderer { +) *MarkdownRenderer { var templates *template.Template templates, _ = template.New("").Funcs(sprig.TxtFuncMap()).ParseFS(templatesFS, "templates/*.tmpl") if overrides, err := templates.ParseGlob(fmt.Sprintf("%s/*.tmpl", markdownTemplateOverridesDir)); err == nil { // doesn't override if templates directory doesn't exist templates = overrides } - return &markdownRenderer{ + return &MarkdownRenderer{ gitlabSupportsCommonMark: gitlabSupportsCommonMark, disableApplyAll: disableApplyAll, disableMarkdownFolding: disableMarkdownFolding, @@ -140,7 +140,7 @@ func NewMarkdownRenderer( // Render formats the data into a markdown string. // nolint: interfacer -func (m *markdownRenderer) Render(res command.Result, cmdName command.Name, log string, verbose bool, vcsHost models.VCSHostType) string { +func (m *MarkdownRenderer) Render(res command.Result, cmdName command.Name, log string, verbose bool, vcsHost models.VCSHostType) string { commandStr := cases.Title(language.English).String(strings.Replace(cmdName.String(), "_", " ", -1)) common := commonData{ Command: commandStr, @@ -165,7 +165,7 @@ func (m *markdownRenderer) Render(res command.Result, cmdName command.Name, log return m.renderProjectResults(res.ProjectResults, common, vcsHost) } -func (m *markdownRenderer) renderProjectResults(results []command.ProjectResult, common commonData, vcsHost models.VCSHostType) string { +func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, common commonData, vcsHost models.VCSHostType) string { var resultsTmplData []projectResultTmplData numPlanSuccesses := 0 numPolicyCheckSuccesses := 0 @@ -277,7 +277,7 @@ func (m *markdownRenderer) renderProjectResults(results []command.ProjectResult, // templates that collapse the output to make the comment smaller on initial // load. Some VCS providers or versions of VCS providers don't support this // syntax. -func (m *markdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, output string) bool { +func (m *MarkdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, output string) bool { if m.disableMarkdownFolding { return false } @@ -294,7 +294,7 @@ func (m *markdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, outp return strings.Count(output, "\n") > maxUnwrappedLines } -func (m *markdownRenderer) renderTemplate(tmpl *template.Template, data interface{}) string { +func (m *MarkdownRenderer) renderTemplate(tmpl *template.Template, data interface{}) string { buf := &bytes.Buffer{} if err := tmpl.Execute(buf, data); err != nil { return fmt.Sprintf("Failed to render template, this is a bug: %v", err) diff --git a/server/events/pull_updater.go b/server/events/pull_updater.go index 1166d142e6..3c83550974 100644 --- a/server/events/pull_updater.go +++ b/server/events/pull_updater.go @@ -8,7 +8,7 @@ import ( type PullUpdater struct { HidePrevPlanComments bool VCSClient vcs.Client - MarkdownRenderer *markdownRenderer + MarkdownRenderer *MarkdownRenderer } func (c *PullUpdater) updatePull(ctx *command.Context, cmd PullCommand, res command.Result) { From b8f1aae17351aaac14da951f542c1ff3de6797dd Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 06:16:07 +0900 Subject: [PATCH 5/6] reuse commonData anywhere in renderer --- server/events/markdown_renderer.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/server/events/markdown_renderer.go b/server/events/markdown_renderer.go index 01e6994208..5b4759189a 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -184,21 +184,9 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, if m.shouldUseWrappedTmpl(vcsHost, result.Error.Error()) { tmpl = templates.Lookup("wrappedErr") } - resultData.Rendered = m.renderTemplate(tmpl, struct { - Command string - Error string - }{ - Command: common.Command, - Error: result.Error.Error(), - }) + resultData.Rendered = m.renderTemplate(tmpl, errData{result.Error.Error(), common}) } else if result.Failure != "" { - resultData.Rendered = m.renderTemplate(templates.Lookup("failure"), struct { - Command string - Failure string - }{ - Command: common.Command, - Failure: result.Failure, - }) + resultData.Rendered = m.renderTemplate(templates.Lookup("failure"), failureData{result.Failure, common}) } else if result.PlanSuccess != nil { if m.shouldUseWrappedTmpl(vcsHost, result.PlanSuccess.TerraformOutput) { resultData.Rendered = m.renderTemplate(templates.Lookup("planSuccessWrapped"), planSuccessData{PlanSuccess: *result.PlanSuccess, PlanSummary: result.PlanSuccess.Summary(), PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking, EnableDiffMarkdownFormat: common.EnableDiffMarkdownFormat}) From b1c68bc3802188a65a193d4ef083785b4ffec54e Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Sat, 7 Jan 2023 07:09:43 +0900 Subject: [PATCH 6/6] revert --- cmd/server_test.go | 2 +- .../controllers/events/events_controller_e2e_test.go | 2 +- server/core/locking/apply_locking.go | 6 +++--- server/core/locking/locking_test.go | 8 ++++---- server/events/apply_command_runner.go | 2 +- server/events/apply_command_runner_test.go | 4 ++-- server/events/comment_parser.go | 12 ++++++------ 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/server_test.go b/cmd/server_test.go index 989766d2a3..572242b7e8 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -57,7 +57,7 @@ var testFlags = map[string]interface{}{ ADWebhookPasswordFlag: "ad-wh-pass", ADWebhookUserFlag: "ad-wh-user", AtlantisURLFlag: "url", - AllowCommandsFlag: "version,plan,unlock,import,approve_policies", // apply is disabled by disableApply + AllowCommandsFlag: "version,plan,unlock,import,approve_policies", // apply is disabled by DisableApply AllowForkPRsFlag: true, AllowRepoConfigFlag: true, AutomergeFlag: true, diff --git a/server/controllers/events/events_controller_e2e_test.go b/server/controllers/events/events_controller_e2e_test.go index 5401e0b594..aade27a029 100644 --- a/server/controllers/events/events_controller_e2e_test.go +++ b/server/controllers/events/events_controller_e2e_test.go @@ -90,7 +90,7 @@ func TestGitHubWorkflow(t *testing.T) { ModifiedFiles []string // Comments are what our mock user writes to the pull request. Comments []string - // disableApply flag used by userConfig object when initializing atlantis server. + // DisableApply flag used by userConfig object when initializing atlantis server. DisableApply bool // ApplyLock creates an apply lock that temporarily disables apply command ApplyLock bool diff --git a/server/core/locking/apply_locking.go b/server/core/locking/apply_locking.go index 0adbc314f6..d071ca8669 100644 --- a/server/core/locking/apply_locking.go +++ b/server/core/locking/apply_locking.go @@ -31,8 +31,8 @@ type ApplyLocker interface { // ApplyCommandLock contains information about apply command lock status. type ApplyCommandLock struct { // Locked is true is when apply commands are locked - // Either by using disableApply flag or creating a global ApplyCommandLock - // disableApply lock take precedence when set + // Either by using DisableApply flag or creating a global ApplyCommandLock + // DisableApply lock take precedence when set Locked bool Time time.Time Failure string @@ -77,7 +77,7 @@ func (c *ApplyClient) LockApply() (ApplyCommandLock, error) { // this function returns an error func (c *ApplyClient) UnlockApply() error { if c.disableApplyFlag { - return errors.New("apply commands are disabled until disableApply flag is unset") + return errors.New("apply commands are disabled until DisableApply flag is unset") } err := c.backend.UnlockCommand(command.Apply) diff --git a/server/core/locking/locking_test.go b/server/core/locking/locking_test.go index 1cb50fa7d4..0d7de72cb9 100644 --- a/server/core/locking/locking_test.go +++ b/server/core/locking/locking_test.go @@ -203,7 +203,7 @@ func TestApplyLocker(t *testing.T) { Assert(t, !lock.Locked, "exp false") }) - t.Run("can't lock if userConfig.disableApply is set", func(t *testing.T) { + t.Run("can't lock if userConfig.DisableApply is set", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) @@ -233,12 +233,12 @@ func TestApplyLocker(t *testing.T) { Equals(t, errExpected, err) }) - t.Run("can't unlock if userConfig.disableApply is set", func(t *testing.T) { + t.Run("can't unlock if userConfig.DisableApply is set", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) err := l.UnlockApply() - ErrEquals(t, "apply commands are disabled until disableApply flag is unset", err) + ErrEquals(t, "apply commands are disabled until DisableApply flag is unset", err) backend.VerifyWasCalled(Never()).UnlockCommand(matchers.AnyCommandName()) }) @@ -265,7 +265,7 @@ func TestApplyLocker(t *testing.T) { Equals(t, lock.Locked, false) }) - t.Run("when disableApply flag is set always return a lock", func(t *testing.T) { + t.Run("when DisableApply flag is set always return a lock", func(t *testing.T) { backend := mocks.NewMockBackend() l := locking.NewApplyClient(backend, true) diff --git a/server/events/apply_command_runner.go b/server/events/apply_command_runner.go index 5094f40d76..eae9ec8593 100644 --- a/server/events/apply_command_runner.go +++ b/server/events/apply_command_runner.go @@ -71,7 +71,7 @@ func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) { pull := ctx.Pull locked, err := a.IsLocked() - // CheckApplyLock falls back to disableApply flag if fetching the lock + // CheckApplyLock falls back to DisableApply flag if fetching the lock // raises an error // We will log failure as warning if err != nil { diff --git a/server/events/apply_command_runner_test.go b/server/events/apply_command_runner_test.go index c79f2233cd..514ca6f5c6 100644 --- a/server/events/apply_command_runner_test.go +++ b/server/events/apply_command_runner_test.go @@ -31,13 +31,13 @@ func TestApplyCommandRunner_IsLocked(t *testing.T) { ExpComment: "**Error:** Running `atlantis apply` is disabled.", }, { - Description: "When no global apply lock is present and disableApply flag is false IsDisabled returns false", + Description: "When no global apply lock is present and DisableApply flag is false IsDisabled returns false", ApplyLocked: false, ApplyLockError: nil, ExpComment: "Ran Apply for 0 projects:\n\n\n\n", }, { - Description: "If ApplyLockChecker returns an error IsDisabled return value of disableApply flag", + Description: "If ApplyLockChecker returns an error IsDisabled return value of DisableApply flag", ApplyLockError: errors.New("error"), ApplyLocked: false, ExpComment: "Ran Apply for 0 projects:\n\n\n\n", diff --git a/server/events/comment_parser.go b/server/events/comment_parser.go index e22338bdd3..3c64cc93f8 100644 --- a/server/events/comment_parser.go +++ b/server/events/comment_parser.go @@ -443,23 +443,23 @@ var helpCommentTemplate = "```cmake\n" + Terraform Pull Request Automation Usage: - {{ .executableName }} [options] -- [terraform options] + {{ .ExecutableName }} [options] -- [terraform options] Examples: # show atlantis help - {{ .executableName }} help + {{ .ExecutableName }} help {{- if .AllowPlan }} # run plan in the root directory passing the -target flag to terraform - {{ .executableName }} plan -d . -- -target=resource + {{ .ExecutableName }} plan -d . -- -target=resource {{- end }} {{- if .AllowApply }} # apply all unapplied plans from this pull request - {{ .executableName }} apply + {{ .ExecutableName }} apply # apply the plan for the root directory and staging workspace - {{ .executableName }} apply -d . -w staging + {{ .ExecutableName }} apply -d . -w staging {{- end }} Commands: @@ -491,7 +491,7 @@ Commands: Flags: -h, --help help for atlantis -Use "{{ .executableName }} [command] --help" for more information about a command.` + +Use "{{ .ExecutableName }} [command] --help" for more information about a command.` + "\n```" // DidYouMeanAtlantisComment is the comment we add to the pull request when