Skip to content

Commit

Permalink
✨ Support create and update builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Sep 5, 2023
1 parent a84a221 commit 5e9317d
Show file tree
Hide file tree
Showing 20 changed files with 1,395 additions and 316 deletions.
2 changes: 1 addition & 1 deletion cmd/builder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (b Builder) initToken() error {
if strings.HasSuffix(registry, "@http") {
btConfig.Registries[strings.TrimSuffix(registry, "@http")] = resolverconfig.RegistryConfig{PlainHTTP: ptr.Of(true)}
} else {
btConfig.Registries[strings.TrimSuffix(registry, "@http")] = resolverconfig.RegistryConfig{Insecure: ptr.Of(true)}
btConfig.Registries[registry] = resolverconfig.RegistryConfig{Insecure: ptr.Of(true)}
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/daemon/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ func (b builder) runner(ctx context.Context, payload types.DaemonBuilderPayload)
BuilderID: payload.BuilderID,
RunnerID: runnerObj.ID,

ScmCredentialType: builderObj.ScmCredentialType,
ScmProvider: enums.ScmProviderGithub,
ScmSshKey: builderObj.ScmSshKey,
ScmToken: builderObj.ScmToken,
ScmUsername: builderObj.ScmUsername,
ScmPassword: builderObj.ScmPassword,
ScmRepository: builderObj.ScmRepository,
ScmBranch: runnerObj.ScmBranch,
ScmDepth: builderObj.ScmDepth,
ScmSubmodule: builderObj.ScmSubmodule,
// ScmCredentialType: builderObj.ScmCredentialType,
// ScmProvider: enums.ScmProviderGithub,
// ScmSshKey: builderObj.ScmSshKey,
// ScmToken: builderObj.ScmToken,
// ScmUsername: builderObj.ScmUsername,
// ScmPassword: builderObj.ScmPassword,
// ScmRepository: builderObj.ScmRepository,
ScmBranch: runnerObj.ScmBranch,
ScmDepth: builderObj.ScmDepth,
ScmSubmodule: builderObj.ScmSubmodule,

OciRegistryDomain: []string{"192.168.31.114:3000"},
OciRegistryUsername: []string{"sigma"},
Expand Down
19 changes: 18 additions & 1 deletion pkg/dal/dao/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
// BuilderService is the interface that provides methods to operate on Builder model
type BuilderService interface {
// Create creates a new builder record in the database
Create(ctx context.Context, audit *models.Builder) error
Create(ctx context.Context, builder *models.Builder) error
// Update update the builder by id
Update(ctx context.Context, id int64, updates map[string]interface{}) error
// Get get builder by repository id
GetByRepositoryID(ctx context.Context, repositoryID int64) (*models.Builder, error)
// CreateRunner creates a new builder runner record in the database
Expand Down Expand Up @@ -76,6 +78,21 @@ func (s builderService) Create(ctx context.Context, builder *models.Builder) err
return s.tx.Builder.WithContext(ctx).Create(builder)
}

// Update update the builder by id
func (s builderService) Update(ctx context.Context, id int64, updates map[string]interface{}) error {
if len(updates) == 0 {
return nil
}
result, err := s.tx.Builder.WithContext(ctx).Where(s.tx.Builder.ID.Eq(id)).UpdateColumns(updates)
if err != nil {
return err
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil

Check warning on line 93 in pkg/dal/dao/builder.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/builder.go#L82-L93

Added lines #L82 - L93 were not covered by tests
}

// Get get builder by repository id
func (s builderService) GetByRepositoryID(ctx context.Context, repositoryID int64) (*models.Builder, error) {
return s.tx.Builder.WithContext(ctx).Where(s.tx.Builder.RepositoryID.Eq(repositoryID)).First()
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ CREATE TABLE IF NOT EXISTS `webhook_logs` (
CREATE TABLE IF NOT EXISTS `builders` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`repository_id` bigint NOT NULL,
`active` tinyint NOT NULL DEFAULT 1,
`source` ENUM ('SelfCodeRepository', 'CodeRepository', 'Dockerfile') NOT NULL,
-- source SelfCodeRepository
`scm_repository` varchar(256),
Expand All @@ -358,6 +357,7 @@ CREATE TABLE IF NOT EXISTS `builders` (
-- source Dockerfile
`dockerfile` BLOB,
-- common settings
`scm_branch` varchar(256),
`scm_depth` MEDIUMINT NOT NULL DEFAULT 0,
`scm_submodule` tinyint NOT NULL DEFAULT 1,
-- cron settings
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ CREATE TYPE builder_source AS ENUM (
CREATE TABLE IF NOT EXISTS "builders" (
"id" bigserial PRIMARY KEY,
"repository_id" bigint NOT NULL,
"active" smallint NOT NULL DEFAULT 1,
"source" builder_source NOT NULL,
-- source SelfCodeRepository
"scm_credential_type" varchar(16) NOT NULL,
Expand All @@ -419,6 +418,7 @@ CREATE TABLE IF NOT EXISTS "builders" (
-- source Dockerfile
"dockerfile" bytea,
-- common settings
"scm_branch" varchar(256),
"scm_depth" smallint NOT NULL DEFAULT 0,
"scm_submodule" smallint NOT NULL DEFAULT 1,
-- cron settings
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ CREATE TABLE IF NOT EXISTS `webhook_logs` (
CREATE TABLE IF NOT EXISTS `builders` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`repository_id` integer NOT NULL,
`active` integer NOT NULL DEFAULT 1,
`source` text CHECK (`source` IN ('SelfCodeRepository', 'CodeRepository', 'Dockerfile')) NOT NULL,
-- source SelfCodeRepository
`scm_credential_type` varchar(16) NOT NULL,
Expand All @@ -358,6 +357,7 @@ CREATE TABLE IF NOT EXISTS `builders` (
-- source Dockerfile
`dockerfile` BLOB,
-- common settings
`scm_branch` varchar(256),
`scm_depth` MEDIUMINT NOT NULL DEFAULT 0,
`scm_submodule` integer NOT NULL DEFAULT 1,
-- cron settings
Expand Down
14 changes: 7 additions & 7 deletions pkg/dal/models/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type Builder struct {
ID int64 `gorm:"primaryKey"`

RepositoryID int64
Active bool

Source enums.BuilderSource

Expand All @@ -39,14 +38,15 @@ type Builder struct {
// source Dockerfile
Dockerfile []byte
// source SelfCodeRepository
ScmRepository string
ScmCredentialType enums.ScmCredentialType
ScmToken string
ScmSshKey string
ScmUsername string
ScmPassword string
ScmRepository *string
ScmCredentialType *enums.ScmCredentialType
ScmToken *string
ScmSshKey *string
ScmUsername *string
ScmPassword *string

// common settings
ScmBranch *string // for SelfCodeRepository and CodeRepository
ScmDepth int
ScmSubmodule bool

Expand Down
8 changes: 4 additions & 4 deletions pkg/dal/query/builders.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5e9317d

Please sign in to comment.