Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit a53d095

Browse files
authored
feat. support teams theme color templating (#256)
* feat. support teams theme color templating * UPD. update markdown syntax conventions, mention themeColor field in teams integration guide * UPD. rollback to single quote for passing CI/CD validations, add screenshot in doc
1 parent 157603b commit a53d095

File tree

9 files changed

+50
-2
lines changed

9 files changed

+50
-2
lines changed

catalog/install.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ data:
9090
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
9191
}]
9292
}]
93+
themeColor: '#000080'
9394
title: New version of an application {{.app.metadata.name}} is up and running.
9495
template.app-health-degraded: |
9596
email:
@@ -161,6 +162,7 @@ data:
161162
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
162163
}]
163164
}]
165+
themeColor: '#FF0000'
164166
title: Application {{.app.metadata.name}} has degraded.
165167
template.app-sync-failed: |
166168
email:
@@ -236,6 +238,7 @@ data:
236238
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
237239
}]
238240
}]
241+
themeColor: '#FF0000'
239242
title: Failed to sync application {{.app.metadata.name}}.
240243
template.app-sync-running: |
241244
email:
@@ -462,6 +465,7 @@ data:
462465
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
463466
}]
464467
}]
468+
themeColor: '#000080'
465469
title: Application {{.app.metadata.name}} has been successfully synced
466470
trigger.on-created: |
467471
- description: Application is created.

catalog/templates/app-deployed.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ slack:
3636
]
3737
}]
3838
teams:
39+
themeColor: "#000080"
3940
title: New version of an application {{.app.metadata.name}} is up and running.
4041
facts: |
4142
[{

catalog/templates/app-health-degraded.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ slack:
3232
]
3333
}]
3434
teams:
35+
themeColor: "#FF0000"
3536
title: Application {{.app.metadata.name}} has degraded.
3637
facts: |
3738
[{

catalog/templates/app-sync-failed.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ slack:
3232
]
3333
}]
3434
teams:
35+
themeColor: "#FF0000"
3536
title: Failed to sync application {{.app.metadata.name}}.
3637
facts: |
3738
[{

catalog/templates/app-sync-succeeded.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ slack:
3232
]
3333
}]
3434
teams:
35+
themeColor: "#000080"
3536
title: Application {{.app.metadata.name}} has been successfully synced
3637
facts: |
3738
[{

docs/catalog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ teams:
112112
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
113113
}]
114114
}]
115+
themeColor: '#000080'
115116
title: New version of an application {{.app.metadata.name}} is up and running.
116117

117118
```
@@ -187,6 +188,7 @@ teams:
187188
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
188189
}]
189190
}]
191+
themeColor: '#FF0000'
190192
title: Application {{.app.metadata.name}} has degraded.
191193

192194
```
@@ -266,6 +268,7 @@ teams:
266268
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
267269
}]
268270
}]
271+
themeColor: '#FF0000'
269272
title: Failed to sync application {{.app.metadata.name}}.
270273

271274
```
@@ -504,6 +507,7 @@ teams:
504507
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
505508
}]
506509
}]
510+
themeColor: '#000080'
507511
title: Application {{.app.metadata.name}} has been successfully synced
508512

509513
```

docs/services/teams.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ metadata:
2121
name: argocd-notifications-cm
2222
data:
2323
service.teams: |
24-
recipientUrls:
24+
recipientUrls:
2525
channelName: $channel-teams-url
2626
```
2727
@@ -48,11 +48,12 @@ metadata:
4848
4949
![](https://user-images.githubusercontent.com/18019529/114271500-9d2b8880-9a4c-11eb-85c1-f6935f0431d5.png)
5050
51-
Notification templates can be customized to leverage teams message sections, facts and potentialAction [feature](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using).
51+
Notification templates can be customized to leverage teams message sections, facts, themeColor, and potentialAction [feature](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using).
5252
5353
```yaml
5454
template.app-sync-succeeded: |
5555
teams:
56+
themeColor: "#000080"
5657
sections: |
5758
[{
5859
"facts": [
@@ -96,3 +97,15 @@ template.app-sync-succeeded: |
9697
"value": "{{.app.spec.source.repoURL}}"
9798
}]
9899
```
100+
101+
### theme color field
102+
103+
You can set theme color as hex string for the message.
104+
105+
![](https://user-images.githubusercontent.com/1164159/114864810-0718a900-9e24-11eb-8127-8d95da9544c1.png)
106+
107+
```yaml
108+
template.app-sync-succeeded: |
109+
teams:
110+
themeColor: "#000080"
111+
```

pkg/services/teams.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type TeamsNotification struct {
1616
Template string `json:"template,omitempty"`
1717
Title string `json:"title,omitempty"`
1818
Text string `json:"text,omitempty"`
19+
ThemeColor string `json:"themeColor,omitempty"`
1920
Facts string `json:"facts,omitempty"`
2021
Sections string `json:"sections,omitempty"`
2122
PotentialAction string `json:"potentialAction,omitempty"`
@@ -37,6 +38,11 @@ func (n *TeamsNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
3738
return nil, fmt.Errorf("error in '%s' teams.text : %w", name, err)
3839
}
3940

41+
themeColor, err := texttemplate.New(name).Funcs(f).Parse(n.ThemeColor)
42+
if err != nil {
43+
return nil, fmt.Errorf("error in '%s' teams.themeColor: %w", name, err)
44+
}
45+
4046
facts, err := texttemplate.New(name).Funcs(f).Parse(n.Facts)
4147
if err != nil {
4248
return nil, fmt.Errorf("error in '%s' teams.facts : %w", name, err)
@@ -81,6 +87,14 @@ func (n *TeamsNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
8187
notification.Teams.Text = val
8288
}
8389

90+
var themeColorBuff bytes.Buffer
91+
if err := themeColor.Execute(&themeColorBuff, vars); err != nil {
92+
return err
93+
}
94+
if val := themeColorBuff.String(); val != "" {
95+
notification.Teams.ThemeColor = val
96+
}
97+
8498
var factsData bytes.Buffer
8599
if err := facts.Execute(&factsData, vars); err != nil {
86100
return err
@@ -177,6 +191,10 @@ func teamsNotificationToMessage(n Notification) (*teamsMessage, error) {
177191
message.Text = n.Teams.Text
178192
}
179193

194+
if n.Teams.ThemeColor != "" {
195+
message.ThemeColor = n.Teams.ThemeColor
196+
}
197+
180198
if n.Teams.Sections != "" {
181199
unmarshalledSections := make([]teamsSection, 2)
182200
err := json.Unmarshal([]byte(n.Teams.Sections), &unmarshalledSections)
@@ -234,6 +252,7 @@ type teamsMessage struct {
234252
Context string `json:"context"`
235253
Title string `json:"title"`
236254
Text string `json:"text"`
255+
ThemeColor string `json:"themeColor,omitempty"`
237256
PotentialAction []teamsAction `json:"potentialAction,omitempty"`
238257
Sections []teamsSection `json:"sections,omitempty"`
239258
}

pkg/services/teams_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestGetTemplater_Teams(t *testing.T) {
2020
Facts: "facts {{.value}}",
2121
Sections: "sections {{.value}}",
2222
PotentialAction: "actions {{.value}}",
23+
ThemeColor: "theme color {{.value}}",
2324
},
2425
}
2526

@@ -47,6 +48,7 @@ func TestGetTemplater_Teams(t *testing.T) {
4748
assert.Equal(t, notification.Teams.Sections, "sections value")
4849
assert.Equal(t, notification.Teams.Facts, "facts value")
4950
assert.Equal(t, notification.Teams.PotentialAction, "actions value")
51+
assert.Equal(t, notification.Teams.ThemeColor, "theme color value")
5052
}
5153

5254
func TestTeams_DefaultMessage(t *testing.T) {
@@ -150,6 +152,7 @@ func TestTeams_MessageFields(t *testing.T) {
150152
Sections: "[{\"sections\": true}]",
151153
PotentialAction: "[{\"actions\": true}]",
152154
Title: "Title",
155+
ThemeColor: "#000080",
153156
},
154157
}
155158

@@ -164,6 +167,7 @@ func TestTeams_MessageFields(t *testing.T) {
164167

165168
assert.Contains(t, receivedBody.Text, notification.Teams.Text)
166169
assert.Contains(t, receivedBody.Title, notification.Teams.Title)
170+
assert.Contains(t, receivedBody.ThemeColor, notification.Teams.ThemeColor)
167171
assert.Contains(t, receivedBody.PotentialAction, teamsAction{"actions": true})
168172
assert.Contains(t, receivedBody.Sections, teamsSection{"sections": true})
169173
assert.EqualValues(t, receivedBody.Sections[len(receivedBody.Sections)-1]["facts"],

0 commit comments

Comments
 (0)