Skip to content

Commit

Permalink
added support for project parameter
Browse files Browse the repository at this point in the history
added output with comment_id and comment_url
  • Loading branch information
k1rk committed Aug 17, 2023
1 parent 6a81463 commit 8948f2b
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 67 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/release-beta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ name: Release Beta
on:
pull_request:

permissions:
contents: write

jobs:
release-beta:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set Beta Git tag
uses: weareyipyip/walking-tag-action@v2
with:
tag-name: v3-beta
tag-message: The current beta is based on this commit
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14 changes: 9 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ name: Release

on:
push:
branches: [ master ]
branches: [master]

permissions:
contents: write
packages: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.0
uses: mathieudutour/github-tag-action@v6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
release_branches: "master"
- uses: sersoft-gmbh/running-release-tags-action@v1
- uses: sersoft-gmbh/running-release-tags-action@v2
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{secrets.GITHUB_TOKEN}}
22 changes: 20 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: Test
on:
pull_request:

permissions:
checks: write
pull-requests: write

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TERRAFORM_VERSION: "1.4.6"
Expand All @@ -11,8 +15,8 @@ env:
GH_ACCEPT_HEADER: "Accept: application/vnd.github+json"
GH_AUTH_HEADER: "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"
GH_API_VERSION: "X-GitHub-Api-Version: 2022-11-28"
GH_COMMENT_URL: https://api.github.com/repos/GetTerminus/terraform-pr-commenter/issues/${{ github.event.number }}/comments
TESTING: "false" #set to false when finished testing
GH_COMMENT_URL: https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/comments
TESTING: "true" #set to false when finished testing

jobs:
set-outputs:
Expand Down Expand Up @@ -70,6 +74,17 @@ jobs:
- uses: actions/checkout@v3
if: ${{ env.TESTING == 'true' }}
- name: Test
if: ${{ env.TESTING == 'true' }}
id: test
uses: GetTerminus/terraform-pr-commenter@v3-beta #set to your branch for testing and switch back to v3-beta when done.
with:
commenter_type: plan
# Should only be setting commenter_input or commenter_plan_path (commenter_plan_path only for plan commenter type)
commenter_input: ${{ needs.set-outputs.outputs.tf_plan_success_with_outputs }}
#commenter_plan_path: ./testing/text-files/tf_plan_success_with_outputs.txt
commenter_exitcode: 2
use_beta_version: "true"
- name: Test-project-2
if: ${{ env.TESTING == 'true' }}
uses: GetTerminus/terraform-pr-commenter@v3-beta #set to your branch for testing and switch back to v3-beta when done.
with:
Expand All @@ -79,3 +94,6 @@ jobs:
#commenter_plan_path: ./testing/text-files/tf_plan_success_with_outputs.txt
commenter_exitcode: 2
use_beta_version: "true"
project: dev-team-2
- name: test output
run: echo "${{ steps.test.outputs.comment_id }}"
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ jobs:
| `commenter_exitcode` | ___required___ | The exit code from a previous step output. |
| `terraform_version` | ___optional___ | The version of terraform from the workflow. Defaults to `1.4.6`. |
| `use_beta_version` | ___optional___ | Whether or not to use the beta version of the commenter. |
| `project` | ___optional___ | Project name to use in comments header. usefull for monorepos |

### Environment Variables

Expand All @@ -122,6 +123,14 @@ jobs:
| `HIGHLIGHT_CHANGES` | ___optional___ | Default: `true`. This switches `~` to `!` in `plan` diffs to highlight Terraform changes in orange. Set to `false` to disable. |
| `COMMENTER_DEBUG` | ___optional___ | Default: `false`. This switches the commenter into debug mode. |

### Outputs

| Name | Description |
|---|---|
| `comment_id` | ID of comment created by step. Can be used for further manipulations with comment |
| `comment_url` | URL pointing to comment created by step. Can be used to create a link from summary or to send a notification |


## Notes

* The commenter requires a pull request to run so the github event must contain a `.pull_request.number`.
Expand Down
19 changes: 19 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ inputs:
description: 'Whether to use the beta version of the commenter'
required: false
default: 'false'
project:
description: project name to use in the comment
required: false
outputs:
comment_id:
description: ID of created comment
value: ${{ steps.commenter-plan.outputs.comment_id || steps.commenter.outputs.comment_id }}
comment_url:
description: URL to created comment
value: ${{ steps.commenter-plan.outputs.comment_url || steps.commenter.outputs.comment_url }}

runs:
using: "composite"
steps:
Expand All @@ -49,14 +60,17 @@ runs:
shell: bash
- name: Run commenter image (plan)
if: ${{ inputs.commenter_type == 'plan' }}
id: commenter-plan
env:
COMMENTER_INPUT: ${{ inputs.commenter_input }}
COMMENTER_PLAN_FILE: ${{ inputs.commenter_plan_path }}
GITHUB_EVENT: ${{ toJSON(github.event) }}
TF_PROJECT: ${{ inputs.project }}
run: |
docker run \
-e GITHUB_TOKEN \
-e TF_WORKSPACE \
-e TF_PROJECT \
-e EXPAND_SUMMARY_DETAILS \
-e HIGHLIGHT_CHANGES \
-e GITHUB_EVENT \
Expand All @@ -66,22 +80,27 @@ runs:
-e COMMENTER_PLAN_FILE \
-e COMMENTER_POST_PLAN_OUTPUTS \
-v "$(pwd)"/:/workspace \
-v "$GITHUB_OUTPUT":/github-ouput \
commenter ${{ inputs.commenter_type }} ${{ inputs.commenter_exitcode }}
shell: bash
- name: Run commenter image (non-plan)
if: ${{ inputs.commenter_type != 'plan' }}
id: commenter
env:
COMMENTER_INPUT: ${{ inputs.commenter_input }}
GITHUB_EVENT: ${{ toJSON(github.event) }}
TF_PROJECT: ${{ inputs.project }}
run: |
docker run \
-e GITHUB_TOKEN \
-e TF_WORKSPACE \
-e TF_PROJECT \
-e EXPAND_SUMMARY_DETAILS \
-e HIGHLIGHT_CHANGES \
-e GITHUB_EVENT \
-e COMMENTER_INPUT \
-e COMMENTER_DEBUG \
-e COMMENTER_ECHO \
-v "$GITHUB_OUTPUT":/github-ouput \
commenter ${{ inputs.commenter_type }} ${{ inputs.commenter_exitcode }}
shell: bash
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
# shellcheck source=handler/
for HF in handlers/* ; do source "$HF" ; done
for HF in handlers/*; do source "$HF"; done
# shellcheck source=utilities/
for UF in utilities/* ; do source "$UF" ; done
for UF in utilities/*; do source "$UF"; done

if [ -n "${COMMENTER_ECHO+x}" ]; then
set -x
Expand Down
12 changes: 6 additions & 6 deletions handlers/fmt_handler.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
execute_fmt () {
execute_fmt() {
delete_existing_comments 'fmt' '### Terraform `fmt` Failed'

# Exit Code: 0
Expand All @@ -16,27 +16,27 @@ execute_fmt () {
fi
}

fmt_success () {
fmt_success() {
info "Terraform fmt completed with no errors. Continuing."
}

fmt_fail () {
fmt_fail() {
local pr_comment

# Exit Code: 1, 2
# Meaning: 1 = Malformed Terraform CLI command. 2 = Terraform parse error.
# Actions: Build PR comment.
if [[ $EXIT_CODE -eq 1 || $EXIT_CODE -eq 2 ]]; then
pr_comment=$(make_details_with_header "Terraform \`fmt\` Failed" "$INPUT")
pr_comment=$(make_details_with_header "Terraform \`fmt\` Failed" "$INPUT")
fi

# Exit Code: 3
# Meaning: One or more files are incorrectly formatted.
# Actions: Iterate over all files and build diff-based PR comment.
if [[ $EXIT_CODE -eq 3 ]]; then
pr_comment=$(make_details_with_header "Terraform \`fmt\` Failed" "$INPUT" "diff")
pr_comment=$(make_details_with_header "Terraform \`fmt\` Failed" "$INPUT" "diff")
fi

# Add fmt failure comment to PR.
make_and_post_payload "fmt failure" "$pr_comment"
}
}
10 changes: 5 additions & 5 deletions handlers/init_handler.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
execute_init () {
execute_init() {
delete_existing_comments "init" '### Terraform `init` Failed'

# Exit Code: 0
Expand All @@ -16,12 +16,12 @@ execute_init () {
fi
}

init_success () {
init_success() {
info "Terraform init completed with no errors. Continuing."
}

init_fail () {
local pr_comment=$(make_details_with_header "Terraform \`init\` Failed" "$INPUT")
init_fail() {
local pr_comment=$(make_details_with_header "Terraform \`init\` Failed" "$INPUT")

make_and_post_payload "init failure" "$pr_comment"
}
}
16 changes: 8 additions & 8 deletions handlers/plan_handler.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
execute_plan () {
execute_plan() {
# shellcheck disable=SC2016
delete_existing_comments 'plan' '### Terraform `plan` .* for Workspace: `'"$WORKSPACE"'`.*'
delete_existing_comments 'outputs' '### Changes to outputs for Workspace: `'"$WORKSPACE"'`.*'
Expand All @@ -18,14 +18,14 @@ execute_plan () {
fi
}

plan_success () {
plan_success() {
post_plan_comments
if [[ $POST_PLAN_OUTPUTS == 'true' ]]; then
post_outputs_comments
fi
}

plan_fail () {
plan_fail() {
local clean_input
local delimiter_start_cmd
local delimiter_start_strings=()
Expand All @@ -39,10 +39,10 @@ plan_fail () {

clean_input=$(echo "$INPUT" | perl -pe "${delimiter_start_cmd}")

post_diff_comments "plan" "Terraform \`plan\` Failed for Workspace: \`$WORKSPACE\`" "$clean_input"
post_diff_comments "plan" "Terraform \`plan\` Failed for Workspace: \`$WORKSPACE\`" "$clean_input"
}

post_plan_comments () {
post_plan_comments() {
local clean_input
local delimiter_start_strings=()
local delimiter_start_cmd
Expand All @@ -62,7 +62,7 @@ post_plan_comments () {
clean_input=$(echo "$INPUT" | perl -pe "${delimiter_start_cmd}")
clean_input=$(echo "$clean_input" | sed -r "${delimiter_end_cmd}")

post_diff_comments "plan" "Terraform \`plan\` Succeeded for Workspace: \`$WORKSPACE\`" "$clean_input"
post_diff_comments "plan" "Terraform \`plan\` Succeeded for Workspace: \`$WORKSPACE\`" "$clean_input"
}

post_outputs_comments() {
Expand All @@ -82,5 +82,5 @@ post_outputs_comments() {
clean_input=$(echo "$INPUT" | perl -pe "${delimiter_start_cmd}")
clean_input=$(echo "$clean_input" | sed -r "${delimiter_end_cmd}")

post_diff_comments "outputs" "Changes to outputs for Workspace: \`$WORKSPACE\`" "$clean_input"
}
post_diff_comments "outputs" "Changes to outputs for Workspace: \`$WORKSPACE\` ⚠️" "$clean_input"
}
10 changes: 5 additions & 5 deletions handlers/tflint_handler.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
execute_tflint () {
execute_tflint() {
# shellcheck disable=SC2016
delete_existing_comments 'tflint' '### Linter `TFLint` .* for Workspace: `'"$WORKSPACE"'`.*'

Expand All @@ -17,14 +17,14 @@ execute_tflint () {
fi
}

tflint_success () {
info "TFLint completed with no errors. Continuing."
tflint_success() {
info "TFLint completed with no errors. Continuing."
}

tflint_fail () {
tflint_fail() {
local pr_comment

pr_comment=$(make_details_with_header "Linter \`TFLint\` Failed for Workspace: \`$WORKSPACE\`" "$INPUT")
pr_comment=$(make_details_with_header "Linter \`TFLint\` Failed for Workspace: \`$WORKSPACE\`" "$INPUT")

make_and_post_payload "tflint failure" "$pr_comment"
}
10 changes: 5 additions & 5 deletions handlers/validate_handler.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
execute_validate () {
execute_validate() {
delete_existing_comments "validate" '### Terraform `validate` Failed'

# Exit Code: 0
Expand All @@ -16,14 +16,14 @@ execute_validate () {
fi
}

validate_success () {
validate_success() {
info "Terraform validate completed with no errors. Continuing."
}

validate_fail () {
validate_fail() {
local pr_comment

pr_comment=$(make_details_with_header "Terraform \`validate\` Failed" "$INPUT" "diff")
pr_comment=$(make_details_with_header "Terraform \`validate\` Failed" "$INPUT" "diff")

make_and_post_payload "validate failure" "$pr_comment"
}
}
Loading

0 comments on commit 8948f2b

Please sign in to comment.