Merge pull request 'fix: feat: review-poll.sh injects review feedback into dev tmux session (#82)' (#89) from fix/issue-82 into main

This commit is contained in:
johba 2026-03-17 22:30:35 +01:00
commit afdcdeb78f

View file

@ -36,7 +36,7 @@ log "--- Poll start ---"
PRS=$(curl -sf -H "Authorization: token ${CODEBERG_TOKEN}" \
"${API_BASE}/pulls?state=open&limit=20" | \
jq -r --arg branch "${PRIMARY_BRANCH}" '.[] | select(.base.ref == $branch) | select(.draft != true) | select(.title | test("^\\[?WIP[\\]:]"; "i") | not) | "\(.number) \(.head.sha)"')
jq -r --arg branch "${PRIMARY_BRANCH}" '.[] | select(.base.ref == $branch) | select(.draft != true) | select(.title | test("^\\[?WIP[\\]:]"; "i") | not) | "\(.number) \(.head.sha) \(.head.ref)"')
if [ -z "$PRS" ]; then
log "No open PRs targeting ${PRIMARY_BRANCH}"
@ -49,9 +49,74 @@ log "Found ${TOTAL} open PRs"
REVIEWED=0
SKIPPED=0
inject_review_into_dev_session() {
local pr_num="$1" pr_sha="$2" pr_branch="$3"
local issue_num
issue_num=$(printf '%s' "$pr_branch" | grep -oP 'issue-\K[0-9]+' || true)
[ -z "$issue_num" ] && return 0
local session="dev-${PROJECT_NAME}-${issue_num}"
local phase_file="/tmp/dev-session-${PROJECT_NAME}-${issue_num}.phase"
tmux has-session -t "${session}" 2>/dev/null || return 0
local current_phase
current_phase=$(head -1 "${phase_file}" 2>/dev/null | tr -d '[:space:]' || true)
[ "${current_phase}" = "PHASE:awaiting_review" ] || return 0
local review_comment
review_comment=$(curl -sf -H "Authorization: token ${CODEBERG_TOKEN}" \
"${API_BASE}/issues/${pr_num}/comments?limit=50" | \
jq -r --arg sha "${pr_sha}" \
'[.[] | select(.body | contains("<!-- reviewed: " + $sha))] | last // empty') || true
if [ -z "${review_comment}" ] || [ "${review_comment}" = "null" ]; then
return 0
fi
local review_text verdict
review_text=$(printf '%s' "${review_comment}" | jq -r '.body')
verdict=$(printf '%s' "${review_text}" | grep -oP '\*\*(APPROVE|REQUEST_CHANGES|DISCUSS)\*\*' | head -1 | tr -d '*' || true)
local inject_msg=""
if [ "${verdict}" = "APPROVE" ]; then
inject_msg="Approved! PR #${pr_num} has been approved by the reviewer.
Write PHASE:done to the phase file — the orchestrator will handle the merge:
echo \"PHASE:done\" > \"${phase_file}\""
elif [ "${verdict}" = "REQUEST_CHANGES" ] || [ "${verdict}" = "DISCUSS" ]; then
inject_msg="Review: ${verdict} on PR #${pr_num}:
${review_text}
Instructions:
1. Address each piece of feedback carefully.
2. Run lint and tests when done.
3. Commit your changes and push: git push origin ${pr_branch}
4. Write: echo \"PHASE:awaiting_ci\" > \"${phase_file}\"
5. Stop and wait for the next CI result."
fi
[ -z "${inject_msg}" ] && return 0
local inject_tmp
inject_tmp=$(mktemp /tmp/review-inject-XXXXXX)
printf '%s' "${inject_msg}" > "${inject_tmp}"
# All tmux calls guarded with || true: the dev session is external and may die
# between the has-session check above and here; a non-zero exit must not abort
# the outer poll loop under set -euo pipefail.
tmux load-buffer -b "review-inject-${pr_num}" "${inject_tmp}" || true
tmux paste-buffer -t "${session}" -b "review-inject-${pr_num}" || true
sleep 0.5
tmux send-keys -t "${session}" "" Enter || true
tmux delete-buffer -b "review-inject-${pr_num}" 2>/dev/null || true
rm -f "${inject_tmp}"
log " #${pr_num} review (${verdict}) injected into session ${session}"
}
while IFS= read -r line; do
PR_NUM=$(echo "$line" | awk '{print $1}')
PR_SHA=$(echo "$line" | awk '{print $2}')
PR_BRANCH=$(echo "$line" | awk '{print $3}')
CI_STATE=$(curl -sf -H "Authorization: token ${CODEBERG_TOKEN}" \
"${API_BASE}/commits/${PR_SHA}/status" | jq -r '.state // "unknown"')
@ -84,6 +149,12 @@ while IFS= read -r line; do
if "${SCRIPT_DIR}/review-pr.sh" "$PR_NUM" 2>&1; then
REVIEWED=$((REVIEWED + 1))
# Re-fetch current SHA: review-pr.sh fetches the PR independently and tags its
# comment with whatever SHA it saw. If a commit arrived while review-pr.sh was
# running those two SHA captures diverge and we would miss the comment.
FRESH_SHA=$(curl -sf -H "Authorization: token ${CODEBERG_TOKEN}" \
"${API_BASE}/pulls/${PR_NUM}" | jq -r '.head.sha // ""') || true
inject_review_into_dev_session "$PR_NUM" "${FRESH_SHA:-$PR_SHA}" "$PR_BRANCH"
else
log " #${PR_NUM} review failed"
matrix_send "review" "❌ PR #${PR_NUM} review failed" 2>/dev/null || true