Skip to content

Commit

Permalink
Merge pull request #14 from robburger/feature/validate
Browse files Browse the repository at this point in the history
Add handling for validate command
  • Loading branch information
robburger authored Apr 27, 2021
2 parents 3f26d51 + ff035bb commit 1e0f01b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# Terraform PR Commenter

Adds opinionated comments to PR's based on Terraform `fmt`, `init` and `plan` outputs.
Adds opinionated comments to PR's based on Terraform `fmt`, `init`, `plan` and `validate` outputs.

## Summary

This Docker-based GitHub Action is designed to work in tandem with [hashicorp/setup-terraform](https://github.com/hashicorp/setup-terraform) with the wrapper enabled, taking the output from a `fmt`, `init` or `plan`, formatting it and adding it to a pull request. Any previous comments from this Action are removed to keep the PR timeline clean.
This Docker-based GitHub Action is designed to work in tandem with [hashicorp/setup-terraform](https://github.com/hashicorp/setup-terraform) with the wrapper enabled, taking the output from a `fmt`, `init`, `plan` or `validate`, formatting it and adding it to a pull request. Any previous comments from this Action are removed to keep the PR timeline clean.

Support (for now) is [limited to Linux](https://help.github.com/en/actions/creating-actions/about-actions#types-of-actions) as Docker-based GitHub Actions can only be used on Linux runners.

## Usage

This action can only be run after a Terraform `fmt`, `init`, or `plan` has completed, and the output has been captured. Terraform rarely writes to `stdout` and `stderr` in the same action, so we concatenate the `commenter_input`:
This action can only be run after a Terraform `fmt`, `init`, `plan` or `validate` has completed, and the output has been captured. Terraform rarely writes to `stdout` and `stderr` in the same action, so we concatenate the `commenter_input`:

```yaml
- uses: robburger/terraform-pr-commenter@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
commenter_type: fmt/init/plan # Choose one
commenter_type: fmt/init/plan/validate # Choose one
commenter_input: ${{ format('{0}{1}', steps.step_id.outputs.stdout, steps.step_id.outputs.stderr) }}
commenter_exitcode: ${{ steps.step_id.outputs.exitcode }}
```
### Inputs
| Name | Requirement | Description |
| -------------------- | ----------- | ----------------------------------------------------- |
| `commenter_type` | _required_ | The type of comment. Options: [`fmt`, `init`, `plan`] |
| `commenter_input` | _required_ | The comment to post from a previous step output. |
| `commenter_exitcode` | _required_ | The exit code from a previous step output. |
| Name | Requirement | Description |
| -------------------- | ----------- | ----------------------------------------------------------------- |
| `commenter_type` | _required_ | The type of comment. Options: [`fmt`, `init`, `plan`, `validate`] |
| `commenter_input` | _required_ | The comment to post from a previous step output. |
| `commenter_exitcode` | _required_ | The exit code from a previous step output. |

### Environment Variables

Expand Down Expand Up @@ -119,6 +119,18 @@ jobs:
commenter_input: ${{ format('{0}{1}', steps.init.outputs.stdout, steps.init.outputs.stderr) }}
commenter_exitcode: ${{ steps.init.outputs.exitcode }}
- name: Terraform Validate
id: validate
run: terraform validate
- name: Post Validate
if: always() && github.ref != 'refs/heads/master' && (steps.validate.outcome == 'success' || steps.validate.outcome == 'failure')
uses: robburger/terraform-pr-commenter@v1
with:
commenter_type: validate
commenter_input: ${{ format('{0}{1}', steps.validate.outputs.stdout, steps.validate.outputs.stderr) }}
commenter_exitcode: ${{ steps.validate.outputs.exitcode }}
- name: Terraform Plan
id: plan
run: terraform plan -out workspace.plan
Expand Down
49 changes: 47 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ if [[ -z $3 ]]; then
exit 1
fi

if [[ ! "$1" =~ ^(fmt|init|plan)$ ]]; then
echo -e "Unsupported command \"$1\". Valid commands are \"fmt\", \"init\", \"plan\"."
if [[ ! "$1" =~ ^(fmt|init|plan|validate)$ ]]; then
echo -e "Unsupported command \"$1\". Valid commands are \"fmt\", \"init\", \"plan\", \"validate\"."
exit 1
fi

Expand Down Expand Up @@ -219,3 +219,48 @@ $INPUT

exit 0
fi

###################
# Handler: validate
###################
if [[ $COMMAND == 'validate' ]]; then
# Look for an existing validate PR comment and delete
echo -e "\033[34;1mINFO:\033[0m Looking for an existing validate PR comment."
PR_COMMENT_ID=$(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENTS_URL" | jq '.[] | select(.body|test ("### Terraform `validate` Failed")) | .id')
if [ "$PR_COMMENT_ID" ]; then
echo -e "\033[34;1mINFO:\033[0m Found existing validate PR comment: $PR_COMMENT_ID. Deleting."
PR_COMMENT_URL="$PR_COMMENT_URI/$PR_COMMENT_ID"
curl -sS -X DELETE -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENT_URL" > /dev/null
else
echo -e "\033[34;1mINFO:\033[0m No existing validate PR comment found."
fi

# Exit Code: 0
# Meaning: Terraform successfully validated.
# Actions: Exit.
if [[ $EXIT_CODE -eq 0 ]]; then
echo -e "\033[34;1mINFO:\033[0m Terraform validate completed with no errors. Continuing."

exit 0
fi

# Exit Code: 1
# Meaning: Terraform validate failed or malformed Terraform CLI command.
# Actions: Build PR comment.
if [[ $EXIT_CODE -eq 1 ]]; then
PR_COMMENT="### Terraform \`validate\` Failed
<details$DETAILS_STATE><summary>Show Output</summary>
\`\`\`
$INPUT
\`\`\`
</details>"
fi

# Add validate failure comment to PR.
PR_PAYLOAD=$(echo '{}' | jq --arg body "$PR_COMMENT" '.body = $body')
echo -e "\033[34;1mINFO:\033[0m Adding validate failure comment to PR."
curl -sS -X POST -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -H "$CONTENT_HEADER" -d "$PR_PAYLOAD" -L "$PR_COMMENTS_URL" > /dev/null

exit 0
fi

0 comments on commit 1e0f01b

Please sign in to comment.