Skip to content

Commit 486bb36

Browse files
committed
Refactor comment formatting.
1 parent dac81ec commit 486bb36

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

plan/entrypoint.sh

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
#!/bin/sh
2+
3+
# wrap takes some output and wraps it in a collapsible markdown section if
4+
# it's over $TF_ACTION_WRAP_LINES long.
5+
wrap() {
6+
if [[ $(echo "$1" | wc -l) -gt ${TF_ACTION_WRAP_LINES:-20} ]]; then
7+
echo "
8+
<details><summary>Show Output</summary>
9+
10+
\`\`\`diff
11+
$1
12+
\`\`\`
13+
14+
</details>
15+
"
16+
else
17+
echo "
18+
\`\`\`diff
19+
$1
20+
\`\`\`
21+
"
22+
fi
23+
}
24+
225
set -e
326

427
cd "${TF_ACTION_WORKING_DIR:-.}"
@@ -16,40 +39,32 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
1639
exit $SUCCESS
1740
fi
1841

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-
42+
# Build the comment we'll post to the PR.
4143
COMMENT=""
42-
43-
# If not successful, post failed plan output.
4444
if [ $SUCCESS -ne 0 ]; then
45+
OUTPUT=$(wrap "$OUTPUT")
4546
COMMENT="#### \`terraform plan\` Failed
4647
$OUTPUT"
4748
else
48-
FMT_PLAN=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
49+
# Remove "Refreshing state..." lines by only keeping output after the
50+
# delimiter (72 dashes) that represents the end of the refresh stage.
51+
# We do this to keep the comment output smaller.
52+
if echo "$OUTPUT" | egrep '^-{72}$'; then
53+
OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
54+
fi
55+
56+
# Remove whitespace at the beginning of the line for added/modified/deleted
57+
# resources so the diff markdown formatting highlights those lines.
58+
OUTPUT=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
59+
60+
# Call wrap to optionally wrap our output in a collapsible markdown section.
61+
OUTPUT=$(wrap "$OUTPUT")
62+
4963
COMMENT="#### \`terraform plan\` Success
50-
$FMT_PLAN"
64+
$OUTPUT"
5165
fi
5266

67+
# Post the comment.
5368
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
5469
COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
5570
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null

0 commit comments

Comments
 (0)