forgejo-ai-pr-review/scripts/fetch-context.sh
2026-03-14 21:43:56 +02:00

34 lines
1.2 KiB
Bash

#!/usr/bin/env bash
# Fetches the previous AI review comment from the PR (if any).
# Required env vars: GITEA_TOKEN, GITEA_SERVER_URL, REPO, PR_NUMBER
# Writes to $GITHUB_OUTPUT: prev_sha, prev_review
set -euo pipefail
COMMENTS=$(curl -sf \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
"${GITEA_SERVER_URL}/api/v1/repos/${REPO}/issues/${PR_NUMBER}/comments")
# Find the most recent comment that contains our SHA marker
PREV_COMMENT=$(echo "$COMMENTS" | jq -r '
[ .[] | select(.body | contains("<!-- ai-review-sha:")) ] | last | .body // ""
')
if [ -z "$PREV_COMMENT" ]; then
echo "No previous AI review found — will run a full review."
echo "prev_sha=" >> "$GITHUB_OUTPUT"
echo "prev_review=" >> "$GITHUB_OUTPUT"
exit 0
fi
PREV_SHA=$(echo "$PREV_COMMENT" | grep -oP '(?<=<!-- ai-review-sha:)[^>]+(?= -->)' || true)
# Strip the trailing marker line to get just the review body
PREV_REVIEW=$(echo "$PREV_COMMENT" | sed '/<!-- ai-review-sha:/d')
echo "Found previous review at commit ${PREV_SHA}"
echo "prev_sha=${PREV_SHA}" >> "$GITHUB_OUTPUT"
echo "prev_review<<EOF" >> "$GITHUB_OUTPUT"
echo "$PREV_REVIEW" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"