2026-06-18 22:48:16 +08:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2026-06-19 19:43:18 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-06-18 22:48:16 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-06-19 19:43:18 +08:00
|
|
|
"github.com/HoHD/PR-Helper/services"
|
2026-06-18 22:48:16 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GenerateHandler struct {
|
|
|
|
|
db *sql.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGenerateHandler(db *sql.DB) *GenerateHandler {
|
|
|
|
|
return &GenerateHandler{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-19 19:43:18 +08:00
|
|
|
// Generate handles POST /api/repos/:id/generate — SSE streaming PR description generation.
|
2026-06-18 22:48:16 +08:00
|
|
|
func (h *GenerateHandler) Generate(c *gin.Context) {
|
2026-06-19 19:43:18 +08:00
|
|
|
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"`
|
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "base and head are required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
|
// Generate PR description
|
|
|
|
|
pr, err := services.GeneratePR(h.db, localPath, req.Base, req.Head, sendEvent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
sendEvent("error", map[string]interface{}{"message": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save analysis to DB
|
|
|
|
|
resultJSON, _ := json.Marshal(pr)
|
|
|
|
|
h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'pr_description', ?, ?, ?)`,
|
|
|
|
|
id, req.Base, req.Head, string(resultJSON))
|
2026-06-18 22:48:16 +08:00
|
|
|
}
|