You've already forked agentic-coding-workflow
feat(workspace): add GET /branches endpoint
This commit is contained in:
@@ -115,3 +115,7 @@ func toFileStatusResp(f git.FileStatus) fileStatusResp {
|
|||||||
type commitResp struct {
|
type commitResp struct {
|
||||||
SHA string `json:"sha"`
|
SHA string `json:"sha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type branchListResp struct {
|
||||||
|
Branches []string `json:"branches"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ func (h *Handler) Mount(r chi.Router) {
|
|||||||
|
|
||||||
r.Get("/worktrees", h.listWorktrees)
|
r.Get("/worktrees", h.listWorktrees)
|
||||||
r.Post("/worktrees", h.createWorktree)
|
r.Post("/worktrees", h.createWorktree)
|
||||||
|
r.Get("/branches", h.listBranches)
|
||||||
})
|
})
|
||||||
r.With(auth).Route("/api/v1/worktrees/{wtID}", func(r chi.Router) {
|
r.With(auth).Route("/api/v1/worktrees/{wtID}", func(r chi.Router) {
|
||||||
r.Delete("/", h.deleteWorktree)
|
r.Delete("/", h.deleteWorktree)
|
||||||
@@ -340,6 +341,25 @@ func (h *Handler) createWorktree(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeJSON(w, http.StatusCreated, toWorktreeResp(wt))
|
writeJSON(w, http.StatusCreated, toWorktreeResp(wt))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) listBranches(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c, err := h.caller(r)
|
||||||
|
if err != nil {
|
||||||
|
writeErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id, err := parseUUID(chi.URLParam(r, "wsID"))
|
||||||
|
if err != nil {
|
||||||
|
writeErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
branches, err := h.ws.ListBranches(r.Context(), c, id)
|
||||||
|
if err != nil {
|
||||||
|
writeErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, branchListResp{Branches: branches})
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) deleteWorktree(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) deleteWorktree(w http.ResponseWriter, r *http.Request) {
|
||||||
c, err := h.caller(r)
|
c, err := h.caller(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func (f *fakeWS) GetCredentialMeta(_ context.Context, _ Caller, _ uuid.UUID) (*C
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeWS) ListBranches(_ context.Context, _ Caller, _ uuid.UUID) ([]string, error) {
|
func (f *fakeWS) ListBranches(_ context.Context, _ Caller, _ uuid.UUID) ([]string, error) {
|
||||||
return nil, nil
|
return []string{"main", "develop"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// fakeResolver 把固定 token 解码为 uuid(沿 internal/project handler_test.go 模式)。
|
// fakeResolver 把固定 token 解码为 uuid(沿 internal/project handler_test.go 模式)。
|
||||||
@@ -86,3 +86,26 @@ func TestHandler_CreateWorkspace_Smoke(t *testing.T) {
|
|||||||
r.ServeHTTP(w, req)
|
r.ServeHTTP(w, req)
|
||||||
require.Equal(t, http.StatusCreated, w.Code)
|
require.Equal(t, http.StatusCreated, w.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler_ListBranches_Smoke(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ws := &fakeWS{}
|
||||||
|
uid := uuid.New()
|
||||||
|
tok := "tok-branches"
|
||||||
|
resolver := &fakeResolver{valid: map[string]uuid.UUID{tok: uid}}
|
||||||
|
h := NewHandler(ws, nil, nil, resolver, fakeAdmin{})
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Use(middleware.RequestID)
|
||||||
|
h.Mount(r)
|
||||||
|
|
||||||
|
wsID := uuid.New()
|
||||||
|
req := httptest.NewRequest("GET", "/api/v1/workspaces/"+wsID.String()+"/branches", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer "+tok)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var resp branchListResp
|
||||||
|
require.NoError(t, json.NewDecoder(w.Body).Decode(&resp))
|
||||||
|
require.Equal(t, []string{"main", "develop"}, resp.Branches)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user