116 lines
3.8 KiB
Bash
116 lines
3.8 KiB
Bash
#!/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}
|
|
"
|
|
|
|
# === Determine PR_COMMENTS_URL if missing ===========================
|
|
if [[ -z "$PR_COMMENTS_URL" ]]; then
|
|
echo "⚠️ PR_COMMENTS_URL not provided, attempting to generate..."
|
|
|
|
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"
|
|
PR_COMMENT_URI="${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues/comments"
|
|
echo "✅ Generated Gitea PR_COMMENTS_URL: $PR_COMMENTS_URL"
|
|
|
|
elif [[ -n "$GITHUB_EVENT_PATH" && -f "$GITHUB_EVENT_PATH" ]]; then
|
|
PR_NUMBER=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH")
|
|
if [[ "$PR_NUMBER" != "null" ]]; then
|
|
PR_COMMENTS_URL=$(jq -r ".pull_request.comments_url" "$GITHUB_EVENT_PATH")
|
|
PR_COMMENT_URI=$(jq -r ".repository.issue_comment_url" "$GITHUB_EVENT_PATH" | sed "s|{/number}||g")
|
|
echo "✅ Generated GitHub PR_COMMENTS_URL: $PR_COMMENTS_URL"
|
|
else
|
|
echo "❌ Not a pull request. Cannot generate PR_COMMENTS_URL."
|
|
exit 0
|
|
fi
|
|
|
|
else
|
|
echo "❌ Cannot determine PR_COMMENTS_URL automatically. Please provide it as input."
|
|
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."
|