Skip to content

Commit

Permalink
fix: markdown renderer use ExecutableName instead atlantis (runatlant…
Browse files Browse the repository at this point in the history
…is#2939)

* make MarkdownRenderer private

* ExecutableName is available on markdown renderer common data

* replace template atlantis name with .ExecutableName

* MarkdownRenderer fields can be private, but itself needs public

* reuse commonData anywhere in renderer

* revert
  • Loading branch information
krrrr38 authored Jan 6, 2023
1 parent c3ad563 commit 46bf046
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 153 deletions.
2 changes: 1 addition & 1 deletion server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
85 changes: 39 additions & 46 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -66,6 +67,7 @@ type commonData struct {
DisableApply bool
DisableRepoLocking bool
EnableDiffMarkdownFormat bool
ExecutableName string
}

// errData is data about an error response.
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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})
Expand All @@ -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{
Expand All @@ -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})
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
Loading

0 comments on commit 46bf046

Please sign in to comment.