#!/usr/bin/env bash # Usage: tools/agent-review.sh set -euo pipefail usage() { echo 'Usage: tools/agent-review.sh ' >&2; exit 2; } [[ $# == 4 ]] || usage pr=$1 sha=$2 verdict=$3 file=$4 [[ $pr =~ ^[1-9][0-9]*$ && $sha =~ ^[0-9a-f]{40}$ ]] || usage case $verdict in pass) state=success ;; fail) state=failure ;; *) usage ;; esac [[ -s $file && -r $file ]] || { echo "Review file '$file' is missing, empty or unreadable" >&2; exit 2; } command -v jq >/dev/null 2>&1 || { echo 'agent-review requires jq' >&2; exit 1; } gitea=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/gitea.sh repo=/repos/FinalFactory/FinalToken info=$(bash "$gitea" GET "$repo/pulls/$pr") || { echo "PR $pr not found or could not be read" >&2; exit 1; } prstate=$(jq -er '.state' <<< "$info") || { echo "PR $pr response has no state" >&2; exit 1; } head=$(jq -er '.head.sha' <<< "$info") || { echo "PR $pr response has no head" >&2; exit 1; } [[ $prstate == open ]] || { echo "PR $pr is $prstate" >&2; exit 1; } [[ $head == "$sha" ]] || { echo "PR $pr head is $head, not the reviewed $sha: review the new head" >&2; exit 1; } url=$(jq -n --arg b "**Agent review: $verdict** on $sha" --rawfile r "$file" '{body: ($b + "\n\n" + $r)}' \ | bash "$gitea" POST "$repo/issues/$pr/comments" - | jq -er '.html_url // empty') || { echo 'Posting the review comment failed' >&2; exit 1; } id=$(jq -n --arg s "$state" --arg d "agent review: $verdict" --arg u "$url" \ '{state: $s, context: "agent-review", description: $d, target_url: $u}' \ | bash "$gitea" POST "$repo/statuses/$sha" - | jq -er '.id // empty') || { echo "The comment is posted ($url) but setting the status failed" >&2; exit 1; } echo "agent-review $verdict on $sha: $url (status $id)"