Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: markdown renderer use ExecutableName instead atlantis #2939

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep this all lowercase for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitrocode reverted these changes. now ready for review 🙇

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, "", "atlantis"),
}

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
krrrr38 marked this conversation as resolved.
Show resolved Hide resolved
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, "", "atlantis"),
}

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
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