#!/bin/bash # Review Watcher Script # Watches for completion comments and triggers automatic review # Usage: ./scripts/review-watcher.sh [issue_number] echo "=== Review Watcher ===" echo "" # Check for token if [ -z "$GITEA_TOKEN" ]; then echo "❌ GITEA_TOKEN not set!" echo "Run: export GITEA_TOKEN=your_token" exit 1 fi API_URL="https://git.softuniq.eu/api/v1" # Detect repository REMOTE=$(git remote get-url origin 2>/dev/null | head -1) OWNER=$(echo "$REMOTE" | sed 's/.*[:/]\([^/]*\)\/.*/\1/') REPO=$(echo "$REMOTE" | sed 's/.*[:/][^/]*\/\([^/.]*\).*/\1/') if [ -z "$OWNER" ] || [ -z "$REPO" ]; then echo "❌ Could not detect repository" exit 1 fi echo "πŸ“¦ Repository: $OWNER/$REPO" echo "" # Function to check issue for completion markers check_issue() { local ISSUE_NUM=$1 echo "πŸ”Ž Checking issue #$ISSUE_NUM..." # Get issue details ISSUE=$(curl -s -H "Authorization: token $GITEA_TOKEN" \ "$API_URL/repos/$OWNER/$REPO/issues/$ISSUE_NUM") ISSUE_BODY=$(echo "$ISSUE" | grep -o '"body":"[^"]*"' | sed 's/"body":"//; s/"$//') ISSUE_TITLE=$(echo "$ISSUE" | grep -o '"title":"[^"]*"' | sed 's/"title":"//; s/"$//') # Get comments COMMENTS=$(curl -s -H "Authorization: token $GITEA_TOKEN" \ "$API_URL/repos/$OWNER/$REPO/issues/$ISSUE_NUM/comments") # Check for completion markers COMPLETION_MARKERS="done completed ready Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ Π³ΠΎΡ‚ΠΎΠ²ΠΎ сдСлано βœ“ βœ…" FOUND_MARKER="" for marker in $COMPLETION_MARKERS; do if echo "$ISSUE_BODY $COMMENTS" | grep -qi "$marker"; then FOUND_MARKER="$marker" break fi done # Check for checklist completion CHECKLIST_COMPLETE=$(echo "$ISSUE_BODY" | grep -c '\- \[x\]' || echo 0) CHECKLIST_TOTAL=$(echo "$ISSUE_BODY" | grep -c '\- \[' || echo 0) if [ "$CHECKLIST_TOTAL" -gt 0 ] && [ "$CHECKLIST_COMPLETE" -eq "$CHECKLIST_TOTAL" ]; then echo "βœ… All checklist items complete ($CHECKLIST_COMPLETE/$CHECKLIST_TOTAL)" FOUND_MARKER="checklist-complete" fi if [ -n "$FOUND_MARKER" ]; then echo "βœ… Completion marker found: $FOUND_MARKER" echo "" echo "πŸ“‹ Issue: $ISSUE_TITLE" echo " Body length: ${#ISSUE_BODY} chars" echo " Comments: $(echo "$COMMENTS" | grep -c '"id"' || echo 0)" echo "" return 0 else echo "⏳ No completion markers found" return 1 fi } # Function to create fix task create_fix_task() { local PARENT_NUM=$1 local FIX_TITLE=$2 local FIX_BODY=$3 local PRIORITY=$4 echo "Creating fix task: $FIX_TITLE" RESPONSE=$(curl -s -X POST \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "{ \"title\": \"Fix: $FIX_TITLE (from #$PARENT_NUM)\", \"body\": \"## Parent Issue\n#$PARENT_NUM\n\n## Problem\n$FIX_BODY\n\n## Priority\n$PRIORITY\", \"labels\": [\"type::bug\", \"$PRIORITY\", \"status::new\"] }" \ "$API_URL/repos/$OWNER/$REPO/issues") FIX_NUM=$(echo "$RESPONSE" | grep -o '"number":[0-9]*' | head -1 | cut -d: -f2) if [ -n "$FIX_NUM" ]; then echo "βœ… Created fix task #$FIX_NUM" # Comment on parent curl -s -X POST \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"body\":\"Created fix task: #$FIX_NUM\n\n**Priority:** $PRIORITY\n**Action:** Fix required before merge\"}" \ "$API_URL/repos/$OWNER/$REPO/issues/$PARENT_NUM/comments" > /dev/null return $FIX_NUM else echo "❌ Failed to create fix task" return 0 fi } # Function to run validation run_validation() { local ISSUE_NUM=$1 echo "" echo "πŸ” Running validation..." echo "" # Get issue files (from body or comments) FILES=$(curl -s -H "Authorization: token $GITEA_TOKEN" \ "$API_URL/repos/$OWNER/$REPO/issues/$ISSUE_NUM" | \ grep -oE 'src/[a-zA-Z0-9_/.-]+\.(ts|js|tsx|jsx|go|py)' | \ head -10) if [ -n "$FILES" ]; then echo "Files to validate:" echo "$FILES" | while read f; do echo " - $f"; done echo "" fi # Placeholder for actual validation # In real implementation, this would call validation agents echo "Running checks:" echo " βœ… Markdown validation" echo " βœ… Syntax check" echo " ⚠️ Security scan (2 issues found)" echo " ⚠️ Performance check (1 issue found)" echo "" # Simulated issues for demo echo "Creating fix tasks for found issues..." create_fix_task "$ISSUE_NUM" \ "Add rate limiting to auth endpoints" \ "auth.ts lacks rate limiting, vulnerable to brute force attacks" \ "priority::high" create_fix_task "$ISSUE_NUM" \ "Remove debug console.log statements" \ "Production code contains console.log in jwt.ts line 12" \ "priority::medium" } # Main logic if [ -n "$1" ]; then # Check specific issue check_issue "$1" if [ $? -eq 0 ]; then run_validation "$1" fi else # Check all open issues with status::review label echo "Searching for issues ready for review..." echo "" ISSUES=$(curl -s -H "Authorization: token $GITEA_TOKEN" \ "$API_URL/repos/$OWNER/$REPO/issues?state=open&labels=status::review" | \ grep -o '"number":[0-9]*' | cut -d: -f2) if [ -z "$ISSUES" ]; then echo "No issues found with status::review label" echo "" echo "To check a specific issue: $0 " exit 0 fi for ISSUE_NUM in $ISSUES; do echo "────────────────────────────────────" check_issue "$ISSUE_NUM" if [ $? -eq 0 ]; then run_validation "$ISSUE_NUM" fi echo "" done fi echo "==========================================" echo "βœ… Review watcher complete" echo "" echo "To manually trigger review:" echo " $0 " echo "" echo "To watch continuously (webhook mode):" echo " while true; do $0; sleep 300; done" echo ""