Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from hashicorp/wrapping
Browse files Browse the repository at this point in the history
Wrap `terraform plan` output in collapsible markdown
  • Loading branch information
lkysow authored Feb 19, 2019
2 parents 9097c2a + 486bb36 commit 3834b2f
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions plan/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
#!/bin/sh

# wrap takes some output and wraps it in a collapsible markdown section if
# it's over $TF_ACTION_WRAP_LINES long.
wrap() {
if [[ $(echo "$1" | wc -l) -gt ${TF_ACTION_WRAP_LINES:-20} ]]; then
echo "
<details><summary>Show Output</summary>
\`\`\`diff
$1
\`\`\`
</details>
"
else
echo "
\`\`\`diff
$1
\`\`\`
"
fi
}

set -e

cd "${TF_ACTION_WORKING_DIR:-.}"
Expand All @@ -16,21 +39,32 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
exit $SUCCESS
fi

# Build the comment we'll post to the PR.
COMMENT=""

# If not successful, post failed plan output.
if [ $SUCCESS -ne 0 ]; then
OUTPUT=$(wrap "$OUTPUT")
COMMENT="#### \`terraform plan\` Failed
\`\`\`
$OUTPUT
\`\`\`"
$OUTPUT"
else
FMT_PLAN=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
COMMENT="\`\`\`diff
$FMT_PLAN
\`\`\`"
# Remove "Refreshing state..." lines by only keeping output after the
# delimiter (72 dashes) that represents the end of the refresh stage.
# We do this to keep the comment output smaller.
if echo "$OUTPUT" | egrep '^-{72}$'; then
OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
fi

# Remove whitespace at the beginning of the line for added/modified/deleted
# resources so the diff markdown formatting highlights those lines.
OUTPUT=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')

# Call wrap to optionally wrap our output in a collapsible markdown section.
OUTPUT=$(wrap "$OUTPUT")

COMMENT="#### \`terraform plan\` Success
$OUTPUT"
fi

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

0 comments on commit 3834b2f

Please sign in to comment.