Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discordへの通知 #2215

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gem 'coffee-rails', '~> 5.0.0'
gem 'commonmarker'
gem 'data_migrate'
gem 'diffy'
gem 'discord-notifier'
gem 'google-cloud-storage', '~> 1.25', require: false
gem 'holiday_jp'
gem 'jp_prefecture'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ GEM
diffy (3.4.0)
digest-crc (0.6.1)
rake (~> 13.0)
discord-notifier (1.0.3)
equatable (0.6.1)
erubi (1.9.0)
execjs (2.7.0)
Expand Down Expand Up @@ -480,6 +481,7 @@ DEPENDENCIES
commonmarker
data_migrate
diffy
discord-notifier
google-cloud-storage (~> 1.25)
holiday_jp
image_processing (~> 1.2)
Expand Down
33 changes: 33 additions & 0 deletions app/models/chat_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

class ChatNotifier
def self.notify(
title:,
title_url:,
description:,
user:,
webhook_url:
)
user_url = Rails.application.routes.url_helpers.user_url(
user,
host: 'bootcamp.fjord.jp',
protocol: 'https'
)

author = { name: user.login_name, url: user_url, icon_url: user.avatar_url }

embed = Discord::Embed.new do
title title
url title_url
description description
author author
color '4638a0'
end

if Rails.env.production?
Discord::Notifier.message(embed, url: webhook_url)
else
Rails.logger.info 'Notify to Discord.'
end
end
end
32 changes: 18 additions & 14 deletions app/models/page_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def after_create(page)
return if page.wip?

send_notification(page)
notify_to_slack(page)
notify_to_chat(page)

page.published_at = Time.current
page.save
end
Expand All @@ -14,7 +15,8 @@ def after_update(page)
return unless page.wip == false && page.published_at.nil?

send_notification(page)
notify_to_slack(page)
notify_to_chat(page)

page.published_at = Time.current
page.save
end
Expand All @@ -28,17 +30,19 @@ def send_notification(page)
end
end

def notify_to_slack(page)
path = Rails.application.routes.url_helpers.polymorphic_path(page)
url = "https://bootcamp.fjord.jp#{path}"
link = "<#{url}|#{page.title}>"
SlackNotification.notify link.to_s,
username: "#{page.user.login_name} (#{page.user.name})",
icon_url: page.user.avatar_url,
channel: '#bootcamp_notification',
attachments: [{
fallback: 'page body.',
text: page.body
}]
def notify_to_chat(page)
page_url = Rails.application.routes.url_helpers.polymorphic_url(
page,
host: 'bootcamp.fjord.jp',
protocol: 'https'
)

ChatNotifier.notify(
title: page.title,
title_url: page_url,
description: page.body,
user: page.user,
webhook_url: ENV['DISCORD_NOTICE_WEBHOOK_URL']
)
end
end
1 change: 1 addition & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ steps:
- '--set-env-vars=GCS_BUCKET=$_GCS_BUCKET'
- '--set-env-vars=REDIS_URL=$_REDIS_URL'
- '--set-env-vars=TOKEN=$_TOKEN'
- '--set-env-vars=DISCORD_NOTICE_WEBHOOK_URL=$_DISCORD_NOTICE_WEBHOOK_URL'
- '--set-env-vars=$_ENVS'
- >-
--labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/discord_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

Discord::Notifier.setup do |config|
config.url = ENV['DISCORD_NOTICE_WEBHOOK_URL']
config.username = 'ピヨルド'
config.avatar_url = 'https://i.gyazo.com/7099977680d8d8c2d72a3f14ddf14cc6.png'
end