Skip to content

Commit 039f60c

Browse files
committed
add label to automerge
1 parent dcd65a7 commit 039f60c

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

bot/actions/automerge/automerge.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ type MergeableEventData interface {
1717
Merge(mergeMethod string)
1818
}
1919

20-
func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
21-
mergeable, ok := meta.(MergeableEventData)
22-
if !ok {
23-
util.Logger.Warning("Event data does not support merge. Check your configurations")
24-
a.err = fmt.Errorf("Event data does not support merge")
25-
return
20+
func (a *action) merge(meta bot.EventData) {
21+
if a.rule.Label == "" {
22+
mergeable, ok := meta.(MergeableEventData)
23+
if !ok {
24+
util.Logger.Warning("Event data does not support merge. Check your configurations")
25+
a.err = fmt.Errorf("Event data does not support merge")
26+
return
27+
}
28+
mergeable.Merge(a.rule.Strategy)
29+
} else {
30+
meta.AddLabel(a.rule.Label)
2631
}
32+
}
33+
34+
func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
2735
approvals := 0
2836
assignees := util.StringSet{}
2937
assignees.AddAll(meta.GetAssignees())
@@ -37,8 +45,12 @@ func (a *action) Apply(config bot.Configuration, meta bot.EventData) {
3745
approvals++
3846
}
3947
}
40-
if approvals >= a.rule.Require {
41-
mergeable.Merge(a.rule.Strategy)
48+
if a.rule.Require == 0 && assignees.Len() == 0 {
49+
util.Logger.Debug("All assignees have approved the PR - merging")
50+
a.merge(meta)
51+
} else if a.rule.Require > 0 && approvals >= a.rule.Require {
52+
util.Logger.Debug("Got %d required approvals for PR - merging", a.rule.Require)
53+
a.merge(meta)
4254
}
4355
}
4456

@@ -50,6 +62,7 @@ func (*factory) BuildAction(config map[string]interface{}) bot.Action {
5062
if e := mapstructure.Decode(config, &item); e != nil {
5163
panic(e)
5264
}
65+
item.Defaults()
5366
return &action{rule: &item}
5467
}
5568

bot/actions/automerge/automerge.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ None
1515
## Options
1616

1717
- `strategy` (optional) - which strategy to use when merging. can be `merge`, `squash` or `rebase` (default: `merge`)
18-
- `require` (optional) - the number of approvals required (default: **1**)
18+
- `require` (optional) - the number of approvals required (default: **0** meaning require all assignees to approve)
19+
- `label` (optional) - label the issue instead of merging (dry-run)
1920

2021
## Example
2122
```yaml
@@ -24,5 +25,11 @@ rules:
2425
automerge:
2526
require: 2
2627
strategy: squash
27-
28+
```
29+
or
30+
```yaml
31+
rules:
32+
example:
33+
automerge:
34+
label: approved
2835
```

bot/actions/automerge/automerge_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ func TestNotCapableToMerge(t *testing.T) {
2929
assert.NotNil(t, action.err, "should be unable to merge")
3030
}
3131

32+
func TestShouldNotMergeMergeMissingApprovals(t *testing.T) {
33+
action := action{rule: &rule{}}
34+
action.rule.Defaults()
35+
config := &mock.MockConfiguration{}
36+
mockEventData := mock.MockEventData{Assignees: []string{"user1", "user2"}, Comments: []bot.Comment{
37+
{Commenter: "user1", Comment: "approved"},
38+
}}
39+
meta := &mockMergableEventData{MockEventData: mockEventData}
40+
action.Apply(config, meta)
41+
assert.False(t, meta.merged, "should not be merged")
42+
}
43+
3244
func TestCapableToMerge(t *testing.T) {
3345
action := action{rule: &rule{}}
3446
action.rule.Defaults()
@@ -78,3 +90,17 @@ func TestSameApprovedComment(t *testing.T) {
7890
action.Apply(config, meta)
7991
assert.False(t, meta.merged, "should not be merged")
8092
}
93+
94+
func TestLabel(t *testing.T) {
95+
action := action{rule: &rule{Label: "approved"}}
96+
action.rule.Defaults()
97+
config := &mock.MockConfiguration{}
98+
mockEventData := mock.MockEventData{Assignees: []string{"user1"}, Origin: "user2", Comments: []bot.Comment{
99+
{Commenter: "user1", Comment: "approved"},
100+
}}
101+
meta := &mockMergableEventData{MockEventData: mockEventData}
102+
action.Apply(config, meta)
103+
assert.False(t, meta.merged, "should not be merged")
104+
assert.Len(t, meta.AddedLabels, 1, "should label and not merge")
105+
assert.Equal(t, "approved", meta.AddedLabels[0], "approved")
106+
}

bot/actions/automerge/rule.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ var (
2121
type rule struct {
2222
Require int `mapstructure:"require"`
2323
Strategy string `mapstructure:"strategy"`
24+
Label string `mapstructure:"label"`
2425
}
2526

2627
func (r *rule) Defaults() {
27-
if r.Require == 0 {
28-
r.Require = 1
29-
}
3028
if r.Strategy == "" {
3129
r.Strategy = "merge"
3230
} else {

0 commit comments

Comments
 (0)