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

Add a flag to optionally enable Github Markdown Formatting for diffs #1751

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
DisableRepoLockingFlag = "disable-repo-locking"
EnablePolicyChecksFlag = "enable-policy-checks"
EnableRegExpCmdFlag = "enable-regexp-cmd"
EnableDiffMarkdownFormat = "enable-diff-markdown-format"
GHHostnameFlag = "gh-hostname"
GHTokenFlag = "gh-token"
GHUserFlag = "gh-user"
Expand Down Expand Up @@ -316,6 +317,10 @@ var boolFlags = map[string]boolFlag{
description: "Enable Atlantis to use regular expressions on plan/apply commands when \"-p\" flag is passed with it.",
defaultValue: false,
},
EnableDiffMarkdownFormat: {
description: "Enable Atlantis to format Terraform plan output into a markdown-diff friendly format for color-coding purposes.",
defaultValue: false,
},
AllowDraftPRs: {
description: "Enable autoplan for Github Draft Pull Requests",
defaultValue: false,
Expand Down
1 change: 1 addition & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var testFlags = map[string]interface{}{
DisableAutoplanFlag: true,
EnablePolicyChecksFlag: false,
EnableRegExpCmdFlag: false,
EnableDiffMarkdownFormat: false,
}

func TestExecute_Defaults(t *testing.T) {
Expand Down
10 changes: 9 additions & 1 deletion runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Values are chosen in this order:
atlantis server --allow-draft-prs
```
Respond to pull requests from draft prs. Defaults to `false`.

* ### `--allow-fork-prs`
```bash
atlantis server --allow-fork-prs
Expand Down Expand Up @@ -274,6 +274,14 @@ Values are chosen in this order:
The command `atlantis apply -p .*` will bypass the restriction and run apply on every projects
:::

* ### `--enable-diff-markdown-format`
```bash
atlantis server --enable-diff-markdown-format
```
Enable Atlantis to format Terraform plan output into a markdown-diff friendly format for color-coding purposes.

Useful to enable for use with Github.

* ### `--gh-hostname`
```bash
atlantis server --gh-hostname="my.github.enterprise.com"
Expand Down
48 changes: 26 additions & 22 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ type MarkdownRenderer struct {
DisableApply bool
DisableMarkdownFolding bool
DisableRepoLocking bool
EnableDiffMarkdownFormat bool
}

// commonData is data that all responses have.
type commonData struct {
Command string
Verbose bool
Log string
PlansDeleted bool
DisableApplyAll bool
DisableApply bool
DisableRepoLocking bool
Command string
Verbose bool
Log string
PlansDeleted bool
DisableApplyAll bool
DisableApply bool
DisableRepoLocking bool
EnableDiffMarkdownFormat bool
}

// errData is data about an error response.
Expand All @@ -77,10 +79,11 @@ type resultData struct {

type planSuccessData struct {
models.PlanSuccess
PlanSummary string
PlanWasDeleted bool
DisableApply bool
DisableRepoLocking bool
PlanSummary string
PlanWasDeleted bool
DisableApply bool
DisableRepoLocking bool
EnableDiffMarkdownFormat bool
}

type policyCheckSuccessData struct {
Expand All @@ -99,13 +102,14 @@ type projectResultTmplData struct {
func (m *MarkdownRenderer) Render(res CommandResult, cmdName models.CommandName, log string, verbose bool, vcsHost models.VCSHostType) string {
commandStr := strings.Title(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,
Command: commandStr,
Verbose: verbose,
Log: log,
PlansDeleted: res.PlansDeleted,
DisableApplyAll: m.DisableApplyAll || m.DisableApply,
DisableApply: m.DisableApply,
DisableRepoLocking: m.DisableRepoLocking,
EnableDiffMarkdownFormat: m.EnableDiffMarkdownFormat,
}
if res.Error != nil {
return m.renderTemplate(unwrappedErrWithLogTmpl, errData{res.Error.Error(), common})
Expand Down Expand Up @@ -150,9 +154,9 @@ func (m *MarkdownRenderer) renderProjectResults(results []models.ProjectResult,
})
} else if result.PlanSuccess != nil {
if m.shouldUseWrappedTmpl(vcsHost, result.PlanSuccess.TerraformOutput) {
resultData.Rendered = m.renderTemplate(planSuccessWrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanSummary: result.PlanSuccess.Summary(), PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
resultData.Rendered = m.renderTemplate(planSuccessWrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanSummary: result.PlanSuccess.Summary(), PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking, EnableDiffMarkdownFormat: common.EnableDiffMarkdownFormat})
} else {
resultData.Rendered = m.renderTemplate(planSuccessUnwrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
resultData.Rendered = m.renderTemplate(planSuccessUnwrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking, EnableDiffMarkdownFormat: common.EnableDiffMarkdownFormat})
}
numPlanSuccesses++
} else if result.PolicyCheckSuccess != nil {
Expand Down Expand Up @@ -300,14 +304,14 @@ var multiProjectVersionTmpl = template.Must(template.New("").Funcs(sprig.TxtFunc
logTmpl))
var planSuccessUnwrappedTmpl = template.Must(template.New("").Parse(
"```diff\n" +
"{{.TerraformOutput}}\n" +
"{{ if .EnableDiffMarkdownFormat }}{{.DiffMarkdownFormattedTerraformOutput}}{{else}}{{.TerraformOutput}}{{end}}\n" +
"```\n\n" + planNextSteps +
"{{ if .HasDiverged }}\n\n:warning: The branch we're merging into is ahead, it is recommended to pull new commits first.{{end}}"))

var planSuccessWrappedTmpl = template.Must(template.New("").Parse(
"<details><summary>Show Output</summary>\n\n" +
"```diff\n" +
"{{.TerraformOutput}}\n" +
"{{ if .EnableDiffMarkdownFormat }}{{.DiffMarkdownFormattedTerraformOutput}}{{else}}{{.TerraformOutput}}{{end}}\n" +
"```\n\n" +
planNextSteps + "\n" +
"</details>" + "\n" +
Expand Down
Loading