Skip to content

Commit

Permalink
Add Rocket.Chat notifier (crazy-max#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max authored Mar 1, 2020
1 parent 8a49f41 commit 08cd3d9
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 82 deletions.
Binary file added .res/notif-rocketchat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
kingpin.Flag("config", "Diun configuration file.").Envar("CONFIG").Required().StringVar(&flags.Cfgfile)
kingpin.Flag("timezone", "Timezone assigned to Diun.").Envar("TZ").Default("UTC").StringVar(&flags.Timezone)
kingpin.Flag("log-level", "Set log level.").Envar("LOG_LEVEL").Default("info").StringVar(&flags.LogLevel)
kingpin.Flag("log-json", "Enable JSON logging output.").Envar("LOG_JSON").Default("false").BoolVar(&flags.LogJson)
kingpin.Flag("log-json", "Enable JSON logging output.").Envar("LOG_JSON").Default("false").BoolVar(&flags.LogJSON)
kingpin.Flag("log-caller", "Enable to add file:line of the caller.").Envar("LOG_CALLER").Default("false").BoolVar(&flags.LogCaller)
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version(version).Author("CrazyMax")
kingpin.CommandLine.Name = "diun"
Expand Down
41 changes: 28 additions & 13 deletions doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ watch:
first_check_notif: false

notif:
gotify:
enable: false
endpoint: http://gotify.foo.com
token: Token123456
priority: 1
timeout: 10
mail:
enable: false
host: localhost
Expand All @@ -32,6 +38,13 @@ notif:
password:
from:
to:
rocketchat:
enable: false
endpoint: http://rocket.foo.com:3000
channel: "#general"
user_id: abcdEFGH012345678
token: Token123456
timeout: 10
slack:
enable: false
webhook_url: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
Expand All @@ -49,12 +62,6 @@ notif:
Content-Type: application/json
Authorization: Token123456
timeout: 10
gotify:
enable: false
endpoint: http://gotify.foo.com
token: Token123456
priority: 1
timeout: 10

regopts:
someregistryoptions:
Expand Down Expand Up @@ -123,6 +130,13 @@ providers:

### notif

* `gotify`
* `enable`: Enable gotify notification (default: `false`).
* `endpoint`: Gotify base URL (e.g. `http://gotify.foo.com`). **required**
* `token`: Application token. **required**
* `priority`: The priority of the message.
* `timeout`: Timeout specifies a time limit for the request to be made. (default: `10`).

* `mail`
* `enable`: Enable email reports (default: `false`).
* `host`: SMTP server host (default: `localhost`). **required**
Expand All @@ -136,6 +150,14 @@ providers:
* `from`: Sender email address. **required**
* `to`: Recipient email address. **required**

* `rocketchat`
* `enable`: Enable Rocket.Chat notification (default: `false`).
* `endpoint`: Rocket.Chat base URL (e.g. `http://rocket.foo.com:3000`). **required**
* `channel`: Channel name with the prefix in front of it. **required**
* `user_id`: User ID. **required**
* `token`: Authentication token. **required**
* `timeout`: Timeout specifies a time limit for the request to be made. (default: `10`).

* `slack`
* `enable`: Enable slack notification (default: `false`).
* `webhook_url`: Slack [incoming webhook URL](https://api.slack.com/messaging/webhooks). **required**
Expand All @@ -152,13 +174,6 @@ providers:
* `headers`: Map of additional headers to be sent.
* `timeout`: Timeout specifies a time limit for the request to be made. (default: `10`).

* `gotify`
* `enable`: Enable gotify notification (default: `false`).
* `endpoint`: Gotify base URL (e.g. `http://gotify.foo.com`). **required**
* `token`: Application token. **required**
* `priority`: The priority of the message.
* `timeout`: Timeout specifies a time limit for the request to be made. (default: `10`).

### regopts

* `username`: Registry username.
Expand Down
9 changes: 8 additions & 1 deletion doc/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [Gotify](#gotify)
* [Mail](#mail)
* [Rocket.Chat](#rocketchat)
* [Slack](#slack)
* [Telegram](#telegram)
* [Webhook](#webhook)
Expand All @@ -18,9 +19,15 @@ Here is an email sample if you add `mail` notification:

![](../.res/notif-mail.png)

## Rocket.Chat

To be able to send notifications to your Rocket.Chat channel, you must first create a Personal Access Token through your account settings:

![](../.res/notif-rocketchat.png)

## Slack

You can send notifications to your slack channel using an [incoming webhook URL](https://api.slack.com/messaging/webhooks):
You can send notifications to your Slack channel using an [incoming webhook URL](https://api.slack.com/messaging/webhooks):

![](../.res/notif-slack.png)

Expand Down
12 changes: 8 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ func Load(flags model.Flags, version string) (*Config, error) {
FirstCheckNotif: false,
},
Notif: model.Notif{
Gotify: model.NotifGotify{
Enable: false,
Timeout: 10,
},
Mail: model.NotifMail{
Enable: false,
Host: "localhost",
Port: 25,
SSL: false,
InsecureSkipVerify: false,
},
RocketChat: model.NotifRocketChat{
Enable: false,
Timeout: 10,
},
Slack: model.NotifSlack{
Enable: false,
},
Expand All @@ -67,10 +75,6 @@ func Load(flags model.Flags, version string) (*Config, error) {
Method: "GET",
Timeout: 10,
},
Gotify: model.NotifGotify{
Enable: false,
Timeout: 10,
},
},
}

Expand Down
19 changes: 13 additions & 6 deletions internal/config/config.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ watch:
first_check_notif: false

notif:
gotify:
enable: false
endpoint: http://gotify.foo.com
token: Token123456
priority: 1
timeout: 10
mail:
enable: false
host: localhost
Expand All @@ -19,6 +25,13 @@ notif:
password_file:
from:
to:
rocketchat:
enable: false
endpoint: http://rocket.foo.com:3000
channel: "#general"
user_id: abcdEFGH012345678
token: Token123456
timeout: 10
slack:
enable: false
webhook_url: https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij
Expand All @@ -36,12 +49,6 @@ notif:
Content-Type: application/json
Authorization: Token123456
timeout: 10
gotify:
enable: false
endpoint: http://gotify.foo.com
token: Token123456
priority: 1
timeout: 10

regopts:
someregopts:
Expand Down
22 changes: 15 additions & 7 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,28 @@ func TestLoad(t *testing.T) {
Schedule: "*/30 * * * *",
},
Notif: model.Notif{
Gotify: model.NotifGotify{
Enable: false,
Endpoint: "http://gotify.foo.com",
Token: "Token123456",
Priority: 1,
Timeout: 10,
},
Mail: model.NotifMail{
Enable: false,
Host: "localhost",
Port: 25,
SSL: false,
InsecureSkipVerify: false,
},
RocketChat: model.NotifRocketChat{
Enable: false,
Endpoint: "http://rocket.foo.com:3000",
Channel: "#general",
UserID: "abcdEFGH012345678",
Token: "Token123456",
Timeout: 10,
},
Slack: model.NotifSlack{
Enable: false,
WebhookURL: "https://hooks.slack.com/services/ABCD12EFG/HIJK34LMN/01234567890abcdefghij",
Expand All @@ -78,13 +93,6 @@ func TestLoad(t *testing.T) {
},
Timeout: 10,
},
Gotify: model.NotifGotify{
Enable: false,
Endpoint: "http://gotify.foo.com",
Token: "Token123456",
Priority: 1,
Timeout: 10,
},
},
RegOpts: map[string]model.RegOpts{
"someregopts": {
Expand Down
2 changes: 1 addition & 1 deletion internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Configure(fl *model.Flags, location *time.Location) {
return time.Now().In(location)
}

if !fl.LogJson {
if !fl.LogJSON {
w = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC1123,
Expand Down
2 changes: 1 addition & 1 deletion internal/model/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ type Flags struct {
Cfgfile string
Timezone string
LogLevel string
LogJson bool
LogJSON bool
LogCaller bool
}
39 changes: 25 additions & 14 deletions internal/model/notif.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ type NotifEntry struct {

// Notif holds data necessary for notification configuration
type Notif struct {
Mail NotifMail `yaml:"mail,omitempty"`
Slack NotifSlack `yaml:"slack,omitempty"`
Telegram NotifTelegram `yaml:"telegram,omitempty"`
Webhook NotifWebhook `yaml:"webhook,omitempty"`
Gotify NotifGotify `yaml:"gotify,omitempty"`
Gotify NotifGotify `yaml:"gotify,omitempty"`
Mail NotifMail `yaml:"mail,omitempty"`
RocketChat NotifRocketChat `yaml:"rocketchat,omitempty"`
Slack NotifSlack `yaml:"slack,omitempty"`
Telegram NotifTelegram `yaml:"telegram,omitempty"`
Webhook NotifWebhook `yaml:"webhook,omitempty"`
}

// NotifGotify holds gotify notification configuration details
type NotifGotify struct {
Enable bool `yaml:"enable,omitempty"`
Endpoint string `yaml:"endpoint,omitempty"`
Token string `yaml:"token,omitempty"`
Priority int `yaml:"priority,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
}

// NotifMail holds mail notification configuration details
Expand All @@ -36,6 +46,16 @@ type NotifMail struct {
To string `yaml:"to,omitempty"`
}

// NotifRocketChat holds Rocket.Chat notification configuration details
type NotifRocketChat struct {
Enable bool `yaml:"enable,omitempty"`
Endpoint string `yaml:"endpoint,omitempty"`
Channel string `yaml:"channel,omitempty"`
UserID string `yaml:"user_id,omitempty"`
Token string `yaml:"token,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
}

// NotifSlack holds slack notification configuration details
type NotifSlack struct {
Enable bool `yaml:"enable,omitempty"`
Expand All @@ -57,12 +77,3 @@ type NotifWebhook struct {
Headers map[string]string `yaml:"headers,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
}

// NotifGotify holds gotify notification configuration details
type NotifGotify struct {
Enable bool `yaml:"enable,omitempty"`
Endpoint string `yaml:"endpoint,omitempty"`
Token string `yaml:"token,omitempty"`
Priority int `yaml:"priority,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
}
10 changes: 7 additions & 3 deletions internal/notif/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/crazy-max/diun/internal/notif/gotify"
"github.com/crazy-max/diun/internal/notif/mail"
"github.com/crazy-max/diun/internal/notif/notifier"
"github.com/crazy-max/diun/internal/notif/rocketchat"
"github.com/crazy-max/diun/internal/notif/slack"
"github.com/crazy-max/diun/internal/notif/telegram"
"github.com/crazy-max/diun/internal/notif/webhook"
Expand All @@ -27,9 +28,15 @@ func New(config model.Notif, app model.App, userAgent string) (*Client, error) {
}

// Add notifiers
if config.Gotify.Enable {
c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app, userAgent))
}
if config.Mail.Enable {
c.notifiers = append(c.notifiers, mail.New(config.Mail, app))
}
if config.RocketChat.Enable {
c.notifiers = append(c.notifiers, rocketchat.New(config.RocketChat, app, userAgent))
}
if config.Slack.Enable {
c.notifiers = append(c.notifiers, slack.New(config.Slack, app))
}
Expand All @@ -39,9 +46,6 @@ func New(config model.Notif, app model.App, userAgent string) (*Client, error) {
if config.Webhook.Enable {
c.notifiers = append(c.notifiers, webhook.New(config.Webhook, app, userAgent))
}
if config.Gotify.Enable {
c.notifiers = append(c.notifiers, gotify.New(config.Gotify, app, userAgent))
}

log.Debug().Msgf("%d notifier(s) created", len(c.notifiers))
return c, nil
Expand Down
Loading

0 comments on commit 08cd3d9

Please sign in to comment.