Skip to content

Commit

Permalink
Option to render fields (#480)
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max and crazy-max committed Sep 4, 2021
1 parent fe84829 commit fc64b13
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 164 deletions.
Binary file modified .res/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/notif/discord-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/notif/slack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions docs/notif/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Allow to send notifications to your Discord channel.
- "<@124>"
- "<@125>"
- "<@&200>"
renderFields: true
timeout: 10s
templateTitle: "{{ .Entry.Image }} released"
templateBody: |
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
```
Expand All @@ -25,23 +25,17 @@ Allow to send notifications to your Discord channel.
|---------------------|---------------------------------------|---------------|
| `webhookURL`[^1] | | Discord [incoming webhook URL](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) |
| `mentions` | | List of users or roles to notify |
| `renderFields` | `true` | Render [field objects](https://discordjs.guide/popular-topics/embeds.html) |
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
| `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title |
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |

!!! abstract "Environment variables"
* `DIUN_NOTIF_DISCORD_WEBHOOKURL`
* `DIUN_NOTIF_DISCORD_MENTIONS` (comma separated)
* `DIUN_NOTIF_DISCORD_RENDERFIELDS`
* `DIUN_NOTIF_DISCORD_TIMEOUT`
* `DIUN_NOTIF_DISCORD_TEMPLATETITLE`
* `DIUN_NOTIF_DISCORD_TEMPLATEBODY`

### Default `templateTitle`

```
[[ config.extra.template.defaultTitle ]]
```

### Default `templateBody`

```
Expand Down
3 changes: 3 additions & 0 deletions docs/notif/rocketchat.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Allow to send notifications to your Rocket.Chat channel.
channel: "#general"
userID: abcdEFGH012345678
token: Token123456
renderAttachment: true
timeout: 10s
templateTitle: "{{ .Entry.Image }} released"
templateBody: |
Expand All @@ -25,6 +26,7 @@ Allow to send notifications to your Rocket.Chat channel.
| `userID`[^1] | | User ID |
| `token` | | Authentication token |
| `tokenFile` | | Use content of secret file as authentication token if `token` not defined |
| `renderAttachment` | `true` | Render [attachment object](https://docs.rocket.chat/guides/user-guides/messaging#send-attachments) |
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
| `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title |
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
Expand All @@ -38,6 +40,7 @@ Allow to send notifications to your Rocket.Chat channel.
* `DIUN_NOTIF_ROCKETCHAT_USERID`
* `DIUN_NOTIF_ROCKETCHAT_TOKEN`
* `DIUN_NOTIF_ROCKETCHAT_TOKENFILE`
* `DIUN_NOTIF_ROCKETCHAT_RENDERATTACHMENT`
* `DIUN_NOTIF_ROCKETCHAT_TIMEOUT`
* `DIUN_NOTIF_ROCKETCHAT_TEMPLATETITLE`
* `DIUN_NOTIF_ROCKETCHAT_TEMPLATEBODY`
Expand Down
5 changes: 4 additions & 1 deletion docs/notif/slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ You can send notifications to your Slack channel using an [incoming webhook URL]
notif:
slack:
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFields: true
templateBody: |
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
```

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

!!! abstract "Environment variables"
* `DIUN_NOTIF_SLACK_WEBHOOKURL`
* `DIUN_NOTIF_SLACK_RENDERFIELDS`
* `DIUN_NOTIF_SLACK_TEMPLATEBODY`

### Default `templateBody`

```
<!channel> Docker tag `{{ .Entry.Image }}` {{ if (eq .Entry.Status "new") }}newly added{{ else }}updated{{ end }}.
<!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 }}.
```

## Sample
Expand Down
3 changes: 3 additions & 0 deletions docs/notif/teams.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ You can send notifications to your Teams team-channel using an [incoming webhook
notif:
teams:
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFacts: true
templateBody: |
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
```

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

!!! abstract "Environment variables"
* `DIUN_NOTIF_TEAMS_WEBHOOKURL`
* `DIUN_NOTIF_TEAMS_RENDERFACTS`
* `DIUN_NOTIF_TEAMS_TEMPLATEBODY`

### Default `templateBody`
Expand Down
23 changes: 13 additions & 10 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestLoadFile(t *testing.T) {
"<@125>",
"<@&200>",
},
Timeout: utl.NewDuration(10 * time.Second),
TemplateTitle: model.NotifDefaultTemplateTitle,
TemplateBody: model.NotifDefaultTemplateBody,
RenderFields: utl.NewTrue(),
Timeout: utl.NewDuration(10 * time.Second),
TemplateBody: model.NotifDefaultTemplateBody,
},
Gotify: &model.NotifGotify{
Endpoint: "http://gotify.foo.com",
Expand Down Expand Up @@ -133,13 +133,14 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
TemplateBody: model.NotifDefaultTemplateBody,
},
RocketChat: &model.NotifRocketChat{
Endpoint: "http://rocket.foo.com:3000",
Channel: "#general",
UserID: "abcdEFGH012345678",
Token: "Token123456",
Timeout: utl.NewDuration(10 * time.Second),
TemplateTitle: model.NotifDefaultTemplateTitle,
TemplateBody: model.NotifRocketChatDefaultTemplateBody,
Endpoint: "http://rocket.foo.com:3000",
Channel: "#general",
UserID: "abcdEFGH012345678",
Token: "Token123456",
RenderAttachment: utl.NewTrue(),
Timeout: utl.NewDuration(10 * time.Second),
TemplateTitle: model.NotifDefaultTemplateTitle,
TemplateBody: model.NotifRocketChatDefaultTemplateBody,
},
Script: &model.NotifScript{
Cmd: "uname",
Expand All @@ -149,10 +150,12 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
},
Slack: &model.NotifSlack{
WebhookURL: "https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij",
RenderFields: utl.NewFalse(),
TemplateBody: model.NotifSlackDefaultTemplateBody,
},
Teams: &model.NotifTeams{
WebhookURL: "https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij",
RenderFacts: utl.NewFalse(),
TemplateBody: model.NotifTeamsDefaultTemplateBody,
},
Telegram: &model.NotifTelegram{
Expand Down
4 changes: 4 additions & 0 deletions internal/config/fixtures/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ notif:
- "<@124>"
- "<@125>"
- "<@&200>"
renderFields: true
timeout: 10s
gotify:
endpoint: http://gotify.foo.com
Expand Down Expand Up @@ -69,15 +70,18 @@ notif:
channel: "#general"
userID: abcdEFGH012345678
token: Token123456
renderAttachment: true
timeout: 10s
script:
cmd: "uname"
args:
- "-a"
slack:
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFields: false
teams:
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFacts: false
telegram:
token: abcdef123456
chatIDs:
Expand Down
4 changes: 4 additions & 0 deletions internal/config/fixtures/config.validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ notif:
- "<@124>"
- "<@125>"
- "<@&200>"
renderFields: true
timeout: 10s
gotify:
endpoint: http://gotify.foo.com
Expand Down Expand Up @@ -67,15 +68,18 @@ notif:
channel: "#general"
userID: abcdEFGH012345678
token: Token123456
renderAttachment: false
timeout: 10s
script:
cmd: "uname"
args:
- "-a"
slack:
webhookURL: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFields: false
teams:
webhookURL: https://outlook.office.com/webhook/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
renderFacts: true
telegram:
token: abcdef123456
chatIDs:
Expand Down
12 changes: 6 additions & 6 deletions internal/model/notif_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// NotifDiscord holds Discord notification configuration details
type NotifDiscord struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
Mentions []string `yaml:"mentions,omitempty" json:"mentions,omitempty"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

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

// SetDefaults sets the default values
func (s *NotifDiscord) SetDefaults() {
s.RenderFields = utl.NewTrue()
s.Timeout = utl.NewDuration(10 * time.Second)
s.TemplateTitle = NotifDefaultTemplateTitle
s.TemplateBody = NotifDefaultTemplateBody
}
18 changes: 10 additions & 8 deletions internal/model/notif_rocketchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ const NotifRocketChatDefaultTemplateBody = `Docker tag {{ .Entry.Image }} which

// NotifRocketChat holds Rocket.Chat notification configuration details
type NotifRocketChat struct {
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
Channel string `yaml:"channel,omitempty" json:"channel,omitempty" validate:"required"`
UserID string `yaml:"userID,omitempty" json:"userID,omitempty" validate:"required"`
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
Channel string `yaml:"channel,omitempty" json:"channel,omitempty" validate:"required"`
UserID string `yaml:"userID,omitempty" json:"userID,omitempty" validate:"required"`
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
RenderAttachment *bool `yaml:"renderAttachment,omitempty" json:"renderAttachment,omitempty" validate:"required"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

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

// SetDefaults sets the default values
func (s *NotifRocketChat) SetDefaults() {
s.RenderAttachment = utl.NewTrue()
s.Timeout = utl.NewDuration(10 * time.Second)
s.TemplateTitle = NotifDefaultTemplateTitle
s.TemplateBody = NotifRocketChatDefaultTemplateBody
Expand Down
6 changes: 5 additions & 1 deletion internal/model/notif_slack.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package model

import "github.com/crazy-max/diun/v4/pkg/utl"

// NotifSlackDefaultTemplateBody ...
const NotifSlackDefaultTemplateBody = "<!channel> Docker tag `{{ .Entry.Image }}` {{ if (eq .Entry.Status \"new\") }}available{{ else }}updated{{ end }}."
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 }}."

// NotifSlack holds slack notification configuration details
type NotifSlack struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
RenderFields *bool `yaml:"renderFields,omitempty" json:"renderFields,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

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

// SetDefaults sets the default values
func (s *NotifSlack) SetDefaults() {
s.RenderFields = utl.NewTrue()
s.TemplateBody = NotifSlackDefaultTemplateBody
}
4 changes: 4 additions & 0 deletions internal/model/notif_teams.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package model

import "github.com/crazy-max/diun/v4/pkg/utl"

// NotifTeamsDefaultTemplateBody ...
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 }}."

// NotifTeams holds Teams notification configuration details
type NotifTeams struct {
WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"`
RenderFacts *bool `yaml:"renderFacts,omitempty" json:"renderFacts,omitempty" validate:"required"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

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

// SetDefaults sets the default values
func (s *NotifTeams) SetDefaults() {
s.RenderFacts = utl.NewTrue()
s.TemplateBody = NotifTeamsDefaultTemplateBody
}
Loading

0 comments on commit fc64b13

Please sign in to comment.