Skip to content

Commit

Permalink
branch update
Browse files Browse the repository at this point in the history
  • Loading branch information
mohaijiang committed Mar 20, 2024
1 parent 0887009 commit dc1daf3
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 44 deletions.
13 changes: 1 addition & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions deploy-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 51 additions & 2 deletions pkg/controller/login_handler.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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"
db2 "github.com/hamster-shared/hamster-develop/pkg/db"
"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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
28 changes: 3 additions & 25 deletions pkg/controller/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
50 changes: 50 additions & 0 deletions pkg/db/migration/v41_repository_branch_support.sql
Original file line number Diff line number Diff line change
@@ -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 '代码提交信息';
60 changes: 57 additions & 3 deletions pkg/service/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
}
}
44 changes: 44 additions & 0 deletions pkg/service/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
1 change: 0 additions & 1 deletion pkg/service/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit dc1daf3

Please sign in to comment.