You've already forked agentic-coding-workflow
feat(workspace): add ListBranches method to WorkspaceService
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -145,6 +145,7 @@ type WorkspaceService interface {
|
||||
|
||||
UpsertCredential(ctx context.Context, c Caller, wsID uuid.UUID, in UpsertCredentialInput) (*Credential, error)
|
||||
GetCredentialMeta(ctx context.Context, c Caller, wsID uuid.UUID) (*Credential, error)
|
||||
ListBranches(ctx context.Context, c Caller, wsID uuid.UUID) ([]string, error)
|
||||
}
|
||||
|
||||
// WorktreeService 暴露支线 worktree 的应用服务方法。
|
||||
|
||||
@@ -48,6 +48,10 @@ func (f *fakeWS) GetCredentialMeta(_ context.Context, _ Caller, _ uuid.UUID) (*C
|
||||
return &Credential{Kind: CredKindNone}, nil
|
||||
}
|
||||
|
||||
func (f *fakeWS) ListBranches(_ context.Context, _ Caller, _ uuid.UUID) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// fakeResolver 把固定 token 解码为 uuid(沿 internal/project handler_test.go 模式)。
|
||||
type fakeResolver struct{ valid map[string]uuid.UUID }
|
||||
|
||||
|
||||
@@ -328,6 +328,23 @@ func (s *workspaceService) GetCredentialMeta(ctx context.Context, c Caller, wsID
|
||||
return cred, nil
|
||||
}
|
||||
|
||||
func (s *workspaceService) ListBranches(ctx context.Context, c Caller, wsID uuid.UUID) ([]string, error) {
|
||||
ws, err := s.repo.GetWorkspaceByID(ctx, wsID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if canRead, _, err := s.pa.ResolveByID(ctx, c.UserID, c.IsAdmin, ws.ProjectID); err != nil {
|
||||
return nil, err
|
||||
} else if !canRead {
|
||||
return nil, errs.New(errs.CodeForbidden, "no read access")
|
||||
}
|
||||
cred, err := s.loadDecryptedCredential(ctx, wsID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.gitr.ListRemoteBranches(ctx, ws.MainPath, cred)
|
||||
}
|
||||
|
||||
func (s *workspaceService) encryptForUpsert(wsID uuid.UUID, in UpsertCredentialInput) (*Credential, error) {
|
||||
cred := &Credential{
|
||||
ID: uuid.New(),
|
||||
|
||||
@@ -450,3 +450,36 @@ func TestWorkspaceService_Update_DefaultBranch_SameValue_NoGitOps(t *testing.T)
|
||||
require.Empty(t, fg.fetchRemoteBranchCalls)
|
||||
require.Empty(t, fg.checkoutCalls)
|
||||
}
|
||||
|
||||
func TestWorkspaceService_ListBranches(t *testing.T) {
|
||||
t.Parallel()
|
||||
pa := &fakePA{pid: uuid.New(), canRead: true, canWrite: true}
|
||||
svc, repo := newSvc(t, pa, nil, &fakeGit{})
|
||||
caller := Caller{UserID: uuid.New()}
|
||||
|
||||
wsID := uuid.New()
|
||||
repo.wss[wsID] = &Workspace{
|
||||
ID: wsID, ProjectID: pa.pid, Slug: "ws1",
|
||||
DefaultBranch: "main", MainPath: "/data/ws1/main",
|
||||
}
|
||||
|
||||
branches, err := svc.ListBranches(context.Background(), caller, wsID)
|
||||
require.NoError(t, err)
|
||||
// fakeGit returns nil, nil → empty slice
|
||||
require.Nil(t, branches)
|
||||
}
|
||||
|
||||
func TestWorkspaceService_ListBranches_NoReadAccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
pa := &fakePA{pid: uuid.New(), canRead: false, canWrite: false}
|
||||
svc, repo := newSvc(t, pa, nil, &fakeGit{})
|
||||
caller := Caller{UserID: uuid.New()}
|
||||
|
||||
wsID := uuid.New()
|
||||
repo.wss[wsID] = &Workspace{
|
||||
ID: wsID, ProjectID: pa.pid, Slug: "ws1",
|
||||
}
|
||||
|
||||
_, err := svc.ListBranches(context.Background(), caller, wsID)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user