Skip to content

Commit

Permalink
Merge pull request #31 from StellarisW/feat-platform
Browse files Browse the repository at this point in the history
sync platform-2
  • Loading branch information
Nihilism0 authored Nov 17, 2023
2 parents 02963fb + cff4edd commit d1964f8
Show file tree
Hide file tree
Showing 40 changed files with 1,150 additions and 529 deletions.
4 changes: 2 additions & 2 deletions platform/manifest/sql/mysql/idl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `idl`;
CREATE TABLE `idl` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`repository_id` bigint(20) NOT NULL COMMENT 'repository id',
`idl_repository_id` bigint(20) NOT NULL COMMENT 'idl_repository id',
`service_repository_id` bigint(20) NOT NULL COMMENT 'service_repository id',
`parent_idl_id` bigint(20) DEFAULT NULL COMMENT 'null if main idl else import idl',
`idl_type` tinyint(4) NOT NULL COMMENT 'idl type (1: thrift, 2: proto)',
`idl_path` varchar(255) NOT NULL COMMENT 'idl path',
`commit_hash` char(40) NOT NULL COMMENT 'idl file commit hash',
`service_name` varchar(255) NOT NULL COMMENT 'service name',
Expand Down
2 changes: 1 addition & 1 deletion platform/server/cmd/agent/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *AgentServiceImpl) SyncRepositoryById(ctx context.Context, req *agent.Sy

// AddIDL implements the AgentServiceImpl interface.
func (s *AgentServiceImpl) AddIDL(ctx context.Context, req *agent.AddIDLReq) (resp *agent.AddIDLRes, err error) {
resp, err = service.NewAddIDLService(ctx, s.svcCtx).Run(req)
resp, err = service.NewAddIDLService(ctx, s.svcCtx, s).Run(req)

return resp, err
}
Expand Down
64 changes: 46 additions & 18 deletions platform/server/cmd/agent/internal/biz/service/add_idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
)

type AddIDLService struct {
ctx context.Context
svcCtx *svc.ServiceContext
ctx context.Context
svcCtx *svc.ServiceContext
agentService agent.AgentService
} // NewAddIDLService new AddIDLService
func NewAddIDLService(ctx context.Context, svcCtx *svc.ServiceContext) *AddIDLService {
func NewAddIDLService(ctx context.Context, svcCtx *svc.ServiceContext, agentService agent.AgentService) *AddIDLService {
return &AddIDLService{
ctx: ctx,
svcCtx: svcCtx,
ctx: ctx,
svcCtx: svcCtx,
agentService: agentService,
}
}

Expand All @@ -62,8 +63,8 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}

return &agent.AddIDLRes{
Code: http.StatusBadRequest,
Msg: "can not get the client",
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}

Expand All @@ -75,6 +76,20 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}, nil
}

isExist, err := s.svcCtx.DaoManager.Idl.CheckMainIdlIfExist(s.ctx, req.RepositoryId, idlPid)
if err != nil {
return &agent.AddIDLRes{
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}
if isExist {
return &agent.AddIDLRes{
Code: http.StatusBadRequest,
Msg: "idl is already exist",
}, nil
}

_, err = repoClient.GetFile(owner, repoName, idlPid, consts.MainRef)
if err != nil {
return &agent.AddIDLRes{
Expand All @@ -101,7 +116,7 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}, nil
}

repo, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, req.RepositoryId)
idlRepoModel, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, req.RepositoryId)
if err != nil {
logger.Logger.Error("get repository failed", zap.Error(err))
return &agent.AddIDLRes{
Expand All @@ -111,7 +126,7 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}

// create temp dir
tempDir, err := ioutil.TempDir("", strconv.FormatInt(repo.Id, 10))
tempDir, err := ioutil.TempDir("", strconv.FormatInt(idlRepoModel.Id, 10))
if err != nil {
logger.Logger.Error("create temp dir failed", zap.Error(err))
return &agent.AddIDLRes{
Expand All @@ -133,7 +148,7 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er

// the archive type of GitHub is tarball instead of tar
isTarBall := false
if repo.RepositoryType == consts.RepositoryTypeNumGithub {
if idlRepoModel.RepositoryType == consts.RepositoryTypeNumGithub {
isTarBall = true
}

Expand Down Expand Up @@ -182,7 +197,7 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}

importIDLs[i] = &model.ImportIDL{
IdlPath: importPath,
IdlPath: calculatedPath,
CommitHash: commitHash,
}
}
Expand All @@ -208,13 +223,13 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}

serviceRepoId, err := s.svcCtx.DaoManager.Repository.AddRepository(s.ctx, model.Repository{
RepositoryType: repo.RepositoryType,
RepositoryType: idlRepoModel.RepositoryType,
StoreType: consts.RepositoryStoreTypeNumService,
RepositoryUrl: serviceRepoURL,
Token: repo.Token,
Token: idlRepoModel.Token,
})
if err != nil {
if strings.Contains(err.Error(), "Duplicate entry") {
if err == consts.ErrDuplicateRecord {
return &agent.AddIDLRes{
Code: http.StatusBadRequest,
Msg: "repository is already exist",
Expand All @@ -227,10 +242,10 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}

// add idl
err = s.svcCtx.DaoManager.Idl.AddIDL(s.ctx, model.IDL{
mainIdlId, err := s.svcCtx.DaoManager.Idl.AddIDL(s.ctx, model.IDL{
IdlRepositoryId: req.RepositoryId,
ServiceRepositoryId: serviceRepoId,
MainIdlPath: req.MainIdlPath,
MainIdlPath: idlPid,
ServiceName: req.ServiceName,
ImportIdls: importIDLs,
CommitHash: mainIdlHash,
Expand All @@ -242,5 +257,18 @@ func (s *AddIDLService) Run(req *agent.AddIDLReq) (resp *agent.AddIDLRes, err er
}, nil
}

return
res, err := s.agentService.GenerateCode(s.ctx, &agent.GenerateCodeReq{
IdlId: mainIdlId,
})
if res.Code != 0 {
return &agent.AddIDLRes{
Code: res.Code,
Msg: res.Msg,
}, nil
}

return &agent.AddIDLRes{
Code: 0,
Msg: "add idl successfully",
}, nil
}
44 changes: 29 additions & 15 deletions platform/server/cmd/agent/internal/biz/service/generate_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

const (
successMsgGenerateCode = "" // TODO: to be filled...
successMsgGenerateCode = "generate code successfully"
)

type GenerateCodeService struct {
Expand All @@ -50,7 +50,7 @@ func NewGenerateCodeService(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
// Run create note info
func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.GenerateCodeRes, err error) {
// get idl info by idl id
idl, err := s.svcCtx.DaoManager.Idl.GetIDL(s.ctx, req.IdlId)
idlModel, err := s.svcCtx.DaoManager.Idl.GetIDL(s.ctx, req.IdlId)
if err != nil {
logger.Logger.Error("get idl info failed", zap.Error(err))
return &agent.GenerateCodeRes{
Expand All @@ -60,7 +60,7 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener
}

// get repository info by repository id
repo, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, idl.IdlRepositoryId)
repoModel, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, idlModel.IdlRepositoryId)
if err != nil {
logger.Logger.Error("get repo info failed", zap.Error(err))
return &agent.GenerateCodeRes{
Expand All @@ -70,17 +70,24 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener
}

// get repo client
client, err := s.svcCtx.RepoManager.GetClient(repo.Id)
client, err := s.svcCtx.RepoManager.GetClient(repoModel.Id)
if err != nil {
logger.Logger.Error("get repo client failed", zap.Error(err), zap.Int64("repo_id", repo.Id))
logger.Logger.Error("get repo client failed", zap.Error(err), zap.Int64("repo_id", repoModel.Id))
return &agent.GenerateCodeRes{
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}

// parsing URLs to obtain information
idlPid, owner, repoName, err := client.ParseIdlUrl(idl.MainIdlPath)
idlPid, owner, repoName, err := client.ParseIdlUrl(
utils.GetRepoFullUrl(
repoModel.RepositoryType,
repoModel.RepositoryUrl,
consts.MainRef,
idlModel.MainIdlPath,
),
)
if err != nil {
logger.Logger.Error("parse repo url failed", zap.Error(err))
return &agent.GenerateCodeRes{
Expand All @@ -90,7 +97,7 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener
}

// create temp dir
tempDir, err := ioutil.TempDir("", strconv.FormatInt(repo.Id, 10))
tempDir, err := ioutil.TempDir("", strconv.FormatInt(repoModel.Id, 10))
if err != nil {
logger.Logger.Error("create temp dir failed", zap.Error(err))
return &agent.GenerateCodeRes{
Expand All @@ -112,7 +119,7 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener

// the archive type of GitHub is tarball instead of tar
isTarBall := false
if repo.RepositoryType == consts.RepositoryTypeNumGithub {
if repoModel.RepositoryType == consts.RepositoryTypeNumGithub {
isTarBall = true
}

Expand All @@ -127,7 +134,7 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener
}

// generate code using cwgo
err = s.svcCtx.Generator.Generate(archiveName+idlPid, idl.ServiceName, tempDir)
err = s.svcCtx.Generator.Generate(tempDir+"/"+archiveName+idlPid, idlModel.ServiceName, tempDir)
if err != nil {
logger.Logger.Error("generate file failed", zap.Error(err))
return &agent.GenerateCodeRes{
Expand All @@ -146,25 +153,32 @@ func (s *GenerateCodeService) Run(req *agent.GenerateCodeReq) (resp *agent.Gener
}

// push files to the repository
serviceRepository, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, idl.ServiceRepositoryId)
_, serviceRepoName, err := client.ParseRepoUrl(serviceRepository.RepositoryUrl)
serviceRepositoryModel, err := s.svcCtx.DaoManager.Repository.GetRepository(s.ctx, idlModel.ServiceRepositoryId)
if err != nil {
return &agent.GenerateCodeRes{
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}

err = client.PushFilesToRepository(fileContentMap, owner, serviceRepoName, consts.MainRef, "generated by cwgo")
_, serviceRepoName, err := client.ParseRepoUrl(serviceRepositoryModel.RepositoryUrl)
if err != nil {
return &agent.GenerateCodeRes{
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}

resp.Code = 0
resp.Msg = successMsgGenerateCode
err = client.PushFilesToRepository(fileContentMap, owner, serviceRepoName, consts.MainRef, "generated by cwgo")
if err != nil {
return &agent.GenerateCodeRes{
Code: http.StatusInternalServerError,
Msg: "internal err",
}, nil
}

return resp, nil
return &agent.GenerateCodeRes{
Code: 0,
Msg: successMsgGenerateCode,
}, nil
}
5 changes: 3 additions & 2 deletions platform/server/cmd/agent/internal/biz/service/get_idls.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewGetIDLsService(ctx context.Context, svcCtx *svc.ServiceContext) *GetIDLs

// Run create note info
func (s *GetIDLsService) Run(req *agent.GetIDLsReq) (resp *agent.GetIDLsRes, err error) {
idls, err := s.svcCtx.DaoManager.Idl.GetIDLList(s.ctx, req.Page, req.Limit, req.Order, req.OrderBy)
idls, total, err := s.svcCtx.DaoManager.Idl.GetIDLList(s.ctx, req.Page, req.Limit, req.Order, req.OrderBy)
if err != nil {
return &agent.GetIDLsRes{
Code: http.StatusBadRequest,
Expand All @@ -51,7 +51,8 @@ func (s *GetIDLsService) Run(req *agent.GetIDLsReq) (resp *agent.GetIDLsRes, err
Code: 0,
Msg: "get idls successfully",
Data: &agent.GetIDLsResData{
Idls: idls,
Idls: idls,
Total: int32(total),
},
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewGetRepositoriesService(ctx context.Context, svcCtx *svc.ServiceContext)

// Run create note info
func (s *GetRepositoriesService) Run(req *agent.GetRepositoriesReq) (resp *agent.GetRepositoriesRes, err error) {
repos, err := s.svcCtx.DaoManager.Repository.GetRepositoryList(s.ctx, req.Page, req.Limit, req.Order, req.OrderBy)
repos, total, err := s.svcCtx.DaoManager.Repository.GetRepositoryList(s.ctx, req.Page, req.Limit, req.Order, req.OrderBy)
if err != nil {
return &agent.GetRepositoriesRes{
Code: http.StatusInternalServerError,
Expand All @@ -52,6 +52,7 @@ func (s *GetRepositoriesService) Run(req *agent.GetRepositoriesReq) (resp *agent
Msg: "get repositories successfully",
Data: &agent.GetRepositoriesResData{
Repositories: repos,
Total: int32(total),
},
}, nil
}
Loading

0 comments on commit d1964f8

Please sign in to comment.