-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
133 lines (104 loc) · 2.64 KB
/
config.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
package main
import (
"fmt"
"strings"
"github.com/opensourceways/community-robot-lib/mq"
"k8s.io/apimachinery/pkg/util/sets"
)
type configuration struct {
Config accessConfig `json:"access,omitempty"`
}
func (c *configuration) Validate() error {
return c.Config.validate()
}
func (c *configuration) SetDefault() {}
type accessConfig struct {
// RepoPlugins list of plugins needed to configure the repository
RepoPlugins map[string][]string `json:"repo_plugins,omitempty"`
// Plugins is a list available plugins.
Plugins []pluginConfig `json:"plugins,omitempty"`
Broker mq.MQConfig `json:"broker,omitempty"`
}
func (a accessConfig) validate() error {
for i := range a.Plugins {
if err := a.Plugins[i].validate(); err != nil {
return err
}
}
ps := make([]string, len(a.Plugins))
for i := range a.Plugins {
ps[i] = a.Plugins[i].Name
}
total := sets.NewString(ps...)
for k, item := range a.RepoPlugins {
if v := sets.NewString(item...).Difference(total); v.Len() != 0 {
return fmt.Errorf(
"%s: unknown plugins(%s) are set", k,
strings.Join(v.UnsortedList(), ", "),
)
}
}
return nil
}
type eventsDemux map[string][]string
func updateDemux(p *pluginConfig, d eventsDemux) {
endpoint := p.Endpoint
for _, e := range p.Events {
if es, ok := d[e]; ok {
d[e] = append(es, endpoint)
} else {
d[e] = []string{endpoint}
}
}
}
func orgOfRepo(repo string) string {
spliter := "/"
if strings.Contains(repo, spliter) {
return strings.Split(repo, spliter)[0]
}
return ""
}
func (a accessConfig) getDemux() map[string]eventsDemux {
plugins := make(map[string]int)
for i := range a.Plugins {
plugins[a.Plugins[i].Name] = i
}
r := make(map[string]eventsDemux)
rp := a.RepoPlugins
for k, ps := range rp {
events, ok := r[k]
if !ok {
events = make(eventsDemux)
r[k] = events
}
// inherit the config of org if k is a repo.
if org := orgOfRepo(k); org != "" {
ps = append(ps, rp[org]...)
}
for _, p := range ps {
if i, ok := plugins[p]; ok {
updateDemux(&a.Plugins[i], events)
}
}
}
return r
}
type pluginConfig struct {
// Name of the service.
Name string `json:"name" required:"true"`
// Endpoint is the location of the service.
Endpoint string `json:"endpoint" required:"true"`
// Events are the events that this service can handle and should be forward to it.
// If no events are specified, everything is sent.
Events []string `json:"events,omitempty"`
}
func (p pluginConfig) validate() error {
if p.Name == "" {
return fmt.Errorf("missing name")
}
if p.Endpoint == "" {
return fmt.Errorf("missing endpoint")
}
// TODO validate the value of p.Endpoint
return nil
}