feat: gitea client
This commit is contained in:
@@ -2,25 +2,32 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/chatter"
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/gitadapters"
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/gitadapters/gitea"
|
||||
"bitbucket.bit.admin.ch/scm/~u80859501/pierre-bot/internal/pierre"
|
||||
"github.com/alecthomas/kong"
|
||||
kongyaml "github.com/alecthomas/kong-yaml"
|
||||
)
|
||||
|
||||
type BitbucketConfig struct {
|
||||
BaseURL string `help:"Bitbucket Base URL (e.g. https://bitbucket.example.com)" required:"" env:"BITBUCKET_URL"`
|
||||
BaseURL string `help:"Bitbucket Base URL (e.g. https://bitbucket.example.com)" env:"BITBUCKET_URL"`
|
||||
Token string `help:"Bearer Token" env:"BITBUCKET_TOKEN"`
|
||||
// Positional arguments
|
||||
Project string `arg:"" help:"Project Key (e.g. PROJ)" env:"BITBUCKET_PROJECT"`
|
||||
Repo string `arg:"" help:"Repository Slug" env:"BITBUCKET_REPO"`
|
||||
PRID int `arg:"" help:"Pull Request ID" name:"pr"`
|
||||
}
|
||||
|
||||
type GiteaConfig struct {
|
||||
BaseURL string `help:"Gitea Base URL (e.g. https://gitea.com)" env:"GITEA_URL"`
|
||||
Token string `help:"API Token" env:"GITEA_TOKEN"`
|
||||
}
|
||||
|
||||
type RepoArgs struct {
|
||||
Owner string `arg:"" help:"Project Key or Owner" env:"PIERRE_OWNER"`
|
||||
Repo string `arg:"" help:"Repository Slug" env:"PIERRE_REPO"`
|
||||
PRID int `arg:"" help:"Pull Request ID" name:"pr"`
|
||||
}
|
||||
|
||||
type LLMConfig struct {
|
||||
@@ -31,9 +38,12 @@ type LLMConfig struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Bitbucket BitbucketConfig `embed:"" prefix:"bitbucket-"`
|
||||
LLM LLMConfig `embed:"" prefix:"llm-"`
|
||||
Config kong.ConfigFlag `help:"Path to a YAML config file"`
|
||||
GitProvider string `help:"Git provider (bitbucket or gitea)" env:"GIT_PROVIDER"`
|
||||
Bitbucket BitbucketConfig `embed:"" prefix:"bitbucket-"`
|
||||
Gitea GiteaConfig `embed:"" prefix:"gitea-"`
|
||||
Repo RepoArgs `embed:""`
|
||||
LLM LLMConfig `embed:"" prefix:"llm-"`
|
||||
Config kong.ConfigFlag `help:"Path to a YAML config file"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -48,22 +58,46 @@ func main() {
|
||||
// Parse flags, env vars, and config files
|
||||
kong.Parse(cfg,
|
||||
kong.Name("pierre"),
|
||||
kong.Description("AI-powered Pull Request reviewer for Bitbucket"),
|
||||
kong.Description("AI-powered Pull Request reviewer"),
|
||||
kong.UsageOnError(),
|
||||
kong.Configuration(kongyaml.Loader, "config.yaml", defaultConfig),
|
||||
)
|
||||
|
||||
// Initialize Bitbucket Adapter
|
||||
bitbucket := gitadapters.NewBitbucket(cfg.Bitbucket.BaseURL, cfg.Bitbucket.Token)
|
||||
// Auto-detect provider
|
||||
provider := cfg.GitProvider
|
||||
if provider == "" {
|
||||
if cfg.Bitbucket.BaseURL != "" && cfg.Gitea.BaseURL == "" {
|
||||
provider = "bitbucket"
|
||||
} else if cfg.Gitea.BaseURL != "" && cfg.Bitbucket.BaseURL == "" {
|
||||
provider = "gitea"
|
||||
} else if cfg.Bitbucket.BaseURL != "" && cfg.Gitea.BaseURL != "" {
|
||||
log.Fatal("Multiple git providers configured. Please specify one using --git-provider.")
|
||||
} else {
|
||||
log.Fatal("No git provider configured. Please provide Bitbucket or Gitea configuration.")
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch Diff using positional args
|
||||
diff, err := bitbucket.GetDiff(cfg.Bitbucket.Project, cfg.Bitbucket.Repo, cfg.Bitbucket.PRID)
|
||||
if err != nil {
|
||||
log.Fatalf("Error fetching diff: %v", err)
|
||||
var git pierre.GitAdapter
|
||||
|
||||
switch provider {
|
||||
case "bitbucket":
|
||||
if cfg.Bitbucket.BaseURL == "" {
|
||||
log.Fatal("Bitbucket Base URL is required when using bitbucket provider.")
|
||||
}
|
||||
git = gitadapters.NewBitbucket(cfg.Bitbucket.BaseURL, cfg.Bitbucket.Token)
|
||||
case "gitea":
|
||||
if cfg.Gitea.BaseURL == "" {
|
||||
log.Fatal("Gitea Base URL is required when using gitea provider.")
|
||||
}
|
||||
git, err = gitea.New(cfg.Gitea.BaseURL, cfg.Gitea.Token)
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing Gitea adapter: %v", err)
|
||||
}
|
||||
default:
|
||||
log.Fatalf("Unknown git provider: %s", provider)
|
||||
}
|
||||
|
||||
// Initialize AI Adapter
|
||||
|
||||
var ai pierre.ChatAdapter
|
||||
|
||||
switch cfg.LLM.Provider {
|
||||
@@ -79,16 +113,6 @@ func main() {
|
||||
log.Fatalf("Error initializing AI: %v", err)
|
||||
}
|
||||
|
||||
// Run Logic
|
||||
comments, err := pierre.JudgePR(context.Background(), ai, diff)
|
||||
if err != nil {
|
||||
log.Fatalf("Error judging PR: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Analysis complete. Found %d issues.\n---\n", len(comments))
|
||||
|
||||
for _, c := range comments {
|
||||
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
|
||||
c.File, c.Line, c.Message, "---")
|
||||
}
|
||||
pierreService := pierre.New(ai, git)
|
||||
pierreService.MakeReview(context.Background(), cfg.Repo.Owner, cfg.Repo.Repo, cfg.Repo.PRID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user