Skip to content

Commit fc64b13

Browse files
authored
Option to render fields (#480)
Co-authored-by: CrazyMax <[email protected]>
1 parent fe84829 commit fc64b13

19 files changed

+202
-164
lines changed

.res/screenshot.png

475 Bytes
Loading

docs/assets/notif/discord-2.png

-1.05 KB
Loading

docs/assets/notif/slack.png

126 Bytes
Loading

docs/notif/discord.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Allow to send notifications to your Discord channel.
1515
- "<@124>"
1616
- "<@125>"
1717
- "<@&200>"
18+
renderFields: true
1819
timeout: 10s
19-
templateTitle: "{{ .Entry.Image }} released"
2020
templateBody: |
2121
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
2222
```
@@ -25,23 +25,17 @@ Allow to send notifications to your Discord channel.
2525
|---------------------|---------------------------------------|---------------|
2626
| `webhookURL`[^1] | | Discord [incoming webhook URL](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) |
2727
| `mentions` | | List of users or roles to notify |
28+
| `renderFields` | `true` | Render [field objects](https://discordjs.guide/popular-topics/embeds.html) |
2829
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
29-
| `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title |
3030
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
3131

3232
!!! abstract "Environment variables"
3333
* `DIUN_NOTIF_DISCORD_WEBHOOKURL`
3434
* `DIUN_NOTIF_DISCORD_MENTIONS` (comma separated)
35+
* `DIUN_NOTIF_DISCORD_RENDERFIELDS`
3536
* `DIUN_NOTIF_DISCORD_TIMEOUT`
36-
* `DIUN_NOTIF_DISCORD_TEMPLATETITLE`
3737
* `DIUN_NOTIF_DISCORD_TEMPLATEBODY`
3838

39-
### Default `templateTitle`
40-
41-
```
42-
[[ config.extra.template.defaultTitle ]]
43-
```
44-
4539
### Default `templateBody`
4640

4741
```

docs/notif/rocketchat.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Allow to send notifications to your Rocket.Chat channel.
1212
channel: "#general"
1313
userID: abcdEFGH012345678
1414
token: Token123456
15+
renderAttachment: true
1516
timeout: 10s
1617
templateTitle: "{{ .Entry.Image }} released"
1718
templateBody: |
@@ -25,6 +26,7 @@ Allow to send notifications to your Rocket.Chat channel.
2526
| `userID`[^1] | | User ID |
2627
| `token` | | Authentication token |
2728
| `tokenFile` | | Use content of secret file as authentication token if `token` not defined |
29+
| `renderAttachment` | `true` | Render [attachment object](https://docs.rocket.chat/guides/user-guides/messaging#send-attachments) |
2830
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
2931
| `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title |
3032
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
@@ -38,6 +40,7 @@ Allow to send notifications to your Rocket.Chat channel.
3840
* `DIUN_NOTIF_ROCKETCHAT_USERID`
3941
* `DIUN_NOTIF_ROCKETCHAT_TOKEN`
4042
* `DIUN_NOTIF_ROCKETCHAT_TOKENFILE`
43+
* `DIUN_NOTIF_ROCKETCHAT_RENDERATTACHMENT`
4144
* `DIUN_NOTIF_ROCKETCHAT_TIMEOUT`
4245
* `DIUN_NOTIF_ROCKETCHAT_TEMPLATETITLE`
4346
* `DIUN_NOTIF_ROCKETCHAT_TEMPLATEBODY`

docs/notif/slack.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ You can send notifications to your Slack channel using an [incoming webhook URL]
1212
notif:
1313
slack:
1414
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
15+
renderFields: true
1516
templateBody: |
1617
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
1718
```
1819

1920
| Name | Default | Description |
2021
|--------------------|--------------------------------------------|---------------|
2122
| `webhookURL`[^1] | | Slack [incoming webhook URL](https://api.slack.com/messaging/webhooks) |
23+
| `renderFields` | `true` | Render [field objects](https://api.slack.com/messaging/composing/layouts#stack_of_blocks) |
2224
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
2325

2426
!!! abstract "Environment variables"
2527
* `DIUN_NOTIF_SLACK_WEBHOOKURL`
28+
* `DIUN_NOTIF_SLACK_RENDERFIELDS`
2629
* `DIUN_NOTIF_SLACK_TEMPLATEBODY`
2730

2831
### Default `templateBody`
2932

3033
```
31-
<!channel> Docker tag `{{ .Entry.Image }}` {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}.
34+
<!channel> Docker tag {{ if .Entry.Image.HubLink }}<{{ .Entry.Image.HubLink }}|`{{ .Entry.Image }}`>{{ else }}`{{ .Entry.Image }}`{{ end }} {{ if (eq .Entry.Status "new") }}available{{ else }}updated{{ end }}.
3235
```
3336

3437
## Sample

docs/notif/teams.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ You can send notifications to your Teams team-channel using an [incoming webhook
99
notif:
1010
teams:
1111
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
12+
renderFacts: true
1213
templateBody: |
1314
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
1415
```
1516

1617
| Name | Default | Description |
1718
|--------------------|--------------------------------------------|---------------|
1819
| `webhookURL`[^1] | | Teams [incoming webhook URL](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/what-are-webhooks-and-connectors) |
20+
| `renderFacts` | `true` | Render fact objects |
1921
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
2022

2123
!!! abstract "Environment variables"
2224
* `DIUN_NOTIF_TEAMS_WEBHOOKURL`
25+
* `DIUN_NOTIF_TEAMS_RENDERFACTS`
2326
* `DIUN_NOTIF_TEAMS_TEMPLATEBODY`
2427

2528
### Default `templateBody`

internal/config/config_test.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ func TestLoadFile(t *testing.T) {
7676
"<@125>",
7777
"<@&200>",
7878
},
79-
Timeout: utl.NewDuration(10 * time.Second),
80-
TemplateTitle: model.NotifDefaultTemplateTitle,
81-
TemplateBody: model.NotifDefaultTemplateBody,
79+
RenderFields: utl.NewTrue(),
80+
Timeout: utl.NewDuration(10 * time.Second),
81+
TemplateBody: model.NotifDefaultTemplateBody,
8282
},
8383
Gotify: &model.NotifGotify{
8484
Endpoint: "http://gotify.foo.com",
@@ -133,13 +133,14 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
133133
TemplateBody: model.NotifDefaultTemplateBody,
134134
},
135135
RocketChat: &model.NotifRocketChat{
136-
Endpoint: "http://rocket.foo.com:3000",
137-
Channel: "#general",
138-
UserID: "abcdEFGH012345678",
139-
Token: "Token123456",
140-
Timeout: utl.NewDuration(10 * time.Second),
141-
TemplateTitle: model.NotifDefaultTemplateTitle,
142-
TemplateBody: model.NotifRocketChatDefaultTemplateBody,
136+
Endpoint: "http://rocket.foo.com:3000",
137+
Channel: "#general",
138+
UserID: "abcdEFGH012345678",
139+
Token: "Token123456",
140+
RenderAttachment: utl.NewTrue(),
141+
Timeout: utl.NewDuration(10 * time.Second),
142+
TemplateTitle: model.NotifDefaultTemplateTitle,
143+
TemplateBody: model.NotifRocketChatDefaultTemplateBody,
143144
},
144145
Script: &model.NotifScript{
145146
Cmd: "uname",
@@ -149,10 +150,12 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
149150
},
150151
Slack: &model.NotifSlack{
151152
WebhookURL: "https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij",
153+
RenderFields: utl.NewFalse(),
152154
TemplateBody: model.NotifSlackDefaultTemplateBody,
153155
},
154156
Teams: &model.NotifTeams{
155157
WebhookURL: "https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij",
158+
RenderFacts: utl.NewFalse(),
156159
TemplateBody: model.NotifTeamsDefaultTemplateBody,
157160
},
158161
Telegram: &model.NotifTelegram{

internal/config/fixtures/config.test.yml

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ notif:
2525
- "<@124>"
2626
- "<@125>"
2727
- "<@&200>"
28+
renderFields: true
2829
timeout: 10s
2930
gotify:
3031
endpoint: http://gotify.foo.com
@@ -69,15 +70,18 @@ notif:
6970
channel: "#general"
7071
userID: abcdEFGH012345678
7172
token: Token123456
73+
renderAttachment: true
7274
timeout: 10s
7375
script:
7476
cmd: "uname"
7577
args:
7678
- "-a"
7779
slack:
7880
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
81+
renderFields: false
7982
teams:
8083
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
84+
renderFacts: false
8185
telegram:
8286
token: abcdef123456
8387
chatIDs:

internal/config/fixtures/config.validate.yml

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ notif:
2525
- "<@124>"
2626
- "<@125>"
2727
- "<@&200>"
28+
renderFields: true
2829
timeout: 10s
2930
gotify:
3031
endpoint: http://gotify.foo.com
@@ -67,15 +68,18 @@ notif:
6768
channel: "#general"
6869
userID: abcdEFGH012345678
6970
token: Token123456
71+
renderAttachment: false
7072
timeout: 10s
7173
script:
7274
cmd: "uname"
7375
args:
7476
- "-a"
7577
slack:
7678
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
79+
renderFields: false
7780
teams:
7881
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
82+
renderFacts: true
7983
telegram:
8084
token: abcdef123456
8185
chatIDs:

internal/model/notif_discord.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
// NotifDiscord holds Discord notification configuration details
1010
type NotifDiscord struct {
11-
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
12-
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
13-
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
14-
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
15-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
11+
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
12+
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
13+
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
14+
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
15+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1616
}
1717

1818
// GetDefaults gets the default values
@@ -24,7 +24,7 @@ func (s *NotifDiscord) GetDefaults() *NotifDiscord {
2424

2525
// SetDefaults sets the default values
2626
func (s *NotifDiscord) SetDefaults() {
27+
s.RenderFields = utl.NewTrue()
2728
s.Timeout = utl.NewDuration(10 * time.Second)
28-
s.TemplateTitle = NotifDefaultTemplateTitle
2929
s.TemplateBody = NotifDefaultTemplateBody
3030
}

internal/model/notif_rocketchat.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ const NotifRocketChatDefaultTemplateBody = `Docker tag {{ .Entry.Image }} which
1111

1212
// NotifRocketChat holds Rocket.Chat notification configuration details
1313
type NotifRocketChat struct {
14-
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
15-
Channel string `yaml:"channel,omitempty" json:"channel,omitempty" validate:"required"`
16-
UserID string `yaml:"userID,omitempty" json:"userID,omitempty" validate:"required"`
17-
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
18-
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
19-
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
20-
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
21-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
14+
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
15+
Channel string `yaml:"channel,omitempty" json:"channel,omitempty" validate:"required"`
16+
UserID string `yaml:"userID,omitempty" json:"userID,omitempty" validate:"required"`
17+
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
18+
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
19+
RenderAttachment *bool `yaml:"renderAttachment,omitempty" json:"renderAttachment,omitempty" validate:"required"`
20+
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
21+
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
22+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
2223
}
2324

2425
// GetDefaults gets the default values
@@ -30,6 +31,7 @@ func (s *NotifRocketChat) GetDefaults() *NotifRocketChat {
3031

3132
// SetDefaults sets the default values
3233
func (s *NotifRocketChat) SetDefaults() {
34+
s.RenderAttachment = utl.NewTrue()
3335
s.Timeout = utl.NewDuration(10 * time.Second)
3436
s.TemplateTitle = NotifDefaultTemplateTitle
3537
s.TemplateBody = NotifRocketChatDefaultTemplateBody

internal/model/notif_slack.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package model
22

3+
import "github.com/crazy-max/diun/v4/pkg/utl"
4+
35
// NotifSlackDefaultTemplateBody ...
4-
const NotifSlackDefaultTemplateBody = "<!channel> Docker tag `{{ .Entry.Image }}` {{ if (eq .Entry.Status \"new\") }}available{{ else }}updated{{ end }}."
6+
const NotifSlackDefaultTemplateBody = "<!channel> Docker tag {{ if .Entry.Image.HubLink }}<{{ .Entry.Image.HubLink }}|`{{ .Entry.Image }}`>{{ else }}`{{ .Entry.Image }}`{{ end }} {{ if (eq .Entry.Status \"new\") }}available{{ else }}updated{{ end }}."
57

68
// NotifSlack holds slack notification configuration details
79
type NotifSlack struct {
810
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
11+
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
912
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1013
}
1114

@@ -18,5 +21,6 @@ func (s *NotifSlack) GetDefaults() *NotifSlack {
1821

1922
// SetDefaults sets the default values
2023
func (s *NotifSlack) SetDefaults() {
24+
s.RenderFields = utl.NewTrue()
2125
s.TemplateBody = NotifSlackDefaultTemplateBody
2226
}

internal/model/notif_teams.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package model
22

3+
import "github.com/crazy-max/diun/v4/pkg/utl"
4+
35
// NotifTeamsDefaultTemplateBody ...
46
const NotifTeamsDefaultTemplateBody = "Docker tag {{ if .Entry.Image.HubLink }}[`{{ .Entry.Image }}`]({{ .Entry.Image.HubLink }}){{ else }}`{{ .Entry.Image }}`{{ end }} {{ if (eq .Entry.Status \"new\") }}available{{ else }}updated{{ end }}."
57

68
// NotifTeams holds Teams notification configuration details
79
type NotifTeams struct {
810
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
11+
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
912
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
1013
}
1114

@@ -18,5 +21,6 @@ func (s *NotifTeams) GetDefaults() *NotifTeams {
1821

1922
// SetDefaults sets the default values
2023
func (s *NotifTeams) SetDefaults() {
24+
s.RenderFacts = utl.NewTrue()
2125
s.TemplateBody = NotifTeamsDefaultTemplateBody
2226
}

0 commit comments

Comments
 (0)