8f48dd838b
Replace task statuses (pending/running/succeeded/failed/cancelled) with
a kanban workflow: todo → in_progress → in_review → done/wontdo.
When a non-review agent task completes, the API auto-creates a child
review task and moves the parent to in_review. Only humans can move
tasks from in_review to done/wontdo via the TUI.
New components:
- cmd/tui: bubbletea kanban board with $EDITOR integration
- POST /api/v1/tasks/{id}/complete: agent completion callback
- Operator --api-url flag for completion callbacks
- ProviderQueue sets tasks to in_progress on pickup
- AgentTask reconciler calls /complete on job finish
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package apiserver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
"git.unkin.net/unkin/forgebot/internal/apiserver/handlers"
|
|
"git.unkin.net/unkin/forgebot/internal/database"
|
|
"git.unkin.net/unkin/forgebot/internal/provider/gitea"
|
|
)
|
|
|
|
type Server struct {
|
|
cfg *Config
|
|
router chi.Router
|
|
db *database.DB
|
|
provider *gitea.Client
|
|
}
|
|
|
|
func New(cfg *Config) (*Server, error) {
|
|
db, err := database.New(cfg.DatabaseDSN())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
provider := gitea.NewClient(cfg.GiteaURL, cfg.GiteaToken)
|
|
|
|
s := &Server{
|
|
cfg: cfg,
|
|
db: db,
|
|
provider: provider,
|
|
}
|
|
s.router = s.routes()
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Server) routes() chi.Router {
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RequestID)
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(middleware.Timeout(60 * time.Second))
|
|
|
|
healthH := handlers.NewHealthHandler(s.db)
|
|
webhookH := handlers.NewWebhookHandler(s.db, s.provider, s.cfg.WebhookSecret)
|
|
tasksH := handlers.NewTasksHandler(s.db, s.provider)
|
|
|
|
r.Get("/health", healthH.Health)
|
|
|
|
r.Route("/api/v1", func(r chi.Router) {
|
|
r.Post("/webhook/gitea", webhookH.HandleGitea)
|
|
|
|
r.Get("/tasks", tasksH.List)
|
|
r.Post("/tasks", tasksH.Create)
|
|
r.Get("/tasks/{id}", tasksH.Get)
|
|
r.Patch("/tasks/{id}", tasksH.UpdateStatus)
|
|
r.Post("/tasks/{id}/complete", tasksH.Complete)
|
|
r.Post("/tasks/{id}/comment", tasksH.PostComment)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
httpServer := &http.Server{
|
|
Addr: s.cfg.ListenAddr,
|
|
Handler: s.router,
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 300 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
slog.Info("shutting down server")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = httpServer.Shutdown(shutdownCtx)
|
|
}()
|
|
|
|
slog.Info("starting server", "addr", s.cfg.ListenAddr)
|
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|