Skip to content

Commit 20301d9

Browse files
committed
test auto merge
1 parent c4d5ae6 commit 20301d9

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

bot/actions/automerge/automerge.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package automerge
22

33
import (
4+
"fmt"
45
"github.com/bivas/rivi/bot"
56
"github.com/bivas/rivi/util"
67
"github.com/mitchellh/mapstructure"
@@ -9,6 +10,7 @@ import (
910

1011
type action struct {
1112
rule *rule
13+
err error
1214
}
1315

1416
type MergeableEventData interface {
@@ -19,6 +21,7 @@ func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
1921
mergeable, ok := meta.(MergeableEventData)
2022
if !ok {
2123
util.Logger.Warning("Event data does not support merge. Check your configurations")
24+
a.err = fmt.Errorf("Event data does not support merge")
2225
return
2326
}
2427
approvals := 0
@@ -30,6 +33,7 @@ func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
3033
}
3134
clean := strings.ToLower(strings.TrimSpace(comment.Comment))
3235
if _, ok := approvedSearchPhrases[clean]; ok {
36+
assignees.Remove(comment.Commenter)
3337
approvals++
3438
}
3539
}
@@ -38,7 +42,6 @@ func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
3842
meta.AddComment(a.rule.Comment)
3943
}
4044
mergeable.Merge(a.rule.Strategy)
41-
4245
}
4346
}
4447

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,80 @@
11
package automerge
2+
3+
import (
4+
"github.com/bivas/rivi/bot"
5+
"github.com/bivas/rivi/bot/mock"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
type mockMergableEventData struct {
11+
mock.MockEventData
12+
merged bool
13+
method string
14+
}
15+
16+
func (m *mockMergableEventData) Merge(mergeMethod string) {
17+
m.merged = true
18+
m.method = mergeMethod
19+
}
20+
21+
func TestNotCapableToMerge(t *testing.T) {
22+
action := action{rule: &rule{}}
23+
action.rule.Defaults()
24+
config := &mock.MockConfiguration{}
25+
meta := &mock.MockEventData{Assignees: []string{"user1"}, Comments: []bot.Comment{
26+
{Commenter: "user1", Comment: "approved"},
27+
}}
28+
action.Apply(config, meta)
29+
assert.NotNil(t, action.err, "should be unable to merge")
30+
}
31+
32+
func TestCapableToMerge(t *testing.T) {
33+
action := action{rule: &rule{}}
34+
action.rule.Defaults()
35+
config := &mock.MockConfiguration{}
36+
mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Comments: []bot.Comment{
37+
{Commenter: "user1", Comment: "approved"},
38+
}}
39+
meta := &mockMergableEventData{MockEventData: mockEventData}
40+
action.Apply(config, meta)
41+
assert.True(t, meta.merged, "should be merged")
42+
assert.Equal(t, "merge", meta.method, "default should be merge")
43+
}
44+
45+
func TestOriginComment(t *testing.T) {
46+
action := action{rule: &rule{}}
47+
action.rule.Defaults()
48+
config := &mock.MockConfiguration{}
49+
mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{
50+
{Commenter: "user2", Comment: "approved"},
51+
}}
52+
meta := &mockMergableEventData{MockEventData: mockEventData}
53+
action.Apply(config, meta)
54+
assert.False(t, meta.merged, "should not be merged")
55+
}
56+
57+
func TestNotApprovedComment(t *testing.T) {
58+
action := action{rule: &rule{}}
59+
action.rule.Defaults()
60+
config := &mock.MockConfiguration{}
61+
mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{
62+
{Commenter: "user1", Comment: "not approved"},
63+
}}
64+
meta := &mockMergableEventData{MockEventData: mockEventData}
65+
action.Apply(config, meta)
66+
assert.False(t, meta.merged, "should not be merged")
67+
}
68+
69+
func TestSameApprovedComment(t *testing.T) {
70+
action := action{rule: &rule{Require: 2}}
71+
action.rule.Defaults()
72+
config := &mock.MockConfiguration{}
73+
mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{
74+
{Commenter: "user1", Comment: "approved"},
75+
{Commenter: "user1", Comment: "approved"},
76+
}}
77+
meta := &mockMergableEventData{MockEventData: mockEventData}
78+
action.Apply(config, meta)
79+
assert.False(t, meta.merged, "should not be merged")
80+
}

0 commit comments

Comments
 (0)