-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.go
143 lines (125 loc) · 2.88 KB
/
new.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
132
133
134
135
136
137
138
139
140
141
142
143
/* 2018-12-27 (cc) <paul4hough@gmail.com>
notification system interface
*/
package notify
import (
"fmt"
"github.com/pahoughton/agate/config"
"github.com/pahoughton/agate/notify/nid"
"github.com/pahoughton/agate/notify/mock"
"github.com/pahoughton/agate/notify/gitlab"
"github.com/pahoughton/agate/notify/hpsm"
proma "github.com/prometheus/client_golang/prometheus/promauto"
promp "github.com/prometheus/client_golang/prometheus"
)
type System interface {
Create(goup, title, desc string) (nid.Nid, error)
Update(nid nid.Nid, desc string) error
Close(nid nid.Nid, desc string) error
Group() string
}
type NSys int
const (
NSysMock NSys = iota
NSysGitlab
NSysHpsm
NSysUnknown
)
type NSysMap map[string]NSys
var (
nsysnames = []string{"mock","gitlab","hpsm","unknown"}
nsysmap = NSysMap{
"mock": NSysMock,
"gitlab": NSysGitlab,
"hpsm": NSysHpsm,
"unknown": NSysUnknown,
}
)
type metrics struct {
notes *promp.CounterVec
errors promp.Counter
}
type Notify struct {
DefSys NSys
CloseResolved bool
metrics metrics
systems []System
debug bool
}
func NewNSys(s string) NSys {
if v, ok := nsysmap[s]; ok {
return v
} else {
return NSysUnknown
}
}
func (t NSys) Int() int {
return int(t)
}
func (t NSys) String() string {
if NSysMock <= t && t <= NSysUnknown {
return nsysnames[t]
} else {
return "invalid"
}
}
func New(cfg config.Notify, dbg bool) *Notify {
n := &Notify{
debug: dbg,
DefSys: NewNSys(cfg.Default),
CloseResolved: cfg.Resolved,
metrics: metrics{
notes: proma.NewCounterVec(
promp.CounterOpts{
Namespace: "agate",
Subsystem: "notes",
Name: "generated",
Help: "number of ticekts created",
}, []string{
"sys",
"grp",
}),
errors: proma.NewCounter(
promp.CounterOpts{
Namespace: "agate",
Subsystem: "notify",
Name: "errors",
Help: "number of ticket errors",
}),
},
}
n.systems = make([]System,len(nsysmap))
n.systems[NSysMock] = mock.New(cfg.Sys.Mock,NSysMock.Int(),dbg)
n.systems[NSysGitlab] = gitlab.New(cfg.Sys.Gitlab,NSysGitlab.Int(),dbg)
n.systems[NSysHpsm] = hpsm.New(cfg.Sys.Hpsm,NSysHpsm.Int(),dbg)
if NSysMock > n.DefSys || n.DefSys >= NSysUnknown {
n.unregister()
panic(fmt.Sprintf("invalid default ticket sys: %s",cfg.Default))
}
return n
}
func (n *Notify) Del() {
n.unregister()
}
func (n *Notify) unregister() {
if n != nil && n.metrics.errors != nil {
promp.Unregister(n.metrics.errors);
n.metrics.errors = nil
}
if n.metrics.notes != nil {
promp.Unregister(n.metrics.notes);
n.metrics.notes = nil
}
}
func (n *Notify) Errorf(format string, args ...interface{}) error {
n.metrics.errors.Inc()
fmt.Println("ERROR: ",fmt.Sprintf(format,args...))
return fmt.Errorf(format,args...)
}
func (n *Notify) System(nsys NSys) System {
if NSysMock <= nsys && nsys < NSysUnknown {
return n.systems[nsys]
} else {
return nil
}
}