diff --git a/constants/allow_events.go b/constants/allow_events.go index 220f75b9..7b2cc9db 100644 --- a/constants/allow_events.go +++ b/constants/allow_events.go @@ -2,14 +2,19 @@ package constants -// Allowed repo events. NOTE: these can NOT change order. +// Allowed repo events. NOTE: these can NOT change order. New events must be added at the end. const ( AllowPushBranch = 1 << iota // 00000001 = 1 AllowPushTag // 00000010 = 2 AllowPullOpen // 00000010 = 4 AllowPullEdit // ... AllowPullSync + _ // AllowPullAssigned - Not Implemented + _ // AllowPullMilestoned - Not Implemented _ // AllowPullLabel - Not Implemented + _ // AllowPullLocked - Not Implemented + _ // AllowPullReady - Not Implemented + _ // AllowPullReopen - Not Implemented _ // AllowPullReviewRequest - Not Implemented _ // AllowPullClosed - Not Implemented AllowDeployCreate diff --git a/database/repo.go b/database/repo.go index bb580bf3..adfa20a0 100644 --- a/database/repo.go +++ b/database/repo.go @@ -61,6 +61,11 @@ type Repo struct { 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"` AllowEvents sql.NullInt64 `sql:"allow_events"` PipelineType sql.NullString `sql:"pipeline_type"` PreviousName sql.NullString `sql:"previous_name"` @@ -224,6 +229,11 @@ func (r *Repo) ToLibrary() *library.Repo { repo.SetPrivate(r.Private.Bool) repo.SetTrusted(r.Trusted.Bool) repo.SetActive(r.Active.Bool) + repo.SetAllowPull(r.AllowPull.Bool) + repo.SetAllowPush(r.AllowPush.Bool) + repo.SetAllowDeploy(r.AllowDeploy.Bool) + repo.SetAllowTag(r.AllowTag.Bool) + repo.SetAllowComment(r.AllowComment.Bool) repo.SetAllowEvents(library.NewEventsFromMask(r.AllowEvents.Int64)) repo.SetPipelineType(r.PipelineType.String) repo.SetPreviousName(r.PreviousName.String) @@ -315,6 +325,11 @@ func RepoFromLibrary(r *library.Repo) *Repo { 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}, AllowEvents: sql.NullInt64{Int64: r.GetAllowEvents().ToDatabase(), Valid: true}, PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true}, PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true}, diff --git a/database/repo_test.go b/database/repo_test.go index e86efa94..69a8e1ff 100644 --- a/database/repo_test.go +++ b/database/repo_test.go @@ -173,6 +173,11 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) { want.SetPrivate(false) want.SetTrusted(false) want.SetActive(true) + want.SetAllowPull(false) + want.SetAllowPush(true) + want.SetAllowDeploy(false) + want.SetAllowTag(false) + want.SetAllowComment(false) want.SetAllowEvents(e) want.SetPipelineType("yaml") want.SetPreviousName("oldName") @@ -324,6 +329,11 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { r.SetPrivate(false) r.SetTrusted(false) r.SetActive(true) + r.SetAllowPull(false) + r.SetAllowPush(true) + r.SetAllowDeploy(false) + r.SetAllowTag(false) + r.SetAllowComment(false) r.SetAllowEvents(e) r.SetPipelineType("yaml") r.SetPreviousName("oldName") @@ -359,6 +369,11 @@ func testRepo() *Repo { 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}, AllowEvents: sql.NullInt64{Int64: 1, Valid: true}, PipelineType: sql.NullString{String: "yaml", Valid: true}, PreviousName: sql.NullString{String: "oldName", Valid: true}, diff --git a/item_test.go b/item_test.go index 245ebf95..8643f3e1 100644 --- a/item_test.go +++ b/item_test.go @@ -55,6 +55,10 @@ func TestTypes_ToItem(t *testing.T) { Private: &booL, Trusted: &booL, Active: &booL, + AllowPull: &booL, + AllowPush: &booL, + AllowDeploy: &booL, + AllowTag: &booL, AllowEvents: e, } u := &library.User{ @@ -103,6 +107,10 @@ func TestTypes_ToItem(t *testing.T) { Private: &booL, Trusted: &booL, Active: &booL, + AllowPull: &booL, + AllowPush: &booL, + AllowDeploy: &booL, + AllowTag: &booL, AllowEvents: e, }, User: &library.User{ diff --git a/library/repo.go b/library/repo.go index cd7c318b..d982fc7a 100644 --- a/library/repo.go +++ b/library/repo.go @@ -30,6 +30,11 @@ type Repo struct { 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"` AllowEvents *Events `json:"allow_events,omitempty"` PipelineType *string `json:"pipeline_type,omitempty"` PreviousName *string `json:"previous_name,omitempty"` @@ -40,6 +45,11 @@ type Repo struct { 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_ALLOW_EVENTS": strings.Join(r.GetAllowEvents().List()[:], ","), "VELA_REPO_BRANCH": ToString(r.GetBranch()), "VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","), @@ -56,18 +66,23 @@ func (r *Repo) Environment() map[string]string { "VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()), // deprecated environment variables - "REPOSITORY_ACTIVE": ToString(r.GetActive()), - "REPOSITORY_ALLOW_EVENTS": strings.Join(r.GetAllowEvents().List()[:], ","), - "REPOSITORY_BRANCH": ToString(r.GetBranch()), - "REPOSITORY_CLONE": ToString(r.GetClone()), - "REPOSITORY_FULL_NAME": ToString(r.GetFullName()), - "REPOSITORY_LINK": ToString(r.GetLink()), - "REPOSITORY_NAME": ToString(r.GetName()), - "REPOSITORY_ORG": ToString(r.GetOrg()), - "REPOSITORY_PRIVATE": ToString(r.GetPrivate()), - "REPOSITORY_TIMEOUT": ToString(r.GetTimeout()), - "REPOSITORY_TRUSTED": ToString(r.GetTrusted()), - "REPOSITORY_VISIBILITY": ToString(r.GetVisibility()), + "REPOSITORY_ACTIVE": ToString(r.GetActive()), + "REPOSITORY_ALLOW_COMMENT": ToString(r.GetAllowComment()), + "REPOSITORY_ALLOW_DEPLOY": ToString(r.GetAllowDeploy()), + "REPOSITORY_ALLOW_PULL": ToString(r.GetAllowPull()), + "REPOSITORY_ALLOW_PUSH": ToString(r.GetAllowPush()), + "REPOSITORY_ALLOW_TAG": ToString(r.GetAllowTag()), + "REPOSITORY_ALLOW_EVENTS": strings.Join(r.GetAllowEvents().List()[:], ","), + "REPOSITORY_BRANCH": ToString(r.GetBranch()), + "REPOSITORY_CLONE": ToString(r.GetClone()), + "REPOSITORY_FULL_NAME": ToString(r.GetFullName()), + "REPOSITORY_LINK": ToString(r.GetLink()), + "REPOSITORY_NAME": ToString(r.GetName()), + "REPOSITORY_ORG": ToString(r.GetOrg()), + "REPOSITORY_PRIVATE": ToString(r.GetPrivate()), + "REPOSITORY_TIMEOUT": ToString(r.GetTimeout()), + "REPOSITORY_TRUSTED": ToString(r.GetTrusted()), + "REPOSITORY_VISIBILITY": ToString(r.GetVisibility()), } } @@ -292,6 +307,71 @@ func (r *Repo) GetActive() bool { return *r.Active } +// GetAllowPull returns the AllowPull 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) GetAllowPull() bool { + // return zero value if Repo type or AllowPull field is nil + if r == nil || r.AllowPull == nil { + return false + } + + return *r.AllowPull +} + +// GetAllowPush returns the AllowPush 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) GetAllowPush() bool { + // return zero value if Repo type or AllowPush field is nil + if r == nil || r.AllowPush == nil { + return false + } + + return *r.AllowPush +} + +// GetAllowDeploy returns the AllowDeploy 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) GetAllowDeploy() bool { + // return zero value if Repo type or AllowDeploy field is nil + if r == nil || r.AllowDeploy == nil { + return false + } + + return *r.AllowDeploy +} + +// GetAllowTag returns the AllowTag 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) GetAllowTag() bool { + // return zero value if Repo type or AllowTag field is nil + if r == nil || r.AllowTag == nil { + return false + } + + return *r.AllowTag +} + +// GetAllowComment returns the AllowComment 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) GetAllowComment() bool { + // return zero value if Repo type or AllowTag field is nil + if r == nil || r.AllowComment == nil { + return false + } + + return *r.AllowComment +} + // GetAllowEvents returns the AllowEvents field. // // When the provided Repo type is nil, or the field within @@ -552,6 +632,71 @@ func (r *Repo) SetActive(v bool) { r.Active = &v } +// SetAllowPull sets the AllowPull field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowPull(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowPull = &v +} + +// SetAllowPush sets the AllowPush field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowPush(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowPush = &v +} + +// SetAllowDeploy sets the AllowDeploy field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowDeploy(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowDeploy = &v +} + +// SetAllowTag sets the AllowTag field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowTag(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowTag = &v +} + +// SetAllowComment sets the AllowComment field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowComment(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowComment = &v +} + // SetAllowEvents sets the AllowEvents field. // // When the provided Repo type is nil, it @@ -625,6 +770,11 @@ func (r *Repo) EventAllowed(event, action string) (allowed bool) { func (r *Repo) String() string { return fmt.Sprintf(`{ Active: %t, + AllowComment: %t, + AllowDeploy: %t, + AllowPull: %t, + AllowPush: %t, + AllowTag: %t, AllowEvents: %s, Branch: %s, BuildLimit: %d, @@ -645,6 +795,11 @@ func (r *Repo) String() string { Visibility: %s, }`, r.GetActive(), + r.GetAllowComment(), + r.GetAllowDeploy(), + r.GetAllowPull(), + r.GetAllowPush(), + r.GetAllowTag(), r.GetAllowEvents().List(), r.GetBranch(), r.GetBuildLimit(), diff --git a/library/repo_test.go b/library/repo_test.go index a8535e4c..8e52dc82 100644 --- a/library/repo_test.go +++ b/library/repo_test.go @@ -11,33 +11,43 @@ import ( func TestLibrary_Repo_Environment(t *testing.T) { // setup types want := map[string]string{ - "VELA_REPO_ACTIVE": "true", - "VELA_REPO_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,tag", - "VELA_REPO_BRANCH": "main", - "VELA_REPO_TOPICS": "cloud,security", - "VELA_REPO_BUILD_LIMIT": "10", - "VELA_REPO_CLONE": "https://github.com/github/octocat.git", - "VELA_REPO_FULL_NAME": "github/octocat", - "VELA_REPO_LINK": "https://github.com/github/octocat", - "VELA_REPO_NAME": "octocat", - "VELA_REPO_ORG": "github", - "VELA_REPO_PRIVATE": "false", - "VELA_REPO_TIMEOUT": "30", - "VELA_REPO_TRUSTED": "false", - "VELA_REPO_VISIBILITY": "public", - "VELA_REPO_PIPELINE_TYPE": "", - "REPOSITORY_ACTIVE": "true", - "REPOSITORY_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,tag", - "REPOSITORY_BRANCH": "main", - "REPOSITORY_CLONE": "https://github.com/github/octocat.git", - "REPOSITORY_FULL_NAME": "github/octocat", - "REPOSITORY_LINK": "https://github.com/github/octocat", - "REPOSITORY_NAME": "octocat", - "REPOSITORY_ORG": "github", - "REPOSITORY_PRIVATE": "false", - "REPOSITORY_TIMEOUT": "30", - "REPOSITORY_TRUSTED": "false", - "REPOSITORY_VISIBILITY": "public", + "VELA_REPO_ACTIVE": "true", + "VELA_REPO_ALLOW_COMMENT": "false", + "VELA_REPO_ALLOW_DEPLOY": "false", + "VELA_REPO_ALLOW_PULL": "false", + "VELA_REPO_ALLOW_PUSH": "true", + "VELA_REPO_ALLOW_TAG": "false", + "VELA_REPO_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,tag", + "VELA_REPO_BRANCH": "main", + "VELA_REPO_TOPICS": "cloud,security", + "VELA_REPO_BUILD_LIMIT": "10", + "VELA_REPO_CLONE": "https://github.com/github/octocat.git", + "VELA_REPO_FULL_NAME": "github/octocat", + "VELA_REPO_LINK": "https://github.com/github/octocat", + "VELA_REPO_NAME": "octocat", + "VELA_REPO_ORG": "github", + "VELA_REPO_PRIVATE": "false", + "VELA_REPO_TIMEOUT": "30", + "VELA_REPO_TRUSTED": "false", + "VELA_REPO_VISIBILITY": "public", + "VELA_REPO_PIPELINE_TYPE": "", + "REPOSITORY_ACTIVE": "true", + "REPOSITORY_ALLOW_COMMENT": "false", + "REPOSITORY_ALLOW_DEPLOY": "false", + "REPOSITORY_ALLOW_PULL": "false", + "REPOSITORY_ALLOW_PUSH": "true", + "REPOSITORY_ALLOW_TAG": "false", + "REPOSITORY_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,tag", + "REPOSITORY_BRANCH": "main", + "REPOSITORY_CLONE": "https://github.com/github/octocat.git", + "REPOSITORY_FULL_NAME": "github/octocat", + "REPOSITORY_LINK": "https://github.com/github/octocat", + "REPOSITORY_NAME": "octocat", + "REPOSITORY_ORG": "github", + "REPOSITORY_PRIVATE": "false", + "REPOSITORY_TIMEOUT": "30", + "REPOSITORY_TRUSTED": "false", + "REPOSITORY_VISIBILITY": "public", } // run test @@ -130,6 +140,26 @@ func TestLibrary_Repo_Getters(t *testing.T) { t.Errorf("GetActive is %v, want %v", test.repo.GetActive(), test.want.GetActive()) } + if test.repo.GetAllowPull() != test.want.GetAllowPull() { + t.Errorf("GetAllowPull is %v, want %v", test.repo.GetAllowPull(), test.want.GetAllowPull()) + } + + if test.repo.GetAllowPush() != test.want.GetAllowPush() { + t.Errorf("GetAllowPush is %v, want %v", test.repo.GetAllowPush(), test.want.GetAllowPush()) + } + + if test.repo.GetAllowDeploy() != test.want.GetAllowDeploy() { + t.Errorf("GetAllowDeploy is %v, want %v", test.repo.GetAllowDeploy(), test.want.GetAllowDeploy()) + } + + if test.repo.GetAllowTag() != test.want.GetAllowTag() { + t.Errorf("GetAllowTag is %v, want %v", test.repo.GetAllowTag(), test.want.GetAllowTag()) + } + + if test.repo.GetAllowComment() != test.want.GetAllowComment() { + t.Errorf("GetAllowComment is %v, want %v", test.repo.GetAllowComment(), test.want.GetAllowComment()) + } + if !reflect.DeepEqual(test.repo.GetAllowEvents(), test.want.GetAllowEvents()) { t.Errorf("GetRepo is %v, want %v", test.repo.GetAllowEvents(), test.want.GetAllowEvents()) } @@ -182,6 +212,11 @@ func TestLibrary_Repo_Setters(t *testing.T) { test.repo.SetPrivate(test.want.GetPrivate()) test.repo.SetTrusted(test.want.GetTrusted()) test.repo.SetActive(test.want.GetActive()) + test.repo.SetAllowPull(test.want.GetAllowPull()) + test.repo.SetAllowPush(test.want.GetAllowPush()) + test.repo.SetAllowDeploy(test.want.GetAllowDeploy()) + test.repo.SetAllowTag(test.want.GetAllowTag()) + test.repo.SetAllowComment(test.want.GetAllowComment()) test.repo.SetAllowEvents(test.want.GetAllowEvents()) test.repo.SetPipelineType(test.want.GetPipelineType()) test.repo.SetPreviousName(test.want.GetPreviousName()) @@ -250,6 +285,26 @@ func TestLibrary_Repo_Setters(t *testing.T) { t.Errorf("SetActive is %v, want %v", test.repo.GetActive(), test.want.GetActive()) } + if test.repo.GetAllowPull() != test.want.GetAllowPull() { + t.Errorf("SetAllowPull is %v, want %v", test.repo.GetAllowPull(), test.want.GetAllowPull()) + } + + if test.repo.GetAllowPush() != test.want.GetAllowPush() { + t.Errorf("SetAllowPush is %v, want %v", test.repo.GetAllowPush(), test.want.GetAllowPush()) + } + + if test.repo.GetAllowDeploy() != test.want.GetAllowDeploy() { + t.Errorf("SetAllowDeploy is %v, want %v", test.repo.GetAllowDeploy(), test.want.GetAllowDeploy()) + } + + if test.repo.GetAllowTag() != test.want.GetAllowTag() { + t.Errorf("SetAllowTag is %v, want %v", test.repo.GetAllowTag(), test.want.GetAllowTag()) + } + + if test.repo.GetAllowComment() != test.want.GetAllowComment() { + t.Errorf("SetAllowComment is %v, want %v", test.repo.GetAllowComment(), test.want.GetAllowComment()) + } + if !reflect.DeepEqual(test.repo.GetAllowEvents(), test.want.GetAllowEvents()) { t.Errorf("GetRepo is %v, want %v", test.repo.GetAllowEvents(), test.want.GetAllowEvents()) } @@ -305,6 +360,11 @@ func TestLibrary_Repo_String(t *testing.T) { want := fmt.Sprintf(`{ Active: %t, + AllowComment: %t, + AllowDeploy: %t, + AllowPull: %t, + AllowPush: %t, + AllowTag: %t, AllowEvents: %s, Branch: %s, BuildLimit: %d, @@ -325,6 +385,11 @@ func TestLibrary_Repo_String(t *testing.T) { Visibility: %s, }`, r.GetActive(), + r.GetAllowComment(), + r.GetAllowDeploy(), + r.GetAllowPull(), + r.GetAllowPush(), + r.GetAllowTag(), r.GetAllowEvents().List(), r.GetBranch(), r.GetBuildLimit(), @@ -375,6 +440,11 @@ func testRepo() *Repo { r.SetPrivate(false) r.SetTrusted(false) r.SetActive(true) + r.SetAllowPull(false) + r.SetAllowPush(true) + r.SetAllowDeploy(false) + r.SetAllowTag(false) + r.SetAllowComment(false) r.SetAllowEvents(e) r.SetPipelineType("") r.SetPreviousName("")