From 20ff675768101cfe77d94d8e72b830e00b868176 Mon Sep 17 00:00:00 2001 From: Petru Vicol Date: Wed, 18 Dec 2024 12:05:18 -0800 Subject: [PATCH] #6: Only Terraform files should be formatted (#12) * 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 --- .github/workflows/latest-release.yml | 6 +-- .github/workflows/test.yml | 56 +++++++++++++++++++--------- README.md | 10 +++++ VERSION | 2 +- entrypoint.sh | 16 ++++++-- example/test.tf | 6 +++ example/this-file-is-ignored.json | 7 ++++ 7 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 example/test.tf create mode 100644 example/this-file-is-ignored.json diff --git a/.github/workflows/latest-release.yml b/.github/workflows/latest-release.yml index 6de8e53..d564d1c 100644 --- a/.github/workflows/latest-release.yml +++ b/.github/workflows/latest-release.yml @@ -1,8 +1,8 @@ name: Latest Tag Updates on: - release: - types: [published] - + push: + tags: + - 'v*' jobs: run: permissions: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bacffe2..25e4f83 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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' diff --git a/README.md b/README.md index 8b8e7a1..d453f33 100644 --- a/README.md +++ b/README.md @@ -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: @@ -48,6 +57,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + pull-requests: write steps: - name: Checkout the contents uses: actions/checkout@v4 diff --git a/VERSION b/VERSION index 0d0c52f..66d62a8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.4.0 +v1.4.1 diff --git a/entrypoint.sh b/entrypoint.sh index bd1488f..829530f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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." @@ -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}" @@ -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 ;; @@ -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 @@ -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 diff --git a/example/test.tf b/example/test.tf new file mode 100644 index 0000000..91b1bf7 --- /dev/null +++ b/example/test.tf @@ -0,0 +1,6 @@ +terraform { +required_version = ">= 0.12.31" + + + +} diff --git a/example/this-file-is-ignored.json b/example/this-file-is-ignored.json new file mode 100644 index 0000000..7ec01ac --- /dev/null +++ b/example/this-file-is-ignored.json @@ -0,0 +1,7 @@ +{ + "ignore": { + "this": + "file" + + } +}