Skip to content

Commit

Permalink
modify notifiers structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Jul 3, 2024
1 parent 6a2098d commit bf2d8a4
Show file tree
Hide file tree
Showing 41 changed files with 444 additions and 521 deletions.
10 changes: 4 additions & 6 deletions app/deliveries/admin_delivery.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# frozen_string_literal: true

class AdminDelivery < ApplicationDelivery
before_notify :ensure_mailer_enabled, on: :mailer
before_notify :ensure_webhook_enabled, on: :webhook
before_notify :ensure_slack_webhook_enabled, on: :slack_webhook
before_notify :ensure_discord_webhook_enabled, on: :discord_webhook
before_notify :ensure_slack_enabled, on: :slack
before_notify :ensure_discord_enabled, on: :discord
before_notify :ensure_telegram_enabled, on: :telegram

delivers :job_execution_report
delivers :feedback_created

private

def ensure_mailer_enabled = false
def ensure_webhook_enabled = false
def ensure_slack_webhook_enabled = false
def ensure_discord_webhook_enabled = false
def ensure_slack_enabled = false
def ensure_discord_enabled = false
def ensure_telegram_enabled = true
end
14 changes: 8 additions & 6 deletions app/deliveries/application_delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
class ApplicationDelivery < ActiveDelivery::Base
self.abstract_class = true

unregister_line :mailer

register_line :webhook, ActiveDelivery::Lines::Notifier,
resolver_pattern: 'Webhooks::%{delivery_name}Notifier'
resolver_pattern: '%{delivery_name}WebhookNotifier'

register_line :slack_webhook, ActiveDelivery::Lines::Notifier,
resolver_pattern: 'SlackWebhooks::%{delivery_name}Notifier'
register_line :slack, ActiveDelivery::Lines::Notifier,
resolver_pattern: '%{delivery_name}SlackNotifier'

register_line :discord_webhook, ActiveDelivery::Lines::Notifier,
resolver_pattern: 'DiscordWebhooks::%{delivery_name}Notifier'
register_line :discord, ActiveDelivery::Lines::Notifier,
resolver_pattern: '%{delivery_name}DiscordNotifier'

register_line :telegram, ActiveDelivery::Lines::Notifier,
resolver_pattern: 'Telegram::%{delivery_name}Notifier'
resolver_pattern: '%{delivery_name}TelegramNotifier'
end
11 changes: 4 additions & 7 deletions app/deliveries/company_delivery.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true

class CompanyDelivery < ApplicationDelivery
before_notify :ensure_mailer_enabled, on: :mailer
before_notify :ensure_webhook_enabled, on: :webhook
before_notify :ensure_slack_webhook_enabled, on: :slack_webhook
before_notify :ensure_discord_webhook_enabled, on: :discord_webhook
before_notify :ensure_slack_enabled, on: :slack
before_notify :ensure_discord_enabled, on: :discord
before_notify :ensure_telegram_enabled, on: :telegram

delivers :insights_report
Expand All @@ -13,23 +12,21 @@ class CompanyDelivery < ApplicationDelivery

private

def ensure_mailer_enabled = false

def ensure_webhook_enabled
return false if webhook_sources.exclude?(Webhook::CUSTOM)
return false if notification_sources.exclude?(Notification::CUSTOM)

true
end

def ensure_slack_webhook_enabled
def ensure_slack_enabled
return false if webhook_sources.exclude?(Webhook::SLACK)
return false if notification_sources.exclude?(Notification::SLACK)

true
end

def ensure_discord_webhook_enabled
def ensure_discord_enabled
return false if webhook_sources.exclude?(Webhook::DISCORD)
return false if notification_sources.exclude?(Notification::DISCORD)

Expand Down
14 changes: 14 additions & 0 deletions app/notifiers/company_discord_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CompanyDiscordNotifier < CompanyNotifier
self.driver = proc do |data|
Pullmetry::Container['api.discord.client'].send_message(
path: data[:path],
body: data[:body]
)
end

def insights_report = report(Webhook::DISCORD)
def repository_insights_report = report(Webhook::DISCORD)
def long_time_review_report = report(Webhook::DISCORD)
end
27 changes: 27 additions & 0 deletions app/notifiers/company_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class CompanyNotifier < AbstractNotifier::Base
private

def report(source)
path = webhook_path(source)
url = URI(path)

notification(
**Payloads::Company.new.call(
type: source,
path: path,
url: url,
insightable: insightable,
notification_name: notification_name
)
)
end

def insightable = params[:insightable]

def webhook_path(source)
webhook = insightable.all_webhooks.where(source: source).order(webhookable_type: :asc).first
URI(webhook.url).path
end
end
14 changes: 14 additions & 0 deletions app/notifiers/company_slack_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CompanyDiscordNotifier < CompanyNotifier
self.driver = proc do |data|
Pullmetry::Container['api.slack.client'].send_message(
path: data[:path],
body: data[:body]
)
end

def insights_report = report(Webhook::SLACK)
def repository_insights_report = report(Webhook::SLACK)
def long_time_review_report = report(Webhook::SLACK)
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# frozen_string_literal: true

class TelegramNotifier < AbstractNotifier::Base
class CompanyTelegramNotifier < CompanyNotifier
self.driver = proc do |data|
Pullmetry::Container['api.telegram.client'].send_message(
bot_secret: Rails.application.credentials.dig(:telegram_oauth, Rails.env.to_sym, :bot_secret),
chat_id: data[:chat_id],
text: data[:body]
)
end

def insights_report = report(Webhook::TELEGRAM)
def repository_insights_report = report(Webhook::TELEGRAM)
def long_time_review_report = report(Webhook::TELEGRAM)
end
16 changes: 16 additions & 0 deletions app/notifiers/company_webhook_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class CompanyWebhookNotifier < CompanyNotifier
self.driver = proc do |data|
url = URI(data[:path])

Webhooks::Client.new(url: url.origin).send_message(
path: url.path,
body: data[:body]
)
end

def insights_report = report(Webhook::CUSTOM)
def repository_insights_report = report(Webhook::CUSTOM)
def long_time_review_report = report(Webhook::CUSTOM)
end
11 changes: 0 additions & 11 deletions app/notifiers/concerns/companiable.rb

This file was deleted.

10 changes: 0 additions & 10 deletions app/notifiers/discord_webhook_notifier.rb

This file was deleted.

52 changes: 0 additions & 52 deletions app/notifiers/discord_webhooks/company/insights_report_payload.rb

This file was deleted.

47 changes: 0 additions & 47 deletions app/notifiers/discord_webhooks/company_notifier.rb

This file was deleted.

73 changes: 73 additions & 0 deletions app/notifiers/payloads/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

module Payloads
class Company
def call(type:, path:, url:, insightable:, notification_name:)
case type
when Notification::TELEGRAM then telegram_payload(path, insightable, notification_name)
when Notification::DISCORD then discord_payload(path, insightable, notification_name)
when Notification::SLACK then slack_payload(path, insightable, notification_name)
when Notification::CUSTOM then custom_payload(url.path, insightable, notification_name)
end
end

private

def telegram_payload(path, insightable, notification_name)
{
chat_id: path,
body: telegram_report(insightable, notification_name)
}
end

def telegram_report(insightable, notification_name)
case notification_name
when :insights_report then Reports::Company::Telegram::Insights.new.call(insightable: insightable)
end
end

def discord_payload(path, insightable, notification_name)
{
path: path,
body: {
username: 'PullKeeper',
content: discord_report(insightable, notification_name)
}
}
end

def discord_report(insightable, notification_name)
case notification_name
when :insights_report then Reports::Company::Discord::Insights.new.call(insightable: insightable)
end
end

def slack_payload(path, insightable, notification_name)
{
path: path,
body: slack_report(insightable, notification_name)
}
end

def slack_report(insightable, notification_name)
case notification_name
when :insights_report then Reports::Company::Slack::Insights.new.call(insightable: insightable)
end
end

def custom_payload(path, insightable, notification_name)
{
path: path,
body: {
content: custom_report(insightable, notification_name)
}
}
end

def custom_report(insightable, notification_name)
case notification_name
when :insights_report then Reports::Company::Custom::Insights.new.call(insightable: insightable)
end
end
end
end
Loading

0 comments on commit bf2d8a4

Please sign in to comment.