Skip to content

Commit

Permalink
modified deliveries/notifiers structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Jul 3, 2024
1 parent bf2d8a4 commit e51fcde
Show file tree
Hide file tree
Showing 45 changed files with 570 additions and 753 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Modified
- skip reseting invites email after accepting invite
- deliveries/notifiers structure

## [1.7.10] - 2024-06-16
### Added
Expand Down
34 changes: 34 additions & 0 deletions app/notifiers/admin_telegram_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

class AdminTelegramNotifier < 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 job_execution_report
notification(
chat_id: Rails.application.credentials[:reports_telegram_chat_id],
body: "#{params[:job_name]} is run at #{DateTime.now}"
)
end

def feedback_created
notification(
chat_id: Rails.application.credentials[:reports_telegram_chat_id],
body: feedback_created_payload(params[:id])
)
end

private

def feedback_created_payload(id)
feedback = Feedback.find_by(id: id)
return '' unless feedback

"User - #{feedback.user_id}\nFeedback created - #{feedback.title}\n#{feedback.description}"
end
end
4 changes: 3 additions & 1 deletion app/notifiers/company_notifier.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

class CompanyNotifier < AbstractNotifier::Base
include Deps[company_payload: 'notifiers.payloads.company']

private

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

notification(
**Payloads::Company.new.call(
**company_payload.call(
type: source,
path: path,
url: url,
Expand Down
2 changes: 1 addition & 1 deletion app/notifiers/company_slack_notifier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CompanyDiscordNotifier < CompanyNotifier
class CompanySlackNotifier < CompanyNotifier
self.driver = proc do |data|
Pullmetry::Container['api.slack.client'].send_message(
path: data[:path],
Expand Down
31 changes: 27 additions & 4 deletions app/notifiers/payloads/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

module Payloads
class Company
include Deps[
telegram_insights: 'reports.company.telegram.insights',
telegram_repository_insights: 'reports.company.telegram.repository_insights',
telegram_long_time_review: 'reports.company.telegram.long_time_review',
discord_insights: 'reports.company.discord.insights',
discord_repository_insights: 'reports.company.discord.repository_insights',
discord_long_time_review: 'reports.company.discord.long_time_review',
slack_insights: 'reports.company.slack.insights',
slack_repository_insights: 'reports.company.slack.repository_insights',
slack_long_time_review: 'reports.company.slack.long_time_review',
custom_insights: 'reports.company.custom.insights',
custom_repository_insights: 'reports.company.custom.repository_insights',
custom_long_time_review: 'reports.company.custom.long_time_review',
]

def call(type:, path:, url:, insightable:, notification_name:)
case type
when Notification::TELEGRAM then telegram_payload(path, insightable, notification_name)
Expand All @@ -22,7 +37,9 @@ def telegram_payload(path, insightable, notification_name)

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

Expand All @@ -38,7 +55,9 @@ def discord_payload(path, insightable, notification_name)

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

Expand All @@ -51,7 +70,9 @@ def slack_payload(path, insightable, notification_name)

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

Expand All @@ -66,7 +87,9 @@ def custom_payload(path, insightable, notification_name)

def custom_report(insightable, notification_name)
case notification_name
when :insights_report then Reports::Company::Custom::Insights.new.call(insightable: insightable)
when :insights_report then custom_insights.call(insightable: insightable)
when :repository_insights_report then custom_repository_insights.call(insightable: insightable)
when :long_time_review_report then custom_long_time_review.call(insightable: insightable)
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions app/notifiers/reports/company/custom/long_time_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Reports
module Company
module Custom
class LongTimeReview < Reports::Company::LongTimeReview
def call(insightable:)
grouped_pull_requests = grouped_pull_requests(insightable)
return no_error_label(insightable) if grouped_pull_requests.empty?

grouped_pull_requests.values.map do |pull_requests|
{
title: title(pull_requests.first[:repositories_title]),
metrics: pull_requests.map { |pull_request| metrics(pull_request) }
}
end
end

private

def metrics(pull_request)
{
pull_number: pull_request[:pull_number],
in_progress_seconds: DateTime.now.to_i - pull_request[:created_at].to_i
}
end
end
end
end
end
35 changes: 35 additions & 0 deletions app/notifiers/reports/company/custom/repository_insights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Reports
module Company
module Custom
class RepositoryInsights < Reports::Company::RepositoryInsights
def call(insightable:)
insights = insights(insightable)
return no_error_label(insightable) if insights.empty?

insights.map do |insight|
{
title: title(insight[:repositories_title]),
accessable: accessable_message(insight[:repositories_accessable]),
metrics: metrics(insight)
}
end
end

private

def metrics(insight)
{
average_comment_seconds: insight[:average_comment_time].to_i,
average_review_seconds: insight[:average_review_time].to_i,
average_merge_seconds: insight[:average_merge_time].to_i,
comments_count: insight[:comments_count].to_i,
changed_loc: insight[:changed_loc].to_i,
open_pull_requests_count: insight[:open_pull_requests_count].to_i
}
end
end
end
end
end
34 changes: 34 additions & 0 deletions app/notifiers/reports/company/discord/long_time_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Reports
module Company
module Discord
class LongTimeReview < Reports::Company::LongTimeReview
include Deps[time_representer: 'services.converters.seconds_to_text']

def call(insightable:)
grouped_pull_requests = grouped_pull_requests(insightable)
return no_error_label(insightable) if grouped_pull_requests.empty?

grouped_pull_requests.values.map { |pull_requests|
[
"**#{title(pull_requests.first[:repositories_title])}**",
pull_requests.map { |pull_request| metrics(pull_request) }.join("\n")
].join("\n")
}.join("\n\n")
end

private

def metrics(pull_request)
in_progress_time = time_representer.call(value: DateTime.now.to_i - pull_request[:created_at].to_i)

[
"**Pull number:** #{pull_request[:pull_number]}",
"**In review time:** #{in_progress_time}"
].join(', ')
end
end
end
end
end
43 changes: 43 additions & 0 deletions app/notifiers/reports/company/discord/repository_insights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Reports
module Company
module Discord
class RepositoryInsights < Reports::Company::RepositoryInsights
include Deps[time_representer: 'services.converters.seconds_to_text']

def call(insightable:)
insights = insights(insightable)
return no_error_label(insightable) if insights.empty?

insights.map { |insight|
[
"**#{title(insight[:repositories_title])}**",
"**#{accessable_message(insight[:repositories_accessable])}**",
metrics(insight)
].join("\n")
}.join("\n\n")
end

private

# rubocop: disable Metrics/AbcSize
def metrics(insight)
average_comment_time = time_representer.call(value: insight[:average_comment_time].to_i)
average_review_time = time_representer.call(value: insight[:average_review_time].to_i)
average_merge_time = time_representer.call(value: insight[:average_merge_time].to_i)

[
"**Average comment time:** #{average_comment_time}",
"**Average review time:** #{average_review_time}",
"**Average merge time:** #{average_merge_time}",
"**Total comments:** #{insight[:comments_count].to_i}",
"**Changed LOC:** #{insight[:changed_loc].to_i}",
"**Open pull requests:** #{insight[:open_pull_requests_count].to_i}"
].join(', ')
end
# rubocop: enable Metrics/AbcSize
end
end
end
end
28 changes: 28 additions & 0 deletions app/notifiers/reports/company/long_time_review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Reports
module Company
class LongTimeReview
private

def no_error_label(insightable)
"Company #{insightable.title} doesn't have long time review"
end

def title(title)
"Repository long time review of #{title}"
end

def grouped_pull_requests(insightable)
insightable
.pull_requests
.opened
.where(pull_requests: { created_at: ...(insightable.configuration.long_time_review_hours || 48).hours.ago })
.joins(:repository)
.order(created_at: :asc)
.hashable_pluck(:created_at, :pull_number, :repository_id, 'repositories.title')
.group_by { |element| element[:repository_id] }
end
end
end
end
35 changes: 35 additions & 0 deletions app/notifiers/reports/company/repository_insights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Reports
module Company
class RepositoryInsights
private

def no_error_label(insightable)
"Company #{insightable.title} doesn't have repository insights"
end

def title(title)
"Repository insights of #{title}"
end

def accessable_message(accessable)
return '' if accessable

'Repository has access error'
end

def insights(insightable)
::Repositories::Insight
.actual
.joins(:repository)
.where(repositories: { company_id: insightable.id })
.hashable_pluck(
:average_comment_time, :average_review_time, :average_merge_time,
:comments_count, :changed_loc, :open_pull_requests_count,
'repositories.title', 'repositories.accessable'
)
end
end
end
end
13 changes: 13 additions & 0 deletions app/notifiers/reports/company/slack/insights.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def accessable_block(insightable)
]
end

def insights_blocks(insightable)
visible_insights(insightable).map { |insight|
{
type: 'context',
elements: [
avatar_element(insight[:entities_avatar_url]),
login_element(insight[:entities_login]),
insight_data(insight)
]
}
}
end

def insight_data(insight)
{
type: 'context',
Expand Down
Loading

0 comments on commit e51fcde

Please sign in to comment.