Skip to content

Commit

Permalink
fix(repo_events): add support for reopen (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Dec 11, 2023
1 parent 23c1585 commit 1eae2f5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
_ // AllowPullLabel - Not Implemented
_ // AllowPullLocked - Not Implemented
_ // AllowPullReady - Not Implemented
_ // AllowPullReopen - Not Implemented
AllowPullReopen
_ // AllowPullReviewRequest - Not Implemented
_ // AllowPullClosed - Not Implemented
AllowDeployCreate
Expand Down
30 changes: 30 additions & 0 deletions library/actions/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ type Pull struct {
Opened *bool `json:"opened"`
Edited *bool `json:"edited"`
Synchronize *bool `json:"synchronize"`
Reopened *bool `json:"reopened"`
}

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

return a
}
Expand All @@ -37,6 +39,10 @@ func (a *Pull) ToMask() int64 {
mask = mask | constants.AllowPullEdit
}

if a.GetReopened() {
mask = mask | constants.AllowPullReopen
}

return mask
}

Expand Down Expand Up @@ -73,6 +79,17 @@ func (a *Pull) GetEdited() bool {
return *a.Edited
}

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

return *a.Reopened
}

// SetOpened sets the Pull Opened field.
//
// When the provided Pull type is nil, it
Expand Down Expand Up @@ -111,3 +128,16 @@ func (a *Pull) SetEdited(v bool) {

a.Edited = &v
}

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

a.Reopened = &v
}
12 changes: 11 additions & 1 deletion library/actions/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func TestLibrary_Pull_Getters(t *testing.T) {
if test.actions.GetEdited() != test.want.GetEdited() {
t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited())
}

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

Expand Down Expand Up @@ -65,6 +69,7 @@ func TestLibrary_Pull_Setters(t *testing.T) {
test.actions.SetOpened(test.want.GetOpened())
test.actions.SetSynchronize(test.want.GetSynchronize())
test.actions.SetEdited(test.want.GetEdited())
test.actions.SetReopened(test.want.GetReopened())

if test.actions.GetOpened() != test.want.GetOpened() {
t.Errorf("SetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened())
Expand All @@ -77,6 +82,10 @@ func TestLibrary_Pull_Setters(t *testing.T) {
if test.actions.GetEdited() != test.want.GetEdited() {
t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited())
}

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

Expand All @@ -98,7 +107,7 @@ func TestLibrary_Pull_ToMask(t *testing.T) {
// setup types
actions := testPull()

want := int64(constants.AllowPullOpen | constants.AllowPullSync)
want := int64(constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen)

// run test
got := actions.ToMask()
Expand All @@ -113,6 +122,7 @@ func testPull() *Pull {
pr.SetOpened(true)
pr.SetSynchronize(true)
pr.SetEdited(false)
pr.SetReopened(true)

return pr
}
10 changes: 9 additions & 1 deletion library/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,13 @@ func testPush() *Push {
}

func testMask() int64 {
return int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowDeployCreate | constants.AllowCommentCreate)
return int64(
constants.AllowPushBranch |
constants.AllowPushTag |
constants.AllowPullOpen |
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowDeployCreate |
constants.AllowCommentCreate,
)
}
9 changes: 8 additions & 1 deletion library/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ func TestLibrary_Events_List(t *testing.T) {

func TestLibrary_Events_NewEventsFromMask(t *testing.T) {
// setup mask
mask := int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPullOpen | constants.AllowPullSync)
mask := int64(
constants.AllowPushBranch |
constants.AllowPushTag |
constants.AllowPullOpen |
constants.AllowPullSync |
constants.AllowPullReopen,
)

want := testEvents()

Expand All @@ -125,6 +131,7 @@ func testEvents() *Events {
pr.SetOpened(true)
pr.SetSynchronize(true)
pr.SetEdited(false)
pr.SetReopened(true)

push := new(actions.Push)
push.SetBranch(true)
Expand Down
3 changes: 2 additions & 1 deletion yaml/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error {

for _, e := range rules.Event {
switch e {
// backwards compatibility - pull_request = pull_request:opened + pull_request:synchronize
// backwards compatibility
// pull_request = pull_request:opened + pull_request:synchronize + pull_request:reopened
// comment = comment:created + comment:edited
case constants.EventPull:
events = append(events,
Expand Down

0 comments on commit 1eae2f5

Please sign in to comment.