117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/HoHD/PR-Helper/services"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ReviewHandler struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewReviewHandler(db *sql.DB) *ReviewHandler {
|
|
return &ReviewHandler{db: db}
|
|
}
|
|
|
|
// Review handles POST /api/repos/:id/review — SSE streaming AI code review.
|
|
func (h *ReviewHandler) Review(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
// Get repo info
|
|
var localPath string
|
|
err := h.db.QueryRow(`SELECT local_path FROM repositories WHERE id = ?`, id).Scan(&localPath)
|
|
if err == sql.ErrNoRows {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "repository not found"})
|
|
return
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Parse request
|
|
var req struct {
|
|
Base string `json:"base" binding:"required"`
|
|
Head string `json:"head" binding:"required"`
|
|
TopN *int `json:"top_n"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "base and head are required"})
|
|
return
|
|
}
|
|
|
|
// Determine Top-N: request value > settings default (20)
|
|
topN := 20
|
|
if req.TopN != nil {
|
|
topN = *req.TopN
|
|
} else {
|
|
var topNStr string
|
|
h.db.QueryRow(`SELECT value FROM settings WHERE key = 'review.top_n'`).Scan(&topNStr)
|
|
if topNStr != "" {
|
|
if n, err := strconv.Atoi(topNStr); err == nil && n > 0 {
|
|
topN = n
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set SSE headers
|
|
c.Header("Content-Type", "text/event-stream")
|
|
c.Header("Cache-Control", "no-cache")
|
|
c.Header("Connection", "keep-alive")
|
|
c.Header("X-Accel-Buffering", "no")
|
|
c.Status(http.StatusOK)
|
|
|
|
flusher, ok := c.Writer.(http.Flusher)
|
|
if !ok {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "streaming not supported"})
|
|
return
|
|
}
|
|
|
|
sendEvent := func(event string, data interface{}) {
|
|
jsonData, _ := json.Marshal(data)
|
|
fmt.Fprintf(c.Writer, "event: %s\ndata: %s\n\n", event, jsonData)
|
|
flusher.Flush()
|
|
}
|
|
|
|
// Update last_used
|
|
h.db.Exec(`UPDATE repositories SET last_used = datetime('now') WHERE id = ?`, id)
|
|
|
|
// Run AI review
|
|
err = services.GenerateReview(h.db, localPath, req.Base, req.Head, topN, sendEvent)
|
|
if err != nil {
|
|
sendEvent("error", map[string]interface{}{"message": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Save analysis to DB
|
|
resultData := map[string]interface{}{
|
|
"base": req.Base,
|
|
"head": req.Head,
|
|
"top_n": topN,
|
|
}
|
|
resultJSON, _ := json.Marshal(resultData)
|
|
h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'code_review', ?, ?, ?)`,
|
|
id, req.Base, req.Head, string(resultJSON))
|
|
}
|
|
|
|
// SaveNotes handles POST /api/repos/:id/review/notes — stub for Phase 5
|
|
func (h *ReviewHandler) SaveNotes(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "notes not implemented yet — coming in Phase 5"})
|
|
}
|
|
|
|
// GetNotes handles GET /api/repos/:id/review/notes — stub for Phase 5
|
|
func (h *ReviewHandler) GetNotes(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "notes not implemented yet — coming in Phase 5"})
|
|
}
|
|
|
|
// GeneratePDF handles POST /api/repos/:id/review/pdf — stub for Phase 5
|
|
func (h *ReviewHandler) GeneratePDF(c *gin.Context) {
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "pdf not implemented yet — coming in Phase 5"})
|
|
}
|