Skip to content

Commit

Permalink
shellcheck (#198)
Browse files Browse the repository at this point in the history
* shellcheck
* fix comment pr
* better format

Signed-off-by: Michele Palazzi <sysdadmin@m1k.cloud>
  • Loading branch information
ironashram authored Dec 31, 2024
1 parent 351465a commit e2facc0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/terraform-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ jobs:

- name: Post plan as PR comment
if: github.event_name == 'pull_request'
run: make comment-pr
run: make comment-pr kub1k
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11 changes: 1 addition & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ plan: init ## Runs a plan.

.PHONY: comment-pr
comment-pr: ## Posts the terraform plan as a PR comment.
@PLAN=$$(terraform show -no-color terraform/terraform.tfplan) && \
PR_NUMBER=$$(jq --raw-output .pull_request.number "$$GITHUB_EVENT_PATH") && \
REPO_OWNER=$$(jq --raw-output .repository.owner.login "$$GITHUB_EVENT_PATH") && \
REPO_NAME=$$(jq --raw-output .repository.name "$$GITHUB_EVENT_PATH") && \
COMMENT_BODY=$$(jq -n --arg body "$$PLAN" '{body: $$body}') && \
curl -s -H "Authorization: token $$GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "$$COMMENT_BODY" \
"https://api.github.com/repos/$$REPO_OWNER/$$REPO_NAME/issues/$$PR_NUMBER/comments"
@./terraform/comment-pr.sh $(TERRAFORM_GLOBAL_OPTIONS)

.PHONY: plan-destroy
plan-destroy: init ## Shows what a destroy would do.
Expand Down
41 changes: 41 additions & 0 deletions terraform/comment-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -euo pipefail

TERRAFORM_OPTIONS="$1"

if ! PLAN=$(terraform $TERRAFORM_OPTIONS show -no-color terraform.tfplan); then
echo "Error: Failed to get terraform plan output"
exit 1
fi

if ! PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"); then
echo "Error: Failed to get PR number"
exit 1
fi

if ! REPO_OWNER=$(jq --raw-output .repository.owner.login "$GITHUB_EVENT_PATH"); then
echo "Error: Failed to get repo owner"
exit 1
fi

if ! REPO_NAME=$(jq --raw-output .repository.name "$GITHUB_EVENT_PATH"); then
echo "Error: Failed to get repo name"
exit 1
fi

# Create JSON payload with proper escaping
PAYLOAD=$(jq -n \
--arg body "$(printf '```\n%s\n```' "$PLAN")" \
'{"body": $body}')

if ! curl -s \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "$PAYLOAD" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/comments"; then
echo "Error: Failed to post comment"
exit 1
fi

echo "Comment posted successfully"
4 changes: 2 additions & 2 deletions terraform/helper.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env bash
# shellcheck disable=SC2124

[[ "$DEBUG" ]] && set -x

set -e

terraform_init() {
declare desc="run terraform init in environment"
local ENVIRONMENT="$1"; shift
local TERRAFORM_GLOBAL_OPTIONS="$@"

terraform "$TERRAFORM_GLOBAL_OPTIONS" init
}

main() {
declare desc="main function"
local ENVIRONMENT="$1"; shift
local TERRAFORM_GLOBAL_OPTIONS="$@"

Expand Down

0 comments on commit e2facc0

Please sign in to comment.