forked from brotandgames/ciao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webhooks notifications based on ENV vars (brotandgames#25)
* Add basic structure for Webhooks notifications * Webhooks final implementation * Add tests for webhooks notifications * Fix Rubocop * Fix typo '#notify' instead of '#notiy' Co-Authored-By: Brot & Games <[email protected]> * Add example configuration for RocketChat. * Create mail notification + introduce url and check_url * Fix a typo at webhook_configuration.md * Fix log entry when status changed.
- Loading branch information
1 parent
2df58d0
commit c6c69bd
Showing
19 changed files
with
295 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
inherit_from: .rubocop_todo.yml | ||
|
||
Metrics/AbcSize: | ||
Max: 40 | ||
Enabled: false | ||
|
||
Metrics/BlockLength: | ||
Max: 300 | ||
Enabled: false | ||
|
||
Metrics/CyclomaticComplexity: | ||
Max: 10 | ||
Enabled: false | ||
|
||
Metrics/MethodLength: | ||
Max: 35 | ||
Enabled: false | ||
|
||
Metrics/PerceivedComplexity: | ||
Max: 10 | ||
Enabled: false | ||
|
||
Metrics/LineLength: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Notifications | ||
class Base | ||
def initialize(endpoint = nil, | ||
payload_template = nil, | ||
payload_renderer_cls = Ciao::Renderers::ReplaceRenderer) | ||
@endpoint = endpoint | ||
@payload_renderer = payload_renderer_cls.new(payload_template) | ||
end | ||
|
||
def notify(_payload_data = {}) | ||
raise NotImplementedError, | ||
'You can not call Ciao::Notifications::Base#notify directly' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Notifications | ||
class MailNotification < Base | ||
def notify(payload_data = {}) | ||
CheckMailer.with(payload_data).change_status_mail.deliver | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Notifications | ||
class WebhookNotification < Base | ||
def notify(payload_data = {}) | ||
uri = URI.parse(@endpoint) | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = uri.scheme == 'https' | ||
|
||
request = Net::HTTP::Post.new( | ||
uri.request_uri, | ||
'Content-Type' => 'application/json' | ||
) | ||
request.body = @payload_renderer.render(payload_data) | ||
http.request(request) | ||
rescue *NET_HTTP_ERRORS => e | ||
Rails.logger.error "Ciao::Notifications::WebhookNotification#notify Could not notify webhook(#{@endpoint}) - #{e}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Parsers | ||
class WebhookParser | ||
WEBHOOKS_ENDPOINT_PREFIX = 'CIAO_WEBHOOK_ENDPOINT_' | ||
WEBHOOKS_PAYLOAD_PREFIX = 'CIAO_WEBHOOK_PAYLOAD_' | ||
|
||
WEBHOOKS_ENDPOINT_FORMAT = "#{WEBHOOKS_ENDPOINT_PREFIX}%s" | ||
WEBHOOKS_PAYLOAD_FORMAT = "#{WEBHOOKS_PAYLOAD_PREFIX}%s" | ||
|
||
WEBHOOKS_FORMAT_REGEX = /^#{WEBHOOKS_ENDPOINT_PREFIX}(?<name>[A-Z0-9_]+)$/.freeze | ||
|
||
def self.webhooks | ||
names.map do |check_name| | ||
{ | ||
endpoint: ENV.fetch(WEBHOOKS_ENDPOINT_FORMAT % check_name, ''), | ||
payload: ENV.fetch(WEBHOOKS_PAYLOAD_FORMAT % check_name, '') | ||
} | ||
end | ||
end | ||
|
||
def self.names | ||
matches.map { |match| match[:name] } | ||
end | ||
|
||
def self.matches | ||
ENV.map do |k, _v| | ||
k.match(WEBHOOKS_FORMAT_REGEX) | ||
end.compact | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Renderers | ||
class Base | ||
def initialize(template) | ||
@template = template | ||
end | ||
|
||
def render(_data) | ||
raise NotImplementedError, | ||
'You can not call Ciao::Renderers::Base#render directly' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ciao | ||
module Renderers | ||
class ReplaceRenderer < Base | ||
CHECK_NAME_PLACEHOLDER = '__name__' | ||
STATUS_AFTER_PLACEHOLDER = '__status_after__' | ||
STATUS_BEFORE_PLACEHOLDER = '__status_before__' | ||
URL_PLACEHOLDER = '__url__' | ||
CHECK_URL_PLACEHOLDER = '__check_url__' | ||
|
||
def render(data) | ||
return '' if @template.nil? | ||
|
||
@template | ||
.gsub(CHECK_NAME_PLACEHOLDER, data.fetch(:name, '').to_s) | ||
.gsub(STATUS_AFTER_PLACEHOLDER, data.fetch(:status_after, '').to_s) | ||
.gsub(STATUS_BEFORE_PLACEHOLDER, data.fetch(:status_before, '').to_s) | ||
.gsub(URL_PLACEHOLDER, data.fetch(:url, '').to_s) | ||
.gsub(CHECK_URL_PLACEHOLDER, data.fetch(:check_url, '').to_s) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# Some time in the future Rails is not going to auto_load these for us :( | ||
# we have to explictly require it here | ||
Dir[Rails.root.join('app', 'lib', 'ciao', '**', '*.rb')].each { |f| require f } | ||
|
||
# export CIAO_WEBHOOK_ENDPOINT_$NAME=https://chat.yourhost.net/***** | ||
# export CIAO_WEBHOOK_PAYLOAD_$NAME=#'{"username":"Brot & Games","icon_url":"https://avatars0.githubusercontent.com/u/43862266?s=400&v=4","text":"Example message","attachments":[{"title":"Rocket.Chat","title_link":"https://rocket.chat","text":"Rocket.Chat, the best open source chat","image_url":"/images/integration-attachment-example.png","color":"#764FA5"}]}' | ||
# `$NAME` can be any word `[A-Z0-9_]+` and must be unique as it is used as an identifier | ||
|
||
NOTIFICATIONS = Ciao::Parsers::WebhookParser.webhooks.map do |webhook| | ||
Ciao::Notifications::WebhookNotification.new( | ||
webhook[:endpoint], | ||
webhook[:payload], | ||
Ciao::Renderers::ReplaceRenderer | ||
) | ||
end | ||
|
||
NOTIFICATIONS << Ciao::Notifications::MailNotification.new if ENV['SMTP_ADDRESS'].present? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.