Skip to content

Commit

Permalink
Merge branch 'hamster-shared:master' into icp
Browse files Browse the repository at this point in the history
  • Loading branch information
skyh24 authored Mar 11, 2024
2 parents ff7d720 + d521f0b commit b48f43f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 9 deletions.
16 changes: 13 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile_base
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions pkg/controller/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions pkg/controller/project_handler.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package controller

import (
"context"
"embed"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/hamster-shared/aline-engine/logger"
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions pkg/parameter/project_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ type K8sDeployParam struct {
ServicePort int32 `json:"servicePort"`
ServiceTargetPort int32 `json:"serviceTargetPort"`
}

type UpdateProjectBranch struct {
Branch string `json:"branch"`
}
13 changes: 13 additions & 0 deletions pkg/service/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
15 changes: 9 additions & 6 deletions pkg/utils/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit b48f43f

Please sign in to comment.