From aaa17aa9d3902ec489666ce59cfc30535500b4c6 Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 11 Mar 2022 10:45:26 +0000 Subject: [PATCH] fix: comment-action uses infracost-comment --- .github/workflows/comment_test.yml | 4 +- .github/workflows/examples_test.yml | 195 ++++++++++-------- .github/workflows/get_comment_test.yml | 32 +-- README.md | 30 ++- comment/action.yml | 57 ++--- examples/multi-project/README.md | 48 +++-- examples/multi-terraform-workspace/README.md | 22 +- examples/private-terraform-module/README.md | 22 +- examples/slack/README.md | 22 +- examples/terraform-cloud-enterprise/README.md | 22 +- examples/terraform-directory/README.md | 22 +- examples/terraform-plan-json/README.md | 22 +- examples/terragrunt/README.md | 22 +- scripts/generateExamplesTests.js | 43 ++-- ...ulti-project-config-file_comment_golden.md | 8 + ...lti-project-matrix-merge_comment_golden.md | 8 + ...rm-workspace-config-file_comment_golden.md | 8 + ...private-terraform-module_comment_golden.md | 8 + testdata/slack_comment_golden.md | 8 + ...rraform-cloud-enterprise_comment_golden.md | 8 + .../terraform-directory_comment_golden.md | 8 + .../terraform-plan-json_comment_golden.md | 8 + testdata/terragrunt_comment_golden.md | 8 + 23 files changed, 423 insertions(+), 212 deletions(-) diff --git a/.github/workflows/comment_test.yml b/.github/workflows/comment_test.yml index 912099e..e7c1499 100644 --- a/.github/workflows/comment_test.yml +++ b/.github/workflows/comment_test.yml @@ -28,6 +28,7 @@ jobs: uses: ./comment with: path: ./testdata/comment/simple_breakdown.json + tag: "infracost-pull-request" test_commit: name: Test Commit Comment runs-on: ubuntu-latest @@ -41,5 +42,6 @@ jobs: - name: Infracost comment uses: ./comment with: - path: '["./testdata/comment/simple_breakdown.json", "./testdata/comment/simple_breakdown.json"]' + path: "./testdata/comment/*.json" target-type: commit + tag: "infracost-commit" diff --git a/.github/workflows/examples_test.yml b/.github/workflows/examples_test.yml index 8c31b0a..1bd69d2 100644 --- a/.github/workflows/examples_test.yml +++ b/.github/workflows/examples_test.yml @@ -31,15 +31,19 @@ jobs: DEV_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_DEV_AWS_SECRET_ACCESS_KEY }} PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }} PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }} - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/multi-project-config-file_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/multi-project-config-file_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -95,20 +99,19 @@ jobs: uses: ./setup with: api-key: ${{ secrets.INFRACOST_API_KEY }} - - name: Combine the results - run: > - infracost output --path="/tmp/infracost_jsons/*.json" --format=json - --out-file=/tmp/infracost_combined.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost_combined.json - --format=github-comment --show-skipped - --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/multi-project-matrix-merge_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path "/tmp/infracost_jsons/*.json" \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/multi-project-matrix-merge_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -138,16 +141,20 @@ jobs: DEV_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_DEV_AWS_SECRET_ACCESS_KEY }} PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }} PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }} - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/multi-terraform-workspace-config-file_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -180,15 +187,19 @@ jobs: run: >- infracost breakdown --path=examples/private-terraform-module/code --format=json --out-file=/tmp/infracost.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/private-terraform-module_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/private-terraform-module_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -243,13 +254,17 @@ jobs: run: >- infracost breakdown --path=examples/slack/code/plan.json --format json --out-file /tmp/infracost.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: diff ./testdata/slack_comment_golden.md /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: diff -y ./testdata/slack_comment_golden.md infracost-comment.md + name: Check the comment - name: Update the golden comment file run: cp /tmp/infracost_comment.md ./testdata/slack_comment_golden.md if: env.UPDATE_GOLDEN_FILES == 'true' @@ -295,15 +310,19 @@ jobs: --format=json --out-file=/tmp/infracost.json env: INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TFC_TOKEN }} - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/terraform-cloud-enterprise_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/terraform-cloud-enterprise_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -326,15 +345,19 @@ jobs: run: >- infracost breakdown --path=examples/terraform-directory/code --format=json --out-file=/tmp/infracost.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/terraform-directory_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/terraform-directory_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -353,15 +376,19 @@ jobs: run: >- infracost breakdown --path=examples/terraform-plan-json/code/plan.json --format=json --out-file=/tmp/infracost.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: >- - diff ./testdata/terraform-plan-json_comment_golden.md - /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: >- + diff -y ./testdata/terraform-plan-json_comment_golden.md + infracost-comment.md + name: Check the comment - name: Update the golden comment file run: >- cp /tmp/infracost_comment.md @@ -388,13 +415,17 @@ jobs: run: >- infracost breakdown --path=examples/terragrunt/code --format=json --out-file=/tmp/infracost.json - - name: Generate Infracost comment - run: >- - infracost output --path=/tmp/infracost.json --format=github-comment - --show-skipped --out-file=/tmp/infracost_comment.md - - name: Check the comment - run: diff ./testdata/terragrunt_comment_golden.md /tmp/infracost_comment.md - if: env.UPDATE_GOLDEN_FILES != 'true' + - name: Post Infracost comment + run: |- + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update \ + --dry-run true \ + > infracost-comment.md + - run: diff -y ./testdata/terragrunt_comment_golden.md infracost-comment.md + name: Check the comment - name: Update the golden comment file run: cp /tmp/infracost_comment.md ./testdata/terragrunt_comment_golden.md if: env.UPDATE_GOLDEN_FILES == 'true' diff --git a/.github/workflows/get_comment_test.yml b/.github/workflows/get_comment_test.yml index e9d193f..11e04e6 100644 --- a/.github/workflows/get_comment_test.yml +++ b/.github/workflows/get_comment_test.yml @@ -25,30 +25,36 @@ jobs: with: api-key: abcdefg123456 + - name: Save comment output + run: | + infracost comment github --path ./testdata/comment/simple_breakdown.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior new \ + --dry-run true \ + > expected.md + + head -n -2 expected.md > tmp.txt && mv tmp.txt expected.md + - name: Infracost comment id: post-comment uses: ./comment with: path: ./testdata/comment/simple_breakdown.json + tag: "custom-tag" + behavior: new - name: Infracost get comment id: get-comment uses: ./get-comment + with: + tag: "custom-tag" - - name: Check comment - shell: bash - run: | - expected=$(cat <<\EOF - ${{ steps.post-comment.outputs.body }} - EOF - ) + - run: | actual=$(cat <<\EOF ${{ steps.get-comment.outputs.body }} EOF ) - - if [ "$actual" != "$expected" ]; then - echo "::error::Expected comment '$expected' got '$actual'" - exit 1 - fi - + diff -y <(echo "$actual") expected.md + name: Check the comment \ No newline at end of file diff --git a/README.md b/README.md index 8e2359a..d2273b3 100644 --- a/README.md +++ b/README.md @@ -76,17 +76,23 @@ The following steps assume a simple Terraform directory is being used, we recomm # env: # MY_ENV: ${{ secrets.MY_ENV }} - # See https://github.com/infracost/actions/tree/master/comment + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests # for other inputs such as target-type. - name: Post Infracost comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - # Choose the commenting behavior, 'update' is a good default: - behavior: update # Create a single comment and update it. The "quietest" option. - # behavior: delete-and-new # Delete previous comments and create a new one. - # behavior: hide-and-new # Minimize previous comments and create a new one. - # behavior: new # Create a new cost estimate comment on every push. + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` 4. 🎉 That's it! Send a new pull request to change something in Terraform that costs money. You should see a pull request comment that gets updated, e.g. the 📉 and 📈 emojis will update as changes are pushed! @@ -120,8 +126,10 @@ Infracost cost estimates can also be configured to work against policies written We recommend you use the above [quick start](#quick-start) guide and examples, which combine the following individual actions: - [setup](setup): downloads and installs the Infracost CLI in your GitHub Actions workflow. -- [comment](comment): adds comments to pull requests. -- [get-comment](get-comment): reads a comment from a pull request. + +### Deprecated Actions +- [comment](comment): adds comments to pull requests. This action is deprecated, please use `infracost comment` directly. +- [get-comment](get-comment): reads a comment from a pull request. This action is deprecated. ## Contributing diff --git a/comment/action.yml b/comment/action.yml index 6bca5c7..a40a292 100644 --- a/comment/action.yml +++ b/comment/action.yml @@ -18,48 +18,27 @@ inputs: description: 'Default to {{ github.token }}. This is the default GitHub token available to actions and is used to post comments. The default token permissions (https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#permissions) work fine; `pull-requests: write` is required if you need to customize these.' default: '${{ github.token }}' required: false -outputs: - body: - description: 'The body of comment that was posted.' - value: ${{ steps.compost-comment.outputs.body }} runs: using: "composite" steps: - - name: Run infracost output + - name: Post infracost comment to a github pull-request + if: ${{ (inputs.target-type != 'commit') && (github.event.pull_request.number != '') }} shell: bash - run: | - # handle multiple paths - if jq . >/dev/null 2>&1 <<< '${{ inputs.path }}'; then - paths=$(jq -r 'if type == "array" then map("--path " + .) | join(" ") else . end' <<< '${{ inputs.path }}') - else - paths="--path ${{ inputs.path }}" - fi - echo "::notice::Using paths: $paths" - - export INFRACOST_CI_POST_CONDITION=${{ inputs.behavior }} - - infracost output $paths --format github-comment --show-skipped --out-file comment.md - - if [ "${{ inputs.target-type }}" != "commit" ]; then - if [ "${{ inputs.behavior }}" = "update" ]; then - printf "\nThis comment will be updated when the cost estimate changes.\n\n" >> comment.md - elif [ "${{ inputs.behavior }}" = "delete-and-new" ]; then - printf "\nThis comment will be replaced when the cost estimate changes.\n\n" >> comment.md - fi - fi - - printf "\n" >> comment.md - printf " Is this comment useful? Yes, No\n" >> comment.md - printf "\n" >> comment.md - - name: Comment - id: compost-comment - uses: infracost/compost-action@master - with: - body-file: comment.md - behavior: ${{ inputs.behavior }} - target-type: ${{ inputs.target-type }} - tag: ${{ inputs.tag }} - github-token: ${{ inputs.github-token }} - + infracost comment github --path ${{inputs.path}} \ + --repo $GITHUB_REPOSITORY \ + --tag "${{inputs.tag}}" \ + --github-token ${{inputs.github-token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior ${{inputs.behavior}} + - name: Post infracost comment to a commit sha + if: ${{ (inputs.target-type == 'commit') }} + shell: bash + run: | + infracost comment github --path ${{inputs.path}} \ + --repo $GITHUB_REPOSITORY \ + --tag "${{inputs.tag}}" \ + --github-token ${{inputs.github-token}} \ + --commit $GITHUB_SHA \ + --behavior ${{inputs.behavior}} diff --git a/examples/multi-project/README.md b/examples/multi-project/README.md index 4c2bdf9..6ca64f2 100644 --- a/examples/multi-project/README.md +++ b/examples/multi-project/README.md @@ -38,17 +38,29 @@ jobs: PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }} PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }} - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) ## Using GitHub Actions build matrix -This example shows how to run Infracost actions with multiple Terraform projects using a GitHub Actions build matrix. The first job uses a build matrix to generate multiple Infracost output JSON files and upload them as artifacts. The second job downloads these JSON files, combines them using `infracost output`, and posts a comment. +This example shows how to run Infracost actions with multiple Terraform projects using a GitHub Actions build matrix. The first job uses a build matrix to generate multiple Infracost output JSON files and upload them as artifacts. The second job downloads these JSON files and posts a combined comment to the PR using `infracost comment` glob support. [//]: <> (BEGIN EXAMPLE) ```yml @@ -115,14 +127,22 @@ jobs: with: api-key: ${{ secrets.INFRACOST_API_KEY }} - - name: Combine the results + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment run: | - infracost output --path="/tmp/infracost_jsons/*.json" --format=json --out-file=/tmp/infracost_combined.json - - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost_combined.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path "/tmp/infracost_jsons/*.json" \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/multi-terraform-workspace/README.md b/examples/multi-terraform-workspace/README.md index b5fdf18..61e72d2 100644 --- a/examples/multi-terraform-workspace/README.md +++ b/examples/multi-terraform-workspace/README.md @@ -34,10 +34,22 @@ jobs: PROD_AWS_ACCESS_KEY_ID: ${{ secrets.EXAMPLE_PROD_AWS_ACCESS_KEY_ID }} PROD_AWS_SECRET_ACCESS_KEY: ${{ secrets.EXAMPLE_PROD_AWS_SECRET_ACCESS_KEY }} - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/private-terraform-module/README.md b/examples/private-terraform-module/README.md index 4d86b1c..c022635 100644 --- a/examples/private-terraform-module/README.md +++ b/examples/private-terraform-module/README.md @@ -37,10 +37,22 @@ jobs: - name: Run Infracost run: infracost breakdown --path=examples/private-terraform-module/code --format=json --out-file=/tmp/infracost.json - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/slack/README.md b/examples/slack/README.md index 2d784ae..d28edb0 100644 --- a/examples/slack/README.md +++ b/examples/slack/README.md @@ -29,11 +29,23 @@ jobs: - name: Generate Infracost JSON run: infracost breakdown --path=examples/slack/code/plan.json --format json --out-file /tmp/infracost.json - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update - name: Generate Slack message id: infracost-slack diff --git a/examples/terraform-cloud-enterprise/README.md b/examples/terraform-cloud-enterprise/README.md index 48dba6f..9d5eeac 100644 --- a/examples/terraform-cloud-enterprise/README.md +++ b/examples/terraform-cloud-enterprise/README.md @@ -36,10 +36,22 @@ jobs: INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TFC_TOKEN }} # INFRACOST_TERRAFORM_CLOUD_HOST: my-tfe-host.com # For Terraform Enterprise users only. - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/terraform-directory/README.md b/examples/terraform-directory/README.md index 42f6145..9b89803 100644 --- a/examples/terraform-directory/README.md +++ b/examples/terraform-directory/README.md @@ -30,10 +30,22 @@ jobs: - name: Run Infracost run: infracost breakdown --path=examples/terraform-directory/code --format=json --out-file=/tmp/infracost.json - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/terraform-plan-json/README.md b/examples/terraform-plan-json/README.md index 6b35b4a..30c04bf 100644 --- a/examples/terraform-plan-json/README.md +++ b/examples/terraform-plan-json/README.md @@ -23,10 +23,22 @@ jobs: - name: Run Infracost run: infracost breakdown --path=examples/terraform-plan-json/code/plan.json --format=json --out-file=/tmp/infracost.json - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/examples/terragrunt/README.md b/examples/terragrunt/README.md index 996eeb1..26f0086 100644 --- a/examples/terragrunt/README.md +++ b/examples/terragrunt/README.md @@ -35,10 +35,22 @@ jobs: - name: Run Infracost run: infracost breakdown --path=examples/terragrunt/code --format=json --out-file=/tmp/infracost.json - - name: Post the comment - uses: infracost/actions/comment@v1 - with: - path: /tmp/infracost.json - behavior: update # Create a single comment and update it. See https://github.com/infracost/actions/tree/master/comment for other options + # See https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests + # for other inputs such as target-type. + - name: Post Infracost comment + run: | + + # Posts a comment to the PR using the 'update' behavior. + # This creates a single comment and updates it. The "quietest" option. + # The other valid behaviors are: + # delete-and-new - Delete previous comments and create a new one. + # hide-and-new - Minimize previous comments and create a new one. + # new - Create a new cost estimate comment on every push. + + infracost comment github --path /tmp/infracost.json \ + --repo $GITHUB_REPOSITORY \ + --github-token ${{github.token}} \ + --pull-request ${{github.event.pull_request.number}} \ + --behavior update ``` [//]: <> (END EXAMPLE) diff --git a/scripts/generateExamplesTests.js b/scripts/generateExamplesTests.js index 9dd221a..6cf587b 100755 --- a/scripts/generateExamplesTests.js +++ b/scripts/generateExamplesTests.js @@ -86,19 +86,22 @@ function fixupExamples(examples) { const steps = []; for (const step of job.steps) { - if (step.uses && step.uses.startsWith('infracost/actions/comment')) { - const path = step.with.path; + if (step.name && step.name.toLowerCase() === 'post infracost comment') { const goldenFilePath = `./testdata/${jobKey}_comment_golden.md`; + const commentArgs = step.run + .replace(/\\/g, '') + .split('\n') + .map(s => s.trim()) + .filter(e => !e.startsWith('#') && e !== '') + + commentArgs.push('--dry-run true', '> infracost-comment.md') + step.run = commentArgs.join(' \\\n'); steps.push( + step, { - name: 'Generate Infracost comment', - run: `infracost output --path=${path} --format=github-comment --show-skipped --out-file=/tmp/infracost_comment.md`, - }, - { + run: `diff -y ${goldenFilePath} infracost-comment.md`, name: 'Check the comment', - run: `diff ${goldenFilePath} /tmp/infracost_comment.md`, - if: `env.UPDATE_GOLDEN_FILES != 'true'`, }, { name: 'Update the golden comment file', @@ -106,7 +109,11 @@ function fixupExamples(examples) { if: `env.UPDATE_GOLDEN_FILES == 'true'`, } ); - } else if (step.uses && step.uses.startsWith('slackapi/slack-github-action')) { + + continue; + } + + if (step.uses && step.uses.startsWith('slackapi/slack-github-action')) { // Assume this path for now. If we add our own Slack action we can get this easier from an input const path = '/tmp/infracost.json'; const goldenFilePath = `./testdata/${jobKey}_slack_message_golden.json`; @@ -127,15 +134,17 @@ function fixupExamples(examples) { if: `env.UPDATE_GOLDEN_FILES == 'true'`, } ); - } else { - // Replace infracost/actions steps with the local path - steps.push({ - ...step, - uses: - step.uses && - step.uses.replace(/infracost\/actions\/(\w+)(@\w+)?/, './$1'), - }); + + continue; } + + // Replace infracost/actions steps with the local path + steps.push({ + ...step, + uses: + step.uses && + step.uses.replace(/infracost\/actions\/(\w+)(@\w+)?/, './$1'), + }); } job.steps = steps; diff --git a/testdata/multi-project-config-file_comment_golden.md b/testdata/multi-project-config-file_comment_golden.md index dc06e7d..7ea9bf8 100644 --- a/testdata/multi-project-config-file_comment_golden.md +++ b/testdata/multi-project-config-file_comment_golden.md @@ -111,3 +111,11 @@ Key: ~ changed, + added, - removed ∙ 4 were estimated, 4 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/multi-project-matrix-merge_comment_golden.md b/testdata/multi-project-matrix-merge_comment_golden.md index dc06e7d..7ea9bf8 100644 --- a/testdata/multi-project-matrix-merge_comment_golden.md +++ b/testdata/multi-project-matrix-merge_comment_golden.md @@ -111,3 +111,11 @@ Key: ~ changed, + added, - removed ∙ 4 were estimated, 4 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/multi-terraform-workspace-config-file_comment_golden.md b/testdata/multi-terraform-workspace-config-file_comment_golden.md index eadc0da..20e793a 100644 --- a/testdata/multi-terraform-workspace-config-file_comment_golden.md +++ b/testdata/multi-terraform-workspace-config-file_comment_golden.md @@ -111,3 +111,11 @@ Key: ~ changed, + added, - removed ∙ 4 were estimated, 4 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/private-terraform-module_comment_golden.md b/testdata/private-terraform-module_comment_golden.md index 8660ca7..13caeb4 100644 --- a/testdata/private-terraform-module_comment_golden.md +++ b/testdata/private-terraform-module_comment_golden.md @@ -47,3 +47,11 @@ Key: ~ changed, + added, - removed ∙ 1 was estimated, 1 includes usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/slack_comment_golden.md b/testdata/slack_comment_golden.md index b469d1c..9da1e73 100644 --- a/testdata/slack_comment_golden.md +++ b/testdata/slack_comment_golden.md @@ -63,3 +63,11 @@ Key: ~ changed, + added, - removed ∙ 2 were estimated, 2 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/terraform-cloud-enterprise_comment_golden.md b/testdata/terraform-cloud-enterprise_comment_golden.md index 57e53f1..03eb965 100644 --- a/testdata/terraform-cloud-enterprise_comment_golden.md +++ b/testdata/terraform-cloud-enterprise_comment_golden.md @@ -63,3 +63,11 @@ Key: ~ changed, + added, - removed ∙ 2 were estimated, 2 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/terraform-directory_comment_golden.md b/testdata/terraform-directory_comment_golden.md index e8a7e08..f5c88d9 100644 --- a/testdata/terraform-directory_comment_golden.md +++ b/testdata/terraform-directory_comment_golden.md @@ -63,3 +63,11 @@ Key: ~ changed, + added, - removed ∙ 2 were estimated, 2 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/terraform-plan-json_comment_golden.md b/testdata/terraform-plan-json_comment_golden.md index e51b7cd..7c2e2f3 100644 --- a/testdata/terraform-plan-json_comment_golden.md +++ b/testdata/terraform-plan-json_comment_golden.md @@ -63,3 +63,11 @@ Key: ~ changed, + added, - removed ∙ 2 were estimated, 2 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified) diff --git a/testdata/terragrunt_comment_golden.md b/testdata/terragrunt_comment_golden.md index 3b903e4..d040a7b 100644 --- a/testdata/terragrunt_comment_golden.md +++ b/testdata/terragrunt_comment_golden.md @@ -111,3 +111,11 @@ Key: ~ changed, + added, - removed ∙ 4 were estimated, 4 include usage-based costs, see https://infracost.io/usage-file ``` + +This comment will be updated when the cost estimate changes. + + + Is this comment useful? Yes, No + + +Comment not posted to GitHub (--dry-run was specified)