59 lines
2.0 KiB
Bash
59 lines
2.0 KiB
Bash
#!/usr/bin/env bash
|
|
# Shared by the run-*.sh scripts. Everything the scripts post goes through the
|
|
# reviewer token, so it appears as the bot account. The agent only ever sees the
|
|
# author token as GITEA_TOKEN, which is what pushes and opens pull requests.
|
|
|
|
ISSUE_URL="${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/issues/${ISSUE_INDEX}"
|
|
PULL_URL="${GITEA_API_URL}/repos/${GITEA_REPOSITORY}/pulls/${ISSUE_INDEX}"
|
|
COMMENT_FILE="$(mktemp)"
|
|
|
|
gitea_api() {
|
|
curl --fail-with-body --silent --show-error \
|
|
-H "Authorization: token ${1}" \
|
|
-H "Content-Type: application/json" \
|
|
"${@:2}"
|
|
}
|
|
|
|
# Commits are made by the author account so the pull request and its commits
|
|
# belong to the same person.
|
|
configure_git_author() {
|
|
AUTHOR="$(gitea_api "${GITEA_TOKEN}" "${GITEA_API_URL}/user")"
|
|
git config --global user.name "$(jq -r '.login' <<< "${AUTHOR}")"
|
|
git config --global user.email \
|
|
"$(jq -r '.email // empty' <<< "${AUTHOR}")"
|
|
}
|
|
|
|
post_comment() {
|
|
gitea_api "${REVIEWER_TOKEN}" -X POST \
|
|
--data "$(jq -n --rawfile body "${COMMENT_FILE}" '{body: $body}')" \
|
|
"${ISSUE_URL}/comments"
|
|
}
|
|
|
|
# A pull request event is a review request, so the response becomes a review
|
|
# rather than a comment: it requests changes when the agent asked @bot to fix
|
|
# something, only comments when the run failed, and approves otherwise.
|
|
post_result() {
|
|
if [ "${EVENT_NAME}" != pull_request ]; then
|
|
post_comment
|
|
return
|
|
fi
|
|
local event=APPROVED
|
|
if grep -q '@bot' "${COMMENT_FILE}"; then
|
|
event=REQUEST_CHANGES
|
|
elif grep -q '^Bot failed:' "${COMMENT_FILE}"; then
|
|
event=COMMENT
|
|
fi
|
|
gitea_api "${REVIEWER_TOKEN}" -X POST \
|
|
--data "$(jq -n --rawfile body "${COMMENT_FILE}" --arg event "${event}" \
|
|
'{body: $body, event: $event}')" \
|
|
"${PULL_URL}/reviews"
|
|
}
|
|
|
|
render_prompt() {
|
|
export ISSUE_COMMENTS="$(
|
|
gitea_api "${REVIEWER_TOKEN}" "${ISSUE_URL}/comments?limit=100" \
|
|
| jq -r '.[] | "## " + .user.login + " at " + .created_at + "\n\n" + .body + "\n"'
|
|
)"
|
|
envsubst < "${ACTION_PATH}/scripts/prompt.md"
|
|
}
|