Skip to content

Commit

Permalink
Add flag to enable markdown diff formatting (runatlantis#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
enochlo authored and krrrr38 committed Dec 16, 2022
1 parent 8e26115 commit 4ca95e1
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 23 deletions.
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 @@ -321,6 +322,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 @@ -108,6 +108,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

0 comments on commit 4ca95e1

Please sign in to comment.