Skip to content

Commit

Permalink
increase length of codefence to 8 because it's enough to render code …
Browse files Browse the repository at this point in the history
…fence that includes code fence in most cases
  • Loading branch information
takaishi committed Sep 5, 2022
1 parent e367f4e commit bd86137
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 52 deletions.
32 changes: 10 additions & 22 deletions internal/terraform/plan.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package terraform

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

Expand Down Expand Up @@ -33,7 +31,9 @@ const planTemplateBody = `### {{len .CreatedAddresses}} to add, {{len .UpdatedAd
{{if .ResourceChanges -}}
<details><summary>Change details</summary>
{{ range .ResourceChanges }}
{{.GetUnifiedDiffString}}
{{codeFence}}diff
# {{.ResourceChange.Type}}.{{.ResourceChange.Name}} {{.HeaderSuffix}}
{{.GetUnifiedDiffString}}{{codeFence}}
{{end}}
</details>
{{end}}`
Expand Down Expand Up @@ -69,10 +69,8 @@ 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 fence + "diff\n" + comment + "\n" + diffText + fence, nil
return diffText, nil
}

func (r ResourceChangeData) HeaderSuffix() string {
Expand All @@ -89,23 +87,13 @@ func (r ResourceChangeData) HeaderSuffix() 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)
}

func (plan *PlanData) Render(w io.Writer) error {
planTemplate, err := template.New("plan").Parse(planTemplateBody)
funcMap := template.FuncMap{
"codeFence": func() string {
return "````````"
},
}
planTemplate, err := template.New("plan").Funcs(funcMap).Parse(planTemplateBody)
if err != nil {
return fmt.Errorf("invalid template text: %w", err)
}
Expand Down
16 changes: 8 additions & 8 deletions test/testdata/all_types_mixed/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- random_id.test4
<details><summary>Change details</summary>

```diff
````````diff
# env_variable.test2 will be updated in-place
@@ -1,5 +1,5 @@
{
Expand All @@ -18,9 +18,9 @@
+ "name": "test2_changed",
"value": ""
}
```
````````

```diff
````````diff
# env_variable.test3 will be destroyed
@@ -1,5 +1 @@
-{
Expand All @@ -29,18 +29,18 @@
- "value": ""
-}
+null
```
````````

```diff
````````diff
# env_variable.test5 will be created
@@ -1 +1,3 @@
-null
+{
+ "name": "test5"
+}
```
````````

```diff
````````diff
# random_id.test4 will be replaced
@@ -1,10 +1,5 @@
{
Expand All @@ -54,6 +54,6 @@
"keepers": null,
"prefix": null
}
```
````````

</details>
20 changes: 10 additions & 10 deletions test/testdata/aws_sample/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- aws_security_group.admin
<details><summary>Change details</summary>

```diff
````````diff
# aws_instance.test will be destroyed
@@ -1,92 +1 @@
-{
Expand Down Expand Up @@ -106,9 +106,9 @@
- ]
-}
+null
```
````````

```diff
````````diff
# aws_route_table.public-route will be created
@@ -1 +1,22 @@
-null
Expand All @@ -134,19 +134,19 @@
+ "timeouts": null,
+ "vpc_id": "vpc-0c08ee65bf93a360f"
+}
```
````````

```diff
````````diff
# aws_route_table_association.puclic-a will be created
@@ -1 +1,4 @@
-null
+{
+ "gateway_id": null,
+ "subnet_id": "subnet-0342dca4d2a611266"
+}
```
````````

```diff
````````diff
# aws_security_group.admin will be replaced
@@ -1,6 +1,5 @@
{
Expand Down Expand Up @@ -177,9 +177,9 @@
"timeouts": null,
"vpc_id": "vpc-0c08ee65bf93a360f"
}
```
````````

```diff
````````diff
# aws_subnet.public-a will be updated in-place
@@ -18,10 +18,10 @@
"owner_id": "999999999999",
Expand All @@ -194,6 +194,6 @@
},
"timeouts": null,
"vpc_id": "vpc-0c08ee65bf93a360f"
```
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/iam_policy/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- aws_iam_policy.test_policy
<details><summary>Change details</summary>

```diff
````````diff
# aws_iam_policy.test_policy will be updated in-place
@@ -12,7 +12,8 @@
"Action": [
Expand All @@ -15,6 +15,6 @@
],
"Resource": "*"
}
```
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/include_code_fence/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- env_variable.test1
<details><summary>Change details</summary>

````diff
````````diff
# env_variable.test1 will be updated in-place
@@ -7,7 +7,7 @@
",
Expand All @@ -14,6 +14,6 @@
```

",
````
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/single_add/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
- null_resource.foo
<details><summary>Change details</summary>

```diff
````````diff
# null_resource.foo will be created
@@ -1 +1,3 @@
-null
+{
+ "triggers": null
+}
```
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/single_change/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- env_variable.test1
<details><summary>Change details</summary>

```diff
````````diff
# env_variable.test1 will be updated in-place
@@ -1,5 +1,5 @@
{
Expand All @@ -12,6 +12,6 @@
+ "name": "test1_changed",
"value": ""
}
```
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/single_destroy/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
- null_resource.foo
<details><summary>Change details</summary>

```diff
````````diff
# null_resource.foo will be destroyed
@@ -1,4 +1 @@
-{
- "id": "7047514762471223910",
- "triggers": null
-}
+null
```
````````

</details>
4 changes: 2 additions & 2 deletions test/testdata/single_replace/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- random_id.test
<details><summary>Change details</summary>

```diff
````````diff
# random_id.test will be replaced
@@ -1,10 +1,5 @@
{
Expand All @@ -17,6 +17,6 @@
"keepers": null,
"prefix": null
}
```
````````

</details>

0 comments on commit bd86137

Please sign in to comment.