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

Commit

Permalink
feat. support teams theme color templating (#256)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jamesyang124 authored and alexmt committed Apr 16, 2021
1 parent 6200f01 commit 641810d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions catalog/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ data:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#000080'
title: New version of an application {{.app.metadata.name}} is up and running.
template.app-health-degraded: |
email:
Expand Down Expand Up @@ -161,6 +162,7 @@ data:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#FF0000'
title: Application {{.app.metadata.name}} has degraded.
template.app-sync-failed: |
email:
Expand Down Expand Up @@ -236,6 +238,7 @@ data:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#FF0000'
title: Failed to sync application {{.app.metadata.name}}.
template.app-sync-running: |
email:
Expand Down Expand Up @@ -462,6 +465,7 @@ data:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#000080'
title: Application {{.app.metadata.name}} has been successfully synced
trigger.on-created: |
- description: Application is created.
Expand Down
1 change: 1 addition & 0 deletions catalog/templates/app-deployed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ slack:
]
}]
teams:
themeColor: "#000080"
title: New version of an application {{.app.metadata.name}} is up and running.
facts: |
[{
Expand Down
1 change: 1 addition & 0 deletions catalog/templates/app-health-degraded.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ slack:
]
}]
teams:
themeColor: "#FF0000"
title: Application {{.app.metadata.name}} has degraded.
facts: |
[{
Expand Down
1 change: 1 addition & 0 deletions catalog/templates/app-sync-failed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ slack:
]
}]
teams:
themeColor: "#FF0000"
title: Failed to sync application {{.app.metadata.name}}.
facts: |
[{
Expand Down
1 change: 1 addition & 0 deletions catalog/templates/app-sync-succeeded.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ slack:
]
}]
teams:
themeColor: "#000080"
title: Application {{.app.metadata.name}} has been successfully synced
facts: |
[{
Expand Down
4 changes: 4 additions & 0 deletions docs/catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ teams:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#000080'
title: New version of an application {{.app.metadata.name}} is up and running.

```
Expand Down Expand Up @@ -187,6 +188,7 @@ teams:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#FF0000'
title: Application {{.app.metadata.name}} has degraded.

```
Expand Down Expand Up @@ -266,6 +268,7 @@ teams:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#FF0000'
title: Failed to sync application {{.app.metadata.name}}.

```
Expand Down Expand Up @@ -504,6 +507,7 @@ teams:
"uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
}]
}]
themeColor: '#000080'
title: Application {{.app.metadata.name}} has been successfully synced

```
17 changes: 15 additions & 2 deletions docs/services/teams.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ metadata:
name: argocd-notifications-cm
data:
service.teams: |
recipientUrls:
recipientUrls:
channelName: $channel-teams-url
```
Expand All @@ -48,11 +48,12 @@ metadata:
![](https://user-images.githubusercontent.com/18019529/114271500-9d2b8880-9a4c-11eb-85c1-f6935f0431d5.png)
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).
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).
```yaml
template.app-sync-succeeded: |
teams:
themeColor: "#000080"
sections: |
[{
"facts": [
Expand Down Expand Up @@ -96,3 +97,15 @@ template.app-sync-succeeded: |
"value": "{{.app.spec.source.repoURL}}"
}]
```

### theme color field

You can set theme color as hex string for the message.

![](https://user-images.githubusercontent.com/1164159/114864810-0718a900-9e24-11eb-8127-8d95da9544c1.png)

```yaml
template.app-sync-succeeded: |
teams:
themeColor: "#000080"
```
19 changes: 19 additions & 0 deletions pkg/services/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TeamsNotification struct {
Template string `json:"template,omitempty"`
Title string `json:"title,omitempty"`
Text string `json:"text,omitempty"`
ThemeColor string `json:"themeColor,omitempty"`
Facts string `json:"facts,omitempty"`
Sections string `json:"sections,omitempty"`
PotentialAction string `json:"potentialAction,omitempty"`
Expand All @@ -37,6 +38,11 @@ func (n *TeamsNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
return nil, fmt.Errorf("error in '%s' teams.text : %w", name, err)
}

themeColor, err := texttemplate.New(name).Funcs(f).Parse(n.ThemeColor)
if err != nil {
return nil, fmt.Errorf("error in '%s' teams.themeColor: %w", name, err)
}

facts, err := texttemplate.New(name).Funcs(f).Parse(n.Facts)
if err != nil {
return nil, fmt.Errorf("error in '%s' teams.facts : %w", name, err)
Expand Down Expand Up @@ -81,6 +87,14 @@ func (n *TeamsNotification) GetTemplater(name string, f texttemplate.FuncMap) (T
notification.Teams.Text = val
}

var themeColorBuff bytes.Buffer
if err := themeColor.Execute(&themeColorBuff, vars); err != nil {
return err
}
if val := themeColorBuff.String(); val != "" {
notification.Teams.ThemeColor = val
}

var factsData bytes.Buffer
if err := facts.Execute(&factsData, vars); err != nil {
return err
Expand Down Expand Up @@ -177,6 +191,10 @@ func teamsNotificationToMessage(n Notification) (*teamsMessage, error) {
message.Text = n.Teams.Text
}

if n.Teams.ThemeColor != "" {
message.ThemeColor = n.Teams.ThemeColor
}

if n.Teams.Sections != "" {
unmarshalledSections := make([]teamsSection, 2)
err := json.Unmarshal([]byte(n.Teams.Sections), &unmarshalledSections)
Expand Down Expand Up @@ -234,6 +252,7 @@ type teamsMessage struct {
Context string `json:"context"`
Title string `json:"title"`
Text string `json:"text"`
ThemeColor string `json:"themeColor,omitempty"`
PotentialAction []teamsAction `json:"potentialAction,omitempty"`
Sections []teamsSection `json:"sections,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/services/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestGetTemplater_Teams(t *testing.T) {
Facts: "facts {{.value}}",
Sections: "sections {{.value}}",
PotentialAction: "actions {{.value}}",
ThemeColor: "theme color {{.value}}",
},
}

Expand Down Expand Up @@ -47,6 +48,7 @@ func TestGetTemplater_Teams(t *testing.T) {
assert.Equal(t, notification.Teams.Sections, "sections value")
assert.Equal(t, notification.Teams.Facts, "facts value")
assert.Equal(t, notification.Teams.PotentialAction, "actions value")
assert.Equal(t, notification.Teams.ThemeColor, "theme color value")
}

func TestTeams_DefaultMessage(t *testing.T) {
Expand Down Expand Up @@ -150,6 +152,7 @@ func TestTeams_MessageFields(t *testing.T) {
Sections: "[{\"sections\": true}]",
PotentialAction: "[{\"actions\": true}]",
Title: "Title",
ThemeColor: "#000080",
},
}

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

assert.Contains(t, receivedBody.Text, notification.Teams.Text)
assert.Contains(t, receivedBody.Title, notification.Teams.Title)
assert.Contains(t, receivedBody.ThemeColor, notification.Teams.ThemeColor)
assert.Contains(t, receivedBody.PotentialAction, teamsAction{"actions": true})
assert.Contains(t, receivedBody.Sections, teamsSection{"sections": true})
assert.EqualValues(t, receivedBody.Sections[len(receivedBody.Sections)-1]["facts"],
Expand Down

0 comments on commit 641810d

Please sign in to comment.