Compare commits
3 Commits
main
...
b5912d0725
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5912d0725 | ||
|
|
343f6ab165 | ||
|
|
cc321be658 |
162
AGENTS.md
Normal file
162
AGENTS.md
Normal file
@@ -0,0 +1,162 @@
|
||||
---
|
||||
layout: default
|
||||
---
|
||||
|
||||
# AGENTS.md – Repository Guidelines for Pierre Bot
|
||||
|
||||
> This document is consulted by autonomous coding agents (including the
|
||||
> Open‑Source `opencode` agent) when they read, modify, test, or lint the
|
||||
> code‑base. It contains the canonical commands for building, testing, and
|
||||
> lint‑ing the project as well as a concise style guide that all Go code must
|
||||
> follow. The file is deliberately kept around **150 lines** to be readable for
|
||||
> both humans and LLMs.
|
||||
|
||||
---
|
||||
|
||||
## 1️⃣ Build / Run Commands
|
||||
|
||||
| Command | Description |
|
||||
|--------|-------------|
|
||||
| `go build -o bin/pierre ./cmd/pierre/main.go` | Build the binary into `./bin/pierre`. |
|
||||
| `go run ./cmd/pierre/main.go --help` | Run the CLI with the help flag (fast sanity check). |
|
||||
| `./bin/pierre --version` | Verify the built binary reports its version. |
|
||||
|
||||
> **Tip for agents** – always prefer the `./bin/pierre` binary over invoking `go run` in CI or tests; it guarantees the same compilation flags.
|
||||
|
||||
---
|
||||
|
||||
## 2️⃣ Test Commands
|
||||
|
||||
The project uses the standard Go testing framework.
|
||||
|
||||
| Command | Use case |
|
||||
|---------|----------|
|
||||
| `go test ./...` | Run **all** unit and integration tests. |
|
||||
| `go test ./... -run ^TestJudgePR$` | Run a **single** test (replace `TestJudgePR` with the desired name). |
|
||||
| `go test -cover ./...` | Run tests and emit a coverage report. |
|
||||
| `go test -run ^TestName$ -count=1 -v` | Verbose, non‑cached run for debugging a single test. |
|
||||
| `go test ./... -bench .` | Execute benchmarks (if any). |
|
||||
|
||||
> **Agents should never** invoke `go test` with `-parallel` flags; the default parallelism is sufficient and ensures deterministic ordering for our table‑driven tests.
|
||||
|
||||
---
|
||||
|
||||
## 3️⃣ Lint / Static Analysis
|
||||
|
||||
We rely on two lightweight built‑in tools plus optional `golangci-lint` when
|
||||
available.
|
||||
|
||||
| Tool | Command | Description |
|
||||
|------|---------|-------------|
|
||||
| `go vet ./...` | `go vet ./...` | Detect obvious bugs, misuse of `fmt.Printf`, etc. |
|
||||
| `staticcheck ./...` | `staticcheck ./...` | Deeper static analysis (must be installed via `go install honnef.co/go/tools/cmd/staticcheck@latest`). |
|
||||
| `golangci-lint` (optional) | `golangci-lint run` | Runs a configurable suite of linters. Install with `brew install golangci-lint` or `go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest`. |
|
||||
|
||||
**Agent tip** – when `golangci-lint` is present, run it after `go vet` to catch style
|
||||
issues early. The CI pipeline (see `.github/workflows/*.yml` if added later) will
|
||||
use the same commands.
|
||||
|
||||
---
|
||||
|
||||
## 4️⃣ Code‑Style Guidelines (Go)
|
||||
|
||||
All changes must satisfy the following conventions. They are enforced by
|
||||
`go fmt`, `go vet`, and the optional `golangci-lint` config.
|
||||
|
||||
### 4.1 Formatting & Imports
|
||||
* Run `go fmt ./...` before committing. The repository uses **tab‑indented** Go code.
|
||||
* Imports are grouped in three blocks, separated by a blank line:
|
||||
1. **Standard library**
|
||||
2. **External dependencies** (e.g., `github.com/alecthomas/kong`)
|
||||
3. **Internal packages** (e.g., `github.com/yourorg/pierre/internal/...`)
|
||||
* Within each block, imports are sorted alphabetically.
|
||||
* Use `goimports` (or `golangci‑lint --fast`) to automatically fix import ordering.
|
||||
|
||||
### 4.2 Naming Conventions
|
||||
| Entity | Rule |
|
||||
|--------|------|
|
||||
| Packages | lower‑case, no underscores or hyphens. |
|
||||
| Files | snake_case (`*_test.go` for tests). |
|
||||
| Types / Structs | PascalCase, descriptive (e.g., `ReviewConfig`). |
|
||||
| Interfaces | End with `er` when appropriate (e.g., `ChatAdapter`). |
|
||||
| Variables / Constants | camelCase for locals, PascalCase for exported. |
|
||||
| Functions | PascalCase if exported, camelCase if private. |
|
||||
| Constants | Use `CamelCase` for groups, `CONST_NAME` only for truly global values. |
|
||||
| Test Functions | `TestXxx` and optionally `BenchmarkXxx`. |
|
||||
|
||||
### 4.3 Error Handling
|
||||
* Errors are **never** ignored. Use the blank identifier only when the value is truly irrelevant.
|
||||
* Wrap contextual information using `fmt.Errorf("context: %w", err)` or `errors.Wrap` if the project imports `github.com/pkg/errors` (currently not used).
|
||||
* Return errors **as soon as** they are detected – "guard clause" style.
|
||||
* In public APIs, prefer error values over panics. Panics are limited to unrecoverable
|
||||
programming errors (e.g., nil‑pointer dereference in `init`).
|
||||
|
||||
### 4.4 Documentation & Comments
|
||||
* Exported identifiers **must** have a preceding doc comment beginning with the name (e.g., `// JudgePR reviews a PR and returns comments.`).
|
||||
* Inline comments should be sentence‑case and end with a period.
|
||||
* Use `//go:generate` directives sparingly; they must be accompanied by a test that ensures the generated file is up‑to‑date.
|
||||
|
||||
### 4.5 Testing Practices
|
||||
* Keep tests **table‑driven** where possible; this yields concise, readable test suites.
|
||||
* Use the `testing` package only; avoid third‑party test frameworks.
|
||||
* Prefer `t.Fatalf` for fatal errors and `t.Errorf` for non‑fatal assertions.
|
||||
* When comparing complex structs, use `github.com/google/go-cmp/cmp` (already in `go.mod`).
|
||||
* Test coverage should be **≥ 80 %** for new code.
|
||||
* All tests must be **deterministic** – no reliance on external services; use mocks or fakes.
|
||||
|
||||
### 4.6 Dependency Management
|
||||
* All dependencies are managed via Go modules (`go.mod`).
|
||||
* Run `go mod tidy` after adding/removing imports.
|
||||
* Do not commit `vendor/` unless a vendoring strategy is explicitly adopted.
|
||||
|
||||
### 4.7 Logging & Output
|
||||
* Use the standard library `log` package for user‑visible output.
|
||||
* For structured logs (debug mode), wrap with a small helper that respects the `--debug` flag.
|
||||
* CLI output must be **machine‑parsable** when `--json-output` is set (future feature).
|
||||
|
||||
---
|
||||
|
||||
## 5️⃣ Git & Pull‑Request Workflow (for agents)
|
||||
|
||||
1. **Branch naming** – `feature/<short‑desc>` or `bugfix/<short‑desc>`.
|
||||
2. **Commits** – One logical change per commit, with a concise subject line (<50 chars) and an optional body explaining *why* the change was needed.
|
||||
3. **CI** – Every PR must pass `go test ./...`, `go vet ./...`, and `golangci‑lint run` (if configured).
|
||||
4. **Review** – Agents should add comments only via the LLM; do not edit code generated by the LLM unless a test fails.
|
||||
5. **Rebasing** – Keep a linear history; use `git rebase -i` locally before merging.
|
||||
|
||||
---
|
||||
|
||||
## 6️⃣ Cursor / Copilot Rules (None Present)
|
||||
|
||||
The repository does not contain a `.cursor` directory or a `.github/copilot‑instructions.md`
|
||||
file. If such files are added in the future, agents should read them and incorporate
|
||||
any additional constraints into this document.
|
||||
|
||||
---
|
||||
|
||||
## 7️⃣ Quick Reference Cheat‑Sheet (for agents)
|
||||
|
||||
```bash
|
||||
# Build
|
||||
go build -o bin/pierre ./cmd/pierre/main.go
|
||||
|
||||
# Run single test (replace TestFoo)
|
||||
go test ./... -run ^TestFoo$ -v
|
||||
|
||||
# Lint & Vet
|
||||
go vet ./...
|
||||
staticcheck ./...
|
||||
# optional
|
||||
golangci-lint run
|
||||
|
||||
# Format & Imports
|
||||
go fmt ./...
|
||||
goimports -w .
|
||||
|
||||
# Module tidy
|
||||
go mod tidy
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
*End of AGENTS.md*
|
||||
77
GEMINI.md
77
GEMINI.md
@@ -1,77 +0,0 @@
|
||||
# Pierre Bot
|
||||
|
||||
Pierre Bot is an intelligent, AI-powered code review assistant designed for Bitbucket Server/Data Center. It automates the initial pass of code review by analyzing Pull Request diffs and identifying potential bugs, logic errors, and style issues using modern LLMs (Google Gemini 2.0 Flash or Ollama).
|
||||
|
||||
## Project Overview
|
||||
|
||||
* **Type:** Go CLI Application
|
||||
* **Core Function:** Fetches PR diffs from Bitbucket -> Sends to LLM -> Prints structured review comments.
|
||||
* **Key Technologies:**
|
||||
* **Language:** Go (1.25+)
|
||||
* **AI SDKs:** `google/generative-ai-go`, `ollama/ollama`
|
||||
* **CLI Framework:** `alecthomas/kong`
|
||||
|
||||
## Architecture
|
||||
|
||||
The project follows a standard Go project layout:
|
||||
|
||||
* **`cmd/pierre/`**: Contains the `main.go` entry point. It handles configuration parsing (flags, env vars, file), initializes adapters, and orchestrates the application flow.
|
||||
* **`internal/pierre/`**: Contains the core business logic.
|
||||
* `judge.go`: Defines the `JudgePR` function which prepares the system prompt and context for the LLM.
|
||||
* **`internal/chatter/`**: Abstraction layer for LLM providers.
|
||||
* `gemini.go`: Implements the `ChatAdapter` interface for Google Gemini. notably includes **dynamic JSON schema generation** via reflection (`schemaFromType`) to enforce structured output from the model.
|
||||
* `ollama.go`: Implements the `ChatAdapter` for Ollama (local models).
|
||||
* **`internal/gitadapters/`**: Abstraction for Version Control Systems.
|
||||
* `bitbucket.go`: Client for fetching PR diffs from Bitbucket Server.
|
||||
|
||||
## Building and Running
|
||||
|
||||
### Prerequisites
|
||||
|
||||
* Go 1.25 or later
|
||||
* Access to a Bitbucket Server instance
|
||||
* API Key for Google Gemini (or a running Ollama instance)
|
||||
|
||||
### Build
|
||||
|
||||
```bash
|
||||
go build -o pierre ./cmd/pierre/main.go
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
Configuration is handled via `kong` and supports a hierarchy: **Flags > Env Vars > Config File**.
|
||||
|
||||
**1. Environment Variables:**
|
||||
|
||||
* `BITBUCKET_URL`: Base URL of the Bitbucket instance.
|
||||
* `BITBUCKET_TOKEN`: Personal Access Token (HTTP) for Bitbucket.
|
||||
* `LLM_PROVIDER`: `gemini` or `ollama`.
|
||||
* `LLM_API_KEY`: API Key for Gemini.
|
||||
* `LLM_MODEL`: Model name (e.g., `gemini-2.0-flash`).
|
||||
|
||||
**2. Configuration File (`config.yaml`):**
|
||||
|
||||
See `config.example.yaml` for a template. Place it in the current directory or `~/.pierre.yaml`.
|
||||
|
||||
### Usage
|
||||
|
||||
Run the bot against a specific Pull Request:
|
||||
|
||||
```bash
|
||||
# Syntax: ./pierre [flags] <PROJECT_KEY> <REPO_SLUG> <PR_ID>
|
||||
./pierre --llm-provider=gemini --llm-model=gemini-2.0-flash MYPROJ my-repo 123
|
||||
```
|
||||
|
||||
## Development Conventions
|
||||
|
||||
* **Structured Output:** The bot relies on the LLM returning valid JSON matching the `Comment` struct. This is enforced in `internal/chatter/gemini.go` by converting the Go struct definition into a `genai.Schema`.
|
||||
* **Dependency Injection:** Adapters (`gitadapters`, `chatter`) are initialized in `main` and passed to the core logic, making testing easier.
|
||||
* **Error Handling:** strict error checks are preferred; the bot will exit if it cannot fetch the diff or initialize the AI.
|
||||
|
||||
## Key Files
|
||||
|
||||
* **`cmd/pierre/main.go`**: Application entry point and config wiring.
|
||||
* **`internal/pierre/judge.go`**: The "brain" that constructs the prompt for the AI.
|
||||
* **`internal/chatter/gemini.go`**: Gemini integration logic, including the reflection-based schema generator.
|
||||
* **`config.example.yaml`**: Reference configuration file.
|
||||
BIN
bin/pierre
Executable file
BIN
bin/pierre
Executable file
Binary file not shown.
@@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/gitadapters/bitbucket"
|
||||
@@ -37,7 +39,26 @@ type LLMConfig struct {
|
||||
Model string `help:"Model to use" env:"LLM_MODEL"`
|
||||
}
|
||||
|
||||
// ReviewConfig holds the review‑specific CLI options.
|
||||
// The `default:"60000"` tag sets an integer default of 60 KB – Kong parses the string value into the int field, which can be confusing for readers.
|
||||
type ReviewConfig struct {
|
||||
MaxChunkSize int `help:"Maximum diff chunk size in bytes" default:"60000"`
|
||||
Guidelines []string `help:"Project guidelines to prepend" sep:","`
|
||||
DisableComments bool `help:"Disable posting comments (dry run)"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
LogLevel string `help:"Log verbosity: debug, info, warn, error"`
|
||||
// Deprecated flags (no prefix). Retained for backward compatibility.
|
||||
// These will be mapped to the embedded ReviewConfig if provided.
|
||||
MaxChunkSize int `help:"Deprecated: use --review-max-chunk-size"`
|
||||
Guidelines []string `help:"Deprecated: use --review-guidelines" sep:","`
|
||||
DisableComments bool `help:"Deprecated: use --review-disable-comments"`
|
||||
|
||||
// Embedding ReviewConfig with a prefix changes flag names to `--review-…`.
|
||||
// Existing configuration files using the old flag names will need to be updated.
|
||||
// Consider keeping backwards compatibility if required.
|
||||
Review ReviewConfig `embed:"" prefix:"review-"`
|
||||
GitProvider string `help:"Git provider (bitbucket or gitea)" env:"GIT_PROVIDER"`
|
||||
Bitbucket BitbucketConfig `embed:"" prefix:"bitbucket-"`
|
||||
Gitea GiteaConfig `embed:"" prefix:"gitea-"`
|
||||
@@ -47,6 +68,7 @@ type Config struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
cfg := &Config{}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
@@ -63,6 +85,32 @@ func main() {
|
||||
kong.Configuration(kongyaml.Loader, "config.yaml", defaultConfig),
|
||||
)
|
||||
|
||||
// Configure global slog logger based on the --log-level flag
|
||||
lvl := slog.LevelInfo
|
||||
switch strings.ToLower(cfg.LogLevel) {
|
||||
case "debug":
|
||||
lvl = slog.LevelDebug
|
||||
case "info":
|
||||
lvl = slog.LevelInfo
|
||||
case "warn":
|
||||
lvl = slog.LevelWarn
|
||||
case "error":
|
||||
lvl = slog.LevelError
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: lvl}))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
// Backwards compatibility: map deprecated flag values (if any) to the embedded ReviewConfig.
|
||||
if cfg.MaxChunkSize != 0 {
|
||||
cfg.Review.MaxChunkSize = cfg.MaxChunkSize
|
||||
}
|
||||
if len(cfg.Guidelines) > 0 {
|
||||
cfg.Review.Guidelines = cfg.Guidelines
|
||||
}
|
||||
if cfg.DisableComments {
|
||||
cfg.Review.DisableComments = cfg.DisableComments
|
||||
}
|
||||
|
||||
// Auto-detect provider
|
||||
provider := cfg.GitProvider
|
||||
if provider == "" {
|
||||
@@ -117,7 +165,7 @@ func main() {
|
||||
log.Fatalf("Error initializing AI: %v", err)
|
||||
}
|
||||
|
||||
pierreService := pierre.New(ai, git)
|
||||
pierreService := pierre.New(ai, git, cfg.Review.MaxChunkSize, cfg.Review.Guidelines, cfg.Review.DisableComments)
|
||||
if err := pierreService.MakeReview(context.Background(), cfg.Repo.Owner, cfg.Repo.Repo, cfg.Repo.PRID); err != nil {
|
||||
log.Fatalf("Error during review: %v", err)
|
||||
}
|
||||
|
||||
3
go.mod
3
go.mod
@@ -7,7 +7,9 @@ require (
|
||||
github.com/alecthomas/kong v1.14.0
|
||||
github.com/alecthomas/kong-yaml v0.2.0
|
||||
github.com/google/generative-ai-go v0.20.1
|
||||
github.com/google/go-cmp v0.7.0
|
||||
github.com/ollama/ollama v0.16.0
|
||||
github.com/sashabaranov/go-openai v1.41.2
|
||||
google.golang.org/api v0.186.0
|
||||
)
|
||||
|
||||
@@ -34,7 +36,6 @@ require (
|
||||
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
|
||||
github.com/hashicorp/go-version v1.7.0 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/sashabaranov/go-openai v1.41.2 // indirect
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
|
||||
|
||||
80
internal/gitadapters/bitbucket/adapter_test.go
Normal file
80
internal/gitadapters/bitbucket/adapter_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBitbucketGetFileContentSuccess(t *testing.T) {
|
||||
const expected = "file content"
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Verify path structure
|
||||
if r.URL.Path != "/rest/api/1.0/projects/owner/repos/repo/raw/path/to/file.go" {
|
||||
t.Fatalf("unexpected URL path: %s", r.URL.Path)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(expected))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Trim trailing slash handling done in NewBitbucket
|
||||
adapter := NewBitbucket(server.URL, "")
|
||||
content, err := adapter.GetFileContent(context.Background(), "owner", "repo", "path/to/file.go", "")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if content != expected {
|
||||
t.Fatalf("expected %q, got %q", expected, content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitbucketGetFileContentError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("not found"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
adapter := NewBitbucket(server.URL, "")
|
||||
_, err := adapter.GetFileContent(context.Background(), "owner", "repo", "missing.go", "")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error for non‑200 response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitbucketGetPRHeadSHASuccess(t *testing.T) {
|
||||
const sha = "deadbeef"
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/rest/api/1.0/projects/owner/repos/repo/pull-requests/42" {
|
||||
t.Fatalf("unexpected URL: %s", r.URL.Path)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"toRef":{"latestCommit":"` + sha + `"}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
adapter := NewBitbucket(server.URL, "")
|
||||
got, err := adapter.GetPRHeadSHA(context.Background(), "owner", "repo", 42)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != sha {
|
||||
t.Fatalf("expected sha %s, got %s", sha, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitbucketGetPRHeadSHAError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("error"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
adapter := NewBitbucket(server.URL, "")
|
||||
_, err := adapter.GetPRHeadSHA(context.Background(), "owner", "repo", 1)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error for non‑200 response")
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func (b *BitbucketAdapter) GetDiff(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (diff io.ReadCloser, err error) {
|
||||
slog.Debug("Bitbucket GetDiff start", "project", projectKey, "repo", repositorySlug, "pr", pullRequestID)
|
||||
r, err := b.CreateRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
@@ -31,7 +33,7 @@ func (b *BitbucketAdapter) GetDiff(ctx context.Context, projectKey, repositorySl
|
||||
if response.StatusCode != http.StatusOK {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, response.Body)
|
||||
err = fmt.Errorf("error while fetching bitbucket diff staus %d, body %s", response.Status, sb.String())
|
||||
err = fmt.Errorf("error while fetching bitbucket diff status %d, body %s", response.StatusCode, sb.String())
|
||||
}
|
||||
|
||||
diff = response.Body
|
||||
@@ -39,6 +41,7 @@ func (b *BitbucketAdapter) GetDiff(ctx context.Context, projectKey, repositorySl
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) GetPR(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (pr PullRequest, err error) {
|
||||
slog.Debug("Bitbucket GetPR start", "project", projectKey, "repo", repositorySlug, "pr", pullRequestID)
|
||||
r, err := b.CreateRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
@@ -47,17 +50,23 @@ func (b *BitbucketAdapter) GetPR(ctx context.Context, projectKey, repositorySlug
|
||||
)
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
defer response.Body.Close() // Add this
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer response.Body.Close() // Add this
|
||||
|
||||
err = json.NewDecoder(response.Body).Decode(&pr)
|
||||
if err != nil {
|
||||
slog.Error("Bitbucket GetPR decode error", "err", err)
|
||||
return
|
||||
}
|
||||
slog.Info("Bitbucket GetPR success", "id", pullRequestID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (b *BitbucketAdapter) AddComment(ctx context.Context, owner, repo string, prID int, comment pierre.Comment) (err error) {
|
||||
slog.Debug("Bitbucket AddComment start", "owner", owner, "repo", repo, "pr", prID, "file", comment.File, "line", comment.Line)
|
||||
// pr, err := b.GetPR(ctx, owner, repo, prID)
|
||||
// if err != nil {
|
||||
// return
|
||||
@@ -86,15 +95,18 @@ func (b *BitbucketAdapter) AddComment(ctx context.Context, owner, repo string, p
|
||||
}
|
||||
|
||||
response, err := http.DefaultClient.Do(r)
|
||||
defer response.Body.Close() // Add this
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close() // Add this
|
||||
|
||||
if response.StatusCode >= 300 || response.StatusCode < 200 {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, response.Body)
|
||||
err = fmt.Errorf("error while creating comment staus %d, body %s", response.StatusCode, sb.String())
|
||||
slog.Error("Bitbucket AddComment failed", "status", response.StatusCode, "err", err)
|
||||
} else {
|
||||
slog.Info("Bitbucket AddComment succeeded", "pr", prID)
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package bitbucket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/gitadapters/baseadapter"
|
||||
@@ -18,3 +22,43 @@ func NewBitbucket(baseURL string, bearerToken string) *BitbucketAdapter {
|
||||
Rest: baseadapter.NewRest(baseURL, bearerToken),
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileContent returns the raw file content at the given ref (commit SHA) or HEAD if ref is empty.
|
||||
func (b *BitbucketAdapter) GetFileContent(ctx context.Context, projectKey, repositorySlug, path, ref string) (string, error) {
|
||||
// Use the Rest helper to build the base URL, then add the "at" query param if needed.
|
||||
r, err := b.CreateRequest(ctx, http.MethodGet, nil,
|
||||
"/projects/", projectKey, "repos", repositorySlug, "raw", path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ref != "" {
|
||||
q := r.URL.Query()
|
||||
q.Set("at", ref)
|
||||
r.URL.RawQuery = q.Encode()
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
sb := &strings.Builder{}
|
||||
io.Copy(sb, resp.Body)
|
||||
return "", fmt.Errorf("error fetching file %s status %d, body %s", path, resp.StatusCode, sb.String())
|
||||
}
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// GetPRHeadSHA fetches the PR and returns the SHA of the source (to) branch.
|
||||
func (b *BitbucketAdapter) GetPRHeadSHA(ctx context.Context, projectKey, repositorySlug string, pullRequestID int) (string, error) {
|
||||
pr, err := b.GetPR(ctx, projectKey, repositorySlug, pullRequestID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return pr.ToRef.LatestCommit, nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package gitea
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/pierre"
|
||||
@@ -24,15 +26,19 @@ func New(baseURL, token string) (*Adapter, error) {
|
||||
}
|
||||
|
||||
func (g *Adapter) GetDiff(ctx context.Context, owner, repo string, prID int) (io.ReadCloser, error) {
|
||||
slog.Debug("Gitea GetDiff start", "owner", owner, "repo", repo, "pr", prID)
|
||||
g.client.SetContext(ctx)
|
||||
diff, _, err := g.client.GetPullRequestDiff(owner, repo, int64(prID), gitea.PullRequestDiffOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(diff)), nil
|
||||
rc := io.NopCloser(bytes.NewReader(diff))
|
||||
slog.Info("Gitea GetDiff success", "owner", owner, "repo", repo, "pr", prID, "bytes", len(diff))
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
func (g *Adapter) AddComment(ctx context.Context, owner, repo string, prID int, comment pierre.Comment) error {
|
||||
slog.Debug("Gitea AddComment start", "owner", owner, "repo", repo, "pr", prID, "file", comment.File, "line", comment.Line)
|
||||
g.client.SetContext(ctx)
|
||||
opts := gitea.CreatePullReviewOptions{
|
||||
State: gitea.ReviewStateComment,
|
||||
@@ -45,5 +51,39 @@ func (g *Adapter) AddComment(ctx context.Context, owner, repo string, prID int,
|
||||
},
|
||||
}
|
||||
_, _, err := g.client.CreatePullReview(owner, repo, int64(prID), opts)
|
||||
if err != nil {
|
||||
slog.Error("Gitea AddComment failed", "err", err)
|
||||
return err
|
||||
}
|
||||
slog.Info("Gitea AddComment succeeded", "pr", prID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFileContent returns the file content at a given path and ref (commit SHA).
|
||||
func (g *Adapter) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error) {
|
||||
slog.Debug("Gitea GetFileContent start", "owner", owner, "repo", repo, "path", path, "ref", ref)
|
||||
g.client.SetContext(ctx)
|
||||
// The SDK's GetFile returns the raw bytes of the file.
|
||||
data, _, err := g.client.GetFile(owner, repo, ref, path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
slog.Info("Gitea GetFileContent success", "bytes", len(data))
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// GetPRHeadSHA fetches the pull request and returns the head commit SHA.
|
||||
func (g *Adapter) GetPRHeadSHA(ctx context.Context, owner, repo string, prID int) (string, error) {
|
||||
slog.Debug("Gitea GetPRHeadSHA start", "owner", owner, "repo", repo, "pr", prID)
|
||||
g.client.SetContext(ctx)
|
||||
// GetPullRequest returns the detailed PR information.
|
||||
pr, _, err := g.client.GetPullRequest(owner, repo, int64(prID))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if pr == nil || pr.Head == nil {
|
||||
return "", fmt.Errorf("pull request %d has no head information", prID)
|
||||
}
|
||||
slog.Info("Gitea GetPRHeadSHA success", "sha", pr.Head.Sha)
|
||||
return pr.Head.Sha, nil
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package pierre
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
)
|
||||
|
||||
// DefaultChunkSize is the fallback maximum size (in bytes) for a diff chunk when no explicit value is configured.
|
||||
const DefaultChunkSize = 60000
|
||||
|
||||
type Comment struct {
|
||||
File string `json:"file"`
|
||||
Line int `json:"line"`
|
||||
@@ -15,25 +21,140 @@ type Comment struct {
|
||||
}
|
||||
|
||||
func (s *Service) judgePR(ctx context.Context, diff io.Reader) (comments []Comment, err error) {
|
||||
slog.Info("judgePR started")
|
||||
diffBytes, err := io.ReadAll(diff)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read diff: %w", err)
|
||||
}
|
||||
err = s.chat.GenerateStructured(ctx, []chatter.Message{
|
||||
{
|
||||
Role: chatter.RoleSystem,
|
||||
Content: `
|
||||
You are a very strict senior software architect.
|
||||
You review **only** newly added or modified lines in a unified diff, together with the immediate hunk context.
|
||||
You do **not** report issues that appear **solely** in deleted lines (“-”) or that have already been fixed by the change.
|
||||
No comments are made on pure formatting/whitespace changes or reordering that does not alter the program’s behavior.
|
||||
`,
|
||||
},
|
||||
{
|
||||
Role: chatter.RoleUser,
|
||||
Content: fmt.Sprintf("Hello please review my PR. Write comments where improvements are necessary in new lines.\n Here is the git diff of it: %s", string(diffBytes)),
|
||||
},
|
||||
}, &comments)
|
||||
|
||||
// Determine chunk size (use default if not set)
|
||||
maxSize := s.maxChunkSize
|
||||
if maxSize <= 0 {
|
||||
maxSize = DefaultChunkSize // default 60KB ~ 15k tokens
|
||||
}
|
||||
|
||||
chunks := splitDiffIntoChunks(diffBytes, maxSize)
|
||||
allComments := []Comment{}
|
||||
|
||||
// Build optional guidelines text (added as a separate section with a clear delimiter)
|
||||
guidelinesText := ""
|
||||
if len(s.guidelines) > 0 {
|
||||
// Two newlines ensure the guidelines start on a fresh paragraph.
|
||||
guidelinesText = "\n\nProject guidelines:\n"
|
||||
for _, g := range s.guidelines {
|
||||
guidelinesText += "- " + g + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
// System prompt that instructs the LLM precisely.
|
||||
baseSystem := strings.TrimSpace(`
|
||||
You are a strict senior software architect.
|
||||
Only comment on newly added or modified lines in the diff; ignore deletions, pure formatting, or re‑ordering that does not change behavior.
|
||||
For each issue output a JSON object with fields "file", "line", and "message" (message should be concise, ≤2 sentences, and actionable).
|
||||
If project guidelines are provided, treat them as hard rules that must be respected.`) + guidelinesText
|
||||
|
||||
for i, chunk := range chunks {
|
||||
// Include the chunk identifier in the system message only if there are multiple chunks.
|
||||
systemContent := baseSystem
|
||||
if len(chunks) > 1 {
|
||||
systemContent = fmt.Sprintf("%s\nChunk %d of %d.", baseSystem, i+1, len(chunks))
|
||||
}
|
||||
userContent := chunk
|
||||
|
||||
var chunkComments []Comment
|
||||
err = s.chat.GenerateStructured(ctx, []chatter.Message{{
|
||||
Role: chatter.RoleSystem,
|
||||
Content: systemContent,
|
||||
}, {
|
||||
Role: chatter.RoleUser,
|
||||
Content: userContent,
|
||||
}}, &chunkComments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allComments = append(allComments, chunkComments...)
|
||||
}
|
||||
|
||||
// De‑duplicate comments (keyed by file:line)
|
||||
unique := make(map[string]Comment)
|
||||
for _, c := range allComments {
|
||||
key := fmt.Sprintf("%s:%d", c.File, c.Line)
|
||||
unique[key] = c
|
||||
}
|
||||
for _, v := range unique {
|
||||
comments = append(comments, v)
|
||||
}
|
||||
slog.Info("judgePR finished", "comments", len(comments))
|
||||
return
|
||||
}
|
||||
|
||||
// splitDiffIntoChunks splits a diff into chunks that do not exceed maxSize bytes.
|
||||
// It tries to split on file boundaries ("diff --git") first, then on hunk boundaries (@@),
|
||||
// and finally on a hard byte limit.
|
||||
func splitDiffIntoChunks(diff []byte, maxSize int) []string {
|
||||
// Preserve the file header for each chunk when a single file's diff is split across multiple chunks.
|
||||
// The header is the portion before the first hunk marker "@@" (including the "diff --git" line).
|
||||
// When we need to split by hunks, we prepend this header to every resulting sub‑chunk.
|
||||
|
||||
if len(diff) <= maxSize {
|
||||
return []string{string(diff)}
|
||||
}
|
||||
content := string(diff)
|
||||
// Split by file headers
|
||||
parts := strings.Split(content, "\ndiff --git ")
|
||||
chunks := []string{}
|
||||
var current strings.Builder
|
||||
for idx, part := range parts {
|
||||
seg := part
|
||||
if idx != 0 {
|
||||
// Preserve the leading newline that was removed by Split
|
||||
seg = "\n" + "diff --git " + part
|
||||
}
|
||||
if current.Len()+len(seg) > maxSize && current.Len() > 0 {
|
||||
chunks = append(chunks, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
if len(seg) > maxSize {
|
||||
// Determine if there is a hunk marker. If not, fall back to simple size‑based chunking.
|
||||
headerEnd := strings.Index(seg, "\n@@ ")
|
||||
if headerEnd == -1 {
|
||||
// No hunk marker – split purely by size.
|
||||
remaining := seg
|
||||
for len(remaining) > maxSize {
|
||||
chunks = append(chunks, remaining[:maxSize])
|
||||
remaining = remaining[maxSize:]
|
||||
}
|
||||
current.WriteString(remaining)
|
||||
continue
|
||||
}
|
||||
// Preserve the header up to the first hunk.
|
||||
header := seg[:headerEnd+1] // include newline before "@@"
|
||||
// Split the rest of the segment into hunks (excluding the header part).
|
||||
hunks := strings.Split(strings.TrimPrefix(seg, header), "\n@@ ")
|
||||
for _, h := range hunks {
|
||||
// Reconstruct each hunk with its header and "@@ " prefix.
|
||||
hseg := header + "@@ " + h
|
||||
if current.Len()+len(hseg) > maxSize && current.Len() > 0 {
|
||||
chunks = append(chunks, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
if len(hseg) > maxSize {
|
||||
// If a single hunk exceeds maxSize, split it further.
|
||||
for len(hseg) > maxSize {
|
||||
chunks = append(chunks, hseg[:maxSize])
|
||||
hseg = hseg[maxSize:]
|
||||
}
|
||||
current.WriteString(hseg)
|
||||
} else {
|
||||
current.WriteString(hseg)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
current.WriteString(seg)
|
||||
}
|
||||
}
|
||||
if current.Len() > 0 {
|
||||
chunks = append(chunks, current.String())
|
||||
}
|
||||
return chunks
|
||||
}
|
||||
|
||||
141
internal/pierre/judge_test.go
Normal file
141
internal/pierre/judge_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package pierre
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
// mockChat implements the ChatAdapter interface for testing.
|
||||
type mockChat struct{ callCount int }
|
||||
|
||||
func (m *mockChat) GenerateStructured(ctx context.Context, msgs []chatter.Message, target interface{}) error {
|
||||
m.callCount++
|
||||
if cSlice, ok := target.(*[]Comment); ok {
|
||||
*cSlice = []Comment{{File: "file.go", Line: 1, Message: "test comment"}}
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockChat) GetProviderName() string { return "mock" }
|
||||
|
||||
// mockGit implements the GitAdapter interface for testing.
|
||||
type mockGit struct{}
|
||||
|
||||
func (g *mockGit) GetDiff(ctx context.Context, owner, repo string, prID int) (io.ReadCloser, error) {
|
||||
diff := "diff --git a/file1.go b/file1.go\n+line1\n" + "diff --git a/file2.go b/file2.go\n+line2\n"
|
||||
return io.NopCloser(bytes.NewReader([]byte(diff))), nil
|
||||
}
|
||||
|
||||
func (g *mockGit) AddComment(ctx context.Context, owner, repo string, prID int, comment Comment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *mockGit) GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error) {
|
||||
// For tests, return a simple placeholder content.
|
||||
return "package main\n\nfunc placeholder() {}", nil
|
||||
}
|
||||
|
||||
func (g *mockGit) GetPRHeadSHA(ctx context.Context, owner, repo string, prID int) (string, error) {
|
||||
return "dummysha", nil
|
||||
}
|
||||
|
||||
func TestSplitDiffIntoChunks(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
diff string
|
||||
maxSize int
|
||||
wantChunks int // 0 means we don't assert exact count
|
||||
wantPrefixes []string
|
||||
checkRecombine bool
|
||||
}{
|
||||
{
|
||||
name: "small diff",
|
||||
diff: "diff --git a/file1.txt b/file1.txt\n+added line\n",
|
||||
maxSize: 1000,
|
||||
wantChunks: 1,
|
||||
wantPrefixes: []string{"diff --git a/file1.txt"},
|
||||
checkRecombine: true,
|
||||
},
|
||||
{
|
||||
name: "multiple files",
|
||||
diff: "diff --git a/file1.txt b/file1.txt\n+added line 1\n" +
|
||||
"diff --git a/file2.txt b/file2.txt\n+added line 2\n",
|
||||
maxSize: 50,
|
||||
wantChunks: 2,
|
||||
wantPrefixes: []string{"diff --git a/file1.txt", "diff --git a/file2.txt"},
|
||||
checkRecombine: false,
|
||||
},
|
||||
{
|
||||
name: "large single file",
|
||||
diff: func() string {
|
||||
line := "+very long added line that will be repeated many times to exceed the chunk size\n"
|
||||
return "diff --git a/large.txt b/large.txt\n" + strings.Repeat(line, 200)
|
||||
}(),
|
||||
maxSize: 500,
|
||||
wantChunks: 0,
|
||||
wantPrefixes: nil,
|
||||
checkRecombine: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
chunks := splitDiffIntoChunks([]byte(tc.diff), tc.maxSize)
|
||||
if tc.wantChunks > 0 && len(chunks) != tc.wantChunks {
|
||||
t.Fatalf("expected %d chunks, got %d", tc.wantChunks, len(chunks))
|
||||
}
|
||||
for i, prefix := range tc.wantPrefixes {
|
||||
if i >= len(chunks) {
|
||||
t.Fatalf("missing chunk %d for prefix check", i)
|
||||
}
|
||||
trimmed := strings.TrimPrefix(chunks[i], "\n")
|
||||
if !strings.HasPrefix(trimmed, prefix) {
|
||||
t.Fatalf("chunk %d does not start with expected prefix %q: %s", i, prefix, chunks[i])
|
||||
}
|
||||
}
|
||||
for i, c := range chunks {
|
||||
if tc.maxSize > 0 && len(c) > tc.maxSize {
|
||||
t.Fatalf("chunk %d exceeds max size %d: %d", i, tc.maxSize, len(c))
|
||||
}
|
||||
}
|
||||
if tc.checkRecombine {
|
||||
recombined := strings.Join(chunks, "")
|
||||
if diff := cmp.Diff(tc.diff, recombined); diff != "" {
|
||||
t.Fatalf("recombined diff differs:\n%s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJudgePR_ChunkAggregationAndDeduplication(t *testing.T) {
|
||||
chatMock := &mockChat{}
|
||||
svc := &Service{
|
||||
maxChunkSize: 50,
|
||||
guidelines: nil,
|
||||
git: &mockGit{},
|
||||
chat: chatMock,
|
||||
}
|
||||
diffReader, err := svc.git.GetDiff(context.Background(), "", "", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get diff: %v", err)
|
||||
}
|
||||
defer diffReader.Close()
|
||||
comments, err := svc.judgePR(context.Background(), diffReader)
|
||||
if err != nil {
|
||||
t.Fatalf("judgePR error: %v", err)
|
||||
}
|
||||
if got, want := len(comments), 1; got != want {
|
||||
t.Fatalf("expected %d comment after deduplication, got %d", want, got)
|
||||
}
|
||||
if chatMock.callCount != 2 {
|
||||
t.Fatalf("expected mockChat to be called for each chunk (2), got %d", chatMock.callCount)
|
||||
}
|
||||
}
|
||||
@@ -7,21 +7,34 @@ import (
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
)
|
||||
|
||||
// Service holds the core collaborators and configuration for Pierre.
|
||||
// The order of the fields is intentional: configuration fields first (used
|
||||
// during initialization) followed by the adapters. This prevents accidental
|
||||
// changes to the serialized layout if encoding/gob or encoding/json is used
|
||||
// elsewhere in the future.
|
||||
type Service struct {
|
||||
maxChunkSize int
|
||||
guidelines []string
|
||||
disableComments bool
|
||||
git GitAdapter
|
||||
chat ChatAdapter
|
||||
}
|
||||
|
||||
func New(chat ChatAdapter, git GitAdapter) *Service {
|
||||
func New(chat ChatAdapter, git GitAdapter, maxChunkSize int, guidelines []string, disableComments bool) *Service {
|
||||
return &Service{
|
||||
git: git,
|
||||
chat: chat,
|
||||
maxChunkSize: maxChunkSize,
|
||||
guidelines: guidelines,
|
||||
disableComments: disableComments,
|
||||
}
|
||||
}
|
||||
|
||||
type GitAdapter interface {
|
||||
GetDiff(ctx context.Context, owner, repo string, prID int) (io.ReadCloser, error)
|
||||
AddComment(ctx context.Context, owner, repo string, prID int, comment Comment) error
|
||||
GetFileContent(ctx context.Context, owner, repo, path, ref string) (string, error)
|
||||
GetPRHeadSHA(ctx context.Context, owner, repo string, prID int) (string, error)
|
||||
}
|
||||
|
||||
type ChatAdapter interface {
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
)
|
||||
|
||||
func (s *Service) MakeReview(ctx context.Context, organisation string, repo string, prID int) error {
|
||||
@@ -20,19 +22,65 @@ func (s *Service) MakeReview(ctx context.Context, organisation string, repo stri
|
||||
return fmt.Errorf("error judging PR: %w", err)
|
||||
}
|
||||
|
||||
// ---------- Sanity‑check step (always enabled) ----------
|
||||
headSHA, err := s.git.GetPRHeadSHA(ctx, organisation, repo, prID)
|
||||
if err != nil {
|
||||
log.Printf("warning: could not fetch PR head SHA (%v); skipping sanity check", err)
|
||||
} else {
|
||||
filtered := []Comment{}
|
||||
for _, c := range comments {
|
||||
// Retrieve full file content at the PR head
|
||||
fileContent, fErr := s.git.GetFileContent(ctx, organisation, repo, c.File, headSHA)
|
||||
if fErr != nil {
|
||||
log.Printf("failed to fetch file %s: %v – keeping original comment", c.File, fErr)
|
||||
filtered = append(filtered, c)
|
||||
continue
|
||||
}
|
||||
|
||||
// Build a simple sanity‑check prompt
|
||||
systemPrompt := `You are a senior software architect. Given the full source code of a file and a review comment that refers to it, decide whether the comment is useful. Return JSON with fields "useful" (bool) and "reason" (short explanation, ≤2 sentences).`
|
||||
userPrompt := fmt.Sprintf("File content:\n%s\n\nComment:\n%s", fileContent, c.Message)
|
||||
|
||||
type sanityResult struct {
|
||||
Useful bool `json:"useful"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
var res sanityResult
|
||||
if err := s.chat.GenerateStructured(ctx, []chatter.Message{{Role: chatter.RoleSystem, Content: systemPrompt}, {Role: chatter.RoleUser, Content: userPrompt}}, &res); err != nil {
|
||||
log.Printf("sanity check error for %s:%d: %v – keeping comment", c.File, c.Line, err)
|
||||
filtered = append(filtered, c)
|
||||
continue
|
||||
}
|
||||
if res.Useful {
|
||||
// Optionally annotate the comment with the reason for debugging
|
||||
c.Message = fmt.Sprintf("%s (Reason: %s)", c.Message, res.Reason)
|
||||
filtered = append(filtered, c)
|
||||
} else {
|
||||
log.Printf("comment on %s:%d discarded: %s", c.File, c.Line, res.Reason)
|
||||
}
|
||||
}
|
||||
comments = filtered
|
||||
}
|
||||
|
||||
fmt.Printf("Analysis complete. Found %d issues.\n---\n", len(comments))
|
||||
|
||||
model := s.chat.GetProviderName()
|
||||
|
||||
for _, c := range comments {
|
||||
c.Message = fmt.Sprintf("%s (Generated by: %s)", c.Message, model)
|
||||
|
||||
if s.disableComments {
|
||||
// Dry‑run: just log what would have been posted.
|
||||
log.Printf("dry‑run: %s:%d => %s", c.File, c.Line, c.Message)
|
||||
} else {
|
||||
// Normal mode: print to stdout and post the comment to the VCS.
|
||||
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
|
||||
c.File, c.Line, c.Message, "---")
|
||||
|
||||
if err := s.git.AddComment(ctx, organisation, repo, prID, c); err != nil {
|
||||
log.Printf("Failed to add comment: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user