Skip to content

Commit 89ab60c

Browse files
add support for multiple templates
1 parent d43cbab commit 89ab60c

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

examples/jiralert.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ receivers:
7272

7373

7474
# File containing template definitions. Required.
75-
template: jiralert.tmpl
75+
template:
76+
- jiralert.tmpl
77+
- other.tmpl

pkg/config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func resolveFilepaths(baseDir string, cfg *Config, logger log.Logger) {
112112
return absFp
113113
}
114114

115-
cfg.Template = join(cfg.Template)
115+
for i, v := range cfg.Template {
116+
cfg.Template[i] = join(v)
117+
}
116118
}
117119

118120
// AutoResolve is the struct used for defining jira resolution state when alert is resolved.
@@ -180,7 +182,7 @@ func (rc *ReceiverConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
180182
type Config struct {
181183
Defaults *ReceiverConfig `yaml:"defaults,omitempty" json:"defaults,omitempty"`
182184
Receivers []*ReceiverConfig `yaml:"receivers,omitempty" json:"receivers,omitempty"`
183-
Template string `yaml:"template" json:"template"`
185+
Template []string `yaml:"template" json:"template"`
184186

185187
// Catches all undefined fields and must be empty after parsing.
186188
XXX map[string]interface{} `yaml:",inline" json:"-"`
@@ -330,7 +332,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
330332
return fmt.Errorf("no receivers defined")
331333
}
332334

333-
if c.Template == "" {
335+
if len(c.Template) == 0 {
334336
return fmt.Errorf("missing template file")
335337
}
336338

pkg/config/config_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ receivers:
7878
customfield_10003: [{"value": "red" }, {"value": "blue" }, {"value": "green" }]
7979
8080
# File containing template definitions. Required.
81-
template: jiralert.tmpl
81+
template: ["jiralert.tmpl"]
8282
`
8383

8484
// Generic test that loads the testConf with no errors.
@@ -144,7 +144,7 @@ type receiverTestConfig struct {
144144
type testConfig struct {
145145
Defaults *receiverTestConfig `yaml:"defaults,omitempty"`
146146
Receivers []*receiverTestConfig `yaml:"receivers,omitempty"`
147-
Template string `yaml:"template,omitempty"`
147+
Template []string `yaml:"template,omitempty"`
148148
}
149149

150150
// Required Config keys tests.
@@ -155,7 +155,7 @@ func TestMissingConfigKeys(t *testing.T) {
155155
var config testConfig
156156

157157
// No receivers.
158-
config = testConfig{Defaults: defaultsConfig, Receivers: []*receiverTestConfig{}, Template: "jiralert.tmpl"}
158+
config = testConfig{Defaults: defaultsConfig, Receivers: []*receiverTestConfig{}, Template: []string{"jiralert.tmpl"}}
159159
configErrorTestRunner(t, config, "no receivers defined")
160160

161161
// No template.
@@ -188,7 +188,7 @@ func TestRequiredReceiverConfigKeys(t *testing.T) {
188188
config := testConfig{
189189
Defaults: defaultsConfig,
190190
Receivers: []*receiverTestConfig{receiverConfig},
191-
Template: "jiratemplate.tmpl",
191+
Template: []string{"jiralert.tmpl"},
192192
}
193193
configErrorTestRunner(t, config, test.errorMessage)
194194
}
@@ -237,7 +237,7 @@ func TestAuthKeysErrors(t *testing.T) {
237237
config := testConfig{
238238
Defaults: defaultsConfig,
239239
Receivers: []*receiverTestConfig{minimalReceiverTestConfig},
240-
Template: "jiralert.tmpl",
240+
Template: []string{"jiralert.tmpl"},
241241
}
242242

243243
configErrorTestRunner(t, config, test.errorMessage)
@@ -294,7 +294,7 @@ func TestAuthKeysOverrides(t *testing.T) {
294294
config := testConfig{
295295
Defaults: defaultsConfig,
296296
Receivers: []*receiverTestConfig{receiverConfig},
297-
Template: "jiralert.tmpl",
297+
Template: []string{"jiralert.tmpl"},
298298
}
299299

300300
yamlConfig, err := yaml.Marshal(&config)
@@ -353,7 +353,7 @@ func TestReceiverOverrides(t *testing.T) {
353353
config := testConfig{
354354
Defaults: defaultsConfig,
355355
Receivers: []*receiverTestConfig{receiverConfig},
356-
Template: "jiralert.tmpl",
356+
Template: []string{"jiralert.tmpl"},
357357
}
358358

359359
yamlConfig, err := yaml.Marshal(&config)
@@ -455,7 +455,7 @@ func TestAutoResolveConfigReceiver(t *testing.T) {
455455
config := testConfig{
456456
Defaults: defaultsConfig,
457457
Receivers: []*receiverTestConfig{minimalReceiverTestConfig},
458-
Template: "jiralert.tmpl",
458+
Template: []string{"jiralert.tmpl"},
459459
}
460460

461461
configErrorTestRunner(t, config, "bad config in receiver \"test\", 'auto_resolve' was defined with empty 'state' field")
@@ -473,7 +473,7 @@ func TestAutoResolveConfigDefault(t *testing.T) {
473473
config := testConfig{
474474
Defaults: defaultsConfig,
475475
Receivers: []*receiverTestConfig{minimalReceiverTestConfig},
476-
Template: "jiralert.tmpl",
476+
Template: []string{"jiralert.tmpl"},
477477
}
478478

479479
configErrorTestRunner(t, config, "bad config in defaults section: state cannot be empty")
@@ -503,7 +503,7 @@ func TestStaticLabelsConfigMerge(t *testing.T) {
503503
config := testConfig{
504504
Defaults: defaultsConfig,
505505
Receivers: []*receiverTestConfig{receiverConfig},
506-
Template: "jiralert.tmpl",
506+
Template: []string{"jiralert.tmpl"},
507507
}
508508

509509
yamlConfig, err := yaml.Marshal(&config)

pkg/template/template.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var funcs = template.FuncMap{
4141
return strings.Join(s, sep)
4242
},
4343
"match": regexp.MatchString,
44+
"split": strings.Split,
4445
"reReplaceAll": func(pattern, repl, text string) string {
4546
re := regexp.MustCompile(pattern)
4647
return re.ReplaceAllString(text, repl)
@@ -54,9 +55,9 @@ var funcs = template.FuncMap{
5455
}
5556

5657
// LoadTemplate reads and parses all templates defined in the given file and constructs a jiralert.Template.
57-
func LoadTemplate(path string, logger log.Logger) (*Template, error) {
58+
func LoadTemplate(path []string, logger log.Logger) (*Template, error) {
5859
level.Debug(logger).Log("msg", "loading templates", "path", path)
59-
tmpl, err := template.New("").Option("missingkey=zero").Funcs(funcs).ParseFiles(path)
60+
tmpl, err := template.New("").Option("missingkey=zero").Funcs(funcs).ParseFiles(path...)
6061
if err != nil {
6162
return nil, err
6263
}

0 commit comments

Comments
 (0)