Skip to content

Commit

Permalink
enhance(repo): add AllowSchedule field (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp committed May 19, 2023
1 parent f538de0 commit 71117bf
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 120 deletions.
101 changes: 52 additions & 49 deletions database/repo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down Expand Up @@ -46,30 +46,31 @@ var (

// Repo is the database representation of a repo.
type Repo struct {
ID sql.NullInt64 `sql:"id"`
UserID sql.NullInt64 `sql:"user_id"`
Hash sql.NullString `sql:"hash"`
Org sql.NullString `sql:"org"`
Name sql.NullString `sql:"name"`
FullName sql.NullString `sql:"full_name"`
Link sql.NullString `sql:"link"`
Clone sql.NullString `sql:"clone"`
Branch sql.NullString `sql:"branch"`
Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"`
BuildLimit sql.NullInt64 `sql:"build_limit"`
Timeout sql.NullInt64 `sql:"timeout"`
Counter sql.NullInt32 `sql:"counter"`
Visibility sql.NullString `sql:"visibility"`
Private sql.NullBool `sql:"private"`
Trusted sql.NullBool `sql:"trusted"`
Active sql.NullBool `sql:"active"`
AllowPull sql.NullBool `sql:"allow_pull"`
AllowPush sql.NullBool `sql:"allow_push"`
AllowDeploy sql.NullBool `sql:"allow_deploy"`
AllowTag sql.NullBool `sql:"allow_tag"`
AllowComment sql.NullBool `sql:"allow_comment"`
PipelineType sql.NullString `sql:"pipeline_type"`
PreviousName sql.NullString `sql:"previous_name"`
ID sql.NullInt64 `sql:"id"`
UserID sql.NullInt64 `sql:"user_id"`
Hash sql.NullString `sql:"hash"`
Org sql.NullString `sql:"org"`
Name sql.NullString `sql:"name"`
FullName sql.NullString `sql:"full_name"`
Link sql.NullString `sql:"link"`
Clone sql.NullString `sql:"clone"`
Branch sql.NullString `sql:"branch"`
Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"`
BuildLimit sql.NullInt64 `sql:"build_limit"`
Timeout sql.NullInt64 `sql:"timeout"`
Counter sql.NullInt32 `sql:"counter"`
Visibility sql.NullString `sql:"visibility"`
Private sql.NullBool `sql:"private"`
Trusted sql.NullBool `sql:"trusted"`
Active sql.NullBool `sql:"active"`
AllowPull sql.NullBool `sql:"allow_pull"`
AllowPush sql.NullBool `sql:"allow_push"`
AllowDeploy sql.NullBool `sql:"allow_deploy"`
AllowTag sql.NullBool `sql:"allow_tag"`
AllowComment sql.NullBool `sql:"allow_comment"`
AllowSchedule sql.NullBool `sql:"allow_schedule"`
PipelineType sql.NullString `sql:"pipeline_type"`
PreviousName sql.NullString `sql:"previous_name"`
}

// Decrypt will manipulate the existing repo hash by
Expand Down Expand Up @@ -230,6 +231,7 @@ func (r *Repo) ToLibrary() *library.Repo {
repo.SetAllowDeploy(r.AllowDeploy.Bool)
repo.SetAllowTag(r.AllowTag.Bool)
repo.SetAllowComment(r.AllowComment.Bool)
repo.SetAllowSchedule(r.AllowSchedule.Bool)
repo.SetPipelineType(r.PipelineType.String)
repo.SetPreviousName(r.PreviousName.String)

Expand Down Expand Up @@ -301,30 +303,31 @@ func (r *Repo) Validate() error {
// to a database repo type.
func RepoFromLibrary(r *library.Repo) *Repo {
repo := &Repo{
ID: sql.NullInt64{Int64: r.GetID(), Valid: true},
UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true},
Hash: sql.NullString{String: r.GetHash(), Valid: true},
Org: sql.NullString{String: r.GetOrg(), Valid: true},
Name: sql.NullString{String: r.GetName(), Valid: true},
FullName: sql.NullString{String: r.GetFullName(), Valid: true},
Link: sql.NullString{String: r.GetLink(), Valid: true},
Clone: sql.NullString{String: r.GetClone(), Valid: true},
Branch: sql.NullString{String: r.GetBranch(), Valid: true},
Topics: pq.StringArray(r.GetTopics()),
BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true},
Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true},
Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true},
Visibility: sql.NullString{String: r.GetVisibility(), Valid: true},
Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true},
Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true},
Active: sql.NullBool{Bool: r.GetActive(), Valid: true},
AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true},
AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true},
AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true},
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true},
PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true},
PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true},
ID: sql.NullInt64{Int64: r.GetID(), Valid: true},
UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true},
Hash: sql.NullString{String: r.GetHash(), Valid: true},
Org: sql.NullString{String: r.GetOrg(), Valid: true},
Name: sql.NullString{String: r.GetName(), Valid: true},
FullName: sql.NullString{String: r.GetFullName(), Valid: true},
Link: sql.NullString{String: r.GetLink(), Valid: true},
Clone: sql.NullString{String: r.GetClone(), Valid: true},
Branch: sql.NullString{String: r.GetBranch(), Valid: true},
Topics: pq.StringArray(r.GetTopics()),
BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true},
Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true},
Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true},
Visibility: sql.NullString{String: r.GetVisibility(), Valid: true},
Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true},
Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true},
Active: sql.NullBool{Bool: r.GetActive(), Valid: true},
AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true},
AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true},
AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true},
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true},
AllowSchedule: sql.NullBool{Bool: r.GetAllowSchedule(), Valid: true},
PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true},
PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true},
}

return repo.Nullify()
Expand Down
53 changes: 28 additions & 25 deletions database/repo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down Expand Up @@ -177,6 +177,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want.SetAllowDeploy(false)
want.SetAllowTag(false)
want.SetAllowComment(false)
want.SetAllowSchedule(false)
want.SetPipelineType("yaml")
want.SetPreviousName("oldName")

Expand Down Expand Up @@ -329,6 +330,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r.SetAllowPush(true)
r.SetAllowDeploy(false)
r.SetAllowTag(false)
r.SetAllowSchedule(false)
r.SetAllowComment(false)
r.SetPipelineType("yaml")
r.SetPreviousName("oldName")
Expand All @@ -347,29 +349,30 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
// type with all fields set to a fake value.
func testRepo() *Repo {
return &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
Org: sql.NullString{String: "github", Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
FullName: sql.NullString{String: "github/octocat", Valid: true},
Link: sql.NullString{String: "https://github.com/github/octocat", Valid: true},
Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true},
Branch: sql.NullString{String: "master", Valid: true},
Topics: []string{"cloud", "security"},
BuildLimit: sql.NullInt64{Int64: 10, Valid: true},
Timeout: sql.NullInt64{Int64: 30, Valid: true},
Counter: sql.NullInt32{Int32: 0, Valid: true},
Visibility: sql.NullString{String: "public", Valid: true},
Private: sql.NullBool{Bool: false, Valid: true},
Trusted: sql.NullBool{Bool: false, Valid: true},
Active: sql.NullBool{Bool: true, Valid: true},
AllowPull: sql.NullBool{Bool: false, Valid: true},
AllowPush: sql.NullBool{Bool: true, Valid: true},
AllowDeploy: sql.NullBool{Bool: false, Valid: true},
AllowTag: sql.NullBool{Bool: false, Valid: true},
AllowComment: sql.NullBool{Bool: false, Valid: true},
PipelineType: sql.NullString{String: "yaml", Valid: true},
PreviousName: sql.NullString{String: "oldName", Valid: true},
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "superSecretHash", Valid: true},
Org: sql.NullString{String: "github", Valid: true},
Name: sql.NullString{String: "octocat", Valid: true},
FullName: sql.NullString{String: "github/octocat", Valid: true},
Link: sql.NullString{String: "https://github.com/github/octocat", Valid: true},
Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true},
Branch: sql.NullString{String: "master", Valid: true},
Topics: []string{"cloud", "security"},
BuildLimit: sql.NullInt64{Int64: 10, Valid: true},
Timeout: sql.NullInt64{Int64: 30, Valid: true},
Counter: sql.NullInt32{Int32: 0, Valid: true},
Visibility: sql.NullString{String: "public", Valid: true},
Private: sql.NullBool{Bool: false, Valid: true},
Trusted: sql.NullBool{Bool: false, Valid: true},
Active: sql.NullBool{Bool: true, Valid: true},
AllowPull: sql.NullBool{Bool: false, Valid: true},
AllowPush: sql.NullBool{Bool: true, Valid: true},
AllowDeploy: sql.NullBool{Bool: false, Valid: true},
AllowTag: sql.NullBool{Bool: false, Valid: true},
AllowSchedule: sql.NullBool{Bool: false, Valid: true},
AllowComment: sql.NullBool{Bool: false, Valid: true},
PipelineType: sql.NullString{String: "yaml", Valid: true},
PreviousName: sql.NullString{String: "oldName", Valid: true},
}
}
120 changes: 75 additions & 45 deletions library/repo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand All @@ -13,55 +13,57 @@ import (
//
// swagger:model Repo
type Repo struct {
ID *int64 `json:"id,omitempty"`
UserID *int64 `json:"user_id,omitempty"`
Hash *string `json:"-"`
Org *string `json:"org,omitempty"`
Name *string `json:"name,omitempty"`
FullName *string `json:"full_name,omitempty"`
Link *string `json:"link,omitempty"`
Clone *string `json:"clone,omitempty"`
Branch *string `json:"branch,omitempty"`
Topics *[]string `json:"topics,omitempty"`
BuildLimit *int64 `json:"build_limit,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
Counter *int `json:"counter,omitempty"`
Visibility *string `json:"visibility,omitempty"`
Private *bool `json:"private,omitempty"`
Trusted *bool `json:"trusted,omitempty"`
Active *bool `json:"active,omitempty"`
AllowPull *bool `json:"allow_pull,omitempty"`
AllowPush *bool `json:"allow_push,omitempty"`
AllowDeploy *bool `json:"allow_deploy,omitempty"`
AllowTag *bool `json:"allow_tag,omitempty"`
AllowComment *bool `json:"allow_comment,omitempty"`
PipelineType *string `json:"pipeline_type,omitempty"`
PreviousName *string `json:"previous_name,omitempty"`
ID *int64 `json:"id,omitempty"`
UserID *int64 `json:"user_id,omitempty"`
Hash *string `json:"-"`
Org *string `json:"org,omitempty"`
Name *string `json:"name,omitempty"`
FullName *string `json:"full_name,omitempty"`
Link *string `json:"link,omitempty"`
Clone *string `json:"clone,omitempty"`
Branch *string `json:"branch,omitempty"`
Topics *[]string `json:"topics,omitempty"`
BuildLimit *int64 `json:"build_limit,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
Counter *int `json:"counter,omitempty"`
Visibility *string `json:"visibility,omitempty"`
Private *bool `json:"private,omitempty"`
Trusted *bool `json:"trusted,omitempty"`
Active *bool `json:"active,omitempty"`
AllowPull *bool `json:"allow_pull,omitempty"`
AllowPush *bool `json:"allow_push,omitempty"`
AllowDeploy *bool `json:"allow_deploy,omitempty"`
AllowTag *bool `json:"allow_tag,omitempty"`
AllowComment *bool `json:"allow_comment,omitempty"`
AllowSchedule *bool `json:"allow_schedule,omitempty"`
PipelineType *string `json:"pipeline_type,omitempty"`
PreviousName *string `json:"previous_name,omitempty"`
}

// Environment returns a list of environment variables
// provided from the fields of the Repo type.
func (r *Repo) Environment() map[string]string {
return map[string]string{
"VELA_REPO_ACTIVE": ToString(r.GetActive()),
"VELA_REPO_ALLOW_COMMENT": ToString(r.GetAllowComment()),
"VELA_REPO_ALLOW_DEPLOY": ToString(r.GetAllowDeploy()),
"VELA_REPO_ALLOW_PULL": ToString(r.GetAllowPull()),
"VELA_REPO_ALLOW_PUSH": ToString(r.GetAllowPush()),
"VELA_REPO_ALLOW_TAG": ToString(r.GetAllowTag()),
"VELA_REPO_BRANCH": ToString(r.GetBranch()),
"VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","),
"VELA_REPO_BUILD_LIMIT": ToString(r.GetBuildLimit()),
"VELA_REPO_CLONE": ToString(r.GetClone()),
"VELA_REPO_FULL_NAME": ToString(r.GetFullName()),
"VELA_REPO_LINK": ToString(r.GetLink()),
"VELA_REPO_NAME": ToString(r.GetName()),
"VELA_REPO_ORG": ToString(r.GetOrg()),
"VELA_REPO_PRIVATE": ToString(r.GetPrivate()),
"VELA_REPO_TIMEOUT": ToString(r.GetTimeout()),
"VELA_REPO_TRUSTED": ToString(r.GetTrusted()),
"VELA_REPO_VISIBILITY": ToString(r.GetVisibility()),
"VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()),
"VELA_REPO_ACTIVE": ToString(r.GetActive()),
"VELA_REPO_ALLOW_COMMENT": ToString(r.GetAllowComment()),
"VELA_REPO_ALLOW_DEPLOY": ToString(r.GetAllowDeploy()),
"VELA_REPO_ALLOW_PULL": ToString(r.GetAllowPull()),
"VELA_REPO_ALLOW_PUSH": ToString(r.GetAllowPush()),
"VELA_REPO_ALLOW_SCHEDULE": ToString(r.GetAllowSchedule()),
"VELA_REPO_ALLOW_TAG": ToString(r.GetAllowTag()),
"VELA_REPO_BRANCH": ToString(r.GetBranch()),
"VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","),
"VELA_REPO_BUILD_LIMIT": ToString(r.GetBuildLimit()),
"VELA_REPO_CLONE": ToString(r.GetClone()),
"VELA_REPO_FULL_NAME": ToString(r.GetFullName()),
"VELA_REPO_LINK": ToString(r.GetLink()),
"VELA_REPO_NAME": ToString(r.GetName()),
"VELA_REPO_ORG": ToString(r.GetOrg()),
"VELA_REPO_PRIVATE": ToString(r.GetPrivate()),
"VELA_REPO_TIMEOUT": ToString(r.GetTimeout()),
"VELA_REPO_TRUSTED": ToString(r.GetTrusted()),
"VELA_REPO_VISIBILITY": ToString(r.GetVisibility()),
"VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()),

// deprecated environment variables
"REPOSITORY_ACTIVE": ToString(r.GetActive()),
Expand Down Expand Up @@ -361,14 +363,27 @@ func (r *Repo) GetAllowTag() bool {
// When the provided Repo type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (r *Repo) GetAllowComment() bool {
// return zero value if Repo type or AllowTag field is nil
// return zero value if Repo type or AllowComment field is nil
if r == nil || r.AllowComment == nil {
return false
}

return *r.AllowComment
}

// GetAllowSchedule returns the AllowSchedule field.
//
// When the provided Repo type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (r *Repo) GetAllowSchedule() bool {
// return zero value if Repo type or AllowSchedule field is nil
if r == nil || r.AllowSchedule == nil {
return false
}

return *r.AllowSchedule
}

// GetPipelineType returns the PipelineType field.
//
// When the provided Repo type is nil, or the field within
Expand Down Expand Up @@ -681,6 +696,19 @@ func (r *Repo) SetAllowComment(v bool) {
r.AllowComment = &v
}

// SetAllowSchedule sets the AllowSchedule field.
//
// When the provided Repo type is nil, it
// will set nothing and immediately return.
func (r *Repo) SetAllowSchedule(v bool) {
// return if Repo type is nil
if r == nil {
return
}

r.AllowSchedule = &v
}

// SetPipelineType sets the PipelineType field.
//
// When the provided Repo type is nil, it
Expand Down Expand Up @@ -715,6 +743,7 @@ func (r *Repo) String() string {
AllowDeploy: %t,
AllowPull: %t,
AllowPush: %t,
AllowSchedule: %t,
AllowTag: %t,
Branch: %s,
BuildLimit: %d,
Expand All @@ -739,6 +768,7 @@ func (r *Repo) String() string {
r.GetAllowDeploy(),
r.GetAllowPull(),
r.GetAllowPush(),
r.GetAllowSchedule(),
r.GetAllowTag(),
r.GetBranch(),
r.GetBuildLimit(),
Expand Down
Loading

0 comments on commit 71117bf

Please sign in to comment.