forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_jira_notifications.go
78 lines (64 loc) · 2.25 KB
/
config_jira_notifications.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
package evergreen
import (
"context"
"fmt"
"text/template"
"github.com/mongodb/grip"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type JIRANotificationsConfig struct {
CustomFields []JIRANotificationsProject `bson:"custom_fields"`
}
type JIRANotificationsProject struct {
Project string `bson:"project"`
Fields []JIRANotificationsCustomField `bson:"fields"`
Components []string `bson:"components"`
Labels []string `bson:"labels"`
}
type JIRANotificationsCustomField struct {
Field string `bson:"field"`
Template string `bson:"template"`
}
func (c *JIRANotificationsConfig) SectionId() string { return "jira_notifications" }
func (c *JIRANotificationsConfig) Get(ctx context.Context) error {
res := GetEnvironment().DB().Collection(ConfigCollection).FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = JIRANotificationsConfig{}
return nil
}
return errors.Wrapf(err, "getting config section '%s'", c.SectionId())
}
if err := res.Decode(&c); err != nil {
return errors.Wrapf(err, "decoding config section '%s'", c.SectionId())
}
return nil
}
func (c *JIRANotificationsConfig) Set(ctx context.Context) error {
_, err := GetEnvironment().DB().Collection(ConfigCollection).ReplaceOne(ctx, byId(c.SectionId()), c, options.Replace().SetUpsert(true))
return errors.Wrapf(err, "updating config section '%s'", c.SectionId())
}
func (c *JIRANotificationsConfig) ValidateAndDefault() error {
catcher := grip.NewSimpleCatcher()
projectSet := make(map[string]bool)
for _, project := range c.CustomFields {
if projectSet[project.Project] {
catcher.Errorf("duplicate project key '%s'", project.Project)
continue
}
projectSet[project.Project] = true
fieldSet := make(map[string]bool)
for _, field := range project.Fields {
if fieldSet[field.Field] {
catcher.Errorf("duplicate field key '%s' in project '%s'", field.Field, project.Project)
continue
}
fieldSet[field.Field] = true
_, err := template.New(fmt.Sprintf("%s-%s", project.Project, field.Field)).Parse(field.Template)
catcher.Add(err)
}
}
return catcher.Resolve()
}