From dac81ec5cf351ee6dd211d56820ec5bf45976be4 Mon Sep 17 00:00:00 2001 From: Brad Erickson Date: Thu, 31 Jan 2019 14:09:52 -0800 Subject: [PATCH 1/2] 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 when there are more than 20 lines. fixes #1 --- plan/entrypoint.sh | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/plan/entrypoint.sh b/plan/entrypoint.sh index fc71dbcf..129220a0 100755 --- a/plan/entrypoint.sh +++ b/plan/entrypoint.sh @@ -16,19 +16,38 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then exit $SUCCESS fi +# Remove "Refreshing Terraform state" details. +OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }') + +# Reduce to summary if output line count is greater than 20. +if [ $(echo "$OUTPUT" | wc -l) -gt 20 ]; then + OUTPUT=" +
Show Output + +\`\`\`diff +$OUTPUT +\`\`\` + +
+" +else + OUTPUT=" +\`\`\`diff +$OUTPUT +\`\`\` +" +fi + COMMENT="" # If not successful, post failed plan output. if [ $SUCCESS -ne 0 ]; then 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 -\`\`\`" + COMMENT="#### \`terraform plan\` Success +$FMT_PLAN" fi PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body') From 486bb3677f167017140203cdbf96c41b20df3c0a Mon Sep 17 00:00:00 2001 From: Luke Kysow <1034429+lkysow@users.noreply.github.com> Date: Tue, 19 Feb 2019 15:03:08 -0500 Subject: [PATCH 2/2] Refactor comment formatting. --- plan/entrypoint.sh | 67 ++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/plan/entrypoint.sh b/plan/entrypoint.sh index 129220a0..e650dfde 100755 --- a/plan/entrypoint.sh +++ b/plan/entrypoint.sh @@ -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 " +
Show Output + +\`\`\`diff +$1 +\`\`\` + +
+" +else + echo " +\`\`\`diff +$1 +\`\`\` +" +fi +} + set -e cd "${TF_ACTION_WORKING_DIR:-.}" @@ -16,40 +39,32 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then exit $SUCCESS fi -# Remove "Refreshing Terraform state" details. -OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }') - -# Reduce to summary if output line count is greater than 20. -if [ $(echo "$OUTPUT" | wc -l) -gt 20 ]; then - OUTPUT=" -
Show Output - -\`\`\`diff -$OUTPUT -\`\`\` - -
-" -else - OUTPUT=" -\`\`\`diff -$OUTPUT -\`\`\` -" -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" else - FMT_PLAN=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g') + # 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 -$FMT_PLAN" +$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