Skip to content

Commit

Permalink
Merge branch 'main' into feat/deploymentTable
Browse files Browse the repository at this point in the history
  • Loading branch information
claire1618 authored Dec 15, 2023
2 parents 242e042 + 1eae2f5 commit 8701631
Show file tree
Hide file tree
Showing 17 changed files with 1,308 additions and 1 deletion.
23 changes: 23 additions & 0 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0

package constants

// 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 // 00000100 = 4
AllowPullEdit // ...
AllowPullSync
_ // AllowPullAssigned - Not Implemented
_ // AllowPullMilestoned - Not Implemented
_ // AllowPullLabel - Not Implemented
_ // AllowPullLocked - Not Implemented
_ // AllowPullReady - Not Implemented
AllowPullReopen
_ // AllowPullReviewRequest - Not Implemented
_ // AllowPullClosed - Not Implemented
AllowDeployCreate
AllowCommentCreate
AllowCommentEdit
)
8 changes: 8 additions & 0 deletions database/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Repo struct {
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"`
ApproveBuild sql.NullString `sql:"approve_build"`
Expand Down Expand Up @@ -184,6 +185,11 @@ func (r *Repo) Nullify() *Repo {
r.Timeout.Valid = false
}

// check if the AllowEvents field should be false
if r.AllowEvents.Int64 == 0 {
r.AllowEvents.Valid = false
}

// check if the Visibility field should be false
if len(r.Visibility.String) == 0 {
r.Visibility.Valid = false
Expand Down Expand Up @@ -234,6 +240,7 @@ func (r *Repo) ToLibrary() *library.Repo {
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)
repo.SetApproveBuild(r.ApproveBuild.String)
Expand Down Expand Up @@ -330,6 +337,7 @@ func RepoFromLibrary(r *library.Repo) *Repo {
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},
ApproveBuild: sql.NullString{String: r.GetApproveBuild(), Valid: true},
Expand Down
8 changes: 8 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
)

func TestDatabase_Repo_Decrypt(t *testing.T) {
Expand Down Expand Up @@ -117,6 +118,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) {
Clone: sql.NullString{String: "", Valid: false},
Branch: sql.NullString{String: "", Valid: false},
Timeout: sql.NullInt64{Int64: 0, Valid: false},
AllowEvents: sql.NullInt64{Int64: 0, Valid: false},
Visibility: sql.NullString{String: "", Valid: false},
PipelineType: sql.NullString{String: "", Valid: false},
ApproveBuild: sql.NullString{String: "", Valid: false},
Expand Down Expand Up @@ -154,6 +156,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) {
func TestDatabase_Repo_ToLibrary(t *testing.T) {
// setup types
want := new(library.Repo)
e := library.NewEventsFromMask(1)

want.SetID(1)
want.SetUserID(1)
Expand All @@ -177,6 +180,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want.SetAllowDeploy(false)
want.SetAllowTag(false)
want.SetAllowComment(false)
want.SetAllowEvents(e)
want.SetPipelineType("yaml")
want.SetPreviousName("oldName")
want.SetApproveBuild(constants.ApproveNever)
Expand Down Expand Up @@ -308,6 +312,8 @@ func TestDatabase_Repo_Validate(t *testing.T) {
func TestDatabase_RepoFromLibrary(t *testing.T) {
// setup types
r := new(library.Repo)
e := new(library.Events)
e.SetPush(new(actions.Push).FromMask(1))

r.SetID(1)
r.SetUserID(1)
Expand All @@ -331,6 +337,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r.SetAllowDeploy(false)
r.SetAllowTag(false)
r.SetAllowComment(false)
r.SetAllowEvents(e)
r.SetPipelineType("yaml")
r.SetPreviousName("oldName")
r.SetApproveBuild(constants.ApproveNever)
Expand Down Expand Up @@ -371,6 +378,7 @@ func testRepo() *Repo {
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},
ApproveBuild: sql.NullString{String: constants.ApproveNever, Valid: true},
Expand Down
4 changes: 4 additions & 0 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func TestTypes_ToItem(t *testing.T) {
num := 1
num64 := int64(num)
str := "foo"
e := new(library.Events)

b := &library.Build{
ID: &num64,
RepoID: &num64,
Expand Down Expand Up @@ -57,6 +59,7 @@ func TestTypes_ToItem(t *testing.T) {
AllowPush: &booL,
AllowDeploy: &booL,
AllowTag: &booL,
AllowEvents: e,
}
u := &library.User{
ID: &num64,
Expand Down Expand Up @@ -108,6 +111,7 @@ func TestTypes_ToItem(t *testing.T) {
AllowPush: &booL,
AllowDeploy: &booL,
AllowTag: &booL,
AllowEvents: e,
},
User: &library.User{
ID: &num64,
Expand Down
84 changes: 84 additions & 0 deletions library/actions/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0
//
//nolint:dupl // ignore dup code
package actions

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

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

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

return a
}

// ToMask returns the integer mask of the values for the Comment set.
func (a *Comment) 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 Comment. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Comment) 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 Comment. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Comment) 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 Comment Created field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (a *Comment) SetCreated(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Created = &v
}

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

a.Edited = &v
}
108 changes: 108 additions & 0 deletions library/actions/comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: Apache-2.0

package actions

import (
"reflect"
"testing"

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

func TestLibrary_Comment_Getters(t *testing.T) {
// setup tests
tests := []struct {
actions *Comment
want *Comment
}{
{
actions: testComment(),
want: testComment(),
},
{
actions: new(Comment),
want: new(Comment),
},
}

// run tests
for _, test := range tests {
if test.actions.GetCreated() != test.want.GetCreated() {
t.Errorf("GetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated())
}

if test.actions.GetEdited() != test.want.GetEdited() {
t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited())
}
}
}

func TestLibrary_Comment_Setters(t *testing.T) {
// setup types
var a *Comment

// setup tests
tests := []struct {
actions *Comment
want *Comment
}{
{
actions: testComment(),
want: testComment(),
},
{
actions: a,
want: new(Comment),
},
}

// run tests
for _, test := range tests {
test.actions.SetCreated(test.want.GetCreated())
test.actions.SetEdited(test.want.GetEdited())

if test.actions.GetCreated() != test.want.GetCreated() {
t.Errorf("SetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated())
}

if test.actions.GetEdited() != test.want.GetEdited() {
t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited())
}
}
}

func TestLibrary_Comment_FromMask(t *testing.T) {
// setup types
mask := testMask()

want := testComment()

// run test
got := new(Comment).FromMask(mask)

if !reflect.DeepEqual(got, want) {
t.Errorf("FromMask is %v, want %v", got, want)
}
}

func TestLibrary_Comment_ToMask(t *testing.T) {
// setup types
actions := testComment()

want := int64(constants.AllowCommentCreate)

// run test
got := actions.ToMask()

if want != got {
t.Errorf("ToMask is %v, want %v", got, want)
}
}

func testComment() *Comment {
comment := new(Comment)
comment.SetCreated(true)
comment.SetEdited(false)

return comment
}
53 changes: 53 additions & 0 deletions library/actions/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: Apache-2.0

package actions

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

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

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

return a
}

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

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

return mask
}

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

return *a.Created
}

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

a.Created = &v
}
Loading

0 comments on commit 8701631

Please sign in to comment.