Skip to content

Commit

Permalink
refactor(tibuild): refactor pipeline trigger logic and improve logging
Browse files Browse the repository at this point in the history
- Introduced constants for pipeline IDs and a map for component to repo mapping.
- Replaced fmt.Println with structured logging using zerolog.
- Simplified parameter transformations using maps and strconv.
- Refactored job name extraction and validation logic.
- Removed unused functions and redundant code.
- Updated data structures to include JSON and YAML tags.
- Improved error handling and response formatting in controllers.
- Renamed functions for consistency and clarity.
- Added new dependency in go.mod and removed unused ones.
- Deleted obsolete service and DTO files.

Signed-off-by: wuhuizuo <wuhuizuo@126.com>
  • Loading branch information
wuhuizuo committed Jun 15, 2024
1 parent 1ef1b97 commit 3945030
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 312 deletions.
21 changes: 4 additions & 17 deletions tibuild/api/api_error.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package api

import (
"log"
"net/http"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)

// error return struct definition
Expand Down Expand Up @@ -33,22 +33,9 @@ func APIErrorJSONReporterHandler(errType gin.ErrorType) gin.HandlerFunc {
Code: http.StatusInternalServerError,
Message: err.Error(),
}
log.Println(parsedError)

// var parsedError *APIError
// switch err.(type) {
// case *APIError:
// parsedError = err.(*APIError)
// default:
// parsedError = &APIError{
// Code: http.StatusInternalServerError,
// Message: "Internal Server Error",
// }
// }

// Put the error into response
c.IndentedJSON(parsedError.Code, parsedError)
c.Abort()
log.Error().Err(parsedError).Send()

c.AbortWithStatusJSON(parsedError.Code, parsedError)
return
}

Expand Down
12 changes: 8 additions & 4 deletions tibuild/cmd/branch_create_helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package main

import (
"context"
"fmt"
"os"

"github.com/PingCAP-QE/ee-apps/tibuild/pkg/rest/controller"
"github.com/PingCAP-QE/ee-apps/tibuild/pkg/rest/service"
"github.com/rs/zerolog/log"
)

func main() {
prodName := "tidb"
baseVersion := "v5.4.1"
prod := service.StringToProduct(prodName)
if prod == service.ProductUnknown {
fmt.Println("bad prod name" + prodName)
if prod == "" {
log.Error().Str("name", prodName).Msg("bad product name")
os.Exit(1)
}
controller.NewChatPrintRepoService().CreateBranch(context.TODO(), service.BranchCreateReq{Prod: prod, BaseVersion: baseVersion})

req := service.BranchCreateReq{Prod: prod, BaseVersion: baseVersion}
controller.NewChatPrintRepoService().CreateBranch(context.TODO(), req)
}
13 changes: 9 additions & 4 deletions tibuild/cmd/tag_create_helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"fmt"
"os"

"github.com/rs/zerolog/log"

"github.com/PingCAP-QE/ee-apps/tibuild/pkg/rest/controller"
"github.com/PingCAP-QE/ee-apps/tibuild/pkg/rest/service"
Expand All @@ -12,8 +14,11 @@ func main() {
prodName := "br"
branch := "release-5.4-20220903-v5.4.1"
prod := service.StringToProduct(prodName)
if prod == service.ProductUnknown {
fmt.Println("bad prod name: " + prodName)
if prod == "" {
log.Error().Str("name", prodName).Msg("bad product name")
os.Exit(1)
}
controller.NewChatPrintRepoService().CreateTag(context.TODO(), service.TagCreateReq{Prod: prod, Branch: branch})

req := service.TagCreateReq{Prod: prod, Branch: branch}
controller.NewChatPrintRepoService().CreateTag(context.TODO(), req)
}
2 changes: 1 addition & 1 deletion tibuild/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/bndr/gojenkins v1.1.0
github.com/cloudevents/sdk-go/v2 v2.14.0
github.com/dougsland/jenkinsctl v0.0.1
github.com/gin-contrib/requestid v0.0.6
github.com/gin-contrib/static v0.0.1
github.com/gin-gonic/gin v1.9.1
Expand Down Expand Up @@ -36,7 +37,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dougsland/jenkinsctl v0.0.1
github.com/dougsland/jenkinsctl/jenkins v0.0.0-20210621004651-0e2c28d94c9c // indirect
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
Expand Down
30 changes: 16 additions & 14 deletions tibuild/internal/controller/github_sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package controller

import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"io/ioutil"
"log"
"net/http"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/rs/zerolog/log"
)

type TestGithubSSO struct {
Expand Down Expand Up @@ -38,24 +38,26 @@ func GithubSSOToken(c *gin.Context) {
return
}

// 请求github接口
client := &http.Client{}
// get请求
//
req, err := http.NewRequest("GET", "https://github.com/login/oauth/access_token?client_id="+param.ClientId+"&client_secret="+param.ClientSecret+"&code="+param.Code+"&redirect_uri="+param.RedirectUri, nil)
if err != nil {
fmt.Println(err)
log.Fatal(err)
log.Error().Err(err).Msg("new request failed")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// 在请求头中加入校验的token
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
log.Fatal(err)
log.Error().Err(err).Msg("send request failed")
c.AbortWithError(http.StatusInternalServerError, err)
return
}
returnMap, err := ParseResponse(resp)
if err != nil {
log.Error().Err(err).Msg("parse response failed")
c.AbortWithError(http.StatusInternalServerError, err)
}

//fmt.Printf("%s\n", bodyText)
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "请求成功",
Expand Down
100 changes: 38 additions & 62 deletions tibuild/internal/controller/pipeline_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
Expand All @@ -18,6 +17,22 @@ import (
"github.com/PingCAP-QE/ee-apps/tibuild/pkg/database"
)

const (
nightlyQAImageBuildPipelineID = 7
nightlyImageBuildPipelineID = 8
nightlyTiupBuildPipelineID = 9
devBuildpipelineID = 12
)

var componentToRepoMap = map[string]string{
"ticdc": "tiflow",
"dm": "tiflow",
"tidb": "tidb",
"dumpling": "tidb",
"lightning": "tidb",
"br": "tidb",
}

type PipelineTriggerStruct struct {
Arch string `form:"arch" json:"arch" validate:"required"`
ArtifactType string `form:"artifact_type" json:"artifact_type" validate:"required"`
Expand Down Expand Up @@ -110,10 +125,10 @@ func PipelineTrigger(c *gin.Context) {

ps := entity.PipelinesListShow{
PipelineId: params.PipelineId,
PipelineName: tibuildInfo[0].TabName,
PipelineName: tibuildInfo.TabName,
Status: "Processing",
Branch: params.Branch,
BuildType: tibuildInfo[0].BuildType,
BuildType: tibuildInfo.BuildType,
Version: params.Version,
Arch: params.Arch,
Component: params.Component,
Expand All @@ -127,14 +142,12 @@ func PipelineTrigger(c *gin.Context) {
}

database.DBConn.DB.Debug().Create(&ps)
fmt.Println("pipeline_build_id : ", ps.PipelineBuildId)
fmt.Println("begin_time: ", ps.BeginTime)
log.Debug().Int("pipeline_build_id", ps.PipelineBuildId).Str("begin_time", ps.BeginTime).Send()

params_trans := make(map[string]string)
params_trans["PIPELINE_BUILD_ID"] = strconv.Itoa(ps.PipelineBuildId)
log.Println(params_trans)

go triggerJenkinsJob(ctx, &tibuildInfo[0], &params, params_trans, jenkins, int64(ps.PipelineBuildId))
go triggerJenkinsJob(ctx, &tibuildInfo, &params, params_trans, jenkins, int64(ps.PipelineBuildId))

c.JSON(http.StatusOK, gin.H{
"code": 200,
Expand All @@ -145,24 +158,9 @@ func PipelineTrigger(c *gin.Context) {

func triggerJenkinsJob(ctx context.Context, tibuildInfo *TibuildInfo, params *PipelineTriggerStruct, params_trans map[string]string, jenkins *gojenkins.Jenkins, pipelineBuildID int64) {
switch params.PipelineId {
case 1, 2, 3, 4, 5, 6:
fmt.Println("触发构建的是多分支流水线,没有传入参数")
case 7:
fmt.Println("Tab展示名:Nightly Image Build For QA")
if params.Branch == "master" {
params_trans["GIT_BRANCH"] = "master"
params_trans["NEED_MULTIARCH"] = "true"
} else {
params_trans["GIT_BRANCH"] = params.Branch
params_trans["NEED_MULTIARCH"] = "false"
}
case nightlyQAImageBuildPipelineID:
params_trans["NEED_MULTIARCH"] = strconv.FormatBool(params.Branch == "master")
params_trans["FORCE_REBUILD"] = "false"

case 8:
fmt.Println("Tab展示名:Nightly Image Build to Dockerhub")

case 9:
fmt.Println("Tab展示名:Nightly TiUP Build")
case 10:
params_trans["RELEASE_BRANCH"] = params.Branch
params_trans["RELEASE_TAG"] = params.Version
Expand All @@ -171,50 +169,28 @@ func triggerJenkinsJob(ctx context.Context, tibuildInfo *TibuildInfo, params *Pi
params_trans["RELEASE_BRANCH"] = params.Branch
params_trans["NEED_MULTIARCH"] = "true"
params_trans["DEBUG_MODE"] = "false"
case 12: // dev-build
case devBuildpipelineID:
params_trans["PRODUCT"] = params.Component
if params.Component == "ticdc" || params.Component == "dm" {
params_trans["REPO"] = "tiflow"
} else if params.Component == "tidb" || params.Component == "dumpling" || params.Component == "lightning" || params.Component == "br" {
params_trans["REPO"] = "tidb"
} else {
params_trans["REPO"] = params.Component
}
params_trans["REPO"] = componentToRepoMap[params.Component]
params_trans["HOTFIX_TAG"] = params.Version
if params.PushGCR == "Yes" {
params_trans["PUSH_GCR"] = "true"
} else {
params_trans["PUSH_GCR"] = "false"
}
if params.ArtifactType == "enterprise image" {
params_trans["PUSH_DOCKER_HUB"] = "false"
params_trans["EDITION"] = "enterprise"
} else {
params_trans["PUSH_DOCKER_HUB"] = "true"
params_trans["EDITION"] = "community"
}
params_trans["PUSH_GCR"] = strconv.FormatBool(params.PushGCR == "Yes")
params_trans["PUSH_DOCKER_HUB"] = strconv.FormatBool(params.ArtifactType != "enterprise image")
params_trans["EDITION"] = map[bool]string{true: "enterprise", false: "community"}[params.ArtifactType == "enterprise image"]
params_trans["FORCE_REBUILD"] = "true"
params_trans["DEBUG"] = "false"
if params.Arch == "All" {
params_trans["ARCH"] = "both"
} else if params.Arch == "linux-amd64" {
params_trans["ARCH"] = "amd64"
} else {
params_trans["ARCH"] = "arm64"
archMap := map[string]string{
"All": "both",
"linux-amd64": "amd64",
}

params_trans["ARCH"] = archMap[params.Arch]
}

job_name_tmp := strings.Split(tibuildInfo.Pipeline, "/job/")
if len(job_name_tmp) <= 1 {
println("Jenkins uri 非法!")
} else if len(job_name_tmp) == 2 {
job_name := strings.Split(job_name_tmp[1], "/")[0]
println(job_name)
service.Job_Build(jenkins, ctx, job_name, params_trans, pipelineBuildID)
} else {
job_name := job_name_tmp[1] + "/" + job_name_tmp[2]
println(job_name)
service.Job_Build(jenkins, ctx, job_name, params_trans, pipelineBuildID)
jobNameParts := strings.Split(tibuildInfo.Pipeline, "/job/")
if len(jobNameParts) < 2 {
log.Error().Str("pipeline_name", tibuildInfo.Pipeline).Msg("Invalid Jenkins URI!")
return
}

jobName := strings.Split(jobNameParts[1], "/")[0]
service.JobBuild(jenkins, ctx, jobName, params_trans, pipelineBuildID)
}
53 changes: 7 additions & 46 deletions tibuild/internal/controller/request_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
Expand All @@ -12,58 +11,20 @@ import (
)

type RequestResultRequestStruct struct {
PipelineBuildId string `form:"pipeline_build_id"`
PipelineBuildId int `form:"pipeline_build_id"`
}

func RequestResult(c *gin.Context) {
// 获取类-每个构建类型下的流水线
params := RequestResultRequestStruct{}
if err := c.ShouldBindWith(&params, binding.Form); err != nil {
c.Error(err)
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "请求失败",
"data": nil,
})
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"code": 400, "message": "请求失败", "data": nil})
return
}

var pipelines_list_show []entity.PipelinesListShow
pipelineBuildId, _ := strconv.Atoi(params.PipelineBuildId)
database.DBConn.DB.Where(&entity.PipelinesListShow{PipelineBuildId: pipelineBuildId}).Find(&pipelines_list_show)

var data map[string]interface{}

if len(pipelines_list_show) == 1 {
data = map[string]interface{}{
"pipeline_id": pipelines_list_show[0].PipelineId,
"pipeline_name": pipelines_list_show[0].PipelineName,
"pipeline_build_id": pipelines_list_show[0].PipelineBuildId,
"status": pipelines_list_show[0].Status,
"branch": pipelines_list_show[0].Branch,
"build_type": pipelines_list_show[0].BuildType,
"version": pipelines_list_show[0].Version,
"arch": pipelines_list_show[0].Arch,
"component": pipelines_list_show[0].Component,
"begin_time": pipelines_list_show[0].BeginTime,
"end_time": pipelines_list_show[0].EndTime,
"artifact_type": pipelines_list_show[0].ArtifactType,
"push_gcr": pipelines_list_show[0].PushGCR,
"artifact_meta": pipelines_list_show[0].ArtifactMeta,
"jenkins_log": pipelines_list_show[0].JenkinsLog,
"triggered_by": pipelines_list_show[0].TriggeredBy,
}
} else if len(pipelines_list_show) == 0 {
println("没有这个流水线id ", params.PipelineBuildId)
} else {
println("流水线id不唯一 ", params.PipelineBuildId)
var pipelineShow entity.PipelinesListShow
if database.DBConn.DB.Where(&entity.PipelinesListShow{PipelineBuildId: params.PipelineBuildId}).First(&pipelineShow).Error != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"code": 404, "message": "没有这个流水线id", "data": nil})
return
}

// 成功返回
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "请求成功",
"data": data,
})

c.JSON(http.StatusOK, gin.H{"code": 200, "message": "请求成功", "data": pipelineShow})
}
Loading

0 comments on commit 3945030

Please sign in to comment.