Skip to content

Commit

Permalink
fix branch issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mohaijiang committed Mar 20, 2024
1 parent dc1daf3 commit cf14553
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 30 deletions.
2 changes: 1 addition & 1 deletion deploy-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/log_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 44 additions & 14 deletions pkg/controller/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"embed"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/workflow_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions pkg/service/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}
29 changes: 21 additions & 8 deletions pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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()
}
Expand Down

0 comments on commit cf14553

Please sign in to comment.