Skip to content

Commit 9588dc3

Browse files
enochlomsarvar
authored andcommitted
Add flag to enable markdown diff formatting (#1751)
1 parent 45abbd1 commit 9588dc3

9 files changed

+335
-23
lines changed

cmd/server.go

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
FFRepoFlag = "ff-repo"
6767
FFBranchFlag = "ff-branch"
6868
FFPathFlag = "ff-path"
69+
EnableDiffMarkdownFormat = "enable-diff-markdown-format"
6970
GHHostnameFlag = "gh-hostname"
7071
GHTokenFlag = "gh-token"
7172
GHUserFlag = "gh-user"
@@ -351,6 +352,10 @@ var boolFlags = map[string]boolFlag{
351352
description: "Enable Atlantis to use regular expressions on plan/apply commands when \"-p\" flag is passed with it.",
352353
defaultValue: false,
353354
},
355+
EnableDiffMarkdownFormat: {
356+
description: "Enable Atlantis to format Terraform plan output into a markdown-diff friendly format for color-coding purposes.",
357+
defaultValue: false,
358+
},
354359
AllowDraftPRs: {
355360
description: "Enable autoplan for Github Draft Pull Requests",
356361
defaultValue: false,

cmd/server_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var testFlags = map[string]interface{}{
110110
DisableAutoplanFlag: true,
111111
EnablePolicyChecksFlag: false,
112112
EnableRegExpCmdFlag: false,
113+
EnableDiffMarkdownFormat: false,
113114
}
114115

115116
func TestExecute_Defaults(t *testing.T) {

runatlantis.io/docs/server-configuration.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Values are chosen in this order:
5252
atlantis server --allow-draft-prs
5353
```
5454
Respond to pull requests from draft prs. Defaults to `false`.
55-
55+
5656
* ### `--allow-fork-prs`
5757
```bash
5858
atlantis server --allow-fork-prs
@@ -274,6 +274,14 @@ Values are chosen in this order:
274274
The command `atlantis apply -p .*` will bypass the restriction and run apply on every projects
275275
:::
276276

277+
* ### `--enable-diff-markdown-format`
278+
```bash
279+
atlantis server --enable-diff-markdown-format
280+
```
281+
Enable Atlantis to format Terraform plan output into a markdown-diff friendly format for color-coding purposes.
282+
283+
Useful to enable for use with Github.
284+
277285
* ### `--gh-hostname`
278286
```bash
279287
atlantis server --gh-hostname="my.github.enterprise.com"

server/events/markdown_renderer.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ type MarkdownRenderer struct {
4444
DisableApply bool
4545
DisableMarkdownFolding bool
4646
DisableRepoLocking bool
47+
EnableDiffMarkdownFormat bool
4748
}
4849

4950
// commonData is data that all responses have.
5051
type commonData struct {
51-
Command string
52-
Verbose bool
53-
Log string
54-
PlansDeleted bool
55-
DisableApplyAll bool
56-
DisableApply bool
57-
DisableRepoLocking bool
52+
Command string
53+
Verbose bool
54+
Log string
55+
PlansDeleted bool
56+
DisableApplyAll bool
57+
DisableApply bool
58+
DisableRepoLocking bool
59+
EnableDiffMarkdownFormat bool
5860
}
5961

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

7880
type planSuccessData struct {
7981
models.PlanSuccess
80-
PlanSummary string
81-
PlanWasDeleted bool
82-
DisableApply bool
83-
DisableRepoLocking bool
82+
PlanSummary string
83+
PlanWasDeleted bool
84+
DisableApply bool
85+
DisableRepoLocking bool
86+
EnableDiffMarkdownFormat bool
8487
}
8588

8689
type policyCheckSuccessData struct {
@@ -99,13 +102,14 @@ type projectResultTmplData struct {
99102
func (m *MarkdownRenderer) Render(res CommandResult, cmdName models.CommandName, log string, verbose bool, vcsHost models.VCSHostType) string {
100103
commandStr := strings.Title(strings.Replace(cmdName.String(), "_", " ", -1))
101104
common := commonData{
102-
Command: commandStr,
103-
Verbose: verbose,
104-
Log: log,
105-
PlansDeleted: res.PlansDeleted,
106-
DisableApplyAll: m.DisableApplyAll || m.DisableApply,
107-
DisableApply: m.DisableApply,
108-
DisableRepoLocking: m.DisableRepoLocking,
105+
Command: commandStr,
106+
Verbose: verbose,
107+
Log: log,
108+
PlansDeleted: res.PlansDeleted,
109+
DisableApplyAll: m.DisableApplyAll || m.DisableApply,
110+
DisableApply: m.DisableApply,
111+
DisableRepoLocking: m.DisableRepoLocking,
112+
EnableDiffMarkdownFormat: m.EnableDiffMarkdownFormat,
109113
}
110114
if res.Error != nil {
111115
return m.renderTemplate(unwrappedErrWithLogTmpl, errData{res.Error.Error(), common})
@@ -150,9 +154,9 @@ func (m *MarkdownRenderer) renderProjectResults(results []models.ProjectResult,
150154
})
151155
} else if result.PlanSuccess != nil {
152156
if m.shouldUseWrappedTmpl(vcsHost, result.PlanSuccess.TerraformOutput) {
153-
resultData.Rendered = m.renderTemplate(planSuccessWrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanSummary: result.PlanSuccess.Summary(), PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
157+
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})
154158
} else {
155-
resultData.Rendered = m.renderTemplate(planSuccessUnwrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking})
159+
resultData.Rendered = m.renderTemplate(planSuccessUnwrappedTmpl, planSuccessData{PlanSuccess: *result.PlanSuccess, PlanWasDeleted: common.PlansDeleted, DisableApply: common.DisableApply, DisableRepoLocking: common.DisableRepoLocking, EnableDiffMarkdownFormat: common.EnableDiffMarkdownFormat})
156160
}
157161
numPlanSuccesses++
158162
} else if result.PolicyCheckSuccess != nil {
@@ -300,14 +304,14 @@ var multiProjectVersionTmpl = template.Must(template.New("").Funcs(sprig.TxtFunc
300304
logTmpl))
301305
var planSuccessUnwrappedTmpl = template.Must(template.New("").Parse(
302306
"```diff\n" +
303-
"{{.TerraformOutput}}\n" +
307+
"{{ if .EnableDiffMarkdownFormat }}{{.DiffMarkdownFormattedTerraformOutput}}{{else}}{{.TerraformOutput}}{{end}}\n" +
304308
"```\n\n" + planNextSteps +
305309
"{{ if .HasDiverged }}\n\n:warning: The branch we're merging into is ahead, it is recommended to pull new commits first.{{end}}"))
306310

307311
var planSuccessWrappedTmpl = template.Must(template.New("").Parse(
308312
"<details><summary>Show Output</summary>\n\n" +
309313
"```diff\n" +
310-
"{{.TerraformOutput}}\n" +
314+
"{{ if .EnableDiffMarkdownFormat }}{{.DiffMarkdownFormattedTerraformOutput}}{{else}}{{.TerraformOutput}}{{end}}\n" +
311315
"```\n\n" +
312316
planNextSteps + "\n" +
313317
"</details>" + "\n" +

0 commit comments

Comments
 (0)