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

Commit

Permalink
feat: support change timezone (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryota Sakamoto <sakamo.ryota+github@gmail.com>
  • Loading branch information
ryota-sakamoto authored Apr 1, 2021
1 parent 4673856 commit 31b8779
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ The `message` field of the template definition allows creating a basic notificat
fields to create complex notifications. For example using service-specific you can add blocks and attachments for Slack, subject for Email or URL path, and body for Webhook.
See corresponding service [documentation](./services/overview.md) for more information.

## Change the timezone

You can change the timezone to show it as follows.

1. Call time functions.

```
{{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02T15:04:05Z07:00" }}
```
2. Set environment to container.
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: argocd-notifications-controller
spec:
(snip)
spec:
containers:
- name: argocd-notifications-controller
env:
- name: TZ
value: Asia/Tokyo
```

## Functions

Templates have access to the set of built-in functions:
Expand Down
1 change: 1 addition & 0 deletions pkg/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"
texttemplate "text/template"
_ "time/tzdata"

"github.com/ghodss/yaml"
)
Expand Down
44 changes: 44 additions & 0 deletions pkg/services/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package services

import (
"os"
"testing"
"text/template"
"time"

"github.com/stretchr/testify/assert"

et "github.com/argoproj-labs/argocd-notifications/expr/time"
)

func TestValidIconEmoij(t *testing.T) {
Expand Down Expand Up @@ -47,3 +51,43 @@ func TestGetTemplater_Slack(t *testing.T) {
assert.Equal(t, "hello", notification.Slack.Attachments)
assert.Equal(t, "world", notification.Slack.Blocks)
}

func TestChangeTimezone_Slack(t *testing.T) {
tz := os.Getenv("TZ")
if err := os.Setenv("TZ", "Asia/Tokyo"); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.Setenv("TZ", tz); err != nil {
t.Fatal(err)
}
})

n := Notification{
Slack: &SlackNotification{
Attachments: `{{ (call .time.Parse .date).Local.Format "2006-01-02T15:04:05Z07:00" }}`,
},
}
templater, err := n.GetTemplater("", template.FuncMap{})

if !assert.NoError(t, err) {
return
}

currentTime, err := time.Parse(time.RFC3339, "2021-03-26T23:38:01Z")
if !assert.NoError(t, err) {
return
}

var notification Notification
err = templater(&notification, map[string]interface{}{
"date": currentTime.Format(time.RFC3339),
"time": et.NewExprs(),
})

if !assert.NoError(t, err) {
return
}

assert.Equal(t, "2021-03-27T08:38:01+09:00", notification.Slack.Attachments)
}

0 comments on commit 31b8779

Please sign in to comment.