Skip to content

Commit

Permalink
support code block in code block
Browse files Browse the repository at this point in the history
  • Loading branch information
takaishi committed Sep 2, 2022
1 parent 09b74e0 commit e367f4e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 10 deletions.
31 changes: 21 additions & 10 deletions internal/terraform/plan.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package terraform

import (
"bufio"
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
"text/template"

Expand Down Expand Up @@ -31,9 +33,7 @@ const planTemplateBody = `### {{len .CreatedAddresses}} to add, {{len .UpdatedAd
{{if .ResourceChanges -}}
<details><summary>Change details</summary>
{{ range .ResourceChanges }}
{{codeFence}}diff
# {{.ResourceChange.Type}}.{{.ResourceChange.Name}} {{.HeaderSuffix}}
{{.GetUnifiedDiffString}}{{codeFence}}
{{.GetUnifiedDiffString}}
{{end}}
</details>
{{end}}`
Expand Down Expand Up @@ -69,8 +69,10 @@ func (r ResourceChangeData) GetUnifiedDiffString() (string, error) {
if err != nil {
return "", fmt.Errorf("failed to create diff: %w", err)
}
comment := fmt.Sprintf("# %s.%s %s", r.ResourceChange.Type, r.ResourceChange.Name, r.HeaderSuffix())
fence := r.codeFence(diffText)

return diffText, nil
return fence + "diff\n" + comment + "\n" + diffText + fence, nil
}

func (r ResourceChangeData) HeaderSuffix() string {
Expand All @@ -87,14 +89,23 @@ func (r ResourceChangeData) HeaderSuffix() string {
return ""
}

func (plan *PlanData) Render(w io.Writer) error {
funcMap := template.FuncMap{
"codeFence": func() string {
return "```"
},
var codeFenceRegexp = regexp.MustCompile(`\s*(` + "`" + `{3,})`)

func (r ResourceChangeData) codeFence(diffText string) string {
count := 3

scanner := bufio.NewScanner(strings.NewReader(diffText))
for scanner.Scan() {
values := codeFenceRegexp.FindStringSubmatch(scanner.Text())
if values != nil && len(values[1]) >= 3 {
count = len(values[1]) + 1
}
}
return strings.Repeat("`", count)
}

planTemplate, err := template.New("plan").Funcs(funcMap).Parse(planTemplateBody)
func (plan *PlanData) Render(w io.Writer) error {
planTemplate, err := template.New("plan").Parse(planTemplateBody)
if err != nil {
return fmt.Errorf("invalid template text: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/terraform/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Test_render(t *testing.T) {
{name: "all_types_mixed", wantErr: false},
{name: "aws_sample", wantErr: false},
{name: "iam_policy", wantErr: false},
{name: "include_code_fence", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions test/testdata/include_code_fence/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### 0 to add, 1 to change, 0 to destroy, 0 to replace.
- change
- env_variable.test1
<details><summary>Change details</summary>

````diff
# env_variable.test1 will be updated in-place
@@ -7,7 +7,7 @@
",
"name": "
```
- test1
+ test2
```

",
````

</details>
22 changes: 22 additions & 0 deletions test/testdata/include_code_fence/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
required_providers {
env = {
source = "tchupp/env"
version = "0.0.2"
}
}
}

provider "env" {
# Configuration options
}

resource "env_variable" "test1" {
name = <<EOF
```
test2
```
EOF
}
Binary file added test/testdata/include_code_fence/plan.txt
Binary file not shown.
1 change: 1 addition & 0 deletions test/testdata/include_code_fence/show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"1.0","terraform_version":"1.1.8","planned_values":{"root_module":{"resources":[{"address":"env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_name":"registry.terraform.io/tchupp/env","schema_version":0,"values":{"id":"\n```\ntest1\n```\n\n","name":"\n```\ntest2\n```\n\n","value":""},"sensitive_values":{}}]}},"resource_changes":[{"address":"env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_name":"registry.terraform.io/tchupp/env","change":{"actions":["update"],"before":{"id":"\n```\ntest1\n```\n\n","name":"\n```\ntest1\n```\n\n","value":""},"after":{"id":"\n```\ntest1\n```\n\n","name":"\n```\ntest2\n```\n\n","value":""},"after_unknown":{},"before_sensitive":{"value":true},"after_sensitive":{"value":true}}}],"prior_state":{"format_version":"1.0","terraform_version":"1.1.8","values":{"root_module":{"resources":[{"address":"env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_name":"registry.terraform.io/tchupp/env","schema_version":0,"values":{"id":"\n```\ntest1\n```\n\n","name":"\n```\ntest1\n```\n\n","value":""},"sensitive_values":{}}]}}},"configuration":{"provider_config":{"env":{"name":"env","version_constraint":"0.0.2"}},"root_module":{"resources":[{"address":"env_variable.test1","mode":"managed","type":"env_variable","name":"test1","provider_config_key":"env","expressions":{"name":{"constant_value":"\n```\ntest2\n```\n\n"}},"schema_version":0}]}}}
27 changes: 27 additions & 0 deletions test/testdata/include_code_fence/terraform.tfstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 4,
"terraform_version": "1.1.8",
"serial": 1,
"lineage": "562fda8d-ad02-efe3-9568-b6dd139ef8f9",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "env_variable",
"name": "test1",
"provider": "provider[\"registry.terraform.io/tchupp/env\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"id": "\n```\ntest1\n```\n\n",
"name": "\n```\ntest1\n```\n\n",
"value": ""
},
"sensitive_attributes": [],
"private": "bnVsbA=="
}
]
}
]
}

0 comments on commit e367f4e

Please sign in to comment.