Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update test #1

Merged
merged 34 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
918e76e
update test
k1rk Aug 16, 2023
0792274
added permissions
k1rk Aug 16, 2023
c5c0727
update permissions
k1rk Aug 16, 2023
890ca9a
add PR perms
k1rk Aug 16, 2023
8d1807e
test project parameter
k1rk Aug 16, 2023
c5f65e3
update project header
k1rk Aug 16, 2023
96ae229
update project param for test
k1rk Aug 16, 2023
c461c3a
test comment update
k1rk Aug 16, 2023
3e65a8e
modify regex, reduce permissions
k1rk Aug 16, 2023
e5a26a8
added ouput
k1rk Aug 16, 2023
683001d
enable multiline mode
k1rk Aug 16, 2023
407d6ea
one more test without project, and echo output
k1rk Aug 16, 2023
2599cb5
recert output change
k1rk Aug 16, 2023
749b154
update output
k1rk Aug 16, 2023
3910f7d
fix output
k1rk Aug 16, 2023
81832cc
try output differently
k1rk Aug 16, 2023
3ba8da5
drop output for now
k1rk Aug 16, 2023
61f200f
outputs test
k1rk Aug 16, 2023
6514579
remove Bold fomratting
k1rk Aug 16, 2023
379ba91
debug jq
k1rk Aug 17, 2023
a81fff3
update handlers and cleanup
k1rk Aug 17, 2023
6d98b6f
add output test again
k1rk Aug 17, 2023
bb9e969
define output value
k1rk Aug 17, 2023
3ce1523
static output
k1rk Aug 17, 2023
b803b74
one more test
k1rk Aug 17, 2023
0ce9900
added shell
k1rk Aug 17, 2023
b992cf1
look
k1rk Aug 17, 2023
9cd4caf
mount github output stream
k1rk Aug 17, 2023
1f41e88
try ls
k1rk Aug 17, 2023
2891238
debug
k1rk Aug 17, 2023
e157508
clean test
k1rk Aug 17, 2023
acbb299
added issue number
k1rk Aug 17, 2023
dc67907
use raw jq
k1rk Aug 17, 2023
5a5b56b
updated test and readme
k1rk Aug 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/release-beta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Release Beta
on:
pull_request:

permissions:
contents: write

jobs:
release-beta:
runs-on: ubuntu-latest
Expand All @@ -14,4 +17,4 @@ jobs:
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 }}
24 changes: 21 additions & 3 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 @@ -71,11 +75,25 @@ jobs:
if: ${{ env.TESTING == 'true' }}
- name: Test
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.
id: test
uses: k1rk/terraform-pr-commenter@monorepo #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: k1rk/terraform-pr-commenter@monorepo #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"
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
16 changes: 14 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ inputs:
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 @@ -42,16 +50,17 @@ runs:
shell: bash
- name: Build commenter docker image (master)
if: ${{ inputs.use_beta_version != 'true' }}
run: docker build --build-arg TERRAFORM_VERSION=${{ steps.tf_version.outputs.version }} -t commenter https://github.com/GetTerminus/terraform-pr-commenter.git#v3
run: docker build --build-arg TERRAFORM_VERSION=${{ steps.tf_version.outputs.version }} -t commenter https://github.com/k1rk/terraform-pr-commenter.git#v3
shell: bash
- name: Build commenter docker image (beta)
if: ${{ inputs.use_beta_version == 'true' }}
# append branch with a pound (#) if developing. e.g., `commenter.git#my-branch`
run: |
docker build --build-arg TERRAFORM_VERSION=${{ steps.tf_version.outputs.version }} -t commenter https://github.com/GetTerminus/terraform-pr-commenter.git#v3-beta
docker build --build-arg TERRAFORM_VERSION=${{ steps.tf_version.outputs.version }} -t commenter https://github.com/k1rk/terraform-pr-commenter.git#v3-beta
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 }}
Expand All @@ -71,10 +80,12 @@ 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) }}
Expand All @@ -90,5 +101,6 @@ runs:
-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"
}
}
2 changes: 1 addition & 1 deletion testing/text-files/tf_plan_success_with_outputs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Terraform will perform the following actions:
Changes to Outputs:
+ test_output = [
+ [
+ "my-tf-test-bucket-0",
+ "my-tf-test-bucket-1",
+ "my-tf-test-bucket-1",
+ "my-tf-test-bucket-2",
+ "my-tf-test-bucket-3",
Expand Down
8 changes: 5 additions & 3 deletions utilities/comment_utility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ make_and_post_payload() {
make_details_with_header() {
local header="### $1"
if [[ ! -z $PROJECT ]]; then
header="## **$PROJECT**
header="## Project: $PROJECT
$header"
fi
local body=$2
Expand All @@ -40,7 +40,9 @@ $body
}

post_comment() {
curl -sS -L -X POST -H "$ACCEPT_HEADER" -H "$AUTH_HEADER" -H "$CONTENT_HEADER" "$PR_COMMENTS_URL" -d "$pr_payload"
local comment=$(curl -sS -L -X POST -H "$ACCEPT_HEADER" -H "$AUTH_HEADER" -H "$CONTENT_HEADER" "$PR_COMMENTS_URL" -d "$pr_payload")
echo "comment_id=$(echo $comment | jq -r '.id')" >>/github-ouput
echo "comment_url=$(echo $comment | jq -r '.html_url')" >>/github-ouput
}

### DIFF AND STRING SUBSTITUTION UTILITIES ###
Expand Down Expand Up @@ -113,7 +115,7 @@ delete_existing_comments() {
local last_page

if [[ ! -z $PROJECT ]]; then
regex="## **$PROJECT**\n$regex"
regex="## Project: $PROJECT\\n$regex"
fi

debug "Type: $type"
Expand Down
Loading