-
Notifications
You must be signed in to change notification settings - Fork 28
/
notification.go
65 lines (55 loc) · 1.94 KB
/
notification.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package result
import (
"fmt"
"os"
argov1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
tmv1beta1 "github.com/gardener/test-infra/pkg/apis/testmachinery/v1beta1"
)
// GenerateNotificationConfigForAlerting creates a notification config file with email recipients if any test step has failed
// The config file is then evaluated by Concourse
func GenerateNotificationConfigForAlerting(tr []*tmv1beta1.Testrun, concourseOnErrorDir string) {
if concourseOnErrorDir == "" {
return
}
notifyConfig := createNotificationString(tr)
if notifyConfig == nil {
return
}
notifyConfigFilePath := fmt.Sprintf("%s/notify.cfg", concourseOnErrorDir)
if err := os.WriteFile(notifyConfigFilePath, notifyConfig, 0777); err != nil {
log.Warnf("Cannot write file email notification config to %s: %s", notifyConfigFilePath, err.Error())
return
}
log.Infof("Successfully created file %s", notifyConfigFilePath)
}
func createNotificationString(testruns []*tmv1beta1.Testrun) []byte {
cfg := notificationCfg{
Email: email{
Subject: "Test Machinery - some steps failed",
MailBody: "Test Machinery steps have failed.\n\nFailed Steps:\n",
},
}
for _, tr := range testruns {
cfg.Email.MailBody = fmt.Sprintf("%s Testrun: %s\n", cfg.Email.MailBody, tr.Name)
for _, step := range tr.Status.Steps {
if step.Phase == argov1.NodeFailed {
cfg.Email.MailBody = fmt.Sprintf("%s - %s\n", cfg.Email.MailBody, step.TestDefinition.Name)
cfg.Email.Recipients = append(cfg.Email.Recipients, step.TestDefinition.RecipientsOnFailure...)
}
}
}
if len(cfg.Email.Recipients) != 0 {
cfgBytes, err := yaml.Marshal(cfg)
if err != nil {
log.Warnf("Cannot encode email notification config %s", err.Error())
return nil
}
return cfgBytes
}
return nil
}