Skip to content

Commit

Permalink
[v1.6.1] [PLAN] Improve char-limit handling for comments & Add link t…
Browse files Browse the repository at this point in the history
…o logs
  • Loading branch information
juan-vg committed Jun 28, 2024
1 parent 2a84bc7 commit 339a9c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v1.6.1

- Improve char-limit handling for plan comments
- Keep 65000 chars instead of 65300 to make enough room for comment wrapper
- Keep the last chars instead of the first ones when truncating (they're usually more useful)
- Always add a link to full logs on plan comments

## v1.6.0

- Bump to Terraform v1.9.0 internally (fixes `curl` problem)
Expand Down
18 changes: 14 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ if [[ $COMMAND == 'plan' ]]; then
if [[ $EXIT_CODE -eq 0 || $EXIT_CODE -eq 2 ]]; then
CLEAN_PLAN=$(echo "$INPUT" | sed -r '/^(An execution plan has been generated and is shown below.|Terraform used the selected providers to generate the following execution|No changes. Infrastructure is up-to-date.|No changes. Your infrastructure matches the configuration.|Note: Objects have changed outside of Terraform)$/,$!d') # Strip refresh section
#CLEAN_PLAN=$(echo "$CLEAN_PLAN" | sed -r '/Plan: /q') # Ignore everything after plan summary ## https://github.com/robburger/terraform-pr-commenter/pull/49/files
CLEAN_PLAN=${CLEAN_PLAN::65300} # GitHub has a 65535-char comment limit - truncate plan, leaving space for comment wrapper
CHAR_LIMIT=65000 # leave space for comment wrapper (max 535 chars)
CLEAN_PLAN=${CLEAN_PLAN:${#CLEAN_PLAN}-CHAR_LIMIT:CHAR_LIMIT} # GitHub has a 65535-char comment limit - truncate plan to CHAR_LIMIT (from the beginning)
CLEAN_PLAN=$(echo "$CLEAN_PLAN" | sed -r 's/^([[:blank:]]*)([-+~])/\2\1/g') # Move any diff characters to start of line
if [[ $COLOURISE == 'true' ]]; then
CLEAN_PLAN=$(echo "$CLEAN_PLAN" | sed -r 's/^~/!/g') # Replace ~ with ! to colourise the diff in GitHub comments
Expand All @@ -196,20 +197,29 @@ if [[ $COMMAND == 'plan' ]]; then
\`\`\`diff
$CLEAN_PLAN
\`\`\`
</details>"
</details>
Please visit [logs]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) for a full, detailed plan output.
"
fi

# Exit Code: 1
# Meaning: Terraform plan failed.
# Actions: Build PR comment.
if [[ $EXIT_CODE -eq 1 ]]; then
CHAR_LIMIT=65000 # leave space for comment wrapper (max 535 chars)
CLEAN_INPUT=${INPUT:${#INPUT}-CHAR_LIMIT:CHAR_LIMIT} # GitHub has a 65535-char comment limit - truncate input to CHAR_LIMIT (from the beginning)

PR_COMMENT="### Terraform \`plan\` Failed for Workspace: \`$WORKSPACE\`
<details$DETAILS_STATE><summary>Show Output</summary>
\`\`\`
$INPUT
$CLEAN_INPUT
\`\`\`
</details>"
</details>
Please visit [logs]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) for a full, detailed plan output.
"
fi

# Add plan comment to PR.
Expand Down

1 comment on commit 339a9c6

@juan-vg
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on robburger#25

Please sign in to comment.