class CodeBuildNotifier::SlackSender
Attributes
config[R]
Public Class Methods
new(config)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 25 def initialize(config) @config = config end
Public Instance Methods
find_alias(email)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 79 def find_alias(email) config.slack_alias_table && config.dynamo_client.get_item( table_name: config.slack_alias_table, key: { 'alternate_email' => email } ).item&.fetch('workspace_email') end
send(message)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 29 def send(message) Slack.configure { |slack_config| slack_config.token = app_token } channel = message.additional_channel if channel channel = "##{channel}" unless /\A#/.match?(channel) post_message(message, channel) end user_ids = message.recipients.map { |email| find_slack_user(email)&.id } user_ids.uniq.compact.each { |user_id| post_message(message, user_id) } end
Private Instance Methods
admin_send(message)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 49 def admin_send(message) config.slack_admins.each do |username| username = "@#{username}" unless /\A@/.match?(username) slack_client.chat_postMessage( as_user: false, text: message, channel: username ) end end
app_is_bot_user?()
click to toggle source
If the app token starts with xoxb- then it is a Bot User Oauth token and slack notifications should be posted with as_user: true. If it starts with xoxp- then it's an app token not associated with a user, and as_user: should be false.
# File lib/codebuild-notifier/slack_sender.rb, line 90 def app_is_bot_user? /\Axoxb/.match?(app_token) end
app_token()
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 102 def app_token @app_token ||= JSON.parse(secret.secret_string)['token'] end
find_slack_user(email)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 60 def find_slack_user(email) slack_client.users_lookupByEmail(email: email)&.user rescue Slack::Web::Api::Errors::SlackError => e alias_email = find_alias(email) if alias_email find_slack_user(alias_email) else report_lookup_failure(email, e.message) nil end end
post_message(message, channel)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 41 def post_message(message, channel) slack_client.chat_postMessage( as_user: app_is_bot_user?, attachments: [message.payload], channel: channel ) end
report_lookup_failure(email, error_message)
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 72 def report_lookup_failure(email, error_message) admin_send( "Slack user lookup by email for #{email} failed with " \ "error: #{error_message}" ) end
secret()
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 106 def secret secrets_client.get_secret_value(secret_id: config.slack_secret_name) end
secrets_client()
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 94 def secrets_client Aws::SecretsManager::Client.new(region: config.region) end
slack_client()
click to toggle source
# File lib/codebuild-notifier/slack_sender.rb, line 98 def slack_client @slack_client ||= Slack::Web::Client.new end