From 794f8fbc0db91dddccefc0c22d9419990d7a323a Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Fri, 8 Mar 2024 14:00:45 +0800 Subject: [PATCH 1/2] add branch modify --- pkg/controller/gin.go | 2 ++ pkg/controller/project_handler.go | 44 +++++++++++++++++++++++++++++++ pkg/parameter/project_param.go | 4 +++ pkg/service/github.go | 13 +++++++++ pkg/service/project.go | 14 ++++++++++ pkg/utils/github_client.go | 15 ++++++----- 6 files changed, 86 insertions(+), 6 deletions(-) diff --git a/pkg/controller/gin.go b/pkg/controller/gin.go index 6316aa3c..253eba41 100644 --- a/pkg/controller/gin.go +++ b/pkg/controller/gin.go @@ -62,6 +62,8 @@ func (h *HttpServer) StartHttpServer() { api.POST("/projects/code", h.handlerServer.createProjectByCodeV2) api.GET("/projects/:id", h.handlerServer.projectDetail) api.PUT("/projects/:id", h.handlerServer.updateProject) + api.GET("/projects/:id/branch", h.handlerServer.getProjectRepositoryBranch) // 查询项目分支信息 + api.PUT("/projects/:id/branch", h.handlerServer.setProjectRepositoryBranch) // 查询项目分支信息 api.DELETE("projects/:id", h.handlerServer.deleteProject) api.POST("/projects/check-name", h.handlerServer.checkName) api.GET("/user", h.handlerServer.getUseInfo) diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index 9d107019..91322524 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -1,6 +1,7 @@ package controller import ( + "context" "embed" "fmt" "io/ioutil" @@ -8,6 +9,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/gin-gonic/gin" "github.com/hamster-shared/aline-engine/logger" @@ -1690,3 +1692,45 @@ func (h *HandlerServer) getChainNetworkByName(gin *gin.Context) { } Success(list, gin) } + +func (h *HandlerServer) getProjectRepositoryBranch(gin *gin.Context) { + id := gin.Param("id") + project, err := h.projectService.GetProjectById(id) + if err != nil { + Fail(err.Error(), gin) + return + } + + githubService := application.GetBean[*service.GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := service.ParsingGitHubURL(project.RepositoryUrl) + if err != nil { + Fail(err.Error(), gin) + return + } + branches, err := githubService.ListRepositoryBranch(ctx, owner, repo) + if err != nil { + Fail(err.Error(), gin) + return + } + Success(branches, gin) +} + +func (h *HandlerServer) setProjectRepositoryBranch(gin *gin.Context) { + id := gin.Param("id") + userAny, _ := gin.Get("user") + user, _ := userAny.(db2.User) + var updateProjectBranch parameter.UpdateProjectBranch + err := gin.BindJSON(&updateProjectBranch) + if err != nil { + Fail(err.Error(), gin) + return + } + err = h.projectService.UpdateProjectBranch(id, int64(user.Id), updateProjectBranch.Branch) + if err != nil { + Fail(err.Error(), gin) + return + } + + Success(nil, gin) +} diff --git a/pkg/parameter/project_param.go b/pkg/parameter/project_param.go index 912c83cf..00b5540c 100644 --- a/pkg/parameter/project_param.go +++ b/pkg/parameter/project_param.go @@ -46,3 +46,7 @@ type K8sDeployParam struct { ServicePort int32 `json:"servicePort"` ServiceTargetPort int32 `json:"serviceTargetPort"` } + +type UpdateProjectBranch struct { + Branch string `json:"branch"` +} diff --git a/pkg/service/github.go b/pkg/service/github.go index 3cf40e6d..4d4b5c74 100644 --- a/pkg/service/github.go +++ b/pkg/service/github.go @@ -14,6 +14,7 @@ import ( "github.com/hamster-shared/hamster-develop/pkg/utils" "github.com/hamster-shared/hamster-develop/pkg/vo" "github.com/pkg/errors" + "github.com/samber/lo" "github.com/wujiangweiphp/go-curl" "golang.org/x/oauth2" "gorm.io/gorm" @@ -1011,3 +1012,15 @@ func (g *GithubService) QueryRepos(installationId int64, page, size int, query s repoPage.PageSize = size return repoPage, nil } + +func (g *GithubService) ListRepositoryBranch(ctx context.Context, owner, repoName string) ([]string, error) { + client := utils.NewGithubClient(g.ctx, "") + branches, _, err := client.Repositories.ListBranches(ctx, owner, repoName, &github.BranchListOptions{}) + if err != nil { + return nil, err + } + + return lo.Map(branches, func(item *github.Branch, index int) string { + return item.GetName() + }), err +} diff --git a/pkg/service/project.go b/pkg/service/project.go index b714d14d..83fc72c9 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -35,6 +35,7 @@ type IProjectService interface { ParsingEVMFrame(repoContents []*github.RepositoryContent) (consts.EVMFrameType, error) GetChainNetworkList() ([]db2.ChainNetwork, error) GetChainNetworkByName(name string) (db2.ChainNetwork, error) + UpdateProjectBranch(id string, userId int64, branch string) error } type ProjectService struct { @@ -459,3 +460,16 @@ func parsingPackageJson(fileContent *github.RepositoryContent, name, userName, t } return 0, fmt.Errorf("canot ensure the frontend frame type") } + +func (p *ProjectService) UpdateProjectBranch(id string, userId int64, branch string) error { + project, err := p.GetProjectById(id) + if err != nil { + return err + } + + if project.UserId != userId { + return errors.New("permission error") + } + + return p.db.Model(&db2.Project{}).Where("id", id).Update("branch", branch).Error +} diff --git a/pkg/utils/github_client.go b/pkg/utils/github_client.go index 5d71d3ab..df556dd2 100644 --- a/pkg/utils/github_client.go +++ b/pkg/utils/github_client.go @@ -7,10 +7,13 @@ import ( ) func NewGithubClient(ctx context.Context, token string) *github.Client { - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - tc := oauth2.NewClient(ctx, ts) - - return github.NewClient(tc) + if token != "" { + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + return github.NewClient(tc) + } else { + return github.NewClient(nil) + } } From 335fde4f3fc2c70f8ac6761fbd8933398dfdd900 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Fri, 8 Mar 2024 15:29:19 +0800 Subject: [PATCH 2/2] update --- Dockerfile | 16 +++++++++++++--- Dockerfile_base | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index e5d49bbe..ec0e9890 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,17 @@ -FROM docker.io/hamstershare/debian_docker_cli:20231010 -RUN npm install -g truffle +FROM golang:1.20.2 as builder -COPY ./aline-test /usr/local/bin/aline-test +WORKDIR /app + +ENV GO111MODULE on +ENV GOPROXY https://goproxy.cn + +COPY . . + +RUN make linux-test + +FROM docker.io/hamstershare/debian_docker_cli:20240308 + +COPY --from=builder /app/aline-test /usr/local/bin/aline-test ENV PORT=8080 ENV GRPC_PORT=50001 diff --git a/Dockerfile_base b/Dockerfile_base index 78381497..79967cfe 100644 --- a/Dockerfile_base +++ b/Dockerfile_base @@ -33,3 +33,5 @@ RUN "$HOME/.cargo/bin/rustup" default stable && \ "$HOME/.cargo/bin/rustup" update nightly && \ "$HOME/.cargo/bin/rustup" target add wasm32-unknown-unknown --toolchain nightly + +RUN npm install -g truffle