#!/usr/bin/env bash set -euo pipefail # === Inputs via ENV =================================================== COMMENTER_TYPE="${COMMENTER_TYPE:-}" COMMENTER_INPUT="${COMMENTER_INPUT:-}" COMMENTER_EXITCODE="${COMMENTER_EXITCODE:-0}" WORKING_DIR="${WORKING_DIR:-}" PR_COMMENTS_URL="${PR_COMMENTS_URL:-}" PR_COMMENT_ID="${PR_COMMENT_ID:-}" PR_COMMENT_URI="${PR_COMMENT_URI:-}" GITHUB_TOKEN="${GITHUB_TOKEN:-}" # === Validate inputs ================================================= if [[ -z "$COMMENTER_TYPE" ]]; then echo "❌ commenter_type is required." exit 1 fi if [[ -z "$GITHUB_TOKEN" ]]; then echo "❌ GITHUB_TOKEN is required to post PR comments." exit 1 fi # === Move into working directory if specified ======================== if [[ -n "$WORKING_DIR" ]]; then TARGET_DIR="${GITHUB_WORKSPACE}/${WORKING_DIR}" if [[ ! -d "$TARGET_DIR" ]]; then echo "❌ Error: Cannot change to working directory '${WORKING_DIR}'" exit 1 fi echo "🔹 Changing to working directory: $WORKING_DIR" cd "$TARGET_DIR" else cd "${GITHUB_WORKSPACE:-.}" fi echo "📁 Current working directory: $(pwd)" echo "đŸ“Ļ Repository root: ${GITHUB_WORKSPACE:-N/A}" # === Detect IaC command ============================================== if command -v tofu &>/dev/null; then TOFU_CMD="tofu" elif command -v terraform &>/dev/null; then TOFU_CMD="terraform" else echo "❌ Neither 'tofu' nor 'terraform' found in PATH." echo "PATH: $PATH" exit 1 fi echo "🧰 Using IaC command: $TOFU_CMD ($(command -v $TOFU_CMD))" # === Prepare comment body =========================================== EXIT_STATUS_MSG="" if [[ "$COMMENTER_EXITCODE" =~ ^[0-9]+$ && "$COMMENTER_EXITCODE" -eq 0 ]]; then EXIT_STATUS_MSG="✅ ${COMMENTER_TYPE^} succeeded." else EXIT_STATUS_MSG="❌ ${COMMENTER_TYPE^} failed with exit code ${COMMENTER_EXITCODE}." fi COMMENT_BODY="### ${COMMENTER_TYPE^} Results \`\`\` ${COMMENTER_INPUT} \`\`\` ${EXIT_STATUS_MSG} " # === Generate PR_COMMENTS_URL if not provided ======================== if [[ -z "${PR_COMMENTS_URL:-}" ]]; then if [[ -n "${GITEA_API_URL:-}" && -n "${GITEA_REPOSITORY:-}" && -n "${GITEA_PULL_REQUEST_ID:-}" ]]; then PR_COMMENTS_URL="${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues/${GITEA_PULL_REQUEST_ID}/comments" echo "â„šī¸ PR_COMMENTS_URL not provided, generated from Gitea context: $PR_COMMENTS_URL" else echo "❌ Missing PR_COMMENTS_URL and insufficient Gitea context (GITEA_API_URL, GITEA_REPOSITORY, GITEA_PULL_REQUEST_ID)." exit 1 fi fi # === Post Comment ==================================================== if [[ -n "$PR_COMMENT_ID" && -n "$PR_COMMENT_URI" ]]; then echo "🌀 Updating existing comment (ID: ${PR_COMMENT_ID})" curl -sS -X PATCH \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ -d "$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')" \ "${PR_COMMENT_URI}" \ || echo "âš ī¸ Failed to update existing comment." else echo "🆕 Creating a new PR comment" curl -sS -X POST \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ -d "$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')" \ "${PR_COMMENTS_URL}" \ || echo "âš ī¸ Failed to create PR comment." fi echo "✅ Comment posted successfully."