forgejo-ai-pr-review/scripts/post-comment.sh

29 lines
1,015 B
Bash

#!/usr/bin/env bash
# Required env vars: GITEA_TOKEN, GITEA_SERVER_URL, REPO, PR_NUMBER, REVIEW, CURRENT_SHA
# Optional env vars: TRUNCATED, INCREMENTAL
set -euo pipefail
FOOTER=""
if [ "${TRUNCATED:-}" = "true" ]; then
FOOTER="\n\n> **Note:** The diff was truncated to 12 KB. Some changes may not have been reviewed."
fi
LABEL="## AI Code Review"
if [ "${INCREMENTAL:-false}" = "true" ]; then
LABEL="## AI Code Review (updated)"
fi
# Embed the current SHA as a hidden marker so future runs can find it
BODY=$(jq -n \
--arg heading "$LABEL" \
--arg review "$REVIEW" \
--arg footer "$FOOTER" \
--arg sha "$CURRENT_SHA" \
'{ body: ($heading + "\n\n" + $review + $footer + "\n\n---\n*Automated review powered by AI. Always verify suggestions before applying.*\n<!-- ai-review-sha:" + $sha + " -->") }')
curl -sf \
-X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$BODY" \
"${GITEA_SERVER_URL}/api/v1/repos/${REPO}/issues/${PR_NUMBER}/comments"