1
1
#! /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
+
2
25
set -e
3
26
4
27
cd " ${TF_ACTION_WORKING_DIR:- .} "
@@ -16,40 +39,32 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
16
39
exit $SUCCESS
17
40
fi
18
41
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.
41
43
COMMENT=" "
42
-
43
- # If not successful, post failed plan output.
44
44
if [ $SUCCESS -ne 0 ]; then
45
+ OUTPUT=$( wrap " $OUTPUT " )
45
46
COMMENT=" #### \` terraform plan\` Failed
46
47
$OUTPUT "
47
48
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
+
49
63
COMMENT=" #### \` terraform plan\` Success
50
- $FMT_PLAN "
64
+ $OUTPUT "
51
65
fi
52
66
67
+ # Post the comment.
53
68
PAYLOAD=$( echo ' {}' | jq --arg body " $COMMENT " ' .body = $body' )
54
69
COMMENTS_URL=$( cat /github/workflow/event.json | jq -r .pull_request.comments_url)
55
70
curl -s -S -H " Authorization: token $GITHUB_TOKEN " --header " Content-Type: application/json" --data " $PAYLOAD " " $COMMENTS_URL " > /dev/null
0 commit comments