feat: Phase 5 — 审查备注编辑器与 PDF 导出

This commit is contained in:
2026-06-19 22:10:13 +08:00
parent 1cfab2d2d1
commit b35d6f874b
10 changed files with 914 additions and 15 deletions
+130 -7
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"strconv"
"github.com/HoHD/PR-Helper/models"
"github.com/HoHD/PR-Helper/services"
"github.com/gin-gonic/gin"
)
@@ -96,21 +97,143 @@ func (h *ReviewHandler) Review(c *gin.Context) {
"top_n": topN,
}
resultJSON, _ := json.Marshal(resultData)
h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'code_review', ?, ?, ?)`,
result, err := h.db.Exec(`INSERT INTO analyses (repo_id, type, base_ref, head_ref, result) VALUES (?, 'code_review', ?, ?, ?)`,
id, req.Base, req.Head, string(resultJSON))
if err == nil {
analysisID, _ := result.LastInsertId()
sendEvent("analysis_saved", map[string]interface{}{
"analysis_id": analysisID,
})
}
}
// SaveNotes handles POST /api/repos/:id/review/notes — stub for Phase 5
// SaveNotes handles POST /api/repos/:id/review/notes — upsert a review note.
func (h *ReviewHandler) SaveNotes(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{"error": "notes not implemented yet — coming in Phase 5"})
var req struct {
AnalysisID int64 `json:"analysis_id" binding:"required"`
Scope string `json:"scope" binding:"required"`
ScopeKey string `json:"scope_key"`
Content string `json:"content"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id and scope are required"})
return
}
// Validate scope
switch req.Scope {
case "overall", "file", "suggestion":
// valid
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "scope must be overall, file, or suggestion"})
return
}
note, err := services.SaveNote(h.db, req.AnalysisID, req.Scope, req.ScopeKey, req.Content)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, note)
}
// GetNotes handles GET /api/repos/:id/review/notes — stub for Phase 5
// GetNotes handles GET /api/repos/:id/review/notes — list review notes for an analysis.
func (h *ReviewHandler) GetNotes(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{"error": "notes not implemented yet — coming in Phase 5"})
analysisIDStr := c.Query("analysis_id")
if analysisIDStr == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id query parameter is required"})
return
}
analysisID, err := strconv.ParseInt(analysisIDStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid analysis_id"})
return
}
scope := c.Query("scope") // optional filter
notes, err := services.GetNotes(h.db, analysisID, scope)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if notes == nil {
notes = []models.ReviewNote{}
}
c.JSON(http.StatusOK, notes)
}
// GeneratePDF handles POST /api/repos/:id/review/pdf — stub for Phase 5
// GeneratePDF handles POST /api/repos/:id/review/pdf — generate and download a PDF report.
func (h *ReviewHandler) GeneratePDF(c *gin.Context) {
c.JSON(http.StatusNotImplemented, gin.H{"error": "pdf not implemented yet — coming in Phase 5"})
id := c.Param("id")
// Get repo info
var repoURL string
err := h.db.QueryRow(`SELECT url FROM repositories WHERE id = ?`, id).Scan(&repoURL)
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 {
AnalysisID int64 `json:"analysis_id" binding:"required"`
Base string `json:"base"`
Head string `json:"head"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "analysis_id is required"})
return
}
// Load the analysis result
var analysisResult, baseRef, headRef string
var createdAt string
err = h.db.QueryRow(`SELECT result, base_ref, head_ref, created_at FROM analyses WHERE id = ?`, req.AnalysisID).Scan(&analysisResult, &baseRef, &headRef, &createdAt)
if err == sql.ErrNoRows {
c.JSON(http.StatusNotFound, gin.H{"error": "analysis not found"})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Load all notes for this analysis
notes, err := services.GetNotes(h.db, req.AnalysisID, "")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Build report data
reportData := services.ReportData{
RepoURL: repoURL,
BaseRef: baseRef,
HeadRef: headRef,
ReviewedAt: createdAt,
AnalysisID: req.AnalysisID,
Result: analysisResult,
Notes: notes,
}
// Generate PDF
pdfBytes, err := services.GeneratePDFReport(reportData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "PDF generation failed: " + err.Error()})
return
}
// Return PDF as download
filename := fmt.Sprintf("pr-helper-review-%s.pdf", createdAt[:10])
c.Header("Content-Type", "application/pdf")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
c.Data(http.StatusOK, "application/pdf", pdfBytes)
}