feat: full file context + patterns-repo support
Major improvements to review quality: 1. Full file context: fetch complete content of all modified files from the PR branch and include as reference. This eliminates false-positive "missing import" findings since the model sees the entire file. 2. Patterns repo: new --patterns-repo / PATTERNS_REPO flag fetches language idiom files from a separate Gitea repo (e.g. rodin/elixir-patterns) and includes them as review criteria. 3. Multi-file patterns: --patterns-files / PATTERNS_FILES accepts comma-separated file paths to fetch from the patterns repo. New API methods: - GetPullRequestFiles: list changed files in a PR - GetFileContentRef: fetch file content from a specific branch/ref Prompt changes: - BuildSystemPrompt now accepts (conventions, patterns) - BuildUserPrompt now accepts fileContext parameter - File context displayed before diff for model reference - Patterns presented as "review criteria" in system prompt Composite action updated with patterns-repo and patterns-files inputs.
This commit is contained in:
+72
-9
@@ -1,6 +1,5 @@
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
@@ -27,6 +26,8 @@ func main() {
|
||||
llmAPIKey := flag.String("llm-api-key", envOrDefault("LLM_API_KEY", ""), "LLM API key")
|
||||
llmModel := flag.String("llm-model", envOrDefault("LLM_MODEL", ""), "LLM model name")
|
||||
conventionsFile := flag.String("conventions-file", envOrDefault("CONVENTIONS_FILE", ""), "Conventions file path in repo (e.g. CLAUDE.md)")
|
||||
patternsRepo := flag.String("patterns-repo", envOrDefault("PATTERNS_REPO", ""), "Repo with language patterns (e.g. rodin/elixir-patterns)")
|
||||
patternsFiles := flag.String("patterns-files", envOrDefault("PATTERNS_FILES", "README.md"), "Comma-separated file paths to fetch from patterns repo")
|
||||
dryRun := flag.Bool("dry-run", false, "Print review to stdout instead of posting")
|
||||
llmTemp := flag.Float64("llm-temperature", envOrDefaultFloat("LLM_TEMPERATURE", 0), "LLM temperature (0 = server default)")
|
||||
|
||||
@@ -79,7 +80,17 @@ func main() {
|
||||
}
|
||||
log.Printf("Diff size: %d bytes", len(diff))
|
||||
|
||||
// Step 3: Check CI status
|
||||
// Step 3: Fetch full file content for modified files
|
||||
fileContext := ""
|
||||
files, err := giteaClient.GetPullRequestFiles(owner, repoName, prNumber)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not fetch PR files list: %v", err)
|
||||
} else {
|
||||
fileContext = fetchFileContext(giteaClient, owner, repoName, pr.Head.Ref, files)
|
||||
log.Printf("Fetched full context for %d files", len(files))
|
||||
}
|
||||
|
||||
// Step 4: Check CI status
|
||||
ciPassed := true
|
||||
ciDetails := ""
|
||||
if pr.Head.Sha != "" {
|
||||
@@ -92,7 +103,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Step 4: Load conventions file if specified
|
||||
// Step 5: Load conventions file if specified
|
||||
conventions := ""
|
||||
if *conventionsFile != "" {
|
||||
content, err := giteaClient.GetFileContent(owner, repoName, *conventionsFile)
|
||||
@@ -104,11 +115,18 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Step 5: Build prompts
|
||||
systemPrompt := review.BuildSystemPrompt(conventions)
|
||||
userPrompt := review.BuildUserPrompt(pr.Title, pr.Body, diff, ciPassed, ciDetails)
|
||||
// Step 6: Load patterns from external repo if specified
|
||||
patterns := ""
|
||||
if *patternsRepo != "" {
|
||||
patterns = fetchPatterns(giteaClient, *patternsRepo, *patternsFiles)
|
||||
log.Printf("Loaded patterns from %s (%d bytes)", *patternsRepo, len(patterns))
|
||||
}
|
||||
|
||||
// Step 6: Call LLM
|
||||
// Step 7: Build prompts
|
||||
systemPrompt := review.BuildSystemPrompt(conventions, patterns)
|
||||
userPrompt := review.BuildUserPrompt(pr.Title, pr.Body, diff, fileContext, ciPassed, ciDetails)
|
||||
|
||||
// Step 8: Call LLM
|
||||
log.Printf("Sending to LLM (%s)...", *llmModel)
|
||||
messages := []llm.Message{
|
||||
{Role: "system", Content: systemPrompt},
|
||||
@@ -121,14 +139,14 @@ func main() {
|
||||
}
|
||||
log.Printf("LLM response received (%d bytes)", len(response))
|
||||
|
||||
// Step 7: Parse response
|
||||
// Step 9: Parse response
|
||||
result, err := review.ParseResponse(response)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse LLM response: %v", err)
|
||||
}
|
||||
log.Printf("Verdict: %s (%d findings)", result.Verdict, len(result.Findings))
|
||||
|
||||
// Step 8: Format and post review
|
||||
// Step 10: Format and post review
|
||||
reviewBody := review.FormatMarkdown(result, *reviewerName)
|
||||
event := review.GiteaEvent(result.Verdict)
|
||||
|
||||
@@ -146,6 +164,51 @@ func main() {
|
||||
log.Printf("Review posted successfully!")
|
||||
}
|
||||
|
||||
// fetchFileContext fetches the full content of modified files from the PR branch.
|
||||
func fetchFileContext(client *gitea.Client, owner, repo, ref string, files []gitea.ChangedFile) string {
|
||||
var sb strings.Builder
|
||||
for _, f := range files {
|
||||
if f.Status == "removed" {
|
||||
continue // Skip deleted files
|
||||
}
|
||||
content, err := client.GetFileContentRef(owner, repo, f.Filename, ref)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not fetch %s: %v", f.Filename, err)
|
||||
continue
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("--- %s ---\n", f.Filename))
|
||||
sb.WriteString("```\n")
|
||||
sb.WriteString(content)
|
||||
sb.WriteString("\n```\n\n")
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// fetchPatterns fetches pattern files from an external repo.
|
||||
func fetchPatterns(client *gitea.Client, patternsRepo, patternsFiles string) string {
|
||||
parts := strings.SplitN(patternsRepo, "/", 2)
|
||||
if len(parts) != 2 {
|
||||
log.Printf("Warning: invalid patterns-repo format %q, expected owner/name", patternsRepo)
|
||||
return ""
|
||||
}
|
||||
owner, repo := parts[0], parts[1]
|
||||
|
||||
var sb strings.Builder
|
||||
for _, filepath := range strings.Split(patternsFiles, ",") {
|
||||
filepath = strings.TrimSpace(filepath)
|
||||
if filepath == "" {
|
||||
continue
|
||||
}
|
||||
content, err := client.GetFileContent(owner, repo, filepath)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not fetch pattern file %s from %s: %v", filepath, patternsRepo, err)
|
||||
continue
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("### %s/%s\n\n%s\n\n", patternsRepo, filepath, content))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// evaluateCIStatus checks if all CI statuses indicate success.
|
||||
func evaluateCIStatus(statuses []gitea.CommitStatus) (passed bool, details string) {
|
||||
if len(statuses) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user