Skip to content

Commit dac81ec

Browse files
committed
feat(plan) Remove "refreshing state" & reduce to summary for 20+ lines
The output of `terraform plan` can become excessive when posted within a Github comment. This adds noise and makes code review discussions more difficult. New features in this commit: * Strip out the "Refreshing Terraform state..." section. * Wrap output in a <detail><summary></summary></block> when there are more than 20 lines. fixes hashicorp#1
1 parent 9097c2a commit dac81ec

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

plan/entrypoint.sh

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,38 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
1616
exit $SUCCESS
1717
fi
1818

19+
# Remove "Refreshing Terraform state" details.
20+
OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
21+
22+
# Reduce to summary if output line count is greater than 20.
23+
if [ $(echo "$OUTPUT" | wc -l) -gt 20 ]; then
24+
OUTPUT="
25+
<details><summary>Show Output</summary>
26+
27+
\`\`\`diff
28+
$OUTPUT
29+
\`\`\`
30+
31+
</details>
32+
"
33+
else
34+
OUTPUT="
35+
\`\`\`diff
36+
$OUTPUT
37+
\`\`\`
38+
"
39+
fi
40+
1941
COMMENT=""
2042

2143
# If not successful, post failed plan output.
2244
if [ $SUCCESS -ne 0 ]; then
2345
COMMENT="#### \`terraform plan\` Failed
24-
\`\`\`
25-
$OUTPUT
26-
\`\`\`"
46+
$OUTPUT"
2747
else
2848
FMT_PLAN=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
29-
COMMENT="\`\`\`diff
30-
$FMT_PLAN
31-
\`\`\`"
49+
COMMENT="#### \`terraform plan\` Success
50+
$FMT_PLAN"
3251
fi
3352

3453
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')

0 commit comments

Comments
 (0)