Skip to content

Commit

Permalink
make MarkdownRenderer private
Browse files Browse the repository at this point in the history
  • Loading branch information
krrrr38 committed Jan 6, 2023
1 parent c3ad563 commit d376d24
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 151 deletions.
2 changes: 1 addition & 1 deletion cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down 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, ""),
}

autoMerger := &events.AutoMerger{
Expand Down
6 changes: 3 additions & 3 deletions server/core/locking/apply_locking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions server/core/locking/locking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
})
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/events/apply_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions server/events/apply_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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, ""),
}

autoMerger = &events.AutoMerger{
Expand Down
12 changes: 6 additions & 6 deletions server/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,23 +443,23 @@ var helpCommentTemplate = "```cmake\n" +
Terraform Pull Request Automation
Usage:
{{ .ExecutableName }} <command> [options] -- [terraform options]
{{ .executableName }} <command> [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:
Expand Down Expand Up @@ -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
Expand Down
80 changes: 40 additions & 40 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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})
Expand All @@ -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{
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
Loading

0 comments on commit d376d24

Please sign in to comment.