From 52e93cc98fc9b86a4dcb1635103ec4c51fc39844 Mon Sep 17 00:00:00 2001 From: Tosone Date: Mon, 4 Sep 2023 13:44:13 +0800 Subject: [PATCH] :sparkles: Change builder table definition --- pkg/cronjob/builder.go | 2 +- .../migrations/mysql/0001_initialize.up.sql | 8 +- .../postgresql/0001_initialize.up.sql | 8 +- .../migrations/sqlite3/0001_initialize.up.sql | 8 +- pkg/dal/models/builder.go | 6 +- pkg/dal/query/builders.gen.go | 26 ++- .../coderepos/coderepos_setupbuilder.go | 162 ------------------ pkg/handlers/coderepos/handler.go | 3 - 8 files changed, 37 insertions(+), 186 deletions(-) delete mode 100644 pkg/handlers/coderepos/coderepos_setupbuilder.go diff --git a/pkg/cronjob/builder.go b/pkg/cronjob/builder.go index 5626aebc..2c9b5fa6 100644 --- a/pkg/cronjob/builder.go +++ b/pkg/cronjob/builder.go @@ -95,7 +95,7 @@ func (r builderRunner) runner(ctx context.Context, tw timewheel.TimeWheel) { if err != nil { return err } - tag, err := builder.BuildTag(ptr.To(builderObj.CronTag), builder.BuildTagOption{ScmBranch: ptr.To(builderObj.CronBranch)}) + tag, err := builder.BuildTag(ptr.To(builderObj.CronTagTemplate), builder.BuildTagOption{ScmBranch: ptr.To(builderObj.CronBranch)}) if err != nil { return err } diff --git a/pkg/dal/migrations/mysql/0001_initialize.up.sql b/pkg/dal/migrations/mysql/0001_initialize.up.sql index dc63bd66..cb3c93e1 100644 --- a/pkg/dal/migrations/mysql/0001_initialize.up.sql +++ b/pkg/dal/migrations/mysql/0001_initialize.up.sql @@ -362,11 +362,13 @@ CREATE TABLE IF NOT EXISTS `builders` ( `scm_submodule` tinyint NOT NULL DEFAULT 1, -- cron settings `cron_rule` varchar(30), - `cron_branch` varchar(30), - `cron_tag` varchar(256), + `cron_branch` varchar(256), + `cron_tag_template` varchar(256), `cron_next_trigger` timestamp, -- webhook settings - `webhook_tag` varchar(256), + `webhook_branch_name` varchar(256), + `webhook_branch_tag_template` varchar(256), + `webhook_tag_tag_template` varchar(256), -- buildkit settings `buildkit_insecure_registries` varchar(256), `buildkit_context` varchar(30) NOT NULL DEFAULT '.', diff --git a/pkg/dal/migrations/postgresql/0001_initialize.up.sql b/pkg/dal/migrations/postgresql/0001_initialize.up.sql index 64b94875..25e0ed67 100644 --- a/pkg/dal/migrations/postgresql/0001_initialize.up.sql +++ b/pkg/dal/migrations/postgresql/0001_initialize.up.sql @@ -423,11 +423,13 @@ CREATE TABLE IF NOT EXISTS "builders" ( "scm_submodule" smallint NOT NULL DEFAULT 1, -- cron settings "cron_rules" varchar(30), - "cron_branch" varchar(30), - "cron_tag" varchar(256), + "cron_branch" varchar(256), + "cron_tag_template" varchar(256), "cron_next_trigger" timestamp, -- webhook settings - "webhook_tag" varchar(256), + "webhook_branch_name" varchar(256), + "webhook_branch_tag_template" varchar(256), + "webhook_tag_tag_template" varchar(256), -- buildkit settings "buildkit_insecure_registries" varchar(256), "buildkit_context" varchar(30) NOT NULL DEFAULT '.', diff --git a/pkg/dal/migrations/sqlite3/0001_initialize.up.sql b/pkg/dal/migrations/sqlite3/0001_initialize.up.sql index 73d608be..6bac21b6 100644 --- a/pkg/dal/migrations/sqlite3/0001_initialize.up.sql +++ b/pkg/dal/migrations/sqlite3/0001_initialize.up.sql @@ -362,11 +362,13 @@ CREATE TABLE IF NOT EXISTS `builders` ( `scm_submodule` integer NOT NULL DEFAULT 1, -- cron settings `cron_rule` varchar(30), - `cron_branch` varchar(30), - `cron_tag` varchar(256), + `cron_branch` varchar(256), + `cron_tag_template` varchar(256), `cron_next_trigger` timestamp, -- webhook settings - `webhook_tag` varchar(256), + `webhook_branch_name` varchar(256), + `webhook_branch_tag_template` varchar(256), + `webhook_tag_tag_template` varchar(256), -- buildkit settings `buildkit_insecure_registries` varchar(256), `buildkit_context` varchar(30) NOT NULL DEFAULT '.', diff --git a/pkg/dal/models/builder.go b/pkg/dal/models/builder.go index 9264b696..c36adc8e 100644 --- a/pkg/dal/models/builder.go +++ b/pkg/dal/models/builder.go @@ -53,11 +53,13 @@ type Builder struct { // cron settings CronRule *string CronBranch *string - CronTag *string + CronTagTemplate *string CronNextTrigger *time.Time // webhook settings - WebhookTag *string + WebhookBranchName *string + WebhookBranchTagTemplate *string + WebhookTagTagTemplate *string // buildkit settings BuildkitInsecureRegistries string diff --git a/pkg/dal/query/builders.gen.go b/pkg/dal/query/builders.gen.go index 6b5b0eac..1b945e2f 100644 --- a/pkg/dal/query/builders.gen.go +++ b/pkg/dal/query/builders.gen.go @@ -46,9 +46,11 @@ func newBuilder(db *gorm.DB, opts ...gen.DOOption) builder { _builder.ScmSubmodule = field.NewBool(tableName, "scm_submodule") _builder.CronRule = field.NewString(tableName, "cron_rule") _builder.CronBranch = field.NewString(tableName, "cron_branch") - _builder.CronTag = field.NewString(tableName, "cron_tag") + _builder.CronTagTemplate = field.NewString(tableName, "cron_tag_template") _builder.CronNextTrigger = field.NewTime(tableName, "cron_next_trigger") - _builder.WebhookTag = field.NewString(tableName, "webhook_tag") + _builder.WebhookBranchName = field.NewString(tableName, "webhook_branch_name") + _builder.WebhookBranchTagTemplate = field.NewString(tableName, "webhook_branch_tag_template") + _builder.WebhookTagTagTemplate = field.NewString(tableName, "webhook_tag_tag_template") _builder.BuildkitInsecureRegistries = field.NewString(tableName, "buildkit_insecure_registries") _builder.BuildkitContext = field.NewString(tableName, "buildkit_context") _builder.BuildkitDockerfile = field.NewString(tableName, "buildkit_dockerfile") @@ -92,9 +94,11 @@ type builder struct { ScmSubmodule field.Bool CronRule field.String CronBranch field.String - CronTag field.String + CronTagTemplate field.String CronNextTrigger field.Time - WebhookTag field.String + WebhookBranchName field.String + WebhookBranchTagTemplate field.String + WebhookTagTagTemplate field.String BuildkitInsecureRegistries field.String BuildkitContext field.String BuildkitDockerfile field.String @@ -135,9 +139,11 @@ func (b *builder) updateTableName(table string) *builder { b.ScmSubmodule = field.NewBool(table, "scm_submodule") b.CronRule = field.NewString(table, "cron_rule") b.CronBranch = field.NewString(table, "cron_branch") - b.CronTag = field.NewString(table, "cron_tag") + b.CronTagTemplate = field.NewString(table, "cron_tag_template") b.CronNextTrigger = field.NewTime(table, "cron_next_trigger") - b.WebhookTag = field.NewString(table, "webhook_tag") + b.WebhookBranchName = field.NewString(table, "webhook_branch_name") + b.WebhookBranchTagTemplate = field.NewString(table, "webhook_branch_tag_template") + b.WebhookTagTagTemplate = field.NewString(table, "webhook_tag_tag_template") b.BuildkitInsecureRegistries = field.NewString(table, "buildkit_insecure_registries") b.BuildkitContext = field.NewString(table, "buildkit_context") b.BuildkitDockerfile = field.NewString(table, "buildkit_dockerfile") @@ -166,7 +172,7 @@ func (b *builder) GetFieldByName(fieldName string) (field.OrderExpr, bool) { } func (b *builder) fillFieldMap() { - b.fieldMap = make(map[string]field.Expr, 27) + b.fieldMap = make(map[string]field.Expr, 29) b.fieldMap["created_at"] = b.CreatedAt b.fieldMap["updated_at"] = b.UpdatedAt b.fieldMap["deleted_at"] = b.DeletedAt @@ -186,9 +192,11 @@ func (b *builder) fillFieldMap() { b.fieldMap["scm_submodule"] = b.ScmSubmodule b.fieldMap["cron_rule"] = b.CronRule b.fieldMap["cron_branch"] = b.CronBranch - b.fieldMap["cron_tag"] = b.CronTag + b.fieldMap["cron_tag_template"] = b.CronTagTemplate b.fieldMap["cron_next_trigger"] = b.CronNextTrigger - b.fieldMap["webhook_tag"] = b.WebhookTag + b.fieldMap["webhook_branch_name"] = b.WebhookBranchName + b.fieldMap["webhook_branch_tag_template"] = b.WebhookBranchTagTemplate + b.fieldMap["webhook_tag_tag_template"] = b.WebhookTagTagTemplate b.fieldMap["buildkit_insecure_registries"] = b.BuildkitInsecureRegistries b.fieldMap["buildkit_context"] = b.BuildkitContext b.fieldMap["buildkit_dockerfile"] = b.BuildkitDockerfile diff --git a/pkg/handlers/coderepos/coderepos_setupbuilder.go b/pkg/handlers/coderepos/coderepos_setupbuilder.go deleted file mode 100644 index d73273c7..00000000 --- a/pkg/handlers/coderepos/coderepos_setupbuilder.go +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright 2023 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package coderepos - -import ( - "errors" - "fmt" - "net/http" - - "github.com/labstack/echo/v4" - "github.com/rs/zerolog/log" - "gorm.io/gorm" - - "github.com/go-sigma/sigma/pkg/consts" - "github.com/go-sigma/sigma/pkg/dal/dao" - "github.com/go-sigma/sigma/pkg/dal/models" - "github.com/go-sigma/sigma/pkg/dal/query" - "github.com/go-sigma/sigma/pkg/types" - "github.com/go-sigma/sigma/pkg/types/enums" - "github.com/go-sigma/sigma/pkg/utils" - "github.com/go-sigma/sigma/pkg/utils/ptr" - "github.com/go-sigma/sigma/pkg/xerrors" -) - -// SetupBuilder setup builder for code repository -// @Summary Setup builder for code repository -// @security BasicAuth -// @Tags CodeRepository -// @Accept json -// @Produce json -// @Router /coderepos/{id}/setup-builder [post] -// @Param id path string true "code repository id" -// @Param message body types.PostCodeRepositorySetupBuilderSwagger true "Code repository setup builder object" -// @Success 201 -// @Failure 401 {object} xerrors.ErrCode -// @Failure 500 {object} xerrors.ErrCode -func (h *handlers) SetupBuilder(c echo.Context) error { - ctx := log.Logger.WithContext(c.Request().Context()) - - iuser := c.Get(consts.ContextUser) - if iuser == nil { - log.Error().Msg("Get user from header failed") - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized) - } - user, ok := iuser.(*models.User) - if !ok { - log.Error().Msg("Convert user from header failed") - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized) - } - - var req types.PostCodeRepositorySetupBuilder - err := utils.BindValidate(c, &req) - if err != nil { - log.Error().Err(err).Msg("Bind and validate request body failed") - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeBadRequest, err.Error()) - } - - namespaceService := h.namespaceServiceFactory.New() - namespaceObj, err := namespaceService.Get(ctx, req.ID) - if err != nil { - log.Error().Err(err).Msg("Get namespace by id failed") - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, err.Error()) - } - - repositoryService := h.repositoryServiceFactory.New() - repositoryObj, err := repositoryService.GetByName(ctx, req.RepositoryName) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - err = query.Q.Transaction(func(tx *query.Query) error { - repositoryService := h.repositoryServiceFactory.New(tx) - repositoryObj = &models.Repository{ - NamespaceID: namespaceObj.ID, - Name: req.RepositoryName, - Visibility: namespaceObj.Visibility, - } - err = repositoryService.Create(ctx, repositoryObj, dao.AutoCreateNamespace{AutoCreate: false}) - if err != nil { - log.Error().Err(err).Msg("Create repository failed") - return xerrors.HTTPErrCodeInternalError.Detail(fmt.Sprintf("Create repository failed: %v", err)) - } - auditService := h.auditServiceFactory.New(tx) - err = auditService.Create(ctx, &models.Audit{ - UserID: user.ID, - NamespaceID: ptr.Of(namespaceObj.ID), - Action: enums.AuditActionCreate, - ResourceType: enums.AuditResourceTypeRepository, - Resource: req.RepositoryName, - ReqRaw: utils.MustMarshal(req), - }) - if err != nil { - log.Error().Err(err).Msg("Create audit failed") - return xerrors.HTTPErrCodeInternalError.Detail(fmt.Sprintf("Create audit failed: %v", err)) - } - return nil - }) - if err != nil { - return xerrors.NewHTTPError(c, err.(xerrors.ErrCode)) - } - } - } - - builderService := h.builderServiceFactory.New() - _, err = builderService.GetByRepositoryID(ctx, repositoryObj.ID) - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - log.Error().Err(err).Msg("Get builder by repository id failed") - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, fmt.Sprintf("Get builder by repository id failed: %v", err)) - } - if err == nil { - log.Error().Msgf("Repository(%s) already have a builder", repositoryObj.Name) - return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeConflict, fmt.Sprintf("Repository(%s) already have a builder", repositoryObj.Name)) - } - - builderObj := &models.Builder{ - RepositoryID: repositoryObj.ID, - Active: true, - Source: enums.BuilderSourceCodeRepository, - - CodeRepositoryID: ptr.Of(req.ID), - - BuildkitContext: req.BuildkitContext, - BuildkitDockerfile: req.BuildkitDockerfile, - BuildkitPlatforms: utils.StringsJoin(req.BuildkitPlatforms, ","), - } - err = query.Q.Transaction(func(tx *query.Query) error { - builderService := h.builderServiceFactory.New(tx) - err = builderService.Create(ctx, builderObj) - if err != nil { - log.Error().Err(err).Msg("Create builder failed") - return xerrors.HTTPErrCodeInternalError.Detail(fmt.Sprintf("Create builder failed: %v", err)) - } - auditService := h.auditServiceFactory.New(tx) - err = auditService.Create(ctx, &models.Audit{ - UserID: user.ID, - NamespaceID: ptr.Of(namespaceObj.ID), - Action: enums.AuditActionCreate, - ResourceType: enums.AuditResourceTypeBuilder, - Resource: req.RepositoryName, - ReqRaw: utils.MustMarshal(req), - }) - if err != nil { - log.Error().Err(err).Msg("Create audit failed") - return xerrors.HTTPErrCodeInternalError.Detail(fmt.Sprintf("Create audit failed: %v", err)) - } - return nil - }) - if err != nil { - return xerrors.NewHTTPError(c, err.(xerrors.ErrCode)) - } - return c.NoContent(http.StatusCreated) -} diff --git a/pkg/handlers/coderepos/handler.go b/pkg/handlers/coderepos/handler.go index b70ddfc4..310051ac 100644 --- a/pkg/handlers/coderepos/handler.go +++ b/pkg/handlers/coderepos/handler.go @@ -37,8 +37,6 @@ type Handlers interface { ListBranches(c echo.Context) error // Resync resync all of the code repositories Resync(c echo.Context) error - // Setup setup builder for code repository - SetupBuilder(c echo.Context) error // Providers get providers Providers(c echo.Context) error // User3rdParty get user 3rdparty @@ -117,7 +115,6 @@ func (f factory) Initialize(e *echo.Echo) error { codereposGroup.GET("/:provider/resync", codeRepositoryHandler.Resync) codereposGroup.GET("/:provider/owners", codeRepositoryHandler.ListOwners) codereposGroup.GET("/:id/branches", codeRepositoryHandler.ListBranches) - codereposGroup.POST("/:id/setup-builder", codeRepositoryHandler.SetupBuilder) return nil }