From 5081b0c014d8e82e7be1907441c246fbd01ca21e Mon Sep 17 00:00:00 2001 From: Claudomator Agent Date: Sun, 22 Mar 2026 00:56:54 +0000 Subject: feat: Phase 3 — stories data model, ValidStoryTransition, storage CRUD, API endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - internal/task/story.go: Story struct, StoryState constants, ValidStoryTransition - internal/task/task.go: add StoryID field - internal/storage/db.go: stories table + story_id on tasks migrations; CreateStory, GetStory, ListStories, UpdateStoryStatus, ListTasksByStory; update all task SELECT/INSERT to include story_id; scanTask extended with sql.NullString for story_id; added modernc timestamp format to GetMaxUpdatedAt - internal/storage/sqlite_cgo.go + sqlite_nocgo.go: build-tag based driver selection (mattn/go-sqlite3 with CGO, modernc.org/sqlite pure-Go fallback) so tests run without a C compiler - internal/api/stories.go: GET/POST /api/stories, GET /api/stories/{id}, GET/POST /api/stories/{id}/tasks (auto-wires depends_on chain), PUT /api/stories/{id}/status (validates transition) - internal/api/server.go: register all story routes - go.mod/go.sum: add modernc.org/sqlite pure-Go dependency Co-Authored-By: Claude Sonnet 4.6 --- internal/api/server.go | 6 ++ internal/api/stories.go | 185 +++++++++++++++++++++++++++++++++++++++++++ internal/api/stories_test.go | 120 ++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 internal/api/stories.go create mode 100644 internal/api/stories_test.go (limited to 'internal/api') diff --git a/internal/api/server.go b/internal/api/server.go index ff6fdb6..092ae3a 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -138,6 +138,12 @@ func (s *Server) routes() { s.mux.HandleFunc("POST /api/projects", s.handleCreateProject) s.mux.HandleFunc("GET /api/projects/{id}", s.handleGetProject) s.mux.HandleFunc("PUT /api/projects/{id}", s.handleUpdateProject) + s.mux.HandleFunc("GET /api/stories", s.handleListStories) + s.mux.HandleFunc("POST /api/stories", s.handleCreateStory) + s.mux.HandleFunc("GET /api/stories/{id}", s.handleGetStory) + s.mux.HandleFunc("GET /api/stories/{id}/tasks", s.handleListStoryTasks) + s.mux.HandleFunc("POST /api/stories/{id}/tasks", s.handleAddTaskToStory) + s.mux.HandleFunc("PUT /api/stories/{id}/status", s.handleUpdateStoryStatus) s.mux.HandleFunc("GET /api/health", s.handleHealth) s.mux.HandleFunc("POST /api/webhooks/github", s.handleGitHubWebhook) s.mux.HandleFunc("GET /api/push/vapid-key", s.handleGetVAPIDKey) diff --git a/internal/api/stories.go b/internal/api/stories.go new file mode 100644 index 0000000..4b91653 --- /dev/null +++ b/internal/api/stories.go @@ -0,0 +1,185 @@ +package api + +import ( + "encoding/json" + "net/http" + "time" + + "github.com/google/uuid" + "github.com/thepeterstone/claudomator/internal/task" +) + +func (s *Server) handleListStories(w http.ResponseWriter, r *http.Request) { + stories, err := s.store.ListStories() + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + if stories == nil { + stories = []*task.Story{} + } + writeJSON(w, http.StatusOK, stories) +} + +func (s *Server) handleCreateStory(w http.ResponseWriter, r *http.Request) { + var st task.Story + if err := json.NewDecoder(r.Body).Decode(&st); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()}) + return + } + if st.Name == "" { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "name is required"}) + return + } + if st.ID == "" { + st.ID = uuid.New().String() + } + if st.Status == "" { + st.Status = task.StoryPending + } + if err := s.store.CreateStory(&st); err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusCreated, st) +} + +func (s *Server) handleGetStory(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + st, err := s.store.GetStory(id) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"}) + return + } + writeJSON(w, http.StatusOK, st) +} + +func (s *Server) handleListStoryTasks(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + if _, err := s.store.GetStory(id); err != nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"}) + return + } + tasks, err := s.store.ListTasksByStory(id) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + if tasks == nil { + tasks = []*task.Task{} + } + writeJSON(w, http.StatusOK, tasks) +} + +func (s *Server) handleAddTaskToStory(w http.ResponseWriter, r *http.Request) { + storyID := r.PathValue("id") + st, err := s.store.GetStory(storyID) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"}) + return + } + _ = st + + var input struct { + Name string `json:"name"` + Description string `json:"description"` + Project string `json:"project"` + RepositoryURL string `json:"repository_url"` + Agent task.AgentConfig `json:"agent"` + Claude task.AgentConfig `json:"claude"` + Timeout string `json:"timeout"` + Priority string `json:"priority"` + Tags []string `json:"tags"` + ParentTaskID string `json:"parent_task_id"` + } + if err := json.NewDecoder(r.Body).Decode(&input); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()}) + return + } + if input.Agent.Instructions == "" && input.Claude.Instructions != "" { + input.Agent = input.Claude + } + + existing, err := s.store.ListTasksByStory(storyID) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + + now := time.Now().UTC() + t := &task.Task{ + ID: uuid.New().String(), + Name: input.Name, + Description: input.Description, + Project: input.Project, + RepositoryURL: input.RepositoryURL, + Agent: input.Agent, + Priority: task.Priority(input.Priority), + Tags: input.Tags, + DependsOn: []string{}, + Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "exponential"}, + State: task.StatePending, + StoryID: storyID, + ParentTaskID: input.ParentTaskID, + CreatedAt: now, + UpdatedAt: now, + } + + if t.Agent.Type == "" { + t.Agent.Type = "claude" + } + if t.Priority == "" { + t.Priority = task.PriorityNormal + } + if t.Tags == nil { + t.Tags = []string{} + } + if input.Timeout != "" { + dur, err := time.ParseDuration(input.Timeout) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid timeout: " + err.Error()}) + return + } + t.Timeout.Duration = dur + } + + // Auto-wire depends_on: new task depends on the last existing task (sorted ASC by created_at). + if len(existing) > 0 { + lastTask := existing[len(existing)-1] + t.DependsOn = []string{lastTask.ID} + } + + if err := s.store.CreateTask(t); err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusCreated, t) +} + +func (s *Server) handleUpdateStoryStatus(w http.ResponseWriter, r *http.Request) { + id := r.PathValue("id") + st, err := s.store.GetStory(id) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "story not found"}) + return + } + + var input struct { + Status task.StoryState `json:"status"` + } + if err := json.NewDecoder(r.Body).Decode(&input); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON: " + err.Error()}) + return + } + if !task.ValidStoryTransition(st.Status, input.Status) { + writeJSON(w, http.StatusConflict, map[string]string{ + "error": "invalid story status transition from " + string(st.Status) + " to " + string(input.Status), + }) + return + } + if err := s.store.UpdateStoryStatus(id, input.Status); err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]string{"message": "story status updated", "story_id": id, "status": string(input.Status)}) +} diff --git a/internal/api/stories_test.go b/internal/api/stories_test.go new file mode 100644 index 0000000..8516ade --- /dev/null +++ b/internal/api/stories_test.go @@ -0,0 +1,120 @@ +package api + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/thepeterstone/claudomator/internal/task" +) + +func TestCreateStory_API(t *testing.T) { + srv, _ := testServer(t) + + body := `{"name":"My Story","project_id":"proj-1"}` + req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + srv.mux.ServeHTTP(w, req) + + if w.Code != http.StatusCreated { + t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String()) + } + var st task.Story + if err := json.NewDecoder(w.Body).Decode(&st); err != nil { + t.Fatalf("decode response: %v", err) + } + if st.Name != "My Story" { + t.Errorf("Name: want 'My Story', got %q", st.Name) + } + if st.ID == "" { + t.Error("ID should be auto-generated") + } + if st.Status != task.StoryPending { + t.Errorf("Status: want PENDING, got %q", st.Status) + } +} + +func TestGetStory_API(t *testing.T) { + srv, _ := testServer(t) + + // Create a story first. + body := `{"name":"Get Me"}` + req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + srv.mux.ServeHTTP(w, req) + if w.Code != http.StatusCreated { + t.Fatalf("create story: expected 201, got %d", w.Code) + } + var created task.Story + json.NewDecoder(w.Body).Decode(&created) + + // Fetch it. + req2 := httptest.NewRequest("GET", "/api/stories/"+created.ID, nil) + w2 := httptest.NewRecorder() + srv.mux.ServeHTTP(w2, req2) + + if w2.Code != http.StatusOK { + t.Fatalf("get story: expected 200, got %d: %s", w2.Code, w2.Body.String()) + } + var got task.Story + if err := json.NewDecoder(w2.Body).Decode(&got); err != nil { + t.Fatalf("decode: %v", err) + } + if got.ID != created.ID { + t.Errorf("ID: want %q, got %q", created.ID, got.ID) + } + if got.Name != "Get Me" { + t.Errorf("Name: want 'Get Me', got %q", got.Name) + } +} + +func TestAddTaskToStory_AutoWiresDependsOn(t *testing.T) { + srv, _ := testServer(t) + + // Create a story. + storyBody := `{"name":"Story For Tasks"}` + req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(storyBody)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + srv.mux.ServeHTTP(w, req) + if w.Code != http.StatusCreated { + t.Fatalf("create story: %d %s", w.Code, w.Body.String()) + } + var story task.Story + json.NewDecoder(w.Body).Decode(&story) + + addTask := func(name string) *task.Task { + body := `{"name":"` + name + `","agent":{"type":"claude","instructions":"do it"}}` + r := httptest.NewRequest("POST", "/api/stories/"+story.ID+"/tasks", bytes.NewBufferString(body)) + r.Header.Set("Content-Type", "application/json") + wr := httptest.NewRecorder() + srv.mux.ServeHTTP(wr, r) + if wr.Code != http.StatusCreated { + t.Fatalf("add task %s: expected 201, got %d: %s", name, wr.Code, wr.Body.String()) + } + var tk task.Task + json.NewDecoder(wr.Body).Decode(&tk) + return &tk + } + + task1 := addTask("Task 1") + task2 := addTask("Task 2") + task3 := addTask("Task 3") + + // task1 has no dependencies. + if len(task1.DependsOn) != 0 { + t.Errorf("task1.DependsOn: want [], got %v", task1.DependsOn) + } + // task2 depends on task1. + if len(task2.DependsOn) != 1 || task2.DependsOn[0] != task1.ID { + t.Errorf("task2.DependsOn: want [%s], got %v", task1.ID, task2.DependsOn) + } + // task3 depends on task2. + if len(task3.DependsOn) != 1 || task3.DependsOn[0] != task2.ID { + t.Errorf("task3.DependsOn: want [%s], got %v", task2.ID, task3.DependsOn) + } +} -- cgit v1.2.3