Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions api/handler/mcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,6 @@ func (h *MCPServerHandler) Deploy(ctx *gin.Context) {
req.MCPRepo.Namespace = namespace
req.MCPRepo.Name = name

if req.DefaultBranch == "" {
req.DefaultBranch = types.MainBranch
}

if len(req.Nickname) < 1 {
req.Nickname = req.Name
}
Expand Down
2 changes: 1 addition & 1 deletion api/handler/mcp_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestMCPServerHandler_Deploy(t *testing.T) {
Namespace: "u1",
Name: "r1",
Nickname: "r1",
DefaultBranch: "main",
DefaultBranch: "",
},
ResourceID: 11,
ClusterID: "ab45d3ba-a2ff-466e-887a-b2e5c0c070c5",
Expand Down
2 changes: 2 additions & 0 deletions component/mcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ func (m *mcpServerComponentImpl) Deploy(ctx context.Context, req *types.DeployMC
return nil, fmt.Errorf("fail to merge env for deploy mcp server %s/%s, %w", req.MCPRepo.Namespace, req.MCPRepo.Name, err)
}

req.DefaultBranch = mcpServer.Repository.DefaultBranch
req.RepoType = types.SpaceRepo
req.License = mcpServer.Repository.License

Expand Down Expand Up @@ -772,6 +773,7 @@ func (m *mcpServerComponentImpl) Deploy(ctx context.Context, req *types.DeployMC
CoverImageUrl: dbSpace.CoverImageUrl,
SKU: dbSpace.SKU,
CreatedAt: dbSpace.CreatedAt,
DefaultBranch: dbRepo.DefaultBranch,
}
return space, nil
}
Expand Down
104 changes: 104 additions & 0 deletions component/mcp_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package component

import (
"context"
"fmt"
"path"
"strconv"
"sync"
"testing"

Expand All @@ -13,6 +16,7 @@ import (
"opencsg.com/csghub-server/builder/rpc"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/types"
"opencsg.com/csghub-server/common/utils/common"
)

func TestMCPServerComponent_Create(t *testing.T) {
Expand Down Expand Up @@ -427,3 +431,103 @@ func TestMCPServerComponent_updateSpaceMetaTag(t *testing.T) {
err := mc.updateSpaceMetaTag(req, user)
require.Nil(t, err)
}

func TestMCPServerComponent_CheckDeployBranch(t *testing.T) {
ctx := context.TODO()
mc := initializeTestMCPServerComponent(ctx, t)

req := &types.DeployMCPServerReq{
CurrentUser: "user",
MCPRepo: types.RepoRequest{
Namespace: "ns",
Name: "n",
},
CreateRepoReq: types.CreateRepoReq{
Username: "user",
Namespace: "test-namespace",
Name: "test-server",
RepoType: types.SpaceRepo,
},
ResourceID: 1,
ClusterID: "cls",
}

dbrepo := &database.Repository{
ID: 321,
Tags: []database.Tag{{Name: "t1"}},
Name: "n",
License: "MIT",
Nickname: "n",
Path: "ns/n",
DefaultBranch: "master",
}

mc.mocks.stores.MCPServerMock().EXPECT().ByPath(ctx, req.MCPRepo.Namespace, req.MCPRepo.Name).Return(&database.MCPServer{
RepositoryID: 123,
Repository: dbrepo,
}, nil)

mc.mocks.components.repo.EXPECT().GetUserRepoPermission(ctx, req.CurrentUser, dbrepo).Return(&types.UserRepoPermission{
CanAdmin: true,
CanRead: true,
}, nil)

mc.mocks.stores.SpaceResourceMock().EXPECT().FindByID(ctx, req.ResourceID).Return(&database.SpaceResource{
ID: 1,
}, nil)

mc.mocks.components.repo.EXPECT().CheckAccountAndResource(ctx, req.CurrentUser, req.ClusterID, int64(0), &database.SpaceResource{
ID: 1,
}).Return(nil)

mc.mocks.stores.NamespaceMock().EXPECT().FindByPath(ctx, req.Namespace).Return(database.Namespace{
ID: 1,
}, nil)

mc.mocks.userSvcClient.EXPECT().GetUserInfo(ctx, req.CurrentUser, req.CurrentUser).Return(&rpc.User{
ID: 1,
Username: req.CurrentUser,
Email: "[email protected]",
Roles: []string{"admin"},
}, nil)

mc.mocks.stores.MCPServerMock().EXPECT().CreateSpaceAndRepoForDeploy(ctx, &database.Repository{
UserID: int64(1),
Path: path.Join(req.Namespace, req.Name),
GitPath: common.BuildRelativePath(fmt.Sprintf("%ss", string(req.RepoType)), req.Namespace, req.Name),
Name: req.Name,
Nickname: req.Nickname,
Description: req.Description,
Private: req.Private,
License: "MIT",
DefaultBranch: "master",
RepositoryType: req.RepoType,
Hashed: true,
}, &database.Space{
Sdk: types.MCPSERVER.Name,
SdkVersion: "",
CoverImageUrl: req.CoverImageUrl,
Env: "",
Hardware: "",
Secrets: "",
Variables: "",
Template: "",
SKU: strconv.FormatInt(1, 10), // space resource id
ClusterID: req.ClusterID,
}).Return(nil)

mc.mocks.gitServer.EXPECT().CopyRepository(mock.Anything, mock.Anything).Return(nil)

mc.mocks.gitServer.EXPECT().GetRepoFileContents(mock.Anything, mock.Anything).Return(&types.File{
Content: "",
}, nil)

mc.mocks.gitServer.EXPECT().UpdateRepoFile(mock.Anything).Return(nil)

mc.mocks.gitServer.EXPECT().DeleteRepo(mock.Anything, mock.Anything).Return(nil)

mc.mocks.stores.MCPServerMock().EXPECT().DeleteSpaceAndRepoForDeploy(mock.Anything, int64(0), int64(0)).Return(nil)

_, err := mc.Deploy(ctx, req)
require.NotNil(t, err)
}
18 changes: 10 additions & 8 deletions component/wireset.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,14 +699,16 @@ var RuleComponentSet = wire.NewSet(NewTestRuleComponent)

func NewTestMCPServerComponent(config *config.Config, stores *tests.MockStores, rpcUser rpc.UserSvcClient, repoComponent RepoComponent, gitServer gitserver.GitServer) *mcpServerComponentImpl {
return &mcpServerComponentImpl{
config: config,
repoComponent: repoComponent,
repoStore: stores.Repo,
gitServer: gitServer,
userSvcClient: rpcUser,
mcpServerStore: stores.MCPServerStore,
userLikesStore: stores.UserLikes,
recomStore: stores.Recom,
config: config,
repoComponent: repoComponent,
repoStore: stores.Repo,
gitServer: gitServer,
userSvcClient: rpcUser,
mcpServerStore: stores.MCPServerStore,
userLikesStore: stores.UserLikes,
recomStore: stores.Recom,
spaceResourceStore: stores.SpaceResource,
namespaceStore: stores.Namespace,
}
}

Expand Down
Loading