diff --git a/server/controllers/events/events_controller_e2e_test.go b/server/controllers/events/events_controller_e2e_test.go index 5729c83060..aade27a029 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.GetMarkdownRenderer(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 1dd74be6a7..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.GetMarkdownRenderer(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 7708adf3ef..5b4759189a 100644 --- a/server/events/markdown_renderer.go +++ b/server/events/markdown_renderer.go @@ -44,16 +44,17 @@ var ( // MarkdownRenderer renders responses as markdown. type MarkdownRenderer struct { - // GitlabSupportsCommonMark is true if the version of GitLab we're + // 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 + 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. @@ -108,29 +110,31 @@ type projectResultTmplData struct { } // Initialize templates -func GetMarkdownRenderer( - GitlabSupportsCommonMark bool, - DisableApplyAll bool, - DisableApply bool, - DisableMarkdownFolding bool, - DisableRepoLocking bool, - EnableDiffMarkdownFormat bool, - MarkdownTemplateOverridesDir string, +func NewMarkdownRenderer( + gitlabSupportsCommonMark bool, + disableApplyAll bool, + disableApply bool, + disableMarkdownFolding bool, + disableRepoLocking bool, + enableDiffMarkdownFormat bool, + markdownTemplateOverridesDir string, + executableName 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, + gitlabSupportsCommonMark: gitlabSupportsCommonMark, + disableApplyAll: disableApplyAll, + disableMarkdownFolding: disableMarkdownFolding, + disableApply: disableApply, + disableRepoLocking: disableRepoLocking, + enableDiffMarkdownFormat: enableDiffMarkdownFormat, + markdownTemplates: templates, + executableName: executableName, } } @@ -143,13 +147,14 @@ func (m *MarkdownRenderer) Render(res command.Result, cmdName command.Name, log 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, + ExecutableName: m.executableName, } - templates := m.MarkdownTemplates + templates := m.markdownTemplates if res.Error != nil { return m.renderTemplate(templates.Lookup("unwrappedErrWithLog"), errData{res.Error.Error(), common}) @@ -166,7 +171,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, numPolicyCheckSuccesses := 0 numVersionSuccesses := 0 - templates := m.MarkdownTemplates + templates := m.markdownTemplates for _, result := range results { resultData := projectResultTmplData{ @@ -179,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}) @@ -273,7 +266,7 @@ func (m *MarkdownRenderer) renderProjectResults(results []command.ProjectResult, // 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 { + if m.disableMarkdownFolding { return false } @@ -282,7 +275,7 @@ func (m *MarkdownRenderer) shouldUseWrappedTmpl(vcsHost models.VCSHostType, outp return false } - if vcsHost == models.Gitlab && !m.GitlabSupportsCommonMark { + if vcsHost == models.Gitlab && !m.gitlabSupportsCommonMark { return false } diff --git a/server/events/markdown_renderer_test.go b/server/events/markdown_renderer_test.go index 6a9151a3cd..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.GetMarkdownRenderer(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.GetMarkdownRenderer(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.GetMarkdownRenderer(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.GetMarkdownRenderer(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{ @@ -942,14 +942,15 @@ $$$ `, }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + 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) { @@ -1094,14 +1095,15 @@ $$$ }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - true, // DisableApplyAll - true, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + 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) { @@ -1131,14 +1133,15 @@ 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 - tmpDir, // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + tmpDir, // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := r.Render(command.Result{ @@ -1162,14 +1165,15 @@ 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 - "", // MarkdownTemplateOverridesDir + mr := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + false, // disableApplyAll + false, // disableApply + true, // disableMarkdownFolding + false, // disableRepoLocking + false, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) rendered := mr.Render(command.Result{ @@ -1252,14 +1256,15 @@ 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 + "atlantis", // executableName ) rendered := mr.Render(command.Result{ @@ -1370,14 +1375,15 @@ 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 + "atlantis", // executableName ) var pr command.ProjectResult switch cmd { @@ -1481,14 +1487,15 @@ $$$ } func TestRenderProjectResults_MultiProjectApplyWrapped(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + mr := events.NewMarkdownRenderer( + 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{ @@ -1535,14 +1542,15 @@ $$$ } func TestRenderProjectResults_MultiProjectPlanWrapped(t *testing.T) { - mr := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + mr := events.NewMarkdownRenderer( + 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{ @@ -1716,14 +1724,15 @@ 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 - "", // MarkdownTemplateOverridesDir + mr := events.NewMarkdownRenderer( + 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) @@ -2184,16 +2193,16 @@ $$$ }, } - r := events.GetMarkdownRenderer( - false, // GitlabSupportsCommonMark - false, // DisableApplyAll - false, // DisableApply - false, // DisableMarkdownFolding - false, // DisableRepoLocking - false, // EnableDiffMarkdownFormat - "", // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + 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{ @@ -2623,14 +2632,15 @@ 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 - "", // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { @@ -2658,14 +2668,15 @@ 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 - "", // MarkdownTemplateOverridesDir + r := events.NewMarkdownRenderer( + false, // gitlabSupportsCommonMark + true, // disableApplyAll + true, // disableApply + false, // disableMarkdownFolding + false, // disableRepoLocking + true, // enableDiffMarkdownFormat + "", // MarkdownTemplateOverridesDir + "atlantis", // executableName ) for _, c := range cases { 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 }} diff --git a/server/server.go b/server/server.go index 4d87277bbb..61e00afde6 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, @@ -420,6 +420,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { userConfig.DisableRepoLocking, userConfig.EnableDiffMarkdownFormat, userConfig.MarkdownTemplateOverridesDir, + userConfig.ExecutableName, ) var lockingClient locking.Locker