Compare commits
17 Commits
cdd4f4fdf4
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 23443ef378 | |||
| bc5a4a1dcd | |||
| d30f3d4278 | |||
| 2507ee22e7 | |||
| c39845ca03 | |||
| cd601bdcf4 | |||
| 50091941e1 | |||
| ed06cdd942 | |||
| ed69d26e87 | |||
| da586a512a | |||
| f6baa41b2c | |||
| ecbae332f4 | |||
| fdd75699d9 | |||
| dc450f7771 | |||
| 3a3c60a3c6 | |||
| 504f616e99 | |||
| bb596db3c1 |
@@ -1 +1,2 @@
|
|||||||
/review-bot
|
/review-bot
|
||||||
|
coverage.out
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
.PHONY: build test test-integration lint clean coverage
|
||||||
|
|
||||||
|
build:
|
||||||
|
go build -o review-bot ./cmd/review-bot/
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test ./...
|
||||||
|
|
||||||
|
test-integration:
|
||||||
|
go test -tags integration -v ./cmd/review-bot/
|
||||||
|
|
||||||
|
lint:
|
||||||
|
go vet ./...
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f review-bot
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
go test -coverprofile=coverage.out ./...
|
||||||
|
go tool cover -func=coverage.out
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
//go:build integration
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.weiker.me/rodin/review-bot/gitea"
|
||||||
|
"gitea.weiker.me/rodin/review-bot/llm"
|
||||||
|
"gitea.weiker.me/rodin/review-bot/review"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Integration test requires a running Gitea instance and LLM endpoint.
|
||||||
|
// Set environment variables:
|
||||||
|
//
|
||||||
|
// INTEGRATION_GITEA_URL - Gitea base URL
|
||||||
|
// INTEGRATION_GITEA_TOKEN - Gitea API token with repo access
|
||||||
|
// INTEGRATION_GITEA_REPO - owner/repo with an open PR
|
||||||
|
// INTEGRATION_PR_NUMBER - PR number to test against
|
||||||
|
// INTEGRATION_LLM_BASE_URL - LLM API base URL
|
||||||
|
// INTEGRATION_LLM_API_KEY - LLM API key
|
||||||
|
// INTEGRATION_LLM_MODEL - Model name
|
||||||
|
func TestIntegration_FullReviewFlow(t *testing.T) {
|
||||||
|
giteaURL := os.Getenv("INTEGRATION_GITEA_URL")
|
||||||
|
giteaToken := os.Getenv("INTEGRATION_GITEA_TOKEN")
|
||||||
|
giteaRepo := os.Getenv("INTEGRATION_GITEA_REPO")
|
||||||
|
prNumStr := os.Getenv("INTEGRATION_PR_NUMBER")
|
||||||
|
llmBaseURL := os.Getenv("INTEGRATION_LLM_BASE_URL")
|
||||||
|
llmAPIKey := os.Getenv("INTEGRATION_LLM_API_KEY")
|
||||||
|
llmModel := os.Getenv("INTEGRATION_LLM_MODEL")
|
||||||
|
|
||||||
|
if giteaURL == "" || giteaToken == "" || giteaRepo == "" || prNumStr == "" ||
|
||||||
|
llmBaseURL == "" || llmAPIKey == "" || llmModel == "" {
|
||||||
|
t.Skip("Integration test env vars not set, skipping")
|
||||||
|
}
|
||||||
|
|
||||||
|
prNumber, err := strconv.Atoi(prNumStr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Invalid PR number %q: %v", prNumStr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse owner/repo
|
||||||
|
parts := strings.SplitN(giteaRepo, "/", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
t.Fatalf("Invalid repo format %q", giteaRepo)
|
||||||
|
}
|
||||||
|
owner, repoName := parts[0], parts[1]
|
||||||
|
if owner == "" || repoName == "" {
|
||||||
|
t.Fatalf("Invalid repo format %q", giteaRepo)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Step 1: Fetch PR
|
||||||
|
giteaClient := gitea.NewClient(giteaURL, giteaToken)
|
||||||
|
pr, err := giteaClient.GetPullRequest(ctx, owner, repoName, prNumber)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetPullRequest: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("PR: %s (sha: %s)", pr.Title, pr.Head.Sha)
|
||||||
|
|
||||||
|
// Step 2: Fetch diff
|
||||||
|
diff, err := giteaClient.GetPullRequestDiff(ctx, owner, repoName, prNumber)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetPullRequestDiff: %v", err)
|
||||||
|
}
|
||||||
|
if diff == "" {
|
||||||
|
t.Fatal("diff is empty")
|
||||||
|
}
|
||||||
|
t.Logf("Diff size: %d bytes", len(diff))
|
||||||
|
|
||||||
|
// Step 3: Build prompts
|
||||||
|
systemPrompt := review.BuildSystemPrompt("", "")
|
||||||
|
userPrompt := review.BuildUserPrompt(pr.Title, pr.Body, diff, "", true, "")
|
||||||
|
|
||||||
|
// Step 4: Call LLM
|
||||||
|
llmClient := llm.NewClient(llmBaseURL, llmAPIKey, llmModel)
|
||||||
|
response, err := llmClient.Complete(ctx, []llm.Message{
|
||||||
|
{Role: "system", Content: systemPrompt},
|
||||||
|
{Role: "user", Content: userPrompt},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("LLM Complete: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("LLM response: %d bytes", len(response))
|
||||||
|
|
||||||
|
// Step 5: Parse response
|
||||||
|
result, err := review.ParseResponse(response)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseResponse: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("Verdict: %s, Findings: %d", result.Verdict, len(result.Findings))
|
||||||
|
|
||||||
|
// Step 6: Format (dry-run validation)
|
||||||
|
body := review.FormatMarkdown(result, "integration-test")
|
||||||
|
if body == "" {
|
||||||
|
t.Fatal("formatted review body is empty")
|
||||||
|
}
|
||||||
|
t.Logf("Review body:\n%s", body)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntegration_PostAndCleanup(t *testing.T) {
|
||||||
|
giteaURL := os.Getenv("INTEGRATION_GITEA_URL")
|
||||||
|
giteaToken := os.Getenv("INTEGRATION_GITEA_TOKEN")
|
||||||
|
giteaRepo := os.Getenv("INTEGRATION_GITEA_REPO")
|
||||||
|
prNumStr := os.Getenv("INTEGRATION_PR_NUMBER")
|
||||||
|
|
||||||
|
if giteaURL == "" || giteaToken == "" || giteaRepo == "" || prNumStr == "" {
|
||||||
|
t.Skip("Integration test env vars not set, skipping")
|
||||||
|
}
|
||||||
|
|
||||||
|
prNumber, err := strconv.Atoi(prNumStr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Invalid PR number %q: %v", prNumStr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(giteaRepo, "/", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
t.Fatalf("Invalid repo format %q", giteaRepo)
|
||||||
|
}
|
||||||
|
owner, repoName := parts[0], parts[1]
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
giteaClient := gitea.NewClient(giteaURL, giteaToken)
|
||||||
|
|
||||||
|
// Post a test review
|
||||||
|
sentinel := "<!-- review-bot:integration-test -->"
|
||||||
|
testBody := "# Integration Test Review\n\nThis is a test review.\n\n" + sentinel
|
||||||
|
posted, err := giteaClient.PostReview(ctx, owner, repoName, prNumber, "COMMENT", testBody, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("PostReview: %v", err)
|
||||||
|
}
|
||||||
|
t.Logf("Posted review ID: %d", posted.ID)
|
||||||
|
|
||||||
|
// Verify it appears in listing
|
||||||
|
reviews, err := giteaClient.ListReviews(ctx, owner, repoName, prNumber)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListReviews: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, r := range reviews {
|
||||||
|
if r.ID == posted.ID && strings.Contains(r.Body, sentinel) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Error("posted review not found in listing")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup: delete the test review
|
||||||
|
err = giteaClient.DeleteReview(ctx, owner, repoName, prNumber, posted.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("Warning: could not delete test review %d: %v", posted.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
+113
-62
@@ -67,7 +67,6 @@ func main() {
|
|||||||
patternsRepo := flag.String("patterns-repo", envOrDefault("PATTERNS_REPO", ""), "Repo with language patterns (e.g. rodin/elixir-patterns)")
|
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")
|
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")
|
dryRun := flag.Bool("dry-run", false, "Print review to stdout instead of posting")
|
||||||
updateExisting := flag.Bool("update-existing", envOrDefaultBool("UPDATE_EXISTING", true), "Delete previous review from same bot before posting (default true)")
|
|
||||||
llmTemp := flag.Float64("llm-temperature", envOrDefaultFloat("LLM_TEMPERATURE", 0), "LLM temperature (0 = server default)")
|
llmTemp := flag.Float64("llm-temperature", envOrDefaultFloat("LLM_TEMPERATURE", 0), "LLM temperature (0 = server default)")
|
||||||
llmTimeout := flag.Int("llm-timeout", envOrDefaultInt("LLM_TIMEOUT", 300), "LLM request timeout in seconds (default 300)")
|
llmTimeout := flag.Int("llm-timeout", envOrDefaultInt("LLM_TIMEOUT", 300), "LLM request timeout in seconds (default 300)")
|
||||||
llmProvider := flag.String("llm-provider", envOrDefault("LLM_PROVIDER", "openai"), "LLM API provider: openai or anthropic")
|
llmProvider := flag.String("llm-provider", envOrDefault("LLM_PROVIDER", "openai"), "LLM API provider: openai or anthropic")
|
||||||
@@ -279,6 +278,16 @@ func main() {
|
|||||||
|
|
||||||
// Step 10: Format and post review
|
// Step 10: Format and post review
|
||||||
reviewBody := review.FormatMarkdown(result, *reviewerName)
|
reviewBody := review.FormatMarkdown(result, *reviewerName)
|
||||||
|
|
||||||
|
// Add commit footer so readers know which commit was evaluated
|
||||||
|
if pr.Head.Sha != "" {
|
||||||
|
shortSHA := pr.Head.Sha
|
||||||
|
if len(shortSHA) > 8 {
|
||||||
|
shortSHA = shortSHA[:8]
|
||||||
|
}
|
||||||
|
reviewBody += fmt.Sprintf("\n\n---\n*Evaluated against %s*", shortSHA)
|
||||||
|
}
|
||||||
|
|
||||||
event := review.GiteaEvent(result.Verdict)
|
event := review.GiteaEvent(result.Verdict)
|
||||||
|
|
||||||
if *dryRun {
|
if *dryRun {
|
||||||
@@ -307,61 +316,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --- Review update strategy ---
|
// --- Review update strategy ---
|
||||||
// 1. No existing review → POST new
|
// 1. POST new review first (gets non-stale approval badge on HEAD)
|
||||||
// 2. Existing review, same state → PATCH body in place (preserves threads)
|
// 2. Then supersede old review with link to the new one
|
||||||
// 3. Existing review, state change → PATCH old to "Superseded", POST new
|
// Order matters: post first so we have the new review's URL for the supersede message.
|
||||||
if *updateExisting && *reviewerName != "" {
|
var existingReview *gitea.Review
|
||||||
|
var existingCommentID int64
|
||||||
|
if *reviewerName != "" {
|
||||||
existingReviews, err := giteaClient.ListReviews(ctx, owner, repoName, prNumber)
|
existingReviews, err := giteaClient.ListReviews(ctx, owner, repoName, prNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("could not list existing reviews", "pr", prNumber, "error", err)
|
slog.Warn("could not list existing reviews", "pr", prNumber, "error", err)
|
||||||
} else {
|
} else {
|
||||||
// Detect shared-token misconfiguration: if detected, skip all
|
|
||||||
// update logic (PATCH/supersede) to avoid clobbering a sibling's review.
|
|
||||||
sharedToken := hasSharedToken(existingReviews, sentinel)
|
sharedToken := hasSharedToken(existingReviews, sentinel)
|
||||||
if sharedToken {
|
if !sharedToken {
|
||||||
slog.Warn("shared token mode: skipping update-in-place logic to avoid clobbering sibling review")
|
existingReview = findOwnReview(existingReviews, sentinel)
|
||||||
} else {
|
if existingReview != nil {
|
||||||
existing := findOwnReview(existingReviews, sentinel)
|
cid, err := giteaClient.GetTimelineReviewCommentID(ctx, owner, repoName, prNumber, sentinel)
|
||||||
|
if err != nil {
|
||||||
if existing != nil {
|
slog.Warn("could not find old review comment ID for supersede", "error", err)
|
||||||
if reviewUnchanged(existingReviews, reviewBody, event, sentinel) {
|
existingReview = nil // can't supersede without comment ID
|
||||||
slog.Info("review unchanged from previous run; skipping to preserve threads", "pr", prNumber)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Same state → PATCH in place
|
|
||||||
if existing.State == event {
|
|
||||||
commentID, err := giteaClient.GetTimelineReviewCommentID(ctx, owner, repoName, prNumber, sentinel)
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("could not find review comment ID, falling back to new post", "error", err)
|
|
||||||
} else {
|
|
||||||
if err := giteaClient.EditComment(ctx, owner, repoName, commentID, reviewBody); err != nil {
|
|
||||||
slog.Warn("could not edit review, falling back to new post", "comment_id", commentID, "error", err)
|
|
||||||
} else {
|
|
||||||
slog.Info("review updated in place", "comment_id", commentID, "pr", prNumber)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// State change → mark old as superseded, post new below
|
existingCommentID = cid
|
||||||
commentID, err := giteaClient.GetTimelineReviewCommentID(ctx, owner, repoName, prNumber, sentinel)
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("could not find old review comment ID", "error", err)
|
|
||||||
} else {
|
|
||||||
supersededBody := fmt.Sprintf("~~*This review has been superseded by a newer review below.*~~\n\n%s", sentinel)
|
|
||||||
if err := giteaClient.EditComment(ctx, owner, repoName, commentID, supersededBody); err != nil {
|
|
||||||
slog.Warn("could not mark old review as superseded", "comment_id", commentID, "error", err)
|
|
||||||
} else {
|
|
||||||
slog.Info("marked old review as superseded", "old_state", existing.State, "new_state", event, "pr", prNumber)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
slog.Warn("shared token mode: skipping supersede to avoid clobbering sibling review")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST new review (first run, or state transition fallthrough)
|
// Self-request as reviewer (ensures we appear in required-reviewer checks)
|
||||||
|
authUser, err := giteaClient.GetAuthenticatedUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("could not determine authenticated user for reviewer self-request", "error", err)
|
||||||
|
} else if authUser != "" {
|
||||||
|
if err := giteaClient.RequestReviewer(ctx, owner, repoName, prNumber, authUser); err != nil {
|
||||||
|
slog.Warn("could not self-request as reviewer", "user", authUser, "error", err)
|
||||||
|
} else {
|
||||||
|
slog.Debug("self-requested as reviewer", "user", authUser, "pr", prNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST new review
|
||||||
slog.Info("posting review", "event", event, "pr", prNumber)
|
slog.Info("posting review", "event", event, "pr", prNumber)
|
||||||
posted, err := giteaClient.PostReview(ctx, owner, repoName, prNumber, event, reviewBody, inlineComments)
|
posted, err := giteaClient.PostReview(ctx, owner, repoName, prNumber, event, reviewBody, inlineComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -370,6 +365,46 @@ func main() {
|
|||||||
}
|
}
|
||||||
slog.Info("review posted", "review_id", posted.ID, "user", posted.User.Login, "pr", prNumber)
|
slog.Info("review posted", "review_id", posted.ID, "user", posted.User.Login, "pr", prNumber)
|
||||||
|
|
||||||
|
// Supersede old review with link to the new one
|
||||||
|
if existingReview != nil && existingCommentID > 0 {
|
||||||
|
newReviewURL := fmt.Sprintf("%s/%s/%s/pulls/%d#pullrequestreview-%d", strings.TrimRight(*giteaURL, "/"), owner, repoName, prNumber, posted.ID)
|
||||||
|
supersededBody := buildSupersededBody(existingReview.Body, existingReview.CommitID, newReviewURL, sentinel)
|
||||||
|
supersedeOK := false
|
||||||
|
if err := giteaClient.EditComment(ctx, owner, repoName, existingCommentID, supersededBody); err != nil {
|
||||||
|
slog.Warn("could not mark old review as superseded", "comment_id", existingCommentID, "error", err)
|
||||||
|
} else {
|
||||||
|
slog.Info("marked old review as superseded", "old_state", existingReview.State, "new_review_id", posted.ID, "pr", prNumber)
|
||||||
|
supersedeOK = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve old review's inline comments only after successful supersede
|
||||||
|
if supersedeOK {
|
||||||
|
oldComments, err := giteaClient.ListReviewComments(ctx, owner, repoName, prNumber, existingReview.ID)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("could not list old review comments for resolution", "review_id", existingReview.ID, "error", err)
|
||||||
|
} else {
|
||||||
|
resolved, failed := 0, 0
|
||||||
|
for _, c := range oldComments {
|
||||||
|
if c.ID == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := giteaClient.ResolveComment(ctx, owner, repoName, c.ID); err != nil {
|
||||||
|
slog.Debug("could not resolve inline comment", "comment_id", c.ID, "error", err)
|
||||||
|
failed++
|
||||||
|
} else {
|
||||||
|
resolved++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if resolved > 0 {
|
||||||
|
slog.Info("resolved old inline comments", "count", resolved, "pr", prNumber)
|
||||||
|
}
|
||||||
|
if failed > 0 {
|
||||||
|
slog.Warn("some inline comments could not be resolved", "failed", failed, "pr", prNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchFileContext fetches the full content of modified files from the PR branch.
|
// fetchFileContext fetches the full content of modified files from the PR branch.
|
||||||
@@ -526,22 +561,29 @@ func validateReviewerName(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// reviewUnchanged checks if an existing review with the same sentinel
|
// buildSupersededBody creates the body for a superseded review: struck-through banner
|
||||||
// already has identical body and state. Returns true if a re-post would
|
// with collapsed original content and the commit it was evaluated against.
|
||||||
// produce the same result (skip to preserve conversation threads).
|
func buildSupersededBody(originalBody, commitSHA, newReviewURL, sentinel string) string {
|
||||||
func reviewUnchanged(reviews []gitea.Review, newBody, newEvent, sentinel string) bool {
|
shortSHA := commitSHA
|
||||||
for _, r := range reviews {
|
if len(shortSHA) > 8 {
|
||||||
if r.Stale {
|
shortSHA = shortSHA[:8]
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !strings.Contains(r.Body, sentinel) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if r.State == newEvent && r.Body == newBody {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false
|
var sb strings.Builder
|
||||||
|
sb.WriteString("~~Original review~~\n\n")
|
||||||
|
sb.WriteString("**Superseded** \u2014 [see current review](")
|
||||||
|
sb.WriteString(newReviewURL)
|
||||||
|
sb.WriteString(") for up-to-date findings.\n\n")
|
||||||
|
if shortSHA != "" {
|
||||||
|
sb.WriteString("<details><summary>Previous findings (commit ")
|
||||||
|
sb.WriteString(shortSHA)
|
||||||
|
sb.WriteString(")</summary>\n\n")
|
||||||
|
} else {
|
||||||
|
sb.WriteString("<details><summary>Previous findings</summary>\n\n")
|
||||||
|
}
|
||||||
|
sb.WriteString(originalBody)
|
||||||
|
sb.WriteString("\n\n</details>\n\n")
|
||||||
|
sb.WriteString(sentinel)
|
||||||
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasSharedToken detects if another review-bot role posted under the same
|
// hasSharedToken detects if another review-bot role posted under the same
|
||||||
@@ -587,10 +629,19 @@ func extractSentinelName(body string) string {
|
|||||||
|
|
||||||
// findOwnReview locates a review matching the given sentinel in its body.
|
// findOwnReview locates a review matching the given sentinel in its body.
|
||||||
func findOwnReview(reviews []gitea.Review, sentinel string) *gitea.Review {
|
func findOwnReview(reviews []gitea.Review, sentinel string) *gitea.Review {
|
||||||
|
var best *gitea.Review
|
||||||
for i := range reviews {
|
for i := range reviews {
|
||||||
if strings.Contains(reviews[i].Body, sentinel) {
|
if !strings.Contains(reviews[i].Body, sentinel) {
|
||||||
return &reviews[i]
|
continue
|
||||||
|
}
|
||||||
|
// Skip superseded reviews (they contain our sentinel in the collapsed body)
|
||||||
|
if strings.Contains(reviews[i].Body, "~~Original review~~") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Take the highest ID (most recent)
|
||||||
|
if best == nil || reviews[i].ID > best.ID {
|
||||||
|
best = &reviews[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return best
|
||||||
}
|
}
|
||||||
|
|||||||
+551
-75
@@ -2,7 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -53,82 +56,52 @@ func makeReview(id int64, login, state string, stale bool, body string) gitea.Re
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReviewUnchanged(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
existing []gitea.Review
|
|
||||||
newBody string
|
|
||||||
newEvent string
|
|
||||||
sentinel string
|
|
||||||
want bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "no existing review",
|
|
||||||
existing: nil,
|
|
||||||
newBody: "new review",
|
|
||||||
newEvent: "APPROVED",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "identical body and state",
|
|
||||||
existing: []gitea.Review{
|
|
||||||
makeReview(100, "bot", "APPROVED", false, "same body\n<!-- review-bot:sonnet -->"),
|
|
||||||
},
|
|
||||||
newBody: "same body\n<!-- review-bot:sonnet -->",
|
|
||||||
newEvent: "APPROVED",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "same body but different state",
|
|
||||||
existing: []gitea.Review{
|
|
||||||
makeReview(100, "bot", "APPROVED", false, "body\n<!-- review-bot:sonnet -->"),
|
|
||||||
},
|
|
||||||
newBody: "body\n<!-- review-bot:sonnet -->",
|
|
||||||
newEvent: "REQUEST_CHANGES",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "different body same state",
|
|
||||||
existing: []gitea.Review{
|
|
||||||
makeReview(100, "bot", "APPROVED", false, "old body\n<!-- review-bot:sonnet -->"),
|
|
||||||
},
|
|
||||||
newBody: "new body\n<!-- review-bot:sonnet -->",
|
|
||||||
newEvent: "APPROVED",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "stale review with same body (should still post)",
|
|
||||||
existing: []gitea.Review{
|
|
||||||
makeReview(100, "bot", "APPROVED", true, "same\n<!-- review-bot:sonnet -->"),
|
|
||||||
},
|
|
||||||
newBody: "same\n<!-- review-bot:sonnet -->",
|
|
||||||
newEvent: "APPROVED",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "different sentinel (not our review)",
|
|
||||||
existing: []gitea.Review{
|
|
||||||
makeReview(100, "bot", "APPROVED", false, "body\n<!-- review-bot:gpt -->"),
|
|
||||||
},
|
|
||||||
newBody: "body\n<!-- review-bot:sonnet -->",
|
|
||||||
newEvent: "APPROVED",
|
|
||||||
sentinel: "<!-- review-bot:sonnet -->",
|
|
||||||
want: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
func TestBuildSupersededBody(t *testing.T) {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
original := "# Review\n\nLooks good.\n\n<!-- review-bot:sonnet -->"
|
||||||
got := reviewUnchanged(tc.existing, tc.newBody, tc.newEvent, tc.sentinel)
|
sentinel := "<!-- review-bot:sonnet -->"
|
||||||
if got != tc.want {
|
newURL := "https://gitea.example.com/owner/repo/pulls/1#pullrequestreview-99"
|
||||||
t.Errorf("reviewUnchanged() = %v, want %v", got, tc.want)
|
|
||||||
}
|
result := buildSupersededBody(original, "abcdef1234567890", newURL, sentinel)
|
||||||
})
|
|
||||||
|
// Should contain the struck-through banner
|
||||||
|
if !strings.Contains(result, "~~Original review~~") {
|
||||||
|
t.Error("missing struck-through banner")
|
||||||
|
}
|
||||||
|
// Should contain superseded notice with link
|
||||||
|
if !strings.Contains(result, "**Superseded**") {
|
||||||
|
t.Error("missing superseded notice")
|
||||||
|
}
|
||||||
|
if !strings.Contains(result, "[see current review]("+newURL+")") {
|
||||||
|
t.Error("missing link to new review")
|
||||||
|
}
|
||||||
|
// Should contain collapsed original
|
||||||
|
if !strings.Contains(result, "<details>") {
|
||||||
|
t.Error("missing details/collapse")
|
||||||
|
}
|
||||||
|
// Should contain short commit SHA
|
||||||
|
if !strings.Contains(result, "abcdef12") {
|
||||||
|
t.Error("missing short SHA")
|
||||||
|
}
|
||||||
|
// Should NOT contain full SHA
|
||||||
|
if strings.Contains(result, "abcdef1234567890") {
|
||||||
|
t.Error("should truncate SHA to 8 chars")
|
||||||
|
}
|
||||||
|
// Should contain the original body inside details
|
||||||
|
if !strings.Contains(result, original) {
|
||||||
|
t.Error("original body not preserved in collapsed section")
|
||||||
|
}
|
||||||
|
// Should end with sentinel
|
||||||
|
if !strings.Contains(result, sentinel) {
|
||||||
|
t.Error("missing sentinel")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildSupersededBodyShortSHA(t *testing.T) {
|
||||||
|
// Short SHA should pass through without panic
|
||||||
|
result := buildSupersededBody("body", "abc", "https://example.com/review", "<!-- review-bot:x -->")
|
||||||
|
if !strings.Contains(result, "abc") {
|
||||||
|
t.Error("short SHA not preserved")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,6 +144,32 @@ func TestFindOwnReview(t *testing.T) {
|
|||||||
sentinel: "<!-- review-bot:sonnet -->",
|
sentinel: "<!-- review-bot:sonnet -->",
|
||||||
wantID: 20,
|
wantID: 20,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "skips superseded review",
|
||||||
|
reviews: []gitea.Review{
|
||||||
|
makeReview(10, "bot", "APPROVED", false, "~~Original review~~\n\n**Superseded**\n<!-- review-bot:sonnet -->"),
|
||||||
|
makeReview(20, "bot", "APPROVED", false, "fresh review\n<!-- review-bot:sonnet -->"),
|
||||||
|
},
|
||||||
|
sentinel: "<!-- review-bot:sonnet -->",
|
||||||
|
wantID: 20,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only superseded reviews exist",
|
||||||
|
reviews: []gitea.Review{
|
||||||
|
makeReview(10, "bot", "APPROVED", false, "~~Original review~~\n\n<!-- review-bot:sonnet -->"),
|
||||||
|
},
|
||||||
|
sentinel: "<!-- review-bot:sonnet -->",
|
||||||
|
wantNil: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "picks highest ID among matches",
|
||||||
|
reviews: []gitea.Review{
|
||||||
|
makeReview(50, "bot", "APPROVED", false, "v1\n<!-- review-bot:sonnet -->"),
|
||||||
|
makeReview(30, "bot", "APPROVED", false, "v0\n<!-- review-bot:sonnet -->"),
|
||||||
|
},
|
||||||
|
sentinel: "<!-- review-bot:sonnet -->",
|
||||||
|
wantID: 50,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -365,3 +364,480 @@ func TestSetupLogger_Integration(t *testing.T) {
|
|||||||
setupLogger("text", "unknown") // should default to info
|
setupLogger("text", "unknown") // should default to info
|
||||||
setupLogger("invalid", "info") // should default to text
|
setupLogger("invalid", "info") // should default to text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsPatternFile(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"README.md", true},
|
||||||
|
{"docs/GUIDE.MD", true},
|
||||||
|
{"config.yml", true},
|
||||||
|
{"config.yaml", true},
|
||||||
|
{"notes.txt", true},
|
||||||
|
{"NOTES.TXT", true},
|
||||||
|
{"main.go", false},
|
||||||
|
{"lib.rs", false},
|
||||||
|
{"index.js", false},
|
||||||
|
{"Makefile", false},
|
||||||
|
{"", false},
|
||||||
|
{"doc.pdf", false},
|
||||||
|
{"patterns.Yml", true},
|
||||||
|
{"deep/path/file.yaml", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.path, func(t *testing.T) {
|
||||||
|
got := isPatternFile(tc.path)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("isPatternFile(%q) = %v, want %v", tc.path, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEvaluateCIStatus(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
statuses []gitea.CommitStatus
|
||||||
|
wantPassed bool
|
||||||
|
wantSubstr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty statuses",
|
||||||
|
statuses: nil,
|
||||||
|
wantPassed: true,
|
||||||
|
wantSubstr: "no CI statuses",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "all success",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "success", Context: "ci/build", Description: "Build passed"},
|
||||||
|
{Status: "success", Context: "ci/test", Description: "Tests passed"},
|
||||||
|
},
|
||||||
|
wantPassed: true,
|
||||||
|
wantSubstr: "all checks passed",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "one failure",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "success", Context: "ci/build", Description: "Build passed"},
|
||||||
|
{Status: "failure", Context: "ci/test", Description: "Tests failed"},
|
||||||
|
},
|
||||||
|
wantPassed: false,
|
||||||
|
wantSubstr: "ci/test",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "error status",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "error", Context: "ci/lint", Description: "Lint error"},
|
||||||
|
},
|
||||||
|
wantPassed: false,
|
||||||
|
wantSubstr: "ci/lint",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "pending treated as not-failed",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "pending", Context: "ci/build", Description: "In progress"},
|
||||||
|
{Status: "success", Context: "ci/test", Description: "Tests passed"},
|
||||||
|
},
|
||||||
|
wantPassed: true,
|
||||||
|
wantSubstr: "all checks passed",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple failures",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "failure", Context: "ci/build", Description: "Build failed"},
|
||||||
|
{Status: "failure", Context: "ci/test", Description: "Tests failed"},
|
||||||
|
},
|
||||||
|
wantPassed: false,
|
||||||
|
wantSubstr: "ci/build",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mixed with pending and failure",
|
||||||
|
statuses: []gitea.CommitStatus{
|
||||||
|
{Status: "success", Context: "ci/build", Description: "Build passed"},
|
||||||
|
{Status: "pending", Context: "ci/deploy", Description: "Deploying"},
|
||||||
|
{Status: "failure", Context: "ci/test", Description: "Tests failed"},
|
||||||
|
},
|
||||||
|
wantPassed: false,
|
||||||
|
wantSubstr: "ci/test",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
passed, details := evaluateCIStatus(tc.statuses)
|
||||||
|
if passed != tc.wantPassed {
|
||||||
|
t.Errorf("evaluateCIStatus() passed = %v, want %v", passed, tc.wantPassed)
|
||||||
|
}
|
||||||
|
if !strings.Contains(details, tc.wantSubstr) {
|
||||||
|
t.Errorf("evaluateCIStatus() details = %q, want substring %q", details, tc.wantSubstr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvOrDefault(t *testing.T) {
|
||||||
|
// Test with unset env var
|
||||||
|
os.Unsetenv("TEST_ENV_OR_DEFAULT_UNSET")
|
||||||
|
got := envOrDefault("TEST_ENV_OR_DEFAULT_UNSET", "fallback")
|
||||||
|
if got != "fallback" {
|
||||||
|
t.Errorf("envOrDefault(unset) = %q, want %q", got, "fallback")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with set env var
|
||||||
|
os.Setenv("TEST_ENV_OR_DEFAULT_SET", "custom")
|
||||||
|
defer os.Unsetenv("TEST_ENV_OR_DEFAULT_SET")
|
||||||
|
got = envOrDefault("TEST_ENV_OR_DEFAULT_SET", "fallback")
|
||||||
|
if got != "custom" {
|
||||||
|
t.Errorf("envOrDefault(set) = %q, want %q", got, "custom")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with empty env var (should return default)
|
||||||
|
os.Setenv("TEST_ENV_OR_DEFAULT_EMPTY", "")
|
||||||
|
defer os.Unsetenv("TEST_ENV_OR_DEFAULT_EMPTY")
|
||||||
|
got = envOrDefault("TEST_ENV_OR_DEFAULT_EMPTY", "fallback")
|
||||||
|
if got != "fallback" {
|
||||||
|
t.Errorf("envOrDefault(empty) = %q, want %q", got, "fallback")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvOrDefaultFloat(t *testing.T) {
|
||||||
|
// Test with unset env var
|
||||||
|
os.Unsetenv("TEST_ENV_FLOAT_UNSET")
|
||||||
|
got := envOrDefaultFloat("TEST_ENV_FLOAT_UNSET", 1.5)
|
||||||
|
if got != 1.5 {
|
||||||
|
t.Errorf("envOrDefaultFloat(unset) = %f, want %f", got, 1.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with valid float
|
||||||
|
os.Setenv("TEST_ENV_FLOAT_SET", "2.7")
|
||||||
|
defer os.Unsetenv("TEST_ENV_FLOAT_SET")
|
||||||
|
got = envOrDefaultFloat("TEST_ENV_FLOAT_SET", 1.5)
|
||||||
|
if got != 2.7 {
|
||||||
|
t.Errorf("envOrDefaultFloat(set) = %f, want %f", got, 2.7)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with invalid float (should return default)
|
||||||
|
os.Setenv("TEST_ENV_FLOAT_INVALID", "not-a-number")
|
||||||
|
defer os.Unsetenv("TEST_ENV_FLOAT_INVALID")
|
||||||
|
got = envOrDefaultFloat("TEST_ENV_FLOAT_INVALID", 3.14)
|
||||||
|
if got != 3.14 {
|
||||||
|
t.Errorf("envOrDefaultFloat(invalid) = %f, want %f", got, 3.14)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with empty string (should return default)
|
||||||
|
os.Setenv("TEST_ENV_FLOAT_EMPTY", "")
|
||||||
|
defer os.Unsetenv("TEST_ENV_FLOAT_EMPTY")
|
||||||
|
got = envOrDefaultFloat("TEST_ENV_FLOAT_EMPTY", 0.5)
|
||||||
|
if got != 0.5 {
|
||||||
|
t.Errorf("envOrDefaultFloat(empty) = %f, want %f", got, 0.5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvOrDefaultInt(t *testing.T) {
|
||||||
|
// Test with unset env var
|
||||||
|
os.Unsetenv("TEST_ENV_INT_UNSET")
|
||||||
|
got := envOrDefaultInt("TEST_ENV_INT_UNSET", 42)
|
||||||
|
if got != 42 {
|
||||||
|
t.Errorf("envOrDefaultInt(unset) = %d, want %d", got, 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with valid int
|
||||||
|
os.Setenv("TEST_ENV_INT_SET", "100")
|
||||||
|
defer os.Unsetenv("TEST_ENV_INT_SET")
|
||||||
|
got = envOrDefaultInt("TEST_ENV_INT_SET", 42)
|
||||||
|
if got != 100 {
|
||||||
|
t.Errorf("envOrDefaultInt(set) = %d, want %d", got, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with invalid int (should return default)
|
||||||
|
os.Setenv("TEST_ENV_INT_INVALID", "abc")
|
||||||
|
defer os.Unsetenv("TEST_ENV_INT_INVALID")
|
||||||
|
got = envOrDefaultInt("TEST_ENV_INT_INVALID", 42)
|
||||||
|
if got != 42 {
|
||||||
|
t.Errorf("envOrDefaultInt(invalid) = %d, want %d", got, 42)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with empty string (should return default)
|
||||||
|
os.Setenv("TEST_ENV_INT_EMPTY", "")
|
||||||
|
defer os.Unsetenv("TEST_ENV_INT_EMPTY")
|
||||||
|
got = envOrDefaultInt("TEST_ENV_INT_EMPTY", 99)
|
||||||
|
if got != 99 {
|
||||||
|
t.Errorf("envOrDefaultInt(empty) = %d, want %d", got, 99)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test with negative int
|
||||||
|
os.Setenv("TEST_ENV_INT_NEG", "-5")
|
||||||
|
defer os.Unsetenv("TEST_ENV_INT_NEG")
|
||||||
|
got = envOrDefaultInt("TEST_ENV_INT_NEG", 42)
|
||||||
|
if got != -5 {
|
||||||
|
t.Errorf("envOrDefaultInt(negative) = %d, want %d", got, -5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvOrDefaultBool(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
envVal string
|
||||||
|
setEnv bool
|
||||||
|
defaultVal bool
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"unset returns default true", "", false, true, true},
|
||||||
|
{"unset returns default false", "", false, false, false},
|
||||||
|
{"true", "true", true, false, true},
|
||||||
|
{"TRUE", "TRUE", true, false, true},
|
||||||
|
{"True", "True", true, false, true},
|
||||||
|
{"1", "1", true, false, true},
|
||||||
|
{"yes", "yes", true, false, true},
|
||||||
|
{"YES", "YES", true, false, true},
|
||||||
|
{"false", "false", true, true, false},
|
||||||
|
{"0", "0", true, true, false},
|
||||||
|
{"no", "no", true, true, false},
|
||||||
|
{"random string", "random", true, true, false},
|
||||||
|
{"empty string returns default", "", true, true, true},
|
||||||
|
{"whitespace true", " true ", true, false, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
envKey := "TEST_ENV_BOOL_" + strings.ReplaceAll(tc.name, " ", "_")
|
||||||
|
if tc.setEnv {
|
||||||
|
os.Setenv(envKey, tc.envVal)
|
||||||
|
defer os.Unsetenv(envKey)
|
||||||
|
} else {
|
||||||
|
os.Unsetenv(envKey)
|
||||||
|
}
|
||||||
|
got := envOrDefaultBool(envKey, tc.defaultVal)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("envOrDefaultBool(%q, %v) = %v, want %v", tc.envVal, tc.defaultVal, got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractSentinelName_EdgeCases(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
body string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"<!-- review-bot:sonnet --> rest", "sonnet"},
|
||||||
|
{"<!-- review-bot:gpt-review --> rest", "gpt-review"},
|
||||||
|
{"no sentinel here", "unknown"},
|
||||||
|
{"<!-- review-bot:", "unknown"}, // prefix but no suffix
|
||||||
|
{"prefix <!-- review-bot:abc --> end", "abc"}, // embedded in text
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
got := extractSentinelName(tc.body)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Errorf("extractSentinelName(%q) = %q, want %q", tc.body, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMainSubprocess runs main() as a subprocess using the test binary itself.
|
||||||
|
// This allows coverage to be captured for main() code paths.
|
||||||
|
func TestMainSubprocess_Version(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
// Reset flags for main()
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot", "--version"}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_Version")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("--version subprocess failed: %v\n%s", err, out)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "review-bot") {
|
||||||
|
t.Errorf("--version output = %q, want to contain 'review-bot'", string(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_MissingFlags(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
// Reset flags for main()
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot"}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_MissingFlags")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with no flags, got success")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "missing required") {
|
||||||
|
t.Errorf("expected error about missing flags, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_InvalidReviewerName(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot",
|
||||||
|
"--gitea-url", "http://localhost",
|
||||||
|
"--repo", "owner/repo",
|
||||||
|
"--pr", "1",
|
||||||
|
"--reviewer-name", "invalid name",
|
||||||
|
"--reviewer-token", "tok",
|
||||||
|
"--llm-base-url", "http://localhost",
|
||||||
|
"--llm-api-key", "key",
|
||||||
|
"--llm-model", "model",
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_InvalidReviewerName")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with invalid reviewer-name, got success")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "invalid reviewer name") {
|
||||||
|
t.Errorf("expected error about invalid reviewer name, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_InvalidRepo(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot",
|
||||||
|
"--gitea-url", "http://localhost",
|
||||||
|
"--repo", "invalidrepo",
|
||||||
|
"--pr", "1",
|
||||||
|
"--reviewer-token", "tok",
|
||||||
|
"--llm-base-url", "http://localhost",
|
||||||
|
"--llm-api-key", "key",
|
||||||
|
"--llm-model", "model",
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_InvalidRepo")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with invalid repo format")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "invalid repo format") {
|
||||||
|
t.Errorf("expected error about invalid repo, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_InvalidPRNumber(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot",
|
||||||
|
"--gitea-url", "http://localhost",
|
||||||
|
"--repo", "owner/repo",
|
||||||
|
"--pr", "notanumber",
|
||||||
|
"--reviewer-token", "tok",
|
||||||
|
"--llm-base-url", "http://localhost",
|
||||||
|
"--llm-api-key", "key",
|
||||||
|
"--llm-model", "model",
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_InvalidPRNumber")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with invalid PR number")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "invalid PR number") {
|
||||||
|
t.Errorf("expected error about invalid PR number, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_InvalidTemperature(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot",
|
||||||
|
"--gitea-url", "http://localhost",
|
||||||
|
"--repo", "owner/repo",
|
||||||
|
"--pr", "1",
|
||||||
|
"--reviewer-token", "tok",
|
||||||
|
"--llm-base-url", "http://localhost",
|
||||||
|
"--llm-api-key", "key",
|
||||||
|
"--llm-model", "model",
|
||||||
|
"--llm-temperature", "5.0",
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_InvalidTemperature")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with invalid temperature")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "invalid LLM temperature") {
|
||||||
|
t.Errorf("expected error about invalid temperature, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMainSubprocess_InvalidProvider(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_SUBPROCESS_MAIN") == "1" {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
os.Args = []string{"review-bot",
|
||||||
|
"--gitea-url", "http://localhost",
|
||||||
|
"--repo", "owner/repo",
|
||||||
|
"--pr", "1",
|
||||||
|
"--reviewer-token", "tok",
|
||||||
|
"--llm-base-url", "http://localhost",
|
||||||
|
"--llm-api-key", "key",
|
||||||
|
"--llm-model", "model",
|
||||||
|
"--llm-provider", "invalid-provider",
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestMainSubprocess_InvalidProvider")
|
||||||
|
cmd.Env = append(cleanEnv(), "TEST_SUBPROCESS_MAIN=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected non-zero exit with invalid provider")
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(out), "invalid LLM provider") {
|
||||||
|
t.Errorf("expected error about invalid provider, got: %s", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanEnv returns environ without any GITEA/LLM/REVIEWER env vars that would
|
||||||
|
// interfere with testing missing-flag scenarios.
|
||||||
|
func cleanEnv() []string {
|
||||||
|
var env []string
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
key := strings.SplitN(e, "=", 2)[0]
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(key, "GITEA_"),
|
||||||
|
strings.HasPrefix(key, "LLM_"),
|
||||||
|
strings.HasPrefix(key, "REVIEWER_"),
|
||||||
|
strings.HasPrefix(key, "PR_"),
|
||||||
|
strings.HasPrefix(key, "LOG_"),
|
||||||
|
strings.HasPrefix(key, "CONVENTIONS_"),
|
||||||
|
strings.HasPrefix(key, "SYSTEM_PROMPT_"),
|
||||||
|
strings.HasPrefix(key, "PATTERNS_"),
|
||||||
|
strings.HasPrefix(key, "UPDATE_"):
|
||||||
|
continue
|
||||||
|
default:
|
||||||
|
env = append(env, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
|||||||
+117
-5
@@ -82,6 +82,7 @@ type ChangedFile struct {
|
|||||||
|
|
||||||
// ReviewComment represents an inline comment to attach to a review.
|
// ReviewComment represents an inline comment to attach to a review.
|
||||||
type ReviewComment struct {
|
type ReviewComment struct {
|
||||||
|
ID int64 `json:"id,omitempty"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
NewPosition int64 `json:"new_position"`
|
NewPosition int64 `json:"new_position"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
@@ -316,13 +317,14 @@ func (c *Client) GetAllFilesInPath(ctx context.Context, owner, repo, path string
|
|||||||
|
|
||||||
// Review represents a pull request review from the Gitea API.
|
// Review represents a pull request review from the Gitea API.
|
||||||
type Review struct {
|
type Review struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
User struct {
|
User struct {
|
||||||
Login string `json:"login"`
|
Login string `json:"login"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
Stale bool `json:"stale"`
|
Stale bool `json:"stale"`
|
||||||
|
CommitID string `json:"commit_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListReviews returns all reviews on a pull request.
|
// ListReviews returns all reviews on a pull request.
|
||||||
@@ -459,3 +461,113 @@ func (c *Client) EditComment(ctx context.Context, owner, repo string, commentID
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAuthenticatedUser returns the login of the user authenticated by the token.
|
||||||
|
func (c *Client) GetAuthenticatedUser(ctx context.Context) (string, error) {
|
||||||
|
reqURL := fmt.Sprintf("%s/api/v1/user", c.baseURL)
|
||||||
|
body, err := c.doGet(ctx, reqURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("get authenticated user: %w", err)
|
||||||
|
}
|
||||||
|
var result struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||||||
|
return "", fmt.Errorf("parse user response: %w", err)
|
||||||
|
}
|
||||||
|
return result.Login, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RequestReviewer adds the given user as a requested reviewer on a pull request.
|
||||||
|
// This is idempotent — requesting an already-requested reviewer is a no-op.
|
||||||
|
func (c *Client) RequestReviewer(ctx context.Context, owner, repo string, number int, reviewer string) error {
|
||||||
|
reqURL := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/requested_reviewers",
|
||||||
|
c.baseURL,
|
||||||
|
url.PathEscape(owner),
|
||||||
|
url.PathEscape(repo),
|
||||||
|
number)
|
||||||
|
|
||||||
|
payload := struct {
|
||||||
|
Reviewers []string `json:"reviewers"`
|
||||||
|
}{Reviewers: []string{reviewer}}
|
||||||
|
data, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("marshal reviewer request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create reviewer request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", "token "+c.token)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("request reviewer: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
||||||
|
return fmt.Errorf("request reviewer failed (status %d): %s", resp.StatusCode, body)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListReviewComments returns the inline comments attached to a specific review.
|
||||||
|
// Paginates through all pages.
|
||||||
|
func (c *Client) ListReviewComments(ctx context.Context, owner, repo string, prNumber int, reviewID int64) ([]ReviewComment, error) {
|
||||||
|
const pageSize = 50
|
||||||
|
var all []ReviewComment
|
||||||
|
for page := 1; ; page++ {
|
||||||
|
reqURL := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/reviews/%d/comments?limit=%d&page=%d",
|
||||||
|
c.baseURL,
|
||||||
|
url.PathEscape(owner),
|
||||||
|
url.PathEscape(repo),
|
||||||
|
prNumber,
|
||||||
|
reviewID,
|
||||||
|
pageSize,
|
||||||
|
page)
|
||||||
|
body, err := c.doGet(ctx, reqURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list review comments (page %d): %w", page, err)
|
||||||
|
}
|
||||||
|
var batch []ReviewComment
|
||||||
|
if err := json.Unmarshal(body, &batch); err != nil {
|
||||||
|
return nil, fmt.Errorf("parse review comments (page %d): %w", page, err)
|
||||||
|
}
|
||||||
|
all = append(all, batch...)
|
||||||
|
if len(batch) < pageSize {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return all, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveComment marks an inline review comment as resolved.
|
||||||
|
func (c *Client) ResolveComment(ctx context.Context, owner, repo string, commentID int64) error {
|
||||||
|
reqURL := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/comments/%d/resolve",
|
||||||
|
c.baseURL,
|
||||||
|
url.PathEscape(owner),
|
||||||
|
url.PathEscape(repo),
|
||||||
|
commentID)
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create resolve request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", "token "+c.token)
|
||||||
|
|
||||||
|
resp, err := c.http.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("resolve comment: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
||||||
|
return fmt.Errorf("resolve comment failed (status %d): %s", resp.StatusCode, body)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -602,3 +604,142 @@ func TestIsNotFound(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetAuthenticatedUser(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/api/v1/user" {
|
||||||
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.Header.Get("Authorization") != "token test-token" {
|
||||||
|
t.Error("missing or wrong auth header")
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
fmt.Fprint(w, `{"login":"my-bot","id":42}`)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
login, err := client.GetAuthenticatedUser(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetAuthenticatedUser() error = %v", err)
|
||||||
|
}
|
||||||
|
if login != "my-bot" {
|
||||||
|
t.Errorf("login = %q, want %q", login, "my-bot")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequestReviewer(t *testing.T) {
|
||||||
|
var gotBody []byte
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected POST, got %s", r.Method)
|
||||||
|
}
|
||||||
|
expected := "/api/v1/repos/owner/repo/pulls/7/requested_reviewers"
|
||||||
|
if r.URL.Path != expected {
|
||||||
|
t.Errorf("path = %q, want %q", r.URL.Path, expected)
|
||||||
|
}
|
||||||
|
gotBody, _ = io.ReadAll(r.Body)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
err := client.RequestReviewer(context.Background(), "owner", "repo", 7, "bot-user")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RequestReviewer() error = %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(gotBody), `"bot-user"`) {
|
||||||
|
t.Errorf("body = %s, want to contain bot-user", gotBody)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequestReviewer_204(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
err := client.RequestReviewer(context.Background(), "owner", "repo", 1, "user")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RequestReviewer() should accept 204, got error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRequestReviewer_Error(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
fmt.Fprint(w, "no permission")
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
err := client.RequestReviewer(context.Background(), "owner", "repo", 1, "user")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for 403 response")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "403") {
|
||||||
|
t.Errorf("error should mention status code: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListReviewComments(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.Contains(r.URL.Path, "/pulls/1/reviews/42/comments") {
|
||||||
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
fmt.Fprint(w, `[{"id":100,"path":"main.go","new_position":5,"body":"finding"},{"id":101,"path":"lib.go","new_position":10,"body":"another"}]`)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
comments, err := client.ListReviewComments(context.Background(), "owner", "repo", 1, 42)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ListReviewComments() error = %v", err)
|
||||||
|
}
|
||||||
|
if len(comments) != 2 {
|
||||||
|
t.Fatalf("got %d comments, want 2", len(comments))
|
||||||
|
}
|
||||||
|
if comments[0].ID != 100 {
|
||||||
|
t.Errorf("comments[0].ID = %d, want 100", comments[0].ID)
|
||||||
|
}
|
||||||
|
if comments[1].Path != "lib.go" {
|
||||||
|
t.Errorf("comments[1].Path = %q, want %q", comments[1].Path, "lib.go")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveComment(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected POST, got %s", r.Method)
|
||||||
|
}
|
||||||
|
if !strings.Contains(r.URL.Path, "/pulls/comments/99/resolve") {
|
||||||
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
err := client.ResolveComment(context.Background(), "owner", "repo", 99)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ResolveComment() error = %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveComment_Error(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprint(w, "not found")
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := NewClient(server.URL, "test-token")
|
||||||
|
err := client.ResolveComment(context.Background(), "owner", "repo", 99)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for 404 response")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user