Skip to content

Commit

Permalink
feat(tibuild): fill result col for devbuild list api (#139)
Browse files Browse the repository at this point in the history
Close #140

Also support filter by creator (`createdBy`).

Signed-off-by: wuhuizuo <[email protected]>

---------

Signed-off-by: wuhuizuo <[email protected]>
  • Loading branch information
wuhuizuo authored May 21, 2024
1 parent 6a033f9 commit bb0e065
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 109 deletions.
5 changes: 2 additions & 3 deletions cloudevents-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ require (
k8s.io/apimachinery v0.23.9
)

require filippo.io/edwards25519 v1.1.0 // indirect

require (
ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 // indirect
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3
Expand Down Expand Up @@ -97,7 +96,7 @@ require (
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.22.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/mod v0.15.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions cloudevents-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.22.0 h1:Zcye5DUgBloQ9BaT4qc9BnjOFog5TvBSAGkJ3Nf70c0=
go.uber.org/zap v1.22.0/go.mod h1:H4siCOZOrAolnUPJEkfaSjDqyP+BDS0DdDWzwcgt3+U=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
4 changes: 3 additions & 1 deletion tibuild/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ build.web:
yarn build

swagger:
swag init -d cmd/tibuild,pkg/rest
if [ ! -f $$(go env GOPATH)/bin/swag ]; then go install github.com/swaggo/swag/cmd/[email protected]; fi
$$(go env GOPATH)/bin/swag fmt -d ./cmd/tibuild,./pkg/rest
$$(go env GOPATH)/bin/swag init -d ./cmd/tibuild,./pkg/rest

test:
go test ./...
Expand Down
11 changes: 9 additions & 2 deletions tibuild/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package docs GENERATED BY SWAG; DO NOT EDIT
// This file was generated by swaggo/swag
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"
Expand Down Expand Up @@ -92,6 +91,12 @@ const docTemplate = `{
"description": "filter hotfix",
"name": "hotfix",
"in": "query"
},
{
"type": "string",
"description": "filter created by",
"name": "createdBy",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -740,6 +745,8 @@ var SwaggerInfo = &swag.Spec{
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
Expand Down
6 changes: 6 additions & 0 deletions tibuild/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
"description": "filter hotfix",
"name": "hotfix",
"in": "query"
},
{
"type": "string",
"description": "filter created by",
"name": "createdBy",
"in": "query"
}
],
"responses": {
Expand Down
4 changes: 4 additions & 0 deletions tibuild/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ paths:
in: query
name: hotfix
type: boolean
- description: filter created by
in: query
name: createdBy
type: string
produces:
- application/json
responses:
Expand Down
20 changes: 10 additions & 10 deletions tibuild/pkg/rest/controller/artifact_helper_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func NewArtifactHelperHandler(j service.Jenkins) *ArtifactHelperHandler {
}

// SyncImage godoc
// @Summary sync hotfix image to dockerhub
// @Description sync
// @Tags artifact
// @Accept json
// @Produce json
// @Param ImageSyncRequest body service.ImageSyncRequest true "image sync to public, only hotfix is accepted right now"
// @Success 200 {object} service.ImageSyncRequest
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/artifact/sync-image [post]
// @Summary sync hotfix image to dockerhub
// @Description sync
// @Tags artifact
// @Accept json
// @Produce json
// @Param ImageSyncRequest body service.ImageSyncRequest true "image sync to public, only hotfix is accepted right now"
// @Success 200 {object} service.ImageSyncRequest
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/artifact/sync-image [post]
func (h ArtifactHelperHandler) SyncImage(c *gin.Context) {
params := service.ImageSyncRequest{}
err := bindParam(&params, c)
Expand Down
112 changes: 59 additions & 53 deletions tibuild/pkg/rest/controller/dev_build_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ func (h DevBuildHandler) authenticate(c *gin.Context) (context.Context, error) {
}

// CreateDevbuild godoc
// @Summary create and trigger devbuild
// @Description create and trigger devbuild
// @Tags devbuild
// @Accept json
// @Produce json
// @Param DevBuild body service.DevBuild true "build to create, only spec filed is required, others are ignored"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds [post]
//
// @Summary create and trigger devbuild
// @Description create and trigger devbuild
// @Tags devbuild
// @Accept json
// @Produce json
// @Param DevBuild body service.DevBuild true "build to create, only spec filed is required, others are ignored"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds [post]
func (h DevBuildHandler) Create(c *gin.Context) {
ctx, err := h.authenticate(c)
if err != nil {
Expand All @@ -102,16 +103,18 @@ func (h DevBuildHandler) Create(c *gin.Context) {
}

// ListDevbuild godoc
// @Summary list devbuild
// @Description list devbuild
// @Tags devbuild
// @Produce json
// @Param size query int false "the size limit of items" default(10)
// @Param offset query int false "the start position of items" default(0)
// @Param hotfix query bool false "filter hotfix" default(null)
// @Success 200 {array} service.DevBuild
// @Failure 400 {object} HTTPError
// @Router /api/devbuilds [get]
//
// @Summary list devbuild
// @Description list devbuild
// @Tags devbuild
// @Produce json
// @Param size query int false "the size limit of items" default(10)
// @Param offset query int false "the start position of items" default(0)
// @Param hotfix query bool false "filter hotfix" default(null)
// @Param createdBy query string false "filter created by"
// @Success 200 {array} service.DevBuild
// @Failure 400 {object} HTTPError
// @Router /api/devbuilds [get]
func (h DevBuildHandler) List(c *gin.Context) {
params := service.DevBuildListOption{}
err := c.ShouldBindQuery(&params)
Expand All @@ -131,16 +134,17 @@ func (h DevBuildHandler) List(c *gin.Context) {
}

// GetDevbuild godoc
// @Summary get devbuild
// @Description get devbuild
// @Tags devbuild
// @Produce json
// @Param id path int true "id of build"
// @Param sync query bool false "whether sync with jenkins" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id} [get]
//
// @Summary get devbuild
// @Description get devbuild
// @Tags devbuild
// @Produce json
// @Param id path int true "id of build"
// @Param sync query bool false "whether sync with jenkins" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id} [get]
func (h DevBuildHandler) Get(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
Expand All @@ -163,16 +167,17 @@ func (h DevBuildHandler) Get(c *gin.Context) {
}

// GetDevbuild godoc
// @Summary rerun devbuild
// @Description rerun devbuild
// @Tags devbuild
// @Produce json
// @Param id path int true "id of build"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id}/rerun [post]
//
// @Summary rerun devbuild
// @Description rerun devbuild
// @Tags devbuild
// @Produce json
// @Param id path int true "id of build"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id}/rerun [post]
func (h DevBuildHandler) Rerun(c *gin.Context) {
ctx, err := h.authenticate(c)
if err != nil {
Expand Down Expand Up @@ -200,18 +205,19 @@ func (h DevBuildHandler) Rerun(c *gin.Context) {
}

// UpdateDevbuild godoc
// @Summary update devbuild status
// @Description update the status field of a build
// @Tags devbuild
// @Accept json
// @Produce json
// @Param id path int true "id of build"
// @Param DevBuild body service.DevBuild true "build to update"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id} [put]
//
// @Summary update devbuild status
// @Description update the status field of a build
// @Tags devbuild
// @Accept json
// @Produce json
// @Param id path int true "id of build"
// @Param DevBuild body service.DevBuild true "build to update"
// @Param dryrun query bool false "dry run" default(false)
// @Success 200 {object} service.DevBuild
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Router /api/devbuilds/{id} [put]
func (h DevBuildHandler) Update(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
Expand Down
40 changes: 20 additions & 20 deletions tibuild/pkg/rest/controller/hotfix_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type HotfixHandler struct {
}

// // CreateHotfixBranch godoc
// @Summary create hotfix branch
// @Description create hotfix branch
// @Tags hotfix
// @Accept json
// @Produce json
// @Param BranchCreateReq body service.BranchCreateReq true "hotfix param"
// @Success 200 {object} service.BranchCreateResp
// @Failure 422 {object} HTTPError
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Summary create hotfix branch
// @Description create hotfix branch
// @Tags hotfix
// @Accept json
// @Produce json
// @Param BranchCreateReq body service.BranchCreateReq true "hotfix param"
// @Success 200 {object} service.BranchCreateResp
// @Failure 422 {object} HTTPError
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// // @Router /api/hotfix/create-branch [post]
func (h HotfixHandler) CreateBranch(c *gin.Context) {
params := service.BranchCreateReq{}
Expand All @@ -40,16 +40,16 @@ func (h HotfixHandler) CreateBranch(c *gin.Context) {
}

// // CreateHotfixTag godoc
// @Summary create hotfix tag
// @Description create hotfix tag
// @Tags hotfix
// @Accept json
// @Produce json
// @Param TagCreateReq body service.TagCreateReq true "hotfix param"
// @Success 200 {object} service.TagCreateResp
// @Failure 422 {object} HTTPError
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// @Summary create hotfix tag
// @Description create hotfix tag
// @Tags hotfix
// @Accept json
// @Produce json
// @Param TagCreateReq body service.TagCreateReq true "hotfix param"
// @Success 200 {object} service.TagCreateResp
// @Failure 422 {object} HTTPError
// @Failure 400 {object} HTTPError
// @Failure 500 {object} HTTPError
// // @Router /api/hotfix/create-tag [post]
func (h HotfixHandler) CreateTag(c *gin.Context) {
params := service.TagCreateReq{}
Expand Down
21 changes: 16 additions & 5 deletions tibuild/pkg/rest/repo/dev_build_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,27 @@ func (m DevBuildRepo) Update(ctx context.Context, id int, req DevBuild) (resp *D
return &req, nil
}

func (m DevBuildRepo) List(ctx context.Context, option DevBuildListOption) (resp []DevBuild, err error) {
result := []DevBuild{}
db := m.Db.Order("created_at DESC")
func (m DevBuildRepo) List(ctx context.Context, option DevBuildListOption) ([]DevBuild, error) {
db := m.Db.Order("created_at DESC").Offset(int(option.Offset)).Limit(int(option.Size))
if option.Hotfix != nil {
db = db.Where(&DevBuild{Spec: DevBuildSpec{IsHotfix: *option.Hotfix}}, "IsHotfix")
}
if err := db.Offset(int(option.Offset)).Limit(int(option.Size)).Find(&result).Error; err != nil {
if option.CreatedBy != nil && *option.CreatedBy != "" {
db = db.Where(&DevBuild{Meta: DevBuildMeta{CreatedBy: *option.CreatedBy}}, "CreatedBy")
}

result := []DevBuild{}
if err := db.Find(&result).Error; err != nil {
return nil, fmt.Errorf("%s%w", err.Error(), ErrInternalError)
}
return result, err

for i := range result {
if err := outofDB(&result[i]); err != nil {
return nil, err
}
}

return result, nil
}

func intoDB(entity *DevBuild) (err error) {
Expand Down
Loading

0 comments on commit bb0e065

Please sign in to comment.