Files
bot-agents/scripts/run-claude.sh
T

50 lines
1.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
git config --global user.name bot
git config --global user.email noreply@capsulizers.com
COMMENT_FILE="$(mktemp)"
post_comment() {
COMMENT_JSON="$(jq -n --rawfile body "${COMMENT_FILE}" '{body: $body}')"
curl --fail-with-body --silent --show-error \
-X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
--data "${COMMENT_JSON}" \
"${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues/${ISSUE_INDEX}/comments"
}
if [ -z "${BOT_TOKEN:-}" ]; then
echo 'Run `claude setup-token` locally and set the `bot-token` action input.' > "${COMMENT_FILE}"
post_comment
exit 1
fi
export CLAUDE_CODE_OAUTH_TOKEN="${BOT_TOKEN}"
export ISSUE_COMMENTS="$(
curl --fail-with-body --silent --show-error \
-H "Authorization: token ${GITEA_TOKEN}" \
"${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues/${ISSUE_INDEX}/comments?limit=100" \
| jq -r '.[] | "## " + .user.login + " at " + .created_at + "\n\n" + .body + "\n"'
)"
FINAL_PROMPT="$(envsubst < "${ACTION_PATH}/scripts/prompt.md")"
# Claude refuses --dangerously-skip-permissions as root outside a sandbox.
export IS_SANDBOX=1
# Stream events so the runner sees output and does not kill the job as a zombie.
STREAM_FILE="$(mktemp)"
claude --print --dangerously-skip-permissions \
--output-format stream-json --verbose "${FINAL_PROMPT}" \
| tee "${STREAM_FILE}" \
| jq -r --unbuffered '[.type, (.message.content[]?.name // empty)] | join(" ")'
jq -r 'select(.type == "result") | .result // ("Bot failed: " + .subtype)' "${STREAM_FILE}" \
| ansifilter > "${COMMENT_FILE}"
post_comment