/
locker.go
74 lines (66 loc) · 2.03 KB
/
locker.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
package locker
import (
"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/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
type LockableData interface {
Lock()
Unlock()
LockState() bool
}
type action struct {
rule *rule
err error
logger log.Logger
}
func (a *action) Apply(state multistep.StateBag) {
meta := state.Get("data").(types.Data)
lockable, ok := meta.(LockableData)
if !ok {
a.logger.WarningWith(
log.MetaFields{log.F("issue", meta.GetShortName())},
"Event data does not support locking. Check your configurations")
a.err = errors.New("event data does not support locking")
return
}
if lockable.LockState() {
a.logger.Debug("Issue is locked")
if a.rule.State == "unlock" || a.rule.State == "change" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "unlocking issue")
counter.Inc()
lockable.Unlock()
} else if a.rule.State == "lock" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already locked - nothing changed")
}
} else {
a.logger.Debug("Issue is unlocked")
if a.rule.State == "lock" || a.rule.State == "change" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Locking issue")
counter.Inc()
lockable.Lock()
} else if a.rule.State == "lock" {
a.logger.DebugWith(log.MetaFields{log.F("issue", meta.GetShortName())}, "Issue is already unlocked - nothing changed")
}
}
}
type factory struct {
}
func (*factory) BuildAction(config map[string]interface{}) actions.Action {
item := rule{}
logger := log.Get("locker")
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("locker")
func init() {
actions.RegisterAction("locker", &factory{})
prometheus.Register(counter)
}