Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
fix: notification config parse (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutachaos authored and alexmt committed Jan 27, 2020
1 parent e48a7cb commit bc46f23
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
16 changes: 10 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,20 @@ func newCommand() *cobra.Command {
command.Flags().StringVar(&namespace, "namespace", "", "Namespace which controller handles. Current namespace if empty.")
return &command
}

func parseConfig(configData map[string]string, notifiersData []byte) (map[string]triggers.Trigger, map[string]notifiers.Notifier, map[string]string, error) {
cfg := &config{}
func parseConfigMapYaml(configData map[string]string) (cfg *config, err error) {
if data, ok := configData["config.yaml"]; ok {
err := yaml.Unmarshal([]byte(data), cfg)
err = yaml.Unmarshal([]byte(data), &cfg)
if err != nil {
return nil, nil, nil, err
return &config{}, err
}
}

return cfg, nil
}
func parseConfig(configData map[string]string, notifiersData []byte) (map[string]triggers.Trigger, map[string]notifiers.Notifier, map[string]string, error) {
cfg, err := parseConfigMapYaml(configData)
if err != nil {
return nil, nil, nil, err
}
cfg = defaultCfg.merge(cfg)
t, err := triggers.GetTriggers(cfg.Templates, cfg.Triggers)
if err != nil {
Expand Down
80 changes: 80 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,86 @@ import (
"github.com/argoproj-labs/argocd-notifications/triggers"
)

func TestParseConfigMapYaml(t *testing.T) {
configData := map[string]string{
"config.yaml": `
triggers:
- name: on-sync-status-custom
condition: app.status.operationState.phase in ['Custom']
description: Application custom trigger
template: app-sync-status
enabled: true
templates:
- name: app-sync-status
title: Application {{.app.metadata.name}} sync status is {{.app.status.sync.status}}
body: |
Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
slack:
attachments: |
[{
"title": "{{.app.metadata.name}}",
"title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#18be52",
"fields": [{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
}, {
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}]
}]
context:
argocdUrl: testUrl`}

chk := true
expectCfg := &config{
Triggers: []triggers.NotificationTrigger{
{
Name: "on-sync-status-custom",
Condition: "app.status.operationState.phase in ['Custom']",
Description: "Application custom trigger",
Template: "app-sync-status",
Enabled: &chk,
},
},
Templates: []triggers.NotificationTemplate{{
Name: "app-sync-status",
Notification: notifiers.Notification{
Title: "Application {{.app.metadata.name}} sync status is {{.app.status.sync.status}}",
Body: `Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
`,
Slack: &notifiers.SlackSpecific{
Attachments: `[{
"title": "{{.app.metadata.name}}",
"title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#18be52",
"fields": [{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
}, {
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}]
}]
`,
Blocks: "",
}},
}},
Context: map[string]string{"argocdUrl": "testUrl"},
}
actualCfg, err := parseConfigMapYaml(configData)
assert.NoError(t, err)
assert.Equal(t, expectCfg.Templates, actualCfg.Templates)
assert.Equal(t, expectCfg.Triggers, actualCfg.Triggers)
assert.Equal(t, expectCfg.Context, actualCfg.Context)
}

func TestMergeConfigTemplate(t *testing.T) {
cfg := config{
Templates: []triggers.NotificationTemplate{{
Expand Down
4 changes: 2 additions & 2 deletions triggers/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type NotificationTrigger struct {
}

type NotificationTemplate struct {
notifiers.Notification `json:"-"`
Name string `json:"name,omitempty"`
notifiers.Notification
Name string `json:"name,omitempty"`
}

type Trigger interface {
Expand Down

0 comments on commit bc46f23

Please sign in to comment.