From 794f8fbc0db91dddccefc0c22d9419990d7a323a Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Fri, 8 Mar 2024 14:00:45 +0800 Subject: [PATCH 01/16] 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 02/16] 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 From db10ce74b6bcf427c0049cc3b0c9c71ec956908d Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Mon, 11 Mar 2024 16:28:16 +0800 Subject: [PATCH 03/16] update --- pkg/service/project.go | 14 ++++++++++++++ pkg/vo/project_vo.go | 1 + 2 files changed, 15 insertions(+) diff --git a/pkg/service/project.go b/pkg/service/project.go index 83fc72c9..a3d7c38d 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -1,6 +1,7 @@ package service import ( + "context" "encoding/json" "errors" "fmt" @@ -127,6 +128,19 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro } data.RecentBuild = recentBuild data.RecentCheck = recentCheck + githubService := application.GetBean[*GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := ParsingGitHubURL(project.RepositoryUrl) + if err != nil { + branches, err2 := githubService.ListRepositoryBranch(ctx, owner, repo) + if err2 != nil { + data.AllBranch = []string{data.Branch} + } + data.AllBranch = branches + } else { + data.AllBranch = []string{data.Branch} + } + projectList = append(projectList, data) } } diff --git a/pkg/vo/project_vo.go b/pkg/vo/project_vo.go index c32d98b8..a4081112 100644 --- a/pkg/vo/project_vo.go +++ b/pkg/vo/project_vo.go @@ -29,6 +29,7 @@ type ProjectListVo struct { RecentCheck RecentCheckVo `json:"recentCheck"` RecentBuild RecentBuildVo `json:"recentBuild"` RecentDeploy interface{} `json:"recentDeploy"` + AllBranch []string `json:"allBranch"` } type ProjectDetailVo struct { From eaef9615956c5c54a5c9f05de2b6587212d7a3c0 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Mon, 11 Mar 2024 16:52:37 +0800 Subject: [PATCH 04/16] update --- pkg/service/project.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/service/project.go b/pkg/service/project.go index a3d7c38d..2c938a3b 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -89,6 +89,22 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro } } } + + // branches + + githubService := application.GetBean[*GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) + if err != nil { + branches, err2 := githubService.ListRepositoryBranch(ctx, owner, repo) + if err2 != nil { + data.AllBranch = []string{data.Branch} + } + data.AllBranch = branches + } else { + data.AllBranch = []string{data.Branch} + } + //recentDeploy var workflowDeployData db2.WorkflowDetail if projectType == int(consts.CONTRACT) { @@ -128,18 +144,6 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro } data.RecentBuild = recentBuild data.RecentCheck = recentCheck - githubService := application.GetBean[*GithubService]("githubService") - ctx, _ := context.WithTimeout(context.Background(), time.Second*20) - owner, repo, err := ParsingGitHubURL(project.RepositoryUrl) - if err != nil { - branches, err2 := githubService.ListRepositoryBranch(ctx, owner, repo) - if err2 != nil { - data.AllBranch = []string{data.Branch} - } - data.AllBranch = branches - } else { - data.AllBranch = []string{data.Branch} - } projectList = append(projectList, data) } From b8753ab9539e7f8e60ea90ba59f0c4d123442fa0 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Mon, 11 Mar 2024 17:03:44 +0800 Subject: [PATCH 05/16] update --- pkg/service/project.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/service/project.go b/pkg/service/project.go index 2c938a3b..5b9e2ddc 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -91,18 +91,17 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro } // branches - githubService := application.GetBean[*GithubService]("githubService") ctx, _ := context.WithTimeout(context.Background(), time.Second*20) owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) if err != nil { + data.AllBranch = []string{data.Branch} + } else { branches, err2 := githubService.ListRepositoryBranch(ctx, owner, repo) if err2 != nil { data.AllBranch = []string{data.Branch} } data.AllBranch = branches - } else { - data.AllBranch = []string{data.Branch} } //recentDeploy From b7eb86f1e0f004805c9a136377cc44811e447e44 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Mon, 11 Mar 2024 17:43:23 +0800 Subject: [PATCH 06/16] update Project Overview page --- pkg/db/workflow.go | 3 +++ pkg/service/project.go | 6 +++--- pkg/service/workflow.go | 1 + pkg/service/workflow_sync.go | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/db/workflow.go b/pkg/db/workflow.go index c5ede28e..410a4e12 100644 --- a/pkg/db/workflow.go +++ b/pkg/db/workflow.go @@ -34,6 +34,9 @@ type WorkflowDetail struct { Status uint StartTime time.Time Duration int64 + Branch string + CommitId string + CommitInfo string CreateTime time.Time `gorm:"column:create_time;default:current_timestamp" json:"create_time"` UpdateTime time.Time `json:"update_time"` DeleteTime gorm.DeletedAt `gorm:"index;column:delete_time;" json:"delete_time"` diff --git a/pkg/service/project.go b/pkg/service/project.go index 5b9e2ddc..be0c183d 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -74,11 +74,11 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro var workflowBuildData db2.WorkflowDetail var workflowCheckData db2.WorkflowDetail _ = copier.Copy(&data, &project) - err := p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ?", project.Id, consts.Check).Order("start_time DESC").Limit(1).Find(&workflowCheckData).Error + err := p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ? and branch = ?", project.Id, consts.Check, project.Branch).Order("start_time DESC").Limit(1).Find(&workflowCheckData).Error if err == nil { _ = copier.Copy(&recentCheck, workflowCheckData) } - err = p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ?", project.Id, consts.Build).Order("start_time DESC").Limit(1).Find(&workflowBuildData).Error + err = p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ? and branch = ?", project.Id, consts.Build, project.Branch).Order("start_time DESC").Limit(1).Find(&workflowBuildData).Error if err == nil { _ = copier.Copy(&recentBuild, &workflowBuildData) if projectType == int(consts.CONTRACT) { @@ -130,7 +130,7 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro } else { var packageDeploy vo.PackageDeployVo var deployData db2.FrontendDeploy - err = p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ?", project.Id, consts.Deploy).Order("create_time DESC").Limit(1).Find(&workflowDeployData).Error + err = p.db.Model(db2.WorkflowDetail{}).Where("project_id = ? and type = ? and branch = ?", project.Id, consts.Deploy, project.Branch).Order("create_time DESC").Limit(1).Find(&workflowDeployData).Error if err == nil { copier.Copy(&packageDeploy, workflowDeployData) err = p.db.Model(db2.FrontendDeploy{}).Where("project_id = ? and workflow_detail_id = ? ", project.Id, workflowDeployData.Id).Order("deploy_time DESC").Limit(1).Find(&deployData).Error diff --git a/pkg/service/workflow.go b/pkg/service/workflow.go index 3b4b58b3..6a05c955 100644 --- a/pkg/service/workflow.go +++ b/pkg/service/workflow.go @@ -391,6 +391,7 @@ func (w *WorkflowService) ExecProjectWorkflow(project db.Project, user vo.UserAu //Status: uint(detail.Status), Status: 1, StartTime: detail.StartTime, + Branch: project.Branch, CreateTime: time.Now(), UpdateTime: time.Now(), } diff --git a/pkg/service/workflow_sync.go b/pkg/service/workflow_sync.go index 9a46a0b8..8cd08c31 100644 --- a/pkg/service/workflow_sync.go +++ b/pkg/service/workflow_sync.go @@ -70,6 +70,9 @@ func (w *WorkflowService) SyncStatus(message model.StatusChangeMessage) { workflowDetail.CodeBranch = codeInfo.Branch workflowDetail.CodeInfo = fmt.Sprintf("%s | commit on %s | %s", codeInfo.CommitId, codeInfo.CommitDate, codeInfo.CommitMessage) workflowDetail.Duration = jobDetail.Duration + workflowDetail.CommitInfo = codeInfo.CommitMessage + workflowDetail.CommitId = codeInfo.CommitId + workflowDetail.Branch = codeInfo.Branch if workflowDetail.Status != uint(message.Status) { // 如果 detail 的状态和 message 的状态不一致,可能是因为 detail 是从文件读取的,读取时还没有保存最新的状态,以 message 的状态为准 From d9148cb0c2bbcd839b8b49059892bf981125e13f Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Tue, 12 Mar 2024 11:03:05 +0800 Subject: [PATCH 07/16] fix github api rate limit --- pkg/controller/project_handler.go | 8 ++++++-- pkg/service/github.go | 4 ++-- pkg/service/project.go | 6 +++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index 91322524..33708f54 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -72,8 +72,10 @@ func (h *HandlerServer) projectList(gin *gin.Context) { } user, _ := userAny.(db2.User) userId = int(user.Id) + tokenAny, _ := gin.Get("token") + token, _ := tokenAny.(string) if userId != 0 { - data, err := h.projectService.GetProjects(userId, query, page, size, projectType) + data, err := h.projectService.GetProjects(userId, token, query, page, size, projectType) if err != nil { Fail(err.Error(), gin) return @@ -1708,7 +1710,9 @@ func (h *HandlerServer) getProjectRepositoryBranch(gin *gin.Context) { Fail(err.Error(), gin) return } - branches, err := githubService.ListRepositoryBranch(ctx, owner, repo) + tokenAny, _ := gin.Get("token") + token, _ := tokenAny.(string) + branches, err := githubService.ListRepositoryBranch(ctx, token, owner, repo) if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/service/github.go b/pkg/service/github.go index 4d4b5c74..5b8a6945 100644 --- a/pkg/service/github.go +++ b/pkg/service/github.go @@ -1013,8 +1013,8 @@ func (g *GithubService) QueryRepos(installationId int64, page, size int, query s return repoPage, nil } -func (g *GithubService) ListRepositoryBranch(ctx context.Context, owner, repoName string) ([]string, error) { - client := utils.NewGithubClient(g.ctx, "") +func (g *GithubService) ListRepositoryBranch(ctx context.Context, token string, owner, repoName string) ([]string, error) { + client := utils.NewGithubClient(g.ctx, token) branches, _, err := client.Repositories.ListBranches(ctx, owner, repoName, &github.BranchListOptions{}) if err != nil { return nil, err diff --git a/pkg/service/project.go b/pkg/service/project.go index be0c183d..7727bf6b 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -23,7 +23,7 @@ import ( ) type IProjectService interface { - GetProjects(userId int, keyword string, page, size, projectType int) (*vo.ProjectPage, error) + GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) HandleProjectsByUserId(user db2.User, page, size int, token, filter string) (vo.RepoListPage, error) CreateProject(createData vo.CreateProjectParam) (uuid.UUID, error) GetProject(id string) (*vo.ProjectDetailVo, error) @@ -51,7 +51,7 @@ func (p *ProjectService) Init(db *gorm.DB) { p.db = db } -func (p *ProjectService) GetProjects(userId int, keyword string, page, size, projectType int) (*vo.ProjectPage, error) { +func (p *ProjectService) GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) { var total int64 var projectPage vo.ProjectPage var projects []db2.Project @@ -97,7 +97,7 @@ func (p *ProjectService) GetProjects(userId int, keyword string, page, size, pro if err != nil { data.AllBranch = []string{data.Branch} } else { - branches, err2 := githubService.ListRepositoryBranch(ctx, owner, repo) + branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) if err2 != nil { data.AllBranch = []string{data.Branch} } From ce5098cc1fbf0537e3fea8449e437e559931eada Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Tue, 12 Mar 2024 16:10:22 +0800 Subject: [PATCH 08/16] project detail return all branch --- pkg/controller/log_handler.go | 2 +- pkg/controller/project_handler.go | 32 ++++++++++-------------------- pkg/controller/workflow_handler.go | 2 +- pkg/db/contract.go | 2 ++ pkg/db/frontend_deploy.go | 2 ++ pkg/db/frontend_package.go | 2 ++ pkg/db/report.go | 3 +++ pkg/service/contract.go | 2 +- pkg/service/project.go | 21 ++++++++++++++++++-- pkg/service/workflow_sync.go | 20 +++++++++++++++++-- pkg/vo/project_vo.go | 1 + pkg/vo/workflow_vo.go | 2 ++ 12 files changed, 63 insertions(+), 28 deletions(-) diff --git a/pkg/controller/log_handler.go b/pkg/controller/log_handler.go index d784383b..dee1d0e7 100644 --- a/pkg/controller/log_handler.go +++ b/pkg/controller/log_handler.go @@ -169,7 +169,7 @@ func (h *HandlerServer) getDeployFrontendLog(gin *gin.Context) { //userAny, _ := gin.Get("user") //user, _ := userAny.(db2.User) - project, err := h.projectService.GetProject(projectIdStr) + project, err := h.projectService.GetProject(projectIdStr, "") if err != nil { log.Println("get project failed", err.Error()) Fail(err.Error(), gin) diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index 33708f54..d51758e9 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -185,7 +185,7 @@ func (h *HandlerServer) importProject(g *gin.Context) { return } // get project(check detail, build detail) - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), g) return @@ -313,7 +313,7 @@ func (h *HandlerServer) createProject(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -472,7 +472,7 @@ func (h *HandlerServer) createProjectByCodeV2(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), gin) return @@ -663,7 +663,7 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -750,7 +750,9 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { func (h *HandlerServer) projectDetail(gin *gin.Context) { id := gin.Param("id") - data, err := h.projectService.GetProject(id) + tokenAny, _ := gin.Get("token") + token, _ := tokenAny.(string) + data, err := h.projectService.GetProject(id, token) if err != nil { Fail(err.Error(), gin) return @@ -1074,7 +1076,7 @@ func (h *HandlerServer) queryAptosParams(g *gin.Context) { } // 先查询到此项目的 github 仓库信息 - data, err := h.projectService.GetProject(projectID) + data, err := h.projectService.GetProject(projectID, "") if err != nil { Fail(err.Error(), g) return @@ -1316,23 +1318,11 @@ func (h *HandlerServer) updateProject(gin *gin.Context) { } user, _ := userAny.(db2.User) updateData.UserId = int(user.Id) - project, err := h.projectService.GetProject(id) + project, err := h.projectService.GetProject(id, "") if err != nil { Fail(err.Error(), gin) return } - //githubService := application.GetBean[*service.GithubService]("githubService") - //repo, res, err := githubService.UpdateRepo(token, user.Username, project.Name, updateData.Name) - //if err != nil { - // if res != nil { - // if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden { - // Failed(http.StatusUnauthorized, "access not authorized", gin) - // return - // } - // } - // Fail(err.Error(), gin) - // return - //} updateData.RepositoryUrl = project.RepositoryUrl err = h.projectService.UpdateProject(id, updateData) if err != nil { @@ -1430,7 +1420,7 @@ func (h *HandlerServer) createProjectByCode(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), gin) return @@ -1518,7 +1508,7 @@ func (h *HandlerServer) workflowSetting(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id) + project, err := h.projectService.GetProject(id, "") if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/controller/workflow_handler.go b/pkg/controller/workflow_handler.go index c99b764d..30f0689d 100644 --- a/pkg/controller/workflow_handler.go +++ b/pkg/controller/workflow_handler.go @@ -176,7 +176,7 @@ func (h *HandlerServer) contractFileContent(gin *gin.Context) { } userAny, _ := gin.Get("user") user, _ := userAny.(db2.User) - data, err := h.projectService.GetProject(idStr) + data, err := h.projectService.GetProject(idStr, "") if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/db/contract.go b/pkg/db/contract.go index 39469601..5a9848eb 100644 --- a/pkg/db/contract.go +++ b/pkg/db/contract.go @@ -24,6 +24,8 @@ type Contract struct { Type uint `json:"type"` // see #consts.ProjectFrameType Status uint `json:"status"` // 1: deploying, 2: success , 3: fail Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` CodeInfo string `json:"codeInfo"` } diff --git a/pkg/db/frontend_deploy.go b/pkg/db/frontend_deploy.go index 1fc0d698..ebfbdcc6 100644 --- a/pkg/db/frontend_deploy.go +++ b/pkg/db/frontend_deploy.go @@ -17,6 +17,8 @@ type FrontendDeploy struct { Image string `json:"image"` Version string `json:"version"` Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` Domain string `json:"domain"` DeployInfo string `json:"deployInfo"` DeployTime sql.NullTime `json:"deployTime"` diff --git a/pkg/db/frontend_package.go b/pkg/db/frontend_package.go index 18362cd9..eba1d7b2 100644 --- a/pkg/db/frontend_package.go +++ b/pkg/db/frontend_package.go @@ -13,6 +13,8 @@ type FrontendPackage struct { Name string `json:"name"` Version string `json:"version"` Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` Domain string `json:"domain"` BuildTime time.Time `json:"buildTime"` PackageIdentity string `json:"packageIdentity"` diff --git a/pkg/db/report.go b/pkg/db/report.go index 5c7e75c0..b718902d 100644 --- a/pkg/db/report.go +++ b/pkg/db/report.go @@ -21,4 +21,7 @@ type Report struct { Issues int `json:"issues"` MetaScanOverview string `json:"metaScanOverview"` CreateTime time.Time `gorm:"column:create_time;default:current_timestamp" json:"createTime"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } diff --git a/pkg/service/contract.go b/pkg/service/contract.go index 1ff73406..bc314925 100644 --- a/pkg/service/contract.go +++ b/pkg/service/contract.go @@ -209,7 +209,7 @@ func (c *ContractService) SaveDeploy(deployParam parameter.ContractDeployParam) } projectService := application.GetBean[*ProjectService]("projectService") - project, err := projectService.GetProject(projectId.String()) + project, err := projectService.GetProject(projectId.String(), "") _ = copier.Copy(&entity, &deployParam) entity.DeployTime = time.Now() entity.ProjectId = projectId diff --git a/pkg/service/project.go b/pkg/service/project.go index 7727bf6b..75b10635 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -26,7 +26,7 @@ type IProjectService interface { GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) HandleProjectsByUserId(user db2.User, page, size int, token, filter string) (vo.RepoListPage, error) CreateProject(createData vo.CreateProjectParam) (uuid.UUID, error) - GetProject(id string) (*vo.ProjectDetailVo, error) + GetProject(id string, token string) (*vo.ProjectDetailVo, error) UpdateProject(id string, updateData vo.UpdateProjectParam) error DeleteProject(id string) error UpdateProjectParams(id string, updateData vo.UpdateProjectParams) error @@ -177,7 +177,7 @@ func (p *ProjectService) CreateProject(createData vo.CreateProjectParam) (uuid.U return project.Id, errors.New(fmt.Sprintf("application:%s already exists", createData.Name)) } -func (p *ProjectService) GetProject(id string) (*vo.ProjectDetailVo, error) { +func (p *ProjectService) GetProject(id string, token string) (*vo.ProjectDetailVo, error) { var data db2.Project var detail vo.ProjectDetailVo result := p.db.Where("id = ? ", id).First(&data) @@ -243,6 +243,23 @@ func (p *ProjectService) GetProject(id string) (*vo.ProjectDetailVo, error) { } detail.RecentBuild = recentBuild detail.RecentCheck = recentCheck + + if token != "" { + // branches + githubService := application.GetBean[*GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) + if err != nil { + detail.AllBranch = []string{data.Branch} + } else { + branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) + if err2 != nil { + detail.AllBranch = []string{data.Branch} + } + detail.AllBranch = branches + } + } + return &detail, nil } diff --git a/pkg/service/workflow_sync.go b/pkg/service/workflow_sync.go index b105d40d..4bb72ad5 100644 --- a/pkg/service/workflow_sync.go +++ b/pkg/service/workflow_sync.go @@ -145,7 +145,9 @@ func (w *WorkflowService) syncFrontendBuild(detail *model.JobDetail, workflowDet WorkflowDetailId: workflowDetail.Id, Name: projectName, Version: fmt.Sprintf("%d", workflowDetail.ExecNumber), - Branch: workflowDetail.CodeInfo, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, BuildTime: workflowDetail.CreateTime, CreateTime: time.Now(), } @@ -206,6 +208,9 @@ func (w *WorkflowService) syncFrontendDeploy(detail *model.JobDetail, workflowDe packageDeploy.DeployTime = sql.NullTime{Time: time.Now(), Valid: true} packageDeploy.Name = data.Name packageDeploy.Branch = data.Branch + packageDeploy.CommitId = data.CommitId + packageDeploy.CommitInfo = data.CommitInfo + packageDeploy.CreateTime = time.Now() packageDeploy.Image = image err = w.db.Save(&packageDeploy).Error @@ -322,6 +327,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow Issues: int(datum.Total), ToolType: consts.CheckToolTypeMap[datum.Tool], MetaScanOverview: datum.ResultOverview, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } w.db.Create(&report) } @@ -369,6 +377,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow CreateTime: time.Now(), Issues: contractCheckResult.Total, ToolType: consts.CheckToolTypeMap[contractCheckResult.Tool], + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } reportList = append(reportList, report) } @@ -387,6 +398,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow ReportFile: string(report.Content), CreateTime: time.Now(), ToolType: 5, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } reportList = append(reportList, report) } @@ -434,8 +448,10 @@ func (w *WorkflowService) syncContractStarknet(projectId uuid.UUID, workflowId u CreateTime: time.Now(), Type: uint(consts.StarkWare), Status: consts.STATUS_SUCCESS, - Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } return w.saveContractToDatabase(&contract) diff --git a/pkg/vo/project_vo.go b/pkg/vo/project_vo.go index a4081112..02434b0d 100644 --- a/pkg/vo/project_vo.go +++ b/pkg/vo/project_vo.go @@ -47,6 +47,7 @@ type ProjectDetailVo struct { RecentBuild RecentBuildVo `json:"recentBuild"` RecentDeploy interface{} `json:"recentDeploy"` EvmTemplateType uint `json:"evmTemplateType"` + AllBranch []string `json:"allBranch"` } type RecentCheckVo struct { diff --git a/pkg/vo/workflow_vo.go b/pkg/vo/workflow_vo.go index 261a052c..7d0fba4b 100644 --- a/pkg/vo/workflow_vo.go +++ b/pkg/vo/workflow_vo.go @@ -28,6 +28,8 @@ type WorkflowVo struct { Duration int64 `json:"duration"` Engine string `json:"engine"` // workflow,arrange_execute Version string `json:"version"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` } type WorkflowDetailVo struct { From 2b6dff46b41f981bf7f6da9e7759b509a5998eb8 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Tue, 12 Mar 2024 16:10:22 +0800 Subject: [PATCH 09/16] interceptor github Token --- pkg/controller/log_handler.go | 2 +- pkg/controller/login_handler.go | 13 ++++++++++++ pkg/controller/project_handler.go | 32 ++++++++++-------------------- pkg/controller/workflow_handler.go | 2 +- pkg/db/contract.go | 2 ++ pkg/db/frontend_deploy.go | 2 ++ pkg/db/frontend_package.go | 2 ++ pkg/db/report.go | 3 +++ pkg/service/contract.go | 2 +- pkg/service/project.go | 21 ++++++++++++++++++-- pkg/service/user.go | 14 +++++++++++++ pkg/service/workflow_sync.go | 20 +++++++++++++++++-- pkg/utils/github_client.go | 15 ++++++-------- pkg/vo/project_vo.go | 1 + pkg/vo/workflow_vo.go | 2 ++ 15 files changed, 96 insertions(+), 37 deletions(-) diff --git a/pkg/controller/log_handler.go b/pkg/controller/log_handler.go index d784383b..dee1d0e7 100644 --- a/pkg/controller/log_handler.go +++ b/pkg/controller/log_handler.go @@ -169,7 +169,7 @@ func (h *HandlerServer) getDeployFrontendLog(gin *gin.Context) { //userAny, _ := gin.Get("user") //user, _ := userAny.(db2.User) - project, err := h.projectService.GetProject(projectIdStr) + project, err := h.projectService.GetProject(projectIdStr, "") if err != nil { log.Println("get project failed", err.Error()) Fail(err.Error(), gin) diff --git a/pkg/controller/login_handler.go b/pkg/controller/login_handler.go index 0787fe48..a5fd1808 100644 --- a/pkg/controller/login_handler.go +++ b/pkg/controller/login_handler.go @@ -384,6 +384,19 @@ func (h *HandlerServer) JwtAuthorize() gin.HandlerFunc { gin.Abort() return } + + if user.Token == "" { + githubService := application.GetBean[*service.GithubService]("githubService") + installId, err := userService.GetGithubInstallId(user.Id) + if err != nil { + installToken, err := githubService.GetToken(installId) + if err != nil { + user.Token = installToken.GetToken() + _ = userService.SaveUserToken(user.Id, user.Token) + } + } + } + githubToken = user.Token gin.Set("user", user) gin.Set("userId", user.Id) diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index 33708f54..d51758e9 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -185,7 +185,7 @@ func (h *HandlerServer) importProject(g *gin.Context) { return } // get project(check detail, build detail) - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), g) return @@ -313,7 +313,7 @@ func (h *HandlerServer) createProject(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -472,7 +472,7 @@ func (h *HandlerServer) createProjectByCodeV2(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), gin) return @@ -663,7 +663,7 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -750,7 +750,9 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { func (h *HandlerServer) projectDetail(gin *gin.Context) { id := gin.Param("id") - data, err := h.projectService.GetProject(id) + tokenAny, _ := gin.Get("token") + token, _ := tokenAny.(string) + data, err := h.projectService.GetProject(id, token) if err != nil { Fail(err.Error(), gin) return @@ -1074,7 +1076,7 @@ func (h *HandlerServer) queryAptosParams(g *gin.Context) { } // 先查询到此项目的 github 仓库信息 - data, err := h.projectService.GetProject(projectID) + data, err := h.projectService.GetProject(projectID, "") if err != nil { Fail(err.Error(), g) return @@ -1316,23 +1318,11 @@ func (h *HandlerServer) updateProject(gin *gin.Context) { } user, _ := userAny.(db2.User) updateData.UserId = int(user.Id) - project, err := h.projectService.GetProject(id) + project, err := h.projectService.GetProject(id, "") if err != nil { Fail(err.Error(), gin) return } - //githubService := application.GetBean[*service.GithubService]("githubService") - //repo, res, err := githubService.UpdateRepo(token, user.Username, project.Name, updateData.Name) - //if err != nil { - // if res != nil { - // if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden { - // Failed(http.StatusUnauthorized, "access not authorized", gin) - // return - // } - // } - // Fail(err.Error(), gin) - // return - //} updateData.RepositoryUrl = project.RepositoryUrl err = h.projectService.UpdateProject(id, updateData) if err != nil { @@ -1430,7 +1420,7 @@ func (h *HandlerServer) createProjectByCode(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String()) + project, err := h.projectService.GetProject(id.String(), "") if err != nil { Fail(err.Error(), gin) return @@ -1518,7 +1508,7 @@ func (h *HandlerServer) workflowSetting(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id) + project, err := h.projectService.GetProject(id, "") if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/controller/workflow_handler.go b/pkg/controller/workflow_handler.go index c99b764d..30f0689d 100644 --- a/pkg/controller/workflow_handler.go +++ b/pkg/controller/workflow_handler.go @@ -176,7 +176,7 @@ func (h *HandlerServer) contractFileContent(gin *gin.Context) { } userAny, _ := gin.Get("user") user, _ := userAny.(db2.User) - data, err := h.projectService.GetProject(idStr) + data, err := h.projectService.GetProject(idStr, "") if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/db/contract.go b/pkg/db/contract.go index 39469601..5a9848eb 100644 --- a/pkg/db/contract.go +++ b/pkg/db/contract.go @@ -24,6 +24,8 @@ type Contract struct { Type uint `json:"type"` // see #consts.ProjectFrameType Status uint `json:"status"` // 1: deploying, 2: success , 3: fail Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` CodeInfo string `json:"codeInfo"` } diff --git a/pkg/db/frontend_deploy.go b/pkg/db/frontend_deploy.go index 1fc0d698..ebfbdcc6 100644 --- a/pkg/db/frontend_deploy.go +++ b/pkg/db/frontend_deploy.go @@ -17,6 +17,8 @@ type FrontendDeploy struct { Image string `json:"image"` Version string `json:"version"` Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` Domain string `json:"domain"` DeployInfo string `json:"deployInfo"` DeployTime sql.NullTime `json:"deployTime"` diff --git a/pkg/db/frontend_package.go b/pkg/db/frontend_package.go index 18362cd9..eba1d7b2 100644 --- a/pkg/db/frontend_package.go +++ b/pkg/db/frontend_package.go @@ -13,6 +13,8 @@ type FrontendPackage struct { Name string `json:"name"` Version string `json:"version"` Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` Domain string `json:"domain"` BuildTime time.Time `json:"buildTime"` PackageIdentity string `json:"packageIdentity"` diff --git a/pkg/db/report.go b/pkg/db/report.go index 5c7e75c0..b718902d 100644 --- a/pkg/db/report.go +++ b/pkg/db/report.go @@ -21,4 +21,7 @@ type Report struct { Issues int `json:"issues"` MetaScanOverview string `json:"metaScanOverview"` CreateTime time.Time `gorm:"column:create_time;default:current_timestamp" json:"createTime"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } diff --git a/pkg/service/contract.go b/pkg/service/contract.go index 1ff73406..bc314925 100644 --- a/pkg/service/contract.go +++ b/pkg/service/contract.go @@ -209,7 +209,7 @@ func (c *ContractService) SaveDeploy(deployParam parameter.ContractDeployParam) } projectService := application.GetBean[*ProjectService]("projectService") - project, err := projectService.GetProject(projectId.String()) + project, err := projectService.GetProject(projectId.String(), "") _ = copier.Copy(&entity, &deployParam) entity.DeployTime = time.Now() entity.ProjectId = projectId diff --git a/pkg/service/project.go b/pkg/service/project.go index 7727bf6b..75b10635 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -26,7 +26,7 @@ type IProjectService interface { GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) HandleProjectsByUserId(user db2.User, page, size int, token, filter string) (vo.RepoListPage, error) CreateProject(createData vo.CreateProjectParam) (uuid.UUID, error) - GetProject(id string) (*vo.ProjectDetailVo, error) + GetProject(id string, token string) (*vo.ProjectDetailVo, error) UpdateProject(id string, updateData vo.UpdateProjectParam) error DeleteProject(id string) error UpdateProjectParams(id string, updateData vo.UpdateProjectParams) error @@ -177,7 +177,7 @@ func (p *ProjectService) CreateProject(createData vo.CreateProjectParam) (uuid.U return project.Id, errors.New(fmt.Sprintf("application:%s already exists", createData.Name)) } -func (p *ProjectService) GetProject(id string) (*vo.ProjectDetailVo, error) { +func (p *ProjectService) GetProject(id string, token string) (*vo.ProjectDetailVo, error) { var data db2.Project var detail vo.ProjectDetailVo result := p.db.Where("id = ? ", id).First(&data) @@ -243,6 +243,23 @@ func (p *ProjectService) GetProject(id string) (*vo.ProjectDetailVo, error) { } detail.RecentBuild = recentBuild detail.RecentCheck = recentCheck + + if token != "" { + // branches + githubService := application.GetBean[*GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) + if err != nil { + detail.AllBranch = []string{data.Branch} + } else { + branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) + if err2 != nil { + detail.AllBranch = []string{data.Branch} + } + detail.AllBranch = branches + } + } + return &detail, nil } diff --git a/pkg/service/user.go b/pkg/service/user.go index 90c0dc02..e4233186 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -8,6 +8,7 @@ import ( type IUserService interface { GetUserByToken(token string) (db2.User, error) + GetGithubInstallId(userId uint) (int64, error) } type UserService struct { @@ -42,6 +43,15 @@ func (u *UserService) GetUserById(id int64) (db2.User, error) { return user, nil } +func (u *UserService) GetGithubInstallId(userId uint) (int64, error) { + var gitAppInstall db2.GitAppInstall + err := u.db.Model(db2.GitAppInstall{}).Where("user_id = ?", userId).First(&gitAppInstall).Error + if err != nil { + return 0, err + } + return gitAppInstall.InstallId, nil +} + func (u *UserService) GetUserCount() (int64, error) { var count int64 if err := u.db.Model(&db2.User{}).Count(&count).Error; err != nil { @@ -82,3 +92,7 @@ func (u *UserService) GetUserWalletById(id int) (db2.UserWallet, error) { func (u *UserService) UpdateUserWallet(userWallet db2.UserWallet) error { return u.db.Save(&userWallet).Error } + +func (u *UserService) SaveUserToken(id uint, token string) error { + return u.db.Model(db2.User{}).Where("id = ?", id).Update("token", token).Error +} diff --git a/pkg/service/workflow_sync.go b/pkg/service/workflow_sync.go index b105d40d..4bb72ad5 100644 --- a/pkg/service/workflow_sync.go +++ b/pkg/service/workflow_sync.go @@ -145,7 +145,9 @@ func (w *WorkflowService) syncFrontendBuild(detail *model.JobDetail, workflowDet WorkflowDetailId: workflowDetail.Id, Name: projectName, Version: fmt.Sprintf("%d", workflowDetail.ExecNumber), - Branch: workflowDetail.CodeInfo, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, BuildTime: workflowDetail.CreateTime, CreateTime: time.Now(), } @@ -206,6 +208,9 @@ func (w *WorkflowService) syncFrontendDeploy(detail *model.JobDetail, workflowDe packageDeploy.DeployTime = sql.NullTime{Time: time.Now(), Valid: true} packageDeploy.Name = data.Name packageDeploy.Branch = data.Branch + packageDeploy.CommitId = data.CommitId + packageDeploy.CommitInfo = data.CommitInfo + packageDeploy.CreateTime = time.Now() packageDeploy.Image = image err = w.db.Save(&packageDeploy).Error @@ -322,6 +327,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow Issues: int(datum.Total), ToolType: consts.CheckToolTypeMap[datum.Tool], MetaScanOverview: datum.ResultOverview, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } w.db.Create(&report) } @@ -369,6 +377,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow CreateTime: time.Now(), Issues: contractCheckResult.Total, ToolType: consts.CheckToolTypeMap[contractCheckResult.Tool], + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } reportList = append(reportList, report) } @@ -387,6 +398,9 @@ func (w *WorkflowService) SyncReport(message model.StatusChangeMessage, workflow ReportFile: string(report.Content), CreateTime: time.Now(), ToolType: 5, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } reportList = append(reportList, report) } @@ -434,8 +448,10 @@ func (w *WorkflowService) syncContractStarknet(projectId uuid.UUID, workflowId u CreateTime: time.Now(), Type: uint(consts.StarkWare), Status: consts.STATUS_SUCCESS, - Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } return w.saveContractToDatabase(&contract) diff --git a/pkg/utils/github_client.go b/pkg/utils/github_client.go index df556dd2..301ace6d 100644 --- a/pkg/utils/github_client.go +++ b/pkg/utils/github_client.go @@ -7,13 +7,10 @@ import ( ) func NewGithubClient(ctx context.Context, token string) *github.Client { - if token != "" { - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, - ) - tc := oauth2.NewClient(ctx, ts) - return github.NewClient(tc) - } else { - return github.NewClient(nil) - } + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + return github.NewClient(tc) + } diff --git a/pkg/vo/project_vo.go b/pkg/vo/project_vo.go index a4081112..02434b0d 100644 --- a/pkg/vo/project_vo.go +++ b/pkg/vo/project_vo.go @@ -47,6 +47,7 @@ type ProjectDetailVo struct { RecentBuild RecentBuildVo `json:"recentBuild"` RecentDeploy interface{} `json:"recentDeploy"` EvmTemplateType uint `json:"evmTemplateType"` + AllBranch []string `json:"allBranch"` } type RecentCheckVo struct { diff --git a/pkg/vo/workflow_vo.go b/pkg/vo/workflow_vo.go index 261a052c..7d0fba4b 100644 --- a/pkg/vo/workflow_vo.go +++ b/pkg/vo/workflow_vo.go @@ -28,6 +28,8 @@ type WorkflowVo struct { Duration int64 `json:"duration"` Engine string `json:"engine"` // workflow,arrange_execute Version string `json:"version"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` } type WorkflowDetailVo struct { From 39cbb2dbca98efd8d9c2ad0c2afa3f92d56e3991 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Thu, 14 Mar 2024 13:52:11 +0800 Subject: [PATCH 10/16] setup redis --- .gitignore | 1 + cmd/daemon.go | 19 +++++++++- go.mod | 3 ++ pkg/controller/login_handler.go | 12 ------- pkg/db/backend.go | 5 +++ pkg/db/contract.go | 3 ++ pkg/service/contract.go | 14 ++++---- pkg/service/github.go | 2 +- pkg/service/project.go | 61 ++++++++++++++++++++++++--------- pkg/service/workflow_sync.go | 11 ++++++ pkg/utils/github_client.go | 34 ++++++++++++++++++ pkg/vo/contract_vo.go | 17 ++++++--- 12 files changed, 141 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index e5c56d1a..e758945c 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ pipeline2.yml /pipeline3.yml /pkg/service/arrange_test.go pkg/service/bnb_test.go +/hamster-test-private-key.pem diff --git a/cmd/daemon.go b/cmd/daemon.go index 9ab3f595..7be01433 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -9,6 +9,7 @@ import ( "github.com/hamster-shared/hamster-develop/pkg/application" "github.com/hamster-shared/hamster-develop/pkg/controller" "github.com/hamster-shared/hamster-develop/pkg/service" + "github.com/redis/go-redis/v9" "github.com/spf13/cobra" "gorm.io/driver/mysql" "gorm.io/gorm" @@ -58,7 +59,23 @@ to quickly create a Cobra application.`, if err != nil { return } + application.SetBean[*gorm.DB]("db", db) + redisHost := os.Getenv("REDIS_HOST") + if redisHost == "" { + redisHost = "127.0.0.1" + } + redisPort := os.Getenv("REDIS_PORT") + if redisPort == "" { + redisPort = "6379" + } + + rdb := redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%s", redisHost, redisPort), + Password: "", // no password set + DB: 0, // use default DB + }) + application.SetBean[*redis.Client]("rdb", rdb) application.SetBean[engine.Engine]("engine", Engine) workflowService := service.NewWorkflowService() application.SetBean[*service.WorkflowService]("workflowService", workflowService) @@ -83,7 +100,7 @@ to quickly create a Cobra application.`, icpService := service.NewIcpService(icNetwork) application.SetBean[*service.IcpService]("icpService", icpService) templateService.Init(db) - projectService.Init(db) + projectService.Init(db, rdb) application.SetBean[service.IProjectService]("projectService", projectService) arrangeService := service.NewArrangeService() application.SetBean[*service.ArrangeService]("arrangeService", arrangeService) diff --git a/go.mod b/go.mod index bad95d9e..a0f5aac5 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/jinzhu/copier v0.3.5 github.com/mohaijiang/agent-go v0.3.1 github.com/pkg/errors v0.9.1 + github.com/redis/go-redis/v9 v9.5.1 github.com/samber/lo v1.36.0 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 @@ -45,11 +46,13 @@ require ( github.com/aviate-labs/secp256k1 v0.0.0-5e6736a // indirect github.com/benbjohnson/clock v1.3.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cloudflare/circl v1.3.3 // indirect github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set/v2 v2.1.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/di-wu/parser v0.3.0 // indirect github.com/emicklei/go-restful/v3 v3.10.2 // indirect github.com/evanphx/json-patch v5.6.0+incompatible // indirect diff --git a/pkg/controller/login_handler.go b/pkg/controller/login_handler.go index a5fd1808..36bfb023 100644 --- a/pkg/controller/login_handler.go +++ b/pkg/controller/login_handler.go @@ -385,18 +385,6 @@ func (h *HandlerServer) JwtAuthorize() gin.HandlerFunc { return } - if user.Token == "" { - githubService := application.GetBean[*service.GithubService]("githubService") - installId, err := userService.GetGithubInstallId(user.Id) - if err != nil { - installToken, err := githubService.GetToken(installId) - if err != nil { - user.Token = installToken.GetToken() - _ = userService.SaveUserToken(user.Id, user.Token) - } - } - } - githubToken = user.Token gin.Set("user", user) gin.Set("userId", user.Id) diff --git a/pkg/db/backend.go b/pkg/db/backend.go index 296e720f..25e95757 100644 --- a/pkg/db/backend.go +++ b/pkg/db/backend.go @@ -21,6 +21,8 @@ type BackendPackage struct { Status consts.DeployStatus `json:"status"` // see #consts. Branch string `json:"branch"` CodeInfo string `json:"codeInfo"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } type BackendDeploy struct { @@ -39,4 +41,7 @@ type BackendDeploy struct { Status consts.DeployStatus `json:"status"` // 1: deploying, 2: success , 3: fail AbiInfo string `json:"abiInfo"` Name string `json:"name"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } diff --git a/pkg/db/contract.go b/pkg/db/contract.go index 5a9848eb..682846a7 100644 --- a/pkg/db/contract.go +++ b/pkg/db/contract.go @@ -43,4 +43,7 @@ type ContractDeploy struct { DeployTxHash string `json:"deployTxHash"` Status uint `json:"status"` // 1: deploying, 2: success , 3: fail AbiInfo string `json:"abiInfo"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } diff --git a/pkg/service/contract.go b/pkg/service/contract.go index bc314925..d1a0c416 100644 --- a/pkg/service/contract.go +++ b/pkg/service/contract.go @@ -275,7 +275,7 @@ func (c *ContractService) QueryContracts(projectId string, query, version, netwo return c.QueryContractsForICP(projectId, query, version, network, page, size) } - sql := fmt.Sprintf("select id, project_id,workflow_id,workflow_detail_id,name,version,group_concat( DISTINCT `network` SEPARATOR ',' ) as network,build_time,abi_info,byte_code,create_time from t_contract where project_id = ? ") + sql := fmt.Sprintf("select id, project_id,workflow_id,workflow_detail_id,name,version,group_concat( DISTINCT `network` SEPARATOR ',' ) as network,build_time,abi_info,byte_code,create_time,branch,commit_id,commit_info from t_contract where project_id = ? ") if query != "" && version != "" && network != "" { sql = sql + "and name like CONCAT('%',?,'%') and version = ? and network like CONCAT('%',?,'%') group by id order by create_time desc" c.db.Raw(sql, projectId, query, version, network).Scan(&contracts) @@ -532,18 +532,18 @@ func (c *ContractService) GetCodeInfoByVersion(projectId, version string) (vo.Co } if project.FrameType == consts.InternetComputer { - res := c.db.Model(db2.BackendPackage{}).Select("version", "branch", "code_info").Where("project_id = ? and version = ?", projectId, version).Order("create_time desc").Limit(1).First(&contractVersionAndCodeInfoVo) + res := c.db.Model(db2.BackendPackage{}).Select("version", "branch", "commit_id", "commit_info").Where("project_id = ? and version = ?", projectId, version).Order("create_time desc").Limit(1).First(&contractVersionAndCodeInfoVo) if res.Error != nil { return contractVersionAndCodeInfoVo, res.Error } } else { - res := c.db.Model(db2.Contract{}).Select("version", "branch", "code_info").Where("project_id = ? and version = ?", projectId, version).Order("create_time desc").Limit(1).First(&contractVersionAndCodeInfoVo) + res := c.db.Model(db2.Contract{}).Select("version", "branch", "commit_id", "commit_info").Where("project_id = ? and version = ?", projectId, version).Order("create_time desc").Limit(1).First(&contractVersionAndCodeInfoVo) if res.Error != nil { return contractVersionAndCodeInfoVo, res.Error } } contractVersionAndCodeInfoVo.Type = int(project.Type) - contractVersionAndCodeInfoVo.Url = project.RepositoryUrl + "/" + contractVersionAndCodeInfoVo.Branch + contractVersionAndCodeInfoVo.Url = project.RepositoryUrl return contractVersionAndCodeInfoVo, nil } @@ -645,8 +645,10 @@ func (c *ContractService) GetContractDeployInfo(id int) (vo.ContractDeployVo, er } copier.Copy(&result, &contractDeploy) result.ContractName = contract.Name - result.Url = project.RepositoryUrl + "/" + contract.Branch - result.CodeInfo = contract.CodeInfo + result.Url = project.RepositoryUrl + result.Branch = contract.Branch + result.CommitId = contract.CommitId + result.CommitInfo = contract.CommitInfo return result, err } diff --git a/pkg/service/github.go b/pkg/service/github.go index 5b8a6945..29839dc4 100644 --- a/pkg/service/github.go +++ b/pkg/service/github.go @@ -1014,7 +1014,7 @@ func (g *GithubService) QueryRepos(installationId int64, page, size int, query s } func (g *GithubService) ListRepositoryBranch(ctx context.Context, token string, owner, repoName string) ([]string, error) { - client := utils.NewGithubClient(g.ctx, token) + client := utils.NewGithubClientWithEmpty() branches, _, err := client.Repositories.ListBranches(ctx, owner, repoName, &github.BranchListOptions{}) if err != nil { return nil, err diff --git a/pkg/service/project.go b/pkg/service/project.go index 75b10635..cbd12927 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -14,6 +14,7 @@ import ( "github.com/hamster-shared/hamster-develop/pkg/vo" uuid "github.com/iris-contrib/go.uuid" "github.com/jinzhu/copier" + "github.com/redis/go-redis/v9" "gorm.io/gorm" "log" "os/exec" @@ -40,15 +41,17 @@ type IProjectService interface { } type ProjectService struct { - db *gorm.DB + db *gorm.DB + rdb *redis.Client } func NewProjectService() *ProjectService { return &ProjectService{} } -func (p *ProjectService) Init(db *gorm.DB) { +func (p *ProjectService) Init(db *gorm.DB, rdb *redis.Client) { p.db = db + p.rdb = rdb } func (p *ProjectService) GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) { @@ -91,16 +94,10 @@ func (p *ProjectService) GetProjects(userId int, token string, keyword string, p } // branches - githubService := application.GetBean[*GithubService]("githubService") - ctx, _ := context.WithTimeout(context.Background(), time.Second*20) - owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) + branches, err := p.getProjectBranches(data.RepositoryUrl, token) if err != nil { data.AllBranch = []string{data.Branch} } else { - branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) - if err2 != nil { - data.AllBranch = []string{data.Branch} - } data.AllBranch = branches } @@ -246,16 +243,10 @@ func (p *ProjectService) GetProject(id string, token string) (*vo.ProjectDetailV if token != "" { // branches - githubService := application.GetBean[*GithubService]("githubService") - ctx, _ := context.WithTimeout(context.Background(), time.Second*20) - owner, repo, err := ParsingGitHubURL(data.RepositoryUrl) + branches, err := p.getProjectBranches(data.RepositoryUrl, token) if err != nil { detail.AllBranch = []string{data.Branch} } else { - branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) - if err2 != nil { - detail.AllBranch = []string{data.Branch} - } detail.AllBranch = branches } } @@ -507,3 +498,41 @@ func (p *ProjectService) UpdateProjectBranch(id string, userId int64, branch str return p.db.Model(&db2.Project{}).Where("id", id).Update("branch", branch).Error } + +func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) ([]string, error) { + + key := fmt.Sprintf("PROJECT_BRANCH:%s", repositoryUrl) + + ctx := context.Background() + exists, err := p.rdb.Exists(ctx, key).Result() + + if exists == 0 || err != nil { + // branches + githubService := application.GetBean[*GithubService]("githubService") + ctx, _ := context.WithTimeout(context.Background(), time.Second*20) + owner, repo, err := ParsingGitHubURL(repositoryUrl) + if err != nil { + return nil, err + } else { + branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) + if err2 != nil { + return nil, err + } + + for _, branch := range branches { + _, err = p.rdb.RPush(ctx, key, branch).Result() + _, err = p.rdb.Expire(ctx, key, time.Hour*12).Result() + } + + return branches, err + } + + } else { + branches, err := p.rdb.LRange(ctx, key, 0, -1).Result() + if err != nil { + return nil, err + } else { + return branches, nil + } + } +} diff --git a/pkg/service/workflow_sync.go b/pkg/service/workflow_sync.go index 4bb72ad5..79dc066e 100644 --- a/pkg/service/workflow_sync.go +++ b/pkg/service/workflow_sync.go @@ -481,6 +481,8 @@ func (w *WorkflowService) syncContractAptos(projectId uuid.UUID, workflowId uint Status: consts.STATUS_SUCCESS, Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } err = w.saveContractToDatabase(&contract) if err != nil { @@ -550,6 +552,8 @@ func (w *WorkflowService) syncContractSui(projectId uuid.UUID, workflowId uint, Status: consts.STATUS_SUCCESS, Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } // logger.Tracef("aptos contract: %+v", contract) @@ -628,6 +632,9 @@ func (w *WorkflowService) syncContractSolana(projectId uuid.UUID, workflowId uin CreateTime: time.Now(), Type: uint(consts.Solana), Status: consts.STATUS_SUCCESS, + Branch: workflowDetail.Branch, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } return w.saveContractToDatabase(&contract) @@ -658,6 +665,8 @@ func (w *WorkflowService) syncContractEvm(projectId uuid.UUID, workflowId uint, Status: consts.STATUS_SUCCESS, Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } return w.saveContractToDatabase(&contract) } @@ -735,6 +744,8 @@ func (w *WorkflowService) syncInternetComputerBuild(projectId uuid.UUID, workflo Status: consts.DEPLOY_STATUS_SUCCESS, Branch: workflowDetail.CodeBranch, CodeInfo: workflowDetail.CodeInfo, + CommitId: workflowDetail.CommitId, + CommitInfo: workflowDetail.CommitInfo, } err := w.db.Save(&backendPackage).Error if err != nil { diff --git a/pkg/utils/github_client.go b/pkg/utils/github_client.go index 301ace6d..8d54521e 100644 --- a/pkg/utils/github_client.go +++ b/pkg/utils/github_client.go @@ -2,8 +2,14 @@ package utils import ( "context" + "errors" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v48/github" + "github.com/hamster-shared/aline-engine/logger" "golang.org/x/oauth2" + "net/http" + "os" + "strconv" ) func NewGithubClient(ctx context.Context, token string) *github.Client { @@ -12,5 +18,33 @@ func NewGithubClient(ctx context.Context, token string) *github.Client { ) tc := oauth2.NewClient(ctx, ts) return github.NewClient(tc) +} + +func NewGithubClientWithPrivateKey() (*github.Client, error) { + appIdString, exist := os.LookupEnv("GITHUB_APP_ID") + if !exist { + logger.Errorf("please contact the administrator to configure 'GITHUB_APP_ID'") + return nil, errors.New("please contact the administrator to configure 'GITHUB_APP_ID'") + } + appId, err := strconv.Atoi(appIdString) + if err != nil { + logger.Errorf("app id format failed:%s", err) + return nil, err + } + appPemPath, exist := os.LookupEnv("GITHUB_APP_PEM") + if !exist { + logger.Errorf("please contact the administrator to configure 'GITHUB_APP_PEM'") + return nil, errors.New("please contact the administrator to configure 'GITHUB_APP_PEM'") + } + atr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, int64(appId), appPemPath) + if err != nil { + logger.Errorf("get github client by private key failed:%s", err) + return nil, err + } + client := github.NewClient(&http.Client{Transport: atr}) + return client, nil +} +func NewGithubClientWithEmpty() *github.Client { + return github.NewClient(nil) } diff --git a/pkg/vo/contract_vo.go b/pkg/vo/contract_vo.go index aa9007b2..f467d92f 100644 --- a/pkg/vo/contract_vo.go +++ b/pkg/vo/contract_vo.go @@ -42,11 +42,13 @@ type ContractVo struct { } type ContractVersionAndCodeInfoVo struct { - Version string `json:"version"` - Type int `json:"type"` - Branch string `json:"branch"` - CodeInfo string `json:"codeInfo"` - Url string `json:"url"` + Version string `json:"version"` + Type int `json:"type"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` + CodeInfo string `json:"codeInfo"` + Url string `json:"url"` } type ContractDeployVo struct { @@ -66,6 +68,9 @@ type ContractDeployVo struct { AbiInfo string `json:"abiInfo"` Url string `json:"url"` CodeInfo string `json:"codeInfo"` + Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` } type ContractArtifactsVo struct { @@ -84,6 +89,8 @@ type ContractArtifactsVo struct { Type uint `json:"type"` // see #consts.ProjectFrameType Status uint `json:"status"` // 1: deploying, 2: success , 3: fail Branch string `json:"branch"` + CommitId string `json:"commitId"` + CommitInfo string `json:"commitInfo"` CodeInfo string `json:"codeInfo"` LastContractDeployId uint `json:"lastContractDeployId"` } From 98b068114cd2ac189a33d13779ef816284a0b4e3 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Thu, 14 Mar 2024 14:13:12 +0800 Subject: [PATCH 11/16] setup redis --- Dockerfile | 1 + deploy-master.yml | 2 ++ pkg/service/project.go | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ec0e9890..3bdc4ae4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,6 +19,7 @@ ENV DB_USER=root ENV DB_PASSWORD=123456 ENV DB_HOST=127.0.0.1 ENV DB_PORT=3306 +ENV REDIS_HOST=redis ENV DB_NAME=aline EXPOSE ${PORT} EXPOSE ${GRPC_PORT} diff --git a/deploy-master.yml b/deploy-master.yml index 8beb3a17..61490bdf 100644 --- a/deploy-master.yml +++ b/deploy-master.yml @@ -144,6 +144,8 @@ spec: key: APPS_RW_CLIENT_SECRETS - name: GITHUB_APP_RW_PEM value: "/home/ubuntu/rwGithubApp/hamster-rw-private-key.pem" + - name: REDIS_HOST + value: redis ports: - containerPort: 8080 volumeMounts: diff --git a/pkg/service/project.go b/pkg/service/project.go index cbd12927..34993a29 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -516,7 +516,8 @@ func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) } else { branches, err2 := githubService.ListRepositoryBranch(ctx, token, owner, repo) if err2 != nil { - return nil, err + fmt.Println(err2) + return nil, err2 } for _, branch := range branches { From dde67fb06c73d24601bf73a04e92f1487b236751 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Thu, 14 Mar 2024 14:23:57 +0800 Subject: [PATCH 12/16] setup redis --- pkg/service/project.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/service/project.go b/pkg/service/project.go index 34993a29..60a53413 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -501,12 +501,17 @@ func (p *ProjectService) UpdateProjectBranch(id string, userId int64, branch str func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) ([]string, error) { + fmt.Println("getProjectBranches") key := fmt.Sprintf("PROJECT_BRANCH:%s", repositoryUrl) ctx := context.Background() exists, err := p.rdb.Exists(ctx, key).Result() - if exists == 0 || err != nil { + fmt.Println("exists:", exists) + fmt.Println("exists err : ", err) + + if exists == 0 { + fmt.Println("api query github repo branch") // branches githubService := application.GetBean[*GithubService]("githubService") ctx, _ := context.WithTimeout(context.Background(), time.Second*20) @@ -529,6 +534,7 @@ func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) } } else { + fmt.Println("redis query github repo branch") branches, err := p.rdb.LRange(ctx, key, 0, -1).Result() if err != nil { return nil, err From e3abbb62d373beb65b9ebd31d699eb3e33c1b7ba Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Thu, 14 Mar 2024 14:36:59 +0800 Subject: [PATCH 13/16] setup redis --- cmd/daemon.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/daemon.go b/cmd/daemon.go index 7be01433..6976b230 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -70,8 +70,12 @@ to quickly create a Cobra application.`, redisPort = "6379" } + redisAddr := fmt.Sprintf("%s:%s", redisHost, redisPort) + + fmt.Println("redis addr : ", redisAddr) + rdb := redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%s", redisHost, redisPort), + Addr: redisAddr, Password: "", // no password set DB: 0, // use default DB }) From 0887009495237536df5bfdde6a4bf1f4d53f3e74 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Thu, 14 Mar 2024 14:47:26 +0800 Subject: [PATCH 14/16] setup redis --- cmd/daemon.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/daemon.go b/cmd/daemon.go index 6976b230..26fe012e 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -70,6 +70,7 @@ to quickly create a Cobra application.`, redisPort = "6379" } + fmt.Println(redisHost) redisAddr := fmt.Sprintf("%s:%s", redisHost, redisPort) fmt.Println("redis addr : ", redisAddr) From dc1daf38b1331a6e241cad544d7d463f95cb07a9 Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Wed, 20 Mar 2024 15:16:18 +0800 Subject: [PATCH 15/16] branch update --- Dockerfile | 13 +--- deploy-master.yml | 6 ++ pkg/controller/gin.go | 1 - pkg/controller/login_handler.go | 53 +++++++++++++++- pkg/controller/project_handler.go | 28 +-------- .../v41_repository_branch_support.sql | 50 ++++++++++++++++ pkg/service/github.go | 60 ++++++++++++++++++- pkg/service/github_test.go | 44 ++++++++++++++ pkg/service/project.go | 1 - pkg/utils/strings.go | 10 ++++ 10 files changed, 222 insertions(+), 44 deletions(-) create mode 100644 pkg/db/migration/v41_repository_branch_support.sql diff --git a/Dockerfile b/Dockerfile index 3bdc4ae4..80053221 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,6 @@ -FROM golang:1.20.2 as builder - -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 +COPY ./aline-test /usr/local/bin/aline-test ENV PORT=8080 ENV GRPC_PORT=50001 diff --git a/deploy-master.yml b/deploy-master.yml index 61490bdf..ba6df979 100644 --- a/deploy-master.yml +++ b/deploy-master.yml @@ -146,6 +146,12 @@ spec: value: "/home/ubuntu/rwGithubApp/hamster-rw-private-key.pem" - name: REDIS_HOST value: redis + - name: REDIS_PORT + value: "6379" + - name: GITHUB_BRANCH_WEBHOOK_NAME + value: hamster_test_webhook + - name: GITHUB_BRANCH_WEBHOOK_URL + value: https://develop.hamster.newtouch.com/api/v2/github/branch-webhook ports: - containerPort: 8080 volumeMounts: diff --git a/pkg/controller/gin.go b/pkg/controller/gin.go index 253eba41..90c22baf 100644 --- a/pkg/controller/gin.go +++ b/pkg/controller/gin.go @@ -62,7 +62,6 @@ 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) diff --git a/pkg/controller/login_handler.go b/pkg/controller/login_handler.go index 36bfb023..f629e5e9 100644 --- a/pkg/controller/login_handler.go +++ b/pkg/controller/login_handler.go @@ -1,9 +1,12 @@ package controller import ( + "context" + "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" + "github.com/google/go-github/v48/github" "github.com/hamster-shared/aline-engine/logger" "github.com/hamster-shared/hamster-develop/pkg/application" "github.com/hamster-shared/hamster-develop/pkg/consts" @@ -11,6 +14,7 @@ import ( "github.com/hamster-shared/hamster-develop/pkg/parameter" "github.com/hamster-shared/hamster-develop/pkg/service" "github.com/hamster-shared/hamster-develop/pkg/utils" + "github.com/redis/go-redis/v9" "gorm.io/gorm" "log" "net/http" @@ -132,13 +136,25 @@ func (h *HandlerServer) githubWebHook(gin *gin.Context) { func (h *HandlerServer) githubWebHookV2(gin *gin.Context) { event := gin.GetHeader("X-GitHub-Event") githubService := application.GetBean[*service.GithubService]("githubService") - var githubInstall parameter.GithubWebHookInstall - err := gin.BindJSON(&githubInstall) + rdb := application.GetBean[*redis.Client]("rdb") + + bytes, err := gin.GetRawData() + + logger.Info("event name: ", event) + logger.Info("github webhook: ", string(bytes)) + + //err := gin.BindJSON(&githubInstall) if err != nil { Fail(err.Error(), gin) return } if event == "installation" { + var githubInstall parameter.GithubWebHookInstall + err = json.Unmarshal(bytes, &githubInstall) + if err != nil { + Fail(err.Error(), gin) + return + } if githubInstall.Action == "created" { githubService.HandleAppsInstall(githubInstall, consts.SAVE_INSTALL, "GITHUB_APP_ID", "GITHUB_APP_PEM") err = githubService.HandlerInstallData(githubInstall.Installation.GetID(), githubInstall.Installation.GetAppID(), consts.INSTALLATION_CREATED) @@ -160,6 +176,12 @@ func (h *HandlerServer) githubWebHookV2(gin *gin.Context) { } } if event == "installation_repositories" { + var githubInstall parameter.GithubWebHookInstall + err = json.Unmarshal(bytes, &githubInstall) + if err != nil { + Fail(err.Error(), gin) + return + } githubService.UpdateRepositorySelection(githubInstall.Installation.GetID(), githubInstall.Installation.GetRepositorySelection()) if githubInstall.Action == "added" { //err = githubService.HandlerInstallData(githubInstall.Installation.GetID(), consts.REPO_ADDED) @@ -179,6 +201,33 @@ func (h *HandlerServer) githubWebHookV2(gin *gin.Context) { } } } + if event == "create" { + var createEvent github.CreateEvent + err = json.Unmarshal(bytes, &createEvent) + if err != nil { + Fail(err.Error(), gin) + return + } + if createEvent.GetRefType() == "branch" { + key := fmt.Sprintf("PROJECT_BRANCH:https://github.com/%s.git", createEvent.GetRepo().GetFullName()) + ctx, _ := context.WithTimeout(context.Background(), time.Second*10) + _, err = rdb.RPush(ctx, key, createEvent.GetRef()).Result() + } + } + if event == "delete" { + var deleteEvent github.DeleteEvent + err = json.Unmarshal(bytes, &deleteEvent) + if err != nil { + Fail(err.Error(), gin) + return + } + if deleteEvent.GetRefType() == "branch" { + key := fmt.Sprintf("PROJECT_BRANCH:https://github.com/%s.git", deleteEvent.GetRepo().GetFullName()) + ctx, _ := context.WithTimeout(context.Background(), time.Second*10) + _, err = rdb.LRem(ctx, key, 0, deleteEvent.GetRef()).Result() + } + } + } func (h *HandlerServer) githubWebHookRw(gin *gin.Context) { diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index d51758e9..330d6a6d 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -127,6 +127,9 @@ func (h *HandlerServer) importProject(g *gin.Context) { return } + ctx, _ := context.WithTimeout(context.Background(), time.Second*10) + githubService.CreateRepoBranchWebhook(ctx, token, owner, name) + data := vo.CreateProjectParam{ Name: importData.Name, Type: importData.Type, @@ -1685,31 +1688,6 @@ 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 - } - tokenAny, _ := gin.Get("token") - token, _ := tokenAny.(string) - branches, err := githubService.ListRepositoryBranch(ctx, token, 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") diff --git a/pkg/db/migration/v41_repository_branch_support.sql b/pkg/db/migration/v41_repository_branch_support.sql new file mode 100644 index 00000000..aa38b7e0 --- /dev/null +++ b/pkg/db/migration/v41_repository_branch_support.sql @@ -0,0 +1,50 @@ +alter table t_backend_package + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_backend_package + add commit_info varchar(100) null comment '代码提交信息'; + +alter table t_frontend_package + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_frontend_package + add commit_info varchar(100) null comment '代码提交信息'; + +alter table t_workflow_detail + add branch varchar(50) null comment '分支信息'; + +alter table t_workflow_detail + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_workflow_detail + add commit_info varchar(100) null comment '代码提交信息'; + +alter table t_contract + add commit_id varchar(50) null comment '代码提交id'; +alter table t_contract + add commit_info varchar(100) null comment '代码提交信息'; + +alter table t_frontend_deploy + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_frontend_deploy + add commit_info varchar(100) null comment '代码提交信息'; + + +alter table t_contract_deploy + add branch varchar(50) null comment '分支信息'; + +alter table t_contract_deploy + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_contract_deploy + add commit_info varchar(100) null comment '代码提交信息'; + +alter table t_backend_deploy + add branch varchar(50) null comment '分支信息'; + +alter table t_backend_deploy + add commit_id varchar(50) null comment '代码提交id'; + +alter table t_backend_deploy + add commit_info varchar(100) null comment '代码提交信息'; diff --git a/pkg/service/github.go b/pkg/service/github.go index 29839dc4..95bf846c 100644 --- a/pkg/service/github.go +++ b/pkg/service/github.go @@ -878,7 +878,7 @@ func (g *GithubService) HandleAppsInstallRw(appInstallData parameter.GithubWebHo return nil }) if err != nil { - logger.Errorf("save install user failed,user is: ", user.GetLogin()) + //logger.Errorf("save install user failed,user is: ", user.GetLogin()) continue } } @@ -1014,13 +1014,67 @@ func (g *GithubService) QueryRepos(installationId int64, page, size int, query s } func (g *GithubService) ListRepositoryBranch(ctx context.Context, token string, owner, repoName string) ([]string, error) { - client := utils.NewGithubClientWithEmpty() + client := utils.NewGithubClient(ctx, token) 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 } + +func (g *GithubService) CreateRepoBranchWebhook(ctx context.Context, token string, owner, repo string) { + + webhookName := os.Getenv("GITHUB_BRANCH_WEBHOOK_NAME") + if webhookName == "" { + webhookName = "hamster_webhook" + } + + webhookUrl := os.Getenv("GITHUB_BRANCH_WEBHOOK_URL") + + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + hooks, _, err := client.Repositories.ListHooks(ctx, owner, repo, &github.ListOptions{}) + if err != nil { + logger.Error("webhook query fail: ", err.Error()) + return + } + + for _, hook := range hooks { + if hook.GetName() == webhookName { + if utils.ContainsString(hook.Events, "create") && utils.ContainsString(hook.Events, "delete") { + return + } else { + hook.Events = append(hook.Events, "create") + hook.Events = append(hook.Events, "delete") + _, _, err := client.Repositories.EditHook(ctx, owner, repo, hook.GetID(), hook) + if err != nil { + logger.Error("edit hook fail: ", err.Error()) + return + } + } + } + } + + // 创建 webhook 配置 + hook := &github.Hook{ + Name: github.String(webhookName), + Events: []string{"create", "delete"}, + Config: map[string]interface{}{ + "url": webhookUrl, + "content_type": "json", + }, + Active: github.Bool(true), + } + + // 添加 webhook + _, _, err = client.Repositories.CreateHook(ctx, owner, repo, hook) + if err != nil { + log.Fatal("Error creating webhook:", err) + } +} diff --git a/pkg/service/github_test.go b/pkg/service/github_test.go index 611bc90c..0f3618e0 100644 --- a/pkg/service/github_test.go +++ b/pkg/service/github_test.go @@ -5,8 +5,10 @@ import ( "fmt" "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v48/github" + "github.com/hamster-shared/aline-engine/logger" "github.com/hamster-shared/hamster-develop/pkg/consts" "github.com/hamster-shared/hamster-develop/pkg/utils" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "golang.org/x/oauth2" "io" @@ -197,3 +199,45 @@ func TestGetRepo(t *testing.T) { fmt.Println(fmt.Sprintf("SHA信息 %s, 提交时间是 %s, Message是 %s \n", sha, time, message)) } } + +func TestCreateWebhook(t *testing.T) { + logger.Init().ToStdoutAndFile().SetLevel(logrus.DebugLevel) + + token := "ghs_grsdp6pxRyGBU9Kk0DjP6xKulQFV0X19O19r" + // 创建一个 GitHub 客户端 + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + + // 设置要添加 webhook 的仓库信息 + owner := "mohaijiang" + repo := "my-vue-branch-test" + + // 创建 webhook 配置 + hook := &github.Hook{ + Name: github.String("hamster"), + Events: []string{"create", "delete"}, + Config: map[string]interface{}{ + "url": "https://develop.hamster.newtouch.com/api/v2/github/branch-webhook", + "content_type": "json", + }, + Active: github.Bool(true), + } + + // 添加 webhook + _, _, err := client.Repositories.CreateHook(ctx, owner, repo, hook) + if err != nil { + log.Fatal("Error creating webhook:", err) + } + + hooks, _, err := client.Repositories.ListHooks(ctx, owner, repo, &github.ListOptions{}) + if err != nil { + return + } + fmt.Println(hooks) + + log.Println("Webhook created successfully") +} diff --git a/pkg/service/project.go b/pkg/service/project.go index 60a53413..0821bdc8 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -527,7 +527,6 @@ func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) for _, branch := range branches { _, err = p.rdb.RPush(ctx, key, branch).Result() - _, err = p.rdb.Expire(ctx, key, time.Hour*12).Result() } return branches, err diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index b5c9e06a..796e5b52 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -26,3 +26,13 @@ func RemoveDuplicatesAndJoin(input string, split string) string { // 将切片重新合并为字符串,以逗号分隔 return strings.Join(result, split) } + +// 判断字符串数组是否包含特定字符串 +func ContainsString(arr []string, target string) bool { + for _, str := range arr { + if str == target { + return true + } + } + return false +} From cf14553fda3530e13bd6d97e31b982f4b194038c Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Wed, 20 Mar 2024 17:22:07 +0800 Subject: [PATCH 16/16] fix branch issue --- deploy-master.yml | 2 +- pkg/controller/log_handler.go | 2 +- pkg/controller/project_handler.go | 58 ++++++++++++++++++++++-------- pkg/controller/workflow_handler.go | 2 +- pkg/service/contract.go | 4 +-- pkg/service/github.go | 10 ++++-- pkg/service/project.go | 29 ++++++++++----- 7 files changed, 77 insertions(+), 30 deletions(-) diff --git a/deploy-master.yml b/deploy-master.yml index ba6df979..09a4809b 100644 --- a/deploy-master.yml +++ b/deploy-master.yml @@ -151,7 +151,7 @@ spec: - name: GITHUB_BRANCH_WEBHOOK_NAME value: hamster_test_webhook - name: GITHUB_BRANCH_WEBHOOK_URL - value: https://develop.hamster.newtouch.com/api/v2/github/branch-webhook + value: https://develop.hamster.newtouch.com/api/v2/github/webhook ports: - containerPort: 8080 volumeMounts: diff --git a/pkg/controller/log_handler.go b/pkg/controller/log_handler.go index dee1d0e7..62cb8527 100644 --- a/pkg/controller/log_handler.go +++ b/pkg/controller/log_handler.go @@ -169,7 +169,7 @@ func (h *HandlerServer) getDeployFrontendLog(gin *gin.Context) { //userAny, _ := gin.Get("user") //user, _ := userAny.(db2.User) - project, err := h.projectService.GetProject(projectIdStr, "") + project, err := h.projectService.GetProject(projectIdStr, 0) if err != nil { log.Println("get project failed", err.Error()) Fail(err.Error(), gin) diff --git a/pkg/controller/project_handler.go b/pkg/controller/project_handler.go index 330d6a6d..84f4e259 100644 --- a/pkg/controller/project_handler.go +++ b/pkg/controller/project_handler.go @@ -3,6 +3,7 @@ package controller import ( "context" "embed" + "errors" "fmt" "io/ioutil" "log" @@ -72,10 +73,8 @@ func (h *HandlerServer) projectList(gin *gin.Context) { } user, _ := userAny.(db2.User) userId = int(user.Id) - tokenAny, _ := gin.Get("token") - token, _ := tokenAny.(string) if userId != 0 { - data, err := h.projectService.GetProjects(userId, token, query, page, size, projectType) + data, err := h.projectService.GetProjects(userId, query, page, size, projectType) if err != nil { Fail(err.Error(), gin) return @@ -112,6 +111,7 @@ func (h *HandlerServer) importProject(g *gin.Context) { return } token := tokenData.GetToken() + fmt.Println("import token: ", token) // parsing url owner, name, err := service.ParsingGitHubURL(importData.CloneURL) if err != nil { @@ -188,7 +188,7 @@ func (h *HandlerServer) importProject(g *gin.Context) { return } // get project(check detail, build detail) - project, err := h.projectService.GetProject(id.String(), "") + project, err := h.projectService.GetProject(id.String(), 0) if err != nil { Fail(err.Error(), g) return @@ -316,7 +316,7 @@ func (h *HandlerServer) createProject(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String(), "") + project, err := h.projectService.GetProject(id.String(), 0) if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -475,7 +475,7 @@ func (h *HandlerServer) createProjectByCodeV2(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String(), "") + project, err := h.projectService.GetProject(id.String(), 0) if err != nil { Fail(err.Error(), gin) return @@ -666,7 +666,7 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { Fail(err.Error(), g) return } - project, err := h.projectService.GetProject(id.String(), "") + project, err := h.projectService.GetProject(id.String(), 0) if err != nil { logger.Error(err) Fail(err.Error(), g) @@ -751,11 +751,41 @@ func (h *HandlerServer) createProjectV2(g *gin.Context) { Success(id, g) } +func getUserFromGin(gin *gin.Context) (*db2.User, error) { + loginType, exit := gin.Get("loginType") + if !exit { + return nil, errors.New("unauthorized") + } + var userAny any + if loginType == consts.GitHub { + userAny, exit = gin.Get("user") + if !exit { + return nil, errors.New("unauthorized") + } + user, _ := userAny.(db2.User) + return &user, nil + } + if loginType == consts.Metamask { + userAny, exit = gin.Get("githubUser") + if !exit { + return nil, errors.New("unauthorized") + } + } + user, _ := userAny.(db2.User) + return &user, nil +} + func (h *HandlerServer) projectDetail(gin *gin.Context) { id := gin.Param("id") - tokenAny, _ := gin.Get("token") - token, _ := tokenAny.(string) - data, err := h.projectService.GetProject(id, token) + + user, err := getUserFromGin(gin) + if err != nil { + Failed(http.StatusUnauthorized, "access not authorized", gin) + return + } + userId := int(user.Id) + + data, err := h.projectService.GetProject(id, userId) if err != nil { Fail(err.Error(), gin) return @@ -1079,7 +1109,7 @@ func (h *HandlerServer) queryAptosParams(g *gin.Context) { } // 先查询到此项目的 github 仓库信息 - data, err := h.projectService.GetProject(projectID, "") + data, err := h.projectService.GetProject(projectID, 0) if err != nil { Fail(err.Error(), g) return @@ -1321,7 +1351,7 @@ func (h *HandlerServer) updateProject(gin *gin.Context) { } user, _ := userAny.(db2.User) updateData.UserId = int(user.Id) - project, err := h.projectService.GetProject(id, "") + project, err := h.projectService.GetProject(id, 0) if err != nil { Fail(err.Error(), gin) return @@ -1423,7 +1453,7 @@ func (h *HandlerServer) createProjectByCode(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id.String(), "") + project, err := h.projectService.GetProject(id.String(), 0) if err != nil { Fail(err.Error(), gin) return @@ -1511,7 +1541,7 @@ func (h *HandlerServer) workflowSetting(gin *gin.Context) { Fail(err.Error(), gin) return } - project, err := h.projectService.GetProject(id, "") + project, err := h.projectService.GetProject(id, 0) if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/controller/workflow_handler.go b/pkg/controller/workflow_handler.go index 30f0689d..4428c0df 100644 --- a/pkg/controller/workflow_handler.go +++ b/pkg/controller/workflow_handler.go @@ -176,7 +176,7 @@ func (h *HandlerServer) contractFileContent(gin *gin.Context) { } userAny, _ := gin.Get("user") user, _ := userAny.(db2.User) - data, err := h.projectService.GetProject(idStr, "") + data, err := h.projectService.GetProject(idStr, 0) if err != nil { Fail(err.Error(), gin) return diff --git a/pkg/service/contract.go b/pkg/service/contract.go index d1a0c416..ed6e89c1 100644 --- a/pkg/service/contract.go +++ b/pkg/service/contract.go @@ -209,7 +209,7 @@ func (c *ContractService) SaveDeploy(deployParam parameter.ContractDeployParam) } projectService := application.GetBean[*ProjectService]("projectService") - project, err := projectService.GetProject(projectId.String(), "") + project, err := projectService.GetProject(projectId.String(), 0) _ = copier.Copy(&entity, &deployParam) entity.DeployTime = time.Now() entity.ProjectId = projectId @@ -331,7 +331,7 @@ func (c *ContractService) QueryContracts(projectId string, query, version, netwo func (c *ContractService) QueryContractsForICP(projectId string, query, version, network string, page int, size int) (vo.Page[vo.ContractArtifactsVo], error) { var backendPackages []db2.BackendPackage var afterData []db2.BackendPackage - sql := fmt.Sprintf("select id, project_id,workflow_id,workflow_detail_id,name,version,group_concat( DISTINCT `network` SEPARATOR ',' ) as network,build_time,abi_info,create_time from t_backend_package where project_id = ? ") + sql := fmt.Sprintf("select id, project_id,workflow_id,workflow_detail_id,name,version,group_concat( DISTINCT `network` SEPARATOR ',' ) as network,build_time,abi_info,create_time,branch,commit_id,commit_info from t_backend_package where project_id = ? ") if query != "" && version != "" && network != "" { sql = sql + "and name like CONCAT('%',?,'%') and version = ? and network like CONCAT('%',?,'%') group by id order by create_time desc" c.db.Raw(sql, projectId, query, version, network).Scan(&backendPackages) diff --git a/pkg/service/github.go b/pkg/service/github.go index 95bf846c..7d01ed2c 100644 --- a/pkg/service/github.go +++ b/pkg/service/github.go @@ -1014,7 +1014,11 @@ func (g *GithubService) QueryRepos(installationId int64, page, size int, query s } func (g *GithubService) ListRepositoryBranch(ctx context.Context, token string, owner, repoName string) ([]string, error) { - client := utils.NewGithubClient(ctx, token) + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) branches, _, err := client.Repositories.ListBranches(ctx, owner, repoName, &github.BranchListOptions{}) if err != nil { return nil, err @@ -1046,7 +1050,7 @@ func (g *GithubService) CreateRepoBranchWebhook(ctx context.Context, token strin } for _, hook := range hooks { - if hook.GetName() == webhookName { + if hook.Config["url"] == webhookUrl { if utils.ContainsString(hook.Events, "create") && utils.ContainsString(hook.Events, "delete") { return } else { @@ -1075,6 +1079,6 @@ func (g *GithubService) CreateRepoBranchWebhook(ctx context.Context, token strin // 添加 webhook _, _, err = client.Repositories.CreateHook(ctx, owner, repo, hook) if err != nil { - log.Fatal("Error creating webhook:", err) + logger.Error("Error creating webhook:", err.Error()) } } diff --git a/pkg/service/project.go b/pkg/service/project.go index 0821bdc8..f5b39f51 100644 --- a/pkg/service/project.go +++ b/pkg/service/project.go @@ -24,10 +24,10 @@ import ( ) type IProjectService interface { - GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) + GetProjects(userId int, keyword string, page, size, projectType int) (*vo.ProjectPage, error) HandleProjectsByUserId(user db2.User, page, size int, token, filter string) (vo.RepoListPage, error) CreateProject(createData vo.CreateProjectParam) (uuid.UUID, error) - GetProject(id string, token string) (*vo.ProjectDetailVo, error) + GetProject(id string, userId int) (*vo.ProjectDetailVo, error) UpdateProject(id string, updateData vo.UpdateProjectParam) error DeleteProject(id string) error UpdateProjectParams(id string, updateData vo.UpdateProjectParams) error @@ -54,7 +54,7 @@ func (p *ProjectService) Init(db *gorm.DB, rdb *redis.Client) { p.rdb = rdb } -func (p *ProjectService) GetProjects(userId int, token string, keyword string, page, size, projectType int) (*vo.ProjectPage, error) { +func (p *ProjectService) GetProjects(userId int, keyword string, page, size, projectType int) (*vo.ProjectPage, error) { var total int64 var projectPage vo.ProjectPage var projects []db2.Project @@ -94,7 +94,7 @@ func (p *ProjectService) GetProjects(userId int, token string, keyword string, p } // branches - branches, err := p.getProjectBranches(data.RepositoryUrl, token) + branches, err := p.getProjectBranches(data.RepositoryUrl, userId) if err != nil { data.AllBranch = []string{data.Branch} } else { @@ -174,7 +174,7 @@ func (p *ProjectService) CreateProject(createData vo.CreateProjectParam) (uuid.U return project.Id, errors.New(fmt.Sprintf("application:%s already exists", createData.Name)) } -func (p *ProjectService) GetProject(id string, token string) (*vo.ProjectDetailVo, error) { +func (p *ProjectService) GetProject(id string, userId int) (*vo.ProjectDetailVo, error) { var data db2.Project var detail vo.ProjectDetailVo result := p.db.Where("id = ? ", id).First(&data) @@ -241,9 +241,9 @@ func (p *ProjectService) GetProject(id string, token string) (*vo.ProjectDetailV detail.RecentBuild = recentBuild detail.RecentCheck = recentCheck - if token != "" { + if userId != 0 { // branches - branches, err := p.getProjectBranches(data.RepositoryUrl, token) + branches, err := p.getProjectBranches(data.RepositoryUrl, userId) if err != nil { detail.AllBranch = []string{data.Branch} } else { @@ -499,9 +499,10 @@ func (p *ProjectService) UpdateProjectBranch(id string, userId int64, branch str return p.db.Model(&db2.Project{}).Where("id", id).Update("branch", branch).Error } -func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) ([]string, error) { +func (p *ProjectService) getProjectBranches(repositoryUrl string, userId int) ([]string, error) { fmt.Println("getProjectBranches") + fmt.Println("userId: ", userId) key := fmt.Sprintf("PROJECT_BRANCH:%s", repositoryUrl) ctx := context.Background() @@ -516,6 +517,16 @@ func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) githubService := application.GetBean[*GithubService]("githubService") ctx, _ := context.WithTimeout(context.Background(), time.Second*20) owner, repo, err := ParsingGitHubURL(repositoryUrl) + + var gitAppInstall db2.GitAppInstall + err = p.db.Model(&db2.GitAppInstall{}).Where("user_id", userId).Where("name", owner).First(&gitAppInstall).Error + + tokenData, err := githubService.GetToken(gitAppInstall.InstallId) + if err != nil { + return nil, err + } + token := tokenData.GetToken() + if err != nil { return nil, err } else { @@ -525,6 +536,8 @@ func (p *ProjectService) getProjectBranches(repositoryUrl string, token string) return nil, err2 } + fmt.Println("query branch result: ", branches) + for _, branch := range branches { _, err = p.rdb.RPush(ctx, key, branch).Result() }