Skip to content

Commit

Permalink
WEB-3340: Adding native slack notifications if the required URL provided
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyd committed Jun 10, 2020
1 parent 1822f61 commit c969c04
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ AWS_REGION=

# CDN for image uploads
IMAGES_CDN_HOST=

# Build notifications as part of CI
SLACK_WEBHOOK_URL=
1 change: 1 addition & 0 deletions app/lib/renderer/image_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def width_class(alt_text)
width_match = /width=(\d+)%/.match(alt_text)
return if width_match.blank?

# Convert width request to a class that's a multiple of 10
width = width_match[1].to_i.round(-1).clamp(0, 100)
"l-image-#{width}"
end
Expand Down
11 changes: 7 additions & 4 deletions app/lib/runner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Runner
# Base class with shared functionality
class Base
include Util::Logging
include Util::SlackNotifiable

def self.runner
return Runner::Ci.new if CI
Expand All @@ -23,16 +24,18 @@ def render(publish_file:, local: false)
book
end

def publish(publish_file:)
def publish(publish_file:) # rubocop:disable Metrics/MethodLength
publish_file ||= default_publish_file

parser = Parser::Publish.new(file: publish_file)
book = parser.parse
image_provider = ImageProvider::Provider.new(book: book)
image_provider.process
renderer = Renderer::Book.new(book: book, image_provider: image_provider)
renderer.render
Renderer::Book.new(book: book, image_provider: image_provider).render
Api::Alexandria::BookUploader.upload(book)
notify_success(book: book)
rescue StandardError => e
notify_failure(book: defined?(book) ? book : nil, details: e.full_message)
raise e
end

def lint(publish_file:, options: {})
Expand Down
119 changes: 119 additions & 0 deletions app/lib/util/slack_notifiable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# frozen_string_literal: true

module Util
# Adds methods that allow slack notifications
module SlackNotifiable # rubocop:disable Metrics/ModuleLength
extend ActiveSupport::Concern

SUCCESS_IMAGE_URL = 'https://wolverine.raywenderlich.com/v3-resources/razebot/images/object_textkit-book.png'
FAILURE_IMAGE_URL = 'https://wolverine.raywenderlich.com/v3-resources/razebot/images/object_errors.png'
ROBLES_CONTEXT_IMAGE_URL = 'https://wolverine.raywenderlich.com/v3-resources/razebot/images/object_box-of-books.png'

def notify_success(book:)
return unless notifiable?

notifier.post(blocks: success_blocks(book: book))
end

def notify_failure(book:, details: nil)
return unless notifiable?

notifier.post(blocks: failure_blocks(book: book, details: details || 'N/A'))
end

def notifiable?
SLACK_WEBHOOK_URL.present?
end

def notifier
@notifier ||= Slack::Notifier.new(SLACK_WEBHOOK_URL, channel: SLACK_CHANNEL, username: SLACK_USERNAME)
end

def success_blocks(book:)
[
intro_section(book: book,
message: ':white_check_mark: Book publication successful!',
image_url: SUCCESS_IMAGE_URL,
alt_text: 'Publication successful'),
{
type: 'divider'
},
context
]
end

def failure_blocks(book:, details:) # rubocop:disable Metrics/MethodLength
[
intro_section(book: book,
message: ':x: Book publication failed!',
image_url: FAILURE_IMAGE_URL,
alt_text: 'Publication failed'),
{
type: 'section',
text: {
type: 'mrkdwn',
text: "```#{details}```"
}
},
{
type: 'divider'
},
context
]
end

def standard_fields(book:) # rubocop:disable Metrics/MethodLength
[
{
type: 'mrkdwn',
text: "*Book*\n#{book&.title || '_unknown_'}"
},
{
type: 'mrkdwn',
text: "*SKU*\n`#{book&.sku || 'unknown'}`"
},
{
type: 'mrkdwn',
text: "*Edition*\n#{book&.edition || '_unknown_'}"
},
{
type: 'mrkdwn',
text: "*Environment*\n`#{ENVIRONMENT}`"
}
]
end

def intro_section(book:, message:, image_url:, alt_text:) # rubocop:disable Metrics/MethodLength
{
type: 'section',
text: {
type: 'mrkdwn',
text: message
},
fields: standard_fields(book: book),
accessory: {
type: 'image',
image_url: image_url,
alt_text: alt_text
}
}
end

def context # rubocop:disable Metrics/MethodLength
{
type: 'context',
elements: [
{
type: 'image',
image_url: ROBLES_CONTEXT_IMAGE_URL,
alt_text: 'robles via razebot'
},
{
type: 'mrkdwn',
text: 'This is a message sent from *robles* via *razebot*.'
}
]
}
end
end
end
3 changes: 3 additions & 0 deletions config/initialisers/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

ENVIRONMENT = ENV['ENV'] || 'development'
2 changes: 2 additions & 0 deletions config/initialisers/slack.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

SLACK_WEBHOOK_URL = ENV['SLACK_WEBHOOK_URL']
SLACK_CHANNEL = ENV['SLACK_CHANNEL'] || '#robles'
SLACK_USERNAME = ENV['SLACK_USERNAME'] || 'razebot'

0 comments on commit c969c04

Please sign in to comment.