From cf14553fda3530e13bd6d97e31b982f4b194038c Mon Sep 17 00:00:00 2001 From: mohaijiang Date: Wed, 20 Mar 2024 17:22:07 +0800 Subject: [PATCH] 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 ba6df97..09a4809 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 dee1d0e..62cb852 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 330d6a6..84f4e25 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 30f0689..4428c0d 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 d1a0c41..ed6e89c 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 95bf846..7d01ed2 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 0821bdc..f5b39f5 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() }