Skip to content

Commit

Permalink
#6: Only Terraform files should be formatted (#12)
Browse files Browse the repository at this point in the history
* doc(#6): Added permissions scopes to doc
chore(#9): Changed to using terraform fmt diff

chore(#6): Revert fmt diff

chore(#6): Add more example files

chore(#6): Add more example files

chore(#6): Add more example files

* chore(#6): Add more example files

* chore(#6): Get files changed for TF only

chore(#6): Fix for printing to console

chore(#6): Fix for printing to console

chore(#6): Debugging print to console

chore(#6): Debugging print to console

chore(#6): Debugging print to console

* fix(#6): Skip deleted files

chore(#6): Debugging print to console

chore(#6): Debugging print to console

chore(#6): Remove extra echos

* chore(#6): Version bump

* chore(#6): Restructure tests and wording

* chore(#6): Debug successful step marked as failed

* chore(#6): Debug successful step marked as failed

* chore: Remove debugs
  • Loading branch information
pvicol authored Dec 18, 2024
1 parent 7dcd33c commit 20ff675
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/latest-release.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Latest Tag Updates
on:
release:
types: [published]

push:
tags:
- 'v*'
jobs:
run:
permissions:
Expand Down
56 changes: 39 additions & 17 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,74 @@ jobs:
if: steps.fail_missing_version.outcome != 'failure'
run: exit 1

- name: "Modify test.tf file to introduce a formatting issue"
- name: "Modify files to introduce a formatting issue - With Token Part 1"
run: |
sed -i '2s/[[:space:]]//g' example/test.tf
sed -i '2s/[[:space:]]//g' test.tf
echo 'resource "random_pet" "test" {length=5}' > pets.tf
- name: Commit the munged `test.tf`
run: |
echo ' resource "random_pet" "test" {length=5 }' > example/pets.tf
echo " " >> example/this-file-is-ignored.json
git add .
git commit -m "Running tests"
- name: "Test: Non-formatted Terraform file"
id: failure_step
git commit -m "Running tests with token - part 1"
- name: "Test: Non-formatted Terraform file - With Token Part 1"
id: failure_step_w_token
continue-on-error: true
uses: ./
with:
terraform_version: 1.9.8
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: "Assert Failed: Non-formatted Terraform file"
if: steps.failure_step.outcome != 'failure'
- name: "Assert Failed: Non-formatted Terraform file - With Token Part 2"
if: steps.failure_step_w_token.outcome != 'failure'
run: exit 1

# Commit formatted file to simulate a successful run
- name: Commit formatted files
- name: "Commit formatted file to simulate a successful run - With Token Part 2"
run: |
git add .
git commit -m "Running tests"
git commit -m "Running tests with token - part 2"
- name: "Test: All files formatted correctly - post comment"
id: success_step_w_token
uses: ./
with:
terraform_version: 1.9.8
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: "Assert Failed: Test: All files formatted correctly - post comment"
if: steps.success_step_w_token.outcome == 'failure'
run: exit 1

# Print message to console when GitHub Token is not passed
- name: "Modify test.tf file to introduce a formatting issue"
- name: "Modify files to introduce a formatting issue - W/O Token Part 1"
run: |
sed -i '2s/[[:space:]]//g' test.tf
echo 'resource "random_pet" "test" {length=5}' > pets.tf
- name: "Test: All files formatted correctly - print to console"
sed -i '2s/[[:space:]]//g' example/test.tf
echo ' resource "random_pet" "test" {length=5 }' > example/pets.tf
echo " " >> example/this-file-is-ignored.json
echo "this text file will be ignored by terraform_fmt" > example/this-file-is-ignored.txt
rm test.tf
git add .
git commit -m "Running tests w/o token - part 1"
- name: "Test: Non-formatted Terraform file - print to console"
id: failure_step_wo_token
uses: ./
with:
terraform_version: 1.9.8
continue-on-error: true
- name: "Assert Failed: All files formatted correctly - print to console"
- name: "Assert Failed: Non-formatted Terraform file - print to console"
if: steps.failure_step_wo_token.outcome != 'failure'
run: exit 1
- name: "Commit formatted file to simulate a successful run - W/O Token Part 2"
run: |
git add .
git commit -m "Running tests w/o token - part 2"
- name: "Test: All files formatted correctly - print to console"
id: success_step_wo_token
uses: ./
with:
terraform_version: 1.9.8
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: "Assert Failed: All files formatted correctly - print to console"
if: steps.success_step_wo_token.outcome == 'failure'
run: exit 1

check_version:
if: github.event_name == 'pull_request'
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ A GitHub Action that enforces Terraform formatting standards by running `terrafo
* Posts detailed comments on pull requests highlighting formatting issues when the `GITHUB_TOKEN` is provided.
* Ensures consistency across Terraform codebases.

## Permissions Scopes

The following permissions scopes are needed:

| Permission | Level | Description |
|-----------------|---------|-----------------------------------------------------------|
| `contents` | `read` | Work with the contents of the repository and list commits |
| `pull-requests` | `write` | Add comments to the Pull Request |

## Pull Request Comments

When the `GITHUB_TOKEN` is passed, the action posts a comment to the pull request. The comment includes:
Expand Down Expand Up @@ -48,6 +57,7 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout the contents
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.4.0
v1.4.1
16 changes: 13 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ git config --global --add safe.directory "${PWD}"

# Get changed files
if git diff --name-only HEAD HEAD~1 >/dev/null 2>&1; then
CHANGED_FILES=$(git diff --name-only HEAD HEAD~1)
CHANGED_FILES=$(git diff --name-only HEAD HEAD~1 | grep -E '\.(tf|tf\.json)$')
echo "Using git diff to determine changed files."
else
echo "Unable to determine changed files using git diff. Checking all Terraform files."
Expand All @@ -43,6 +43,11 @@ fi
# Check Terraform formatting
FAILED="false"
for FILENAME in $CHANGED_FILES; do
# Skip deleted files
if [ ! -f "$FILENAME" ]; then
echo "Skipping deleted file: ${FILENAME}"
continue
fi
case "$FILENAME" in
*.tf|*.tf.json)
echo "Checking formatting for ${FILENAME}"
Expand All @@ -51,6 +56,7 @@ for FILENAME in $CHANGED_FILES; do
FAILED="true"
echo "${FILENAME} failed formatting."
else
FAILED="false"
echo "${FILENAME} is properly formatted."
fi
;;
Expand Down Expand Up @@ -89,6 +95,11 @@ if [ "${FAILED}" = "true" ]; then
echo "Formatting errors found in the files"
COMMENT_BODY=":x: **Formatting errors found in the following files:**\n\n"
for FILE in $CHANGED_FILES; do
# Skip deleted files
if [ ! -f "${FILE}" ]; then
echo "Skipping deleted file: ${FILE}"
continue
fi
DIFF=$(git diff "${FILE}")
COMMENT_BODY="${COMMENT_BODY}${FILE}\n\`\`\`\n${DIFF}\n\`\`\`\n\n"
done
Expand All @@ -99,6 +110,5 @@ if [ "${FAILED}" = "true" ]; then
exit 1
else
post_comment ":white_check_mark: All Terraform files are properly formatted."
exit 0
fi

exit 0
6 changes: 6 additions & 0 deletions example/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
terraform {
required_version = ">= 0.12.31"



}
7 changes: 7 additions & 0 deletions example/this-file-is-ignored.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ignore": {
"this":
"file"

}
}

0 comments on commit 20ff675

Please sign in to comment.