diff --git a/action.yml b/action.yml index e00e6ea..ba4f8b9 100644 --- a/action.yml +++ b/action.yml @@ -22,7 +22,7 @@ inputs: pr_comments_url: description: "PR comments URL (GitHub or Gitea)" - required: true + required: false pr_comment_id: description: "Existing PR comment ID if updating" diff --git a/tf-pr-comment.sh b/tf-pr-comment.sh index 093ff0d..29e63c9 100644 --- a/tf-pr-comment.sh +++ b/tf-pr-comment.sh @@ -12,7 +12,7 @@ PR_COMMENT_ID="${PR_COMMENT_ID:-}" PR_COMMENT_URI="${PR_COMMENT_URI:-}" GITHUB_TOKEN="${GITHUB_TOKEN:-}" -# === Validate required inputs ======================================== +# === Validate inputs ================================================= if [[ -z "$COMMENTER_TYPE" ]]; then echo "❌ commenter_type is required." exit 1 @@ -24,7 +24,7 @@ if [[ -z "$GITHUB_TOKEN" ]]; then fi # === Move into working directory if specified ======================== -if [[ -n "$WORKING_DIR" && "$WORKING_DIR" != "0" ]]; then +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}'" @@ -33,7 +33,6 @@ if [[ -n "$WORKING_DIR" && "$WORKING_DIR" != "0" ]]; then echo "🔹 Changing to working directory: $WORKING_DIR" cd "$TARGET_DIR" else - echo "⚠️ Detected empty or numeric working_dir ('${WORKING_DIR:-unset}'); treating as unset." cd "${GITHUB_WORKSPACE:-.}" fi @@ -68,12 +67,33 @@ ${COMMENTER_INPUT} ${EXIT_STATUS_MSG} " -# === Post Comment ==================================================== +# === Determine PR_COMMENTS_URL if missing =========================== if [[ -z "$PR_COMMENTS_URL" ]]; then - echo "❌ Missing PR_COMMENTS_URL environment variable." - exit 1 + 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 \