MCPcopy Create free account
hub / github.com/bytebase/bytebase / GetWorkspace

Method GetWorkspace

backend/api/v1/workspace_service.go:58–118  ·  view source on GitHub ↗

GetWorkspace gets a workspace by name. Supports "workspaces/-" to resolve the current/default workspace.

(ctx context.Context, req *connect.Request[v1pb.GetWorkspaceRequest])

Source from the content-addressed store, hash-verified

56// GetWorkspace gets a workspace by name.
57// Supports "workspaces/-" to resolve the current/default workspace.
58func (s *WorkspaceService) GetWorkspace(ctx context.Context, req *connect.Request[v1pb.GetWorkspaceRequest]) (*connect.Response[v1pb.Workspace], error) {
59 var workspaceID string
60
61 name := req.Msg.Name
62 if name == "workspaces/-" {
63 // "workspaces/-" is allowed without auth (login page logo).
64 // Resolve from context (authenticated) or fall back to default (self-hosted).
65 workspaceID = common.GetWorkspaceIDFromContext(ctx)
66 if workspaceID == "" && !s.profile.SaaS {
67 ws, err := s.store.GetWorkspaceID(ctx)
68 if err != nil {
69 return nil, connect.NewError(connect.CodeInternal, err)
70 }
71 workspaceID = ws
72 }
73 } else {
74 var err error
75 workspaceID, err = common.GetWorkspaceID(name)
76 if err != nil {
77 return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrap(err, "invalid workspace name"))
78 }
79
80 // Require authentication for non-saas mode.
81 if s.profile.SaaS {
82 // Specific workspace requires authentication and membership.
83 user, ok := GetUserFromContext(ctx)
84 if !ok || user == nil {
85 return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("authentication required"))
86 }
87
88 // Verify the user is a member of the requested workspace.
89 ws, err := s.store.FindWorkspace(ctx, &store.FindWorkspaceMessage{
90 WorkspaceID: &workspaceID,
91 Email: user.Email,
92 IncludeAllUser: !s.profile.SaaS,
93 })
94 if err != nil {
95 return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to verify workspace membership"))
96 }
97 if ws == nil {
98 return nil, connect.NewError(connect.CodeUnauthenticated, errors.Errorf("failed to verify workspace membership"))
99 }
100 }
101 }
102
103 result := &v1pb.Workspace{}
104 if workspaceID != "" {
105 ws, err := s.store.GetWorkspaceByID(ctx, workspaceID)
106 if err != nil {
107 return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace"))
108 }
109 if ws == nil {
110 return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("workspace %q not found", name))
111 }
112 result.Name = common.FormatWorkspace(ws.ResourceID)
113 result.Title = ws.Payload.GetTitle()
114 result.Logo = ws.Payload.GetLogo()
115 }

Callers

nothing calls this directly

Calls 10

GetWorkspaceIDFunction · 0.92
FormatWorkspaceFunction · 0.92
GetUserFromContextFunction · 0.85
ErrorfMethod · 0.80
GetWorkspaceByIDMethod · 0.80
GetWorkspaceIDMethod · 0.65
FindWorkspaceMethod · 0.65
GetTitleMethod · 0.45
GetLogoMethod · 0.45

Tested by

no test coverage detected