/
labeler.go
68 lines (58 loc) · 1.5 KB
/
labeler.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
package labeler
import (
"fmt"
"github.com/bivas/rivi/pkg/engine/actions"
"github.com/bivas/rivi/pkg/types"
"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) Apply(state multistep.StateBag) {
meta := state.Get("data").(types.Data)
apply := a.rule.Label
if apply != "" {
if meta.HasLabel(apply) {
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Skipping label '%s' as it already exists", apply)
} else {
counter.Inc()
meta.AddLabel(apply)
}
}
remove := a.rule.Remove
if remove != "" {
if !meta.HasLabel(remove) {
a.logger.DebugWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Skipping label '%s' removal as it does not exists", remove)
} else {
counter.Inc()
meta.RemoveLabel(remove)
}
}
}
type factory struct {
}
func (*factory) BuildAction(config map[string]interface{}) actions.Action {
item := rule{}
logger := log.Get("labeler")
if e := mapstructure.Decode(config, &item); e != nil {
logger.ErrorWith(log.MetaFields{log.E(e)}, "Unable to build action")
return nil
}
return &action{rule: &item, logger: logger}
}
var counter = actions.NewCounter("labeler")
func init() {
actions.RegisterAction("labeler", &factory{})
prometheus.Register(counter)
}