/
autoassign.go
131 lines (117 loc) · 3.59 KB
/
autoassign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package autoassign
import (
"fmt"
"math/rand"
"strings"
"github.com/bivas/rivi/pkg/config"
"github.com/bivas/rivi/pkg/engine/actions"
"github.com/bivas/rivi/pkg/types"
"github.com/bivas/rivi/pkg/util"
"github.com/bivas/rivi/pkg/util/log"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep"
"github.com/prometheus/client_golang/prometheus"
)
type action struct {
rule *rule
logger log.Logger
}
func (a *action) String() string {
return fmt.Sprintf("%T{rule: %+v}", *a, a.rule)
}
func (a *action) findAssignedRoles(assignees []string, config config.Configuration) []string {
var assignedRoles []string
if len(assignees) > 0 {
assignedRolesSet := util.StringSet{}
for _, role := range config.GetRoles() {
for _, member := range config.GetRoleMembers(role) {
for _, assignee := range assignees {
if member == assignee {
assignedRolesSet.Add(role)
}
}
}
}
assignedRoles = assignedRolesSet.Values()
a.logger.Debug("There are %d assignees from %d roles", len(assignees), len(assignedRoles))
}
return assignedRoles
}
func (a *action) findLookupRoles(config config.Configuration, assignedRoles []string) []string {
lookupRoles := config.GetRoles()
if len(a.rule.FromRoles) > 0 {
lookupRoles = a.rule.FromRoles
}
a.logger.Debug("lookup roles are %s", lookupRoles)
return lookupRoles
}
func (a *action) Apply(state multistep.StateBag) {
meta := state.Get("data").(types.Data)
conf := state.Get("config").(config.Configuration)
assignees := meta.GetAssignees()
if len(assignees) >= a.rule.Require {
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Skipping as there are matched required assignees")
return
}
assignedRoles := a.findAssignedRoles(assignees, conf)
lookupRoles := a.findLookupRoles(conf, assignedRoles)
winners := a.randomUsers(conf, meta, lookupRoles)
if len(winners) > 0 {
counter.Inc()
meta.AddAssignees(winners...)
}
}
func (a *action) randomUsers(config config.Configuration, meta types.Data, lookupRoles []string) []string {
possibleSet := util.StringSet{Transformer: strings.ToLower}
possibleSet.AddAll(config.GetRoleMembers(lookupRoles...)).Remove(meta.GetOrigin().User)
for _, assignee := range meta.GetAssignees() {
possibleSet.Remove(assignee)
}
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"There are %d possible assignees from %d roles", possibleSet.Len(), len(lookupRoles))
if possibleSet.Len() == 0 {
return []string{}
}
remainingRequired := a.rule.Require - len(meta.GetAssignees())
if remainingRequired < 0 {
remainingRequired = 0
}
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Require %d assignees", remainingRequired)
possible := possibleSet.Values()
winners := make([]string, remainingRequired)
for i := 0; i < remainingRequired; i++ {
index := rand.Intn(len(possible))
if possible[index] == "" {
i--
} else {
winners[i] = possible[index]
possible[index] = ""
}
}
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Selecting users %s as assignees", winners)
return winners
}
type factory struct {
}
func (*factory) BuildAction(config map[string]interface{}) actions.Action {
item := rule{}
logger := log.Get("autoassign")
if e := mapstructure.Decode(config, &item); e != nil {
logger.ErrorWith(log.MetaFields{log.E(e)}, "Unable to build action")
return nil
}
item.Defaults()
return &action{rule: &item, logger: logger}
}
var counter = actions.NewCounter("autoassign")
func init() {
actions.RegisterAction("autoassign", &factory{})
prometheus.Register(counter)
}