feat: implement proto-gen platform
- Config loader with env var expansion and validation - SQLite database with tasks and toolchain_versions tables - Gitea REST API client for repository and branch operations - K8s client for Job lifecycle management (with noop fallback for local dev) - REST API: tasks CRUD, retry, plugins, version management - Worker/scheduler with polling, concurrency control, and job tracking - Static frontend: generate, tasks, settings pages - Toolchain images: Go and Node.js Dockerfiles with entrypoint scripts - Helm chart with deployment, service, configmap, ingress, PVC - Multi-stage Dockerfile for service - Unit tests for config, database, gitclient, k8s, api, worker - E2E tests for idempotency, version switch, failure recovery
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"proto-gen/internal/config"
|
||||
"proto-gen/internal/models"
|
||||
)
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, msg string) {
|
||||
writeJSON(w, status, map[string]string{"error": msg})
|
||||
}
|
||||
|
||||
type CreateTaskRequest struct {
|
||||
Language string `json:"language"`
|
||||
ProtoRepo string `json:"proto_repo"`
|
||||
ProtoBranch string `json:"proto_branch"`
|
||||
TargetRepo string `json:"target_repo"`
|
||||
}
|
||||
|
||||
func (s *Server) handleCreateTask(w http.ResponseWriter, r *http.Request) {
|
||||
var req CreateTaskRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Language == "" || req.ProtoRepo == "" || req.ProtoBranch == "" {
|
||||
writeError(w, http.StatusBadRequest, "language, proto_repo, and proto_branch are required")
|
||||
return
|
||||
}
|
||||
|
||||
plugin := s.cfg.GetPluginByLanguage(req.Language)
|
||||
if plugin == nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("unsupported language: %s", req.Language))
|
||||
return
|
||||
}
|
||||
|
||||
if req.TargetRepo == "" {
|
||||
req.TargetRepo = strings.ReplaceAll(
|
||||
strings.ReplaceAll(s.cfg.TargetRepoTemplate, "{proto_repo}", req.ProtoRepo),
|
||||
"{lang}", req.Language,
|
||||
)
|
||||
}
|
||||
|
||||
branch, err := s.git.GetBranch(req.ProtoRepo, req.ProtoRepo, req.ProtoBranch)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("branch not found: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
hasRunning, err := s.db.HasRunningTask(req.TargetRepo)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal error")
|
||||
return
|
||||
}
|
||||
if hasRunning {
|
||||
writeError(w, http.StatusConflict, "target repo already has a running task")
|
||||
return
|
||||
}
|
||||
|
||||
task := &models.Task{
|
||||
ID: uuid.New().String(),
|
||||
Status: models.StatusPending,
|
||||
Language: req.Language,
|
||||
ProtoRepo: req.ProtoRepo,
|
||||
ProtoBranch: req.ProtoBranch,
|
||||
ProtoCommit: branch.Commit,
|
||||
TargetRepo: req.TargetRepo,
|
||||
ToolchainImage: s.cfg.ToolchainRegistry + "/" + plugin.Image,
|
||||
}
|
||||
|
||||
if err := s.db.CreateTask(task); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to create task")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, task)
|
||||
}
|
||||
|
||||
func (s *Server) handleListTasks(w http.ResponseWriter, r *http.Request) {
|
||||
status := r.URL.Query().Get("status")
|
||||
limit := 50
|
||||
if l := r.URL.Query().Get("limit"); l != "" {
|
||||
if parsed, err := strconv.Atoi(l); err == nil && parsed > 0 {
|
||||
limit = parsed
|
||||
}
|
||||
}
|
||||
offset := 0
|
||||
if o := r.URL.Query().Get("offset"); o != "" {
|
||||
if parsed, err := strconv.Atoi(o); err == nil && parsed >= 0 {
|
||||
offset = parsed
|
||||
}
|
||||
}
|
||||
|
||||
tasks, err := s.db.ListTasks(status, limit, offset)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to list tasks")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, tasks)
|
||||
}
|
||||
|
||||
func (s *Server) handleGetTask(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
task, err := s.db.GetTask(id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, "task not found")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, task)
|
||||
}
|
||||
|
||||
func (s *Server) handleRetryTask(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
task, err := s.db.GetTask(id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, "task not found")
|
||||
return
|
||||
}
|
||||
|
||||
if task.Status != models.StatusFailed {
|
||||
writeError(w, http.StatusBadRequest, "can only retry failed tasks")
|
||||
return
|
||||
}
|
||||
|
||||
hasRunning, err := s.db.HasRunningTask(task.TargetRepo)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal error")
|
||||
return
|
||||
}
|
||||
if hasRunning {
|
||||
writeError(w, http.StatusConflict, "target repo already has a running task")
|
||||
return
|
||||
}
|
||||
|
||||
task.Status = models.StatusPending
|
||||
task.Error = ""
|
||||
task.Log = ""
|
||||
task.Retries++
|
||||
if err := s.db.UpdateTask(task); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to retry task")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, task)
|
||||
}
|
||||
|
||||
func (s *Server) handleListPlugins(w http.ResponseWriter, r *http.Request) {
|
||||
var plugins []config.PluginConfig
|
||||
for _, p := range s.cfg.Plugins {
|
||||
plugins = append(plugins, p)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, plugins)
|
||||
}
|
||||
|
||||
func (s *Server) handleGetPluginVersion(w http.ResponseWriter, r *http.Request) {
|
||||
lang := chi.URLParam(r, "lang")
|
||||
plugin := s.cfg.GetPluginByLanguage(lang)
|
||||
if plugin == nil {
|
||||
writeError(w, http.StatusNotFound, "plugin not found")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, plugin)
|
||||
}
|
||||
|
||||
func (s *Server) handleUpdatePluginVersion(w http.ResponseWriter, r *http.Request) {
|
||||
lang := chi.URLParam(r, "lang")
|
||||
plugin := s.cfg.GetPluginByLanguage(lang)
|
||||
if plugin == nil {
|
||||
writeError(w, http.StatusNotFound, "plugin not found")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Image string `json:"image"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
if req.Image == "" {
|
||||
writeError(w, http.StatusBadRequest, "image is required")
|
||||
return
|
||||
}
|
||||
|
||||
plugin.Image = req.Image
|
||||
writeJSON(w, http.StatusOK, plugin)
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"proto-gen/internal/config"
|
||||
"proto-gen/internal/database"
|
||||
"proto-gen/internal/gitclient"
|
||||
"proto-gen/internal/models"
|
||||
)
|
||||
|
||||
type mockGitClient struct{}
|
||||
|
||||
func (m *mockGitClient) ListRepositories(owner string) ([]gitclient.Repository, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *mockGitClient) CreateRepository(owner, name, desc string, private bool) (*gitclient.Repository, error) {
|
||||
return &gitclient.Repository{Name: name}, nil
|
||||
}
|
||||
func (m *mockGitClient) GetRepository(owner, name string) (*gitclient.Repository, error) {
|
||||
return &gitclient.Repository{Name: name}, nil
|
||||
}
|
||||
func (m *mockGitClient) ListBranches(owner, repo string) ([]gitclient.Branch, error) {
|
||||
return []gitclient.Branch{{Name: "main", Commit: "abc123"}}, nil
|
||||
}
|
||||
func (m *mockGitClient) GetBranch(owner, repo, branch string) (*gitclient.Branch, error) {
|
||||
return &gitclient.Branch{Name: branch, Commit: "abc123"}, nil
|
||||
}
|
||||
|
||||
func newTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
db, err := database.New(t.TempDir() + "/test.db")
|
||||
if err != nil {
|
||||
t.Fatalf("creating db: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
cfg := &config.Config{
|
||||
ToolchainRegistry: "registry.example.com/proto-gen",
|
||||
TargetRepoTemplate: "{proto_repo}-gen-{lang}",
|
||||
Plugins: []config.PluginConfig{
|
||||
{Language: "go", Image: "proto-gen-go:v1.0"},
|
||||
{Language: "nodejs", Image: "proto-gen-ts:v1.0"},
|
||||
},
|
||||
}
|
||||
|
||||
return NewServer(db, cfg, &mockGitClient{})
|
||||
}
|
||||
|
||||
func TestHandleCreateTask(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
|
||||
body, _ := json.Marshal(CreateTaskRequest{
|
||||
Language: "go",
|
||||
ProtoRepo: "test-owner/yoresee_doc",
|
||||
ProtoBranch: "main",
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/v1/tasks", bytes.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
srv.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("status = %d, want %d", w.Code, http.StatusCreated)
|
||||
}
|
||||
|
||||
var task models.Task
|
||||
json.NewDecoder(w.Body).Decode(&task)
|
||||
if task.Language != "go" {
|
||||
t.Errorf("Language = %q, want %q", task.Language, "go")
|
||||
}
|
||||
if task.Status != models.StatusPending {
|
||||
t.Errorf("Status = %q, want %q", task.Status, models.StatusPending)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCreateTask_UnsupportedLanguage(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
|
||||
body, _ := json.Marshal(CreateTaskRequest{
|
||||
Language: "python",
|
||||
ProtoRepo: "test-owner/yoresee_doc",
|
||||
ProtoBranch: "main",
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/v1/tasks", bytes.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
srv.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want %d", w.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleListPlugins(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/plugins", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
var plugins []config.PluginConfig
|
||||
json.NewDecoder(w.Body).Decode(&plugins)
|
||||
if len(plugins) != 2 {
|
||||
t.Errorf("len(plugins) = %d, want 2", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetTask_NotFound(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/tasks/nonexistent", nil)
|
||||
w := httptest.NewRecorder()
|
||||
srv.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want %d", w.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
|
||||
"proto-gen/internal/config"
|
||||
"proto-gen/internal/database"
|
||||
"proto-gen/internal/gitclient"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
router *chi.Mux
|
||||
db *database.DB
|
||||
cfg *config.Config
|
||||
git gitclient.GitClient
|
||||
}
|
||||
|
||||
func NewServer(db *database.DB, cfg *config.Config, git gitclient.GitClient) *Server {
|
||||
s := &Server{
|
||||
router: chi.NewRouter(),
|
||||
db: db,
|
||||
cfg: cfg,
|
||||
git: git,
|
||||
}
|
||||
s.routes()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) routes() {
|
||||
s.router.Use(middleware.Logger)
|
||||
s.router.Use(middleware.Recoverer)
|
||||
s.router.Use(middleware.Compress(5))
|
||||
|
||||
s.router.Route("/api/v1", func(r chi.Router) {
|
||||
r.Post("/tasks", s.handleCreateTask)
|
||||
r.Get("/tasks", s.handleListTasks)
|
||||
r.Get("/tasks/{id}", s.handleGetTask)
|
||||
r.Post("/tasks/{id}/retry", s.handleRetryTask)
|
||||
r.Get("/plugins", s.handleListPlugins)
|
||||
r.Get("/plugins/{lang}/versions", s.handleGetPluginVersion)
|
||||
r.Put("/plugins/{lang}/versions", s.handleUpdatePluginVersion)
|
||||
})
|
||||
|
||||
s.router.Handle("/*", http.FileServer(http.Dir("internal/static")))
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) Start(addr string) error {
|
||||
log.Printf("server listening on %s", addr)
|
||||
return http.ListenAndServe(addr, s.router)
|
||||
}
|
||||
Reference in New Issue
Block a user