Skip to content

Commit

Permalink
feat(repo)change allow events fields to an AllowEvents struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 15, 2023
1 parent ce74b93 commit 2024a8c
Show file tree
Hide file tree
Showing 9 changed files with 931 additions and 470 deletions.
9 changes: 0 additions & 9 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@ const (
// ActionEdited defines the action for the editing of pull requests or issue comments.
ActionEdited = "edited"

// ActionLabeled defines the action for labeling a pull request.
ActionLabeled = "labeled"

// ActionRenamed defines the action for renaming a repository.
ActionRenamed = "renamed"

// ActionReviewRequested defines the action for requesting a pull request review.
ActionReviewRequested = "review_requested"

// ActionSynchronize defines the action for the synchronizing of pull requests.
ActionSynchronize = "synchronize"

// ActionSubmitted defines the action for submitting pull request reviews.
ActionSubmitted = "submitted"

// ActionTransferred defines the action for transferring repository ownership.
ActionTransferred = "transferred"
)
17 changes: 6 additions & 11 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ package constants

// Allowed repo events.
const (
AllowPush = 1 << iota // 00000001 = 1
AllowPROpen // 00000010 = 2
AllowPREdit // 00000100 = 4
AllowPRSync // ...
AllowPRLabel
AllowPRReviewRequest
AllowTag
AllowDeploy
AllowPushBranch = 1 << iota // 00000001 = 1
AllowPullOpen // 00000010 = 2
AllowPullEdit // 00000100 = 4
AllowPullSync // ...
AllowPushTag
AllowDeployCreate
AllowCommentCreate
AllowCommentEdit
AllowReviewSubmit
AllowReviewEdit
AllowSchedule
)
2 changes: 1 addition & 1 deletion database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
// setup types
r := new(library.Repo)
e := new(library.Events)
e.SetPush(true)
e.SetPush(new(library.PushActions).FromMask(1))

r.SetID(1)
r.SetUserID(1)
Expand Down
341 changes: 341 additions & 0 deletions library/event_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package library

import "github.com/go-vela/types/constants"

// PushActions is the library representation of the various actions associated
// with the push event webhook from the SCM.
type PushActions struct {
Branch *bool `json:"branch"`
Tag *bool `json:"tag"`
}

// PullActions is the library representation of the various actions associated
// with the pull_request event webhook from the SCM.
type PullActions struct {
Opened *bool `json:"opened"`
Edited *bool `json:"edited"`
Synchronize *bool `json:"synchronize"`
}

// DeployActions is the library representation of the various actions associated
// with the deploy event webhook from the SCM.
type DeployActions struct {
Created *bool `json:"created"`
}

// CommentActions is the library representation of the various actions associated
// with the comment event webhook from the SCM.
type CommentActions struct {
Created *bool `json:"created"`
Edited *bool `json:"edited"`
}

// ScheduleActions is the library representation of the various actions associated
// with the schedule event processed by the server.
type ScheduleActions struct {
Schedule *bool `json:"schedule"`
}

// **
// PUSH ACTIONS
// **

// FromMask returns the PushActions type resulting from the provided integer mask.
func (a *PushActions) FromMask(mask int64) *PushActions {
a.SetBranch(mask&constants.AllowPushBranch > 0)
a.SetTag(mask&constants.AllowPushTag > 0)

return a
}

// ToMask returns the integer mask of the values for the PushActions set.
func (a *PushActions) ToMask() int64 {
mask := int64(0)

if a.GetBranch() {
mask = mask | constants.AllowPushBranch
}

if a.GetTag() {
mask = mask | constants.AllowPushTag
}

return mask
}

// GetBranch returns the Branch field from the provided PushActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *PushActions) GetBranch() bool {
// return zero value if PushActions type or Branch field is nil
if a == nil || a.Branch == nil {
return false
}

return *a.Branch
}

// GetTag returns the Tag field from the provided PushActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *PushActions) GetTag() bool {
// return zero value if PushActions type or Tag field is nil
if a == nil || a.Tag == nil {
return false
}

return *a.Tag
}

// SetBranch sets the PushActions Branch field.
//
// When the provided PushActions type is nil, it
// will set nothing and immediately return.
func (a *PushActions) SetBranch(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Branch = &v
}

// SetTag sets the PushActions Tag field.
//
// When the provided PushActions type is nil, it
// will set nothing and immediately return.
func (a *PushActions) SetTag(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Tag = &v
}

// **
// PULL ACTIONS
// **

// FromMask returns the PullActions type resulting from the provided integer mask.
func (a *PullActions) FromMask(mask int64) *PullActions {
a.SetOpened(mask&constants.AllowPullOpen > 0)
a.SetSynchronize(mask&constants.AllowPullSync > 0)
a.SetEdited(mask&constants.AllowPullEdit > 0)

return a
}

// ToMask returns the integer mask of the values for the PullActions set.
func (a *PullActions) ToMask() int64 {
mask := int64(0)

if a.GetOpened() {
mask = mask | constants.AllowPullOpen
}

if a.GetSynchronize() {
mask = mask | constants.AllowPullSync
}

if a.GetEdited() {
mask = mask | constants.AllowPullEdit
}

return mask
}

// GetOpened returns the Opened field from the provided PullActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *PullActions) GetOpened() bool {
// return zero value if PullActions type or Opened field is nil
if a == nil || a.Opened == nil {
return false
}

return *a.Opened
}

// GetSynchronize returns the Synchronize field from the provided PullActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *PullActions) GetSynchronize() bool {
// return zero value if PullActions type or Synchronize field is nil
if a == nil || a.Synchronize == nil {
return false
}

return *a.Synchronize
}

// GetEdited returns the Edited field from the provided PullActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *PullActions) GetEdited() bool {
// return zero value if PullActions type or Edited field is nil
if a == nil || a.Edited == nil {
return false
}

return *a.Edited
}

// SetOpened sets the PullActions Opened field.
//
// When the provided PullActions type is nil, it
// will set nothing and immediately return.
func (a *PullActions) SetOpened(v bool) {
// return if PullActions type is nil
if a == nil {
return
}

a.Opened = &v
}

// SetSynchronize sets the PullActions Synchronize field.
//
// When the provided PullActions type is nil, it
// will set nothing and immediately return.
func (a *PullActions) SetSynchronize(v bool) {
// return if PullActions type is nil
if a == nil {
return
}

a.Synchronize = &v
}

// SetEdited sets the PullActions Edited field.
//
// When the provided PullActions type is nil, it
// will set nothing and immediately return.
func (a *PullActions) SetEdited(v bool) {
// return if PullActions type is nil
if a == nil {
return
}

a.Edited = &v
}

// **
// DEPLOY ACTIONS
// **

// FromMask returns the DeployActions type resulting from the provided integer mask.
func (a *DeployActions) FromMask(mask int64) *DeployActions {
a.SetCreated(mask&constants.AllowDeployCreate > 0)

return a
}

// ToMask returns the integer mask of the values for the DeployActions set.
func (a *DeployActions) ToMask() int64 {
mask := int64(0)

if a.GetCreated() {
mask = mask | constants.AllowDeployCreate
}

return mask
}

// GetCreated returns the Created field from the provided DeployActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *DeployActions) GetCreated() bool {
// return zero value if DeployActions type or Created field is nil
if a == nil || a.Created == nil {
return false
}

return *a.Created
}

// SetCreated sets the DeployActions Created field.
//
// When the provided DeployActions type is nil, it
// will set nothing and immediately return.
func (a *DeployActions) SetCreated(v bool) {
// return if DeployActions type is nil
if a == nil {
return
}

a.Created = &v
}

// **
// COMMENT ACTIONS
// **

// FromMask returns the CommentActions type resulting from the provided integer mask.
func (a *CommentActions) FromMask(mask int64) *CommentActions {
a.SetCreated(mask&constants.AllowCommentCreate > 0)
a.SetEdited(mask&constants.AllowCommentEdit > 0)

return a
}

// ToMask returns the integer mask of the values for the CommentActions set.
func (a *CommentActions) ToMask() int64 {
mask := int64(0)

if a.GetCreated() {
mask = mask | constants.AllowCommentCreate
}

if a.GetEdited() {
mask = mask | constants.AllowCommentEdit
}

return mask
}

// GetCreated returns the Created field from the provided CommentActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *CommentActions) GetCreated() bool {
// return zero value if Events type or Created field is nil
if a == nil || a.Created == nil {
return false
}

return *a.Created
}

// GetEdited returns the Edited field from the provided CommentActions. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *CommentActions) GetEdited() bool {
// return zero value if Events type or Edited field is nil
if a == nil || a.Edited == nil {
return false
}

return *a.Edited
}

// SetCreated sets the CommentActions Created field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (a *CommentActions) SetCreated(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Created = &v
}

// SetEdited sets the CommentActions Edited field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (a *CommentActions) SetEdited(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Edited = &v
}
Loading

0 comments on commit 2024a8c

Please sign in to comment.