class Kuroko2::Workflow::Notifier::Webhook
Constants
- HASH_ALGORITHM
- HMAC_DIGEST
Attributes
message_builder[R]
Public Class Methods
new(instance)
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 10 def initialize(instance) @instance = instance @definition = instance.job_definition @message_builder = Workflow::Notifier::Concerns::ChatMessageBuilder.new(instance) @secret_token = Kuroko2.config.notifiers.webhook.try!(:secret_token) end
Public Instance Methods
notify_back_to_normal()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 103 def notify_back_to_normal request( build_payload( action: 'notify_back_to_normal', level: 'SUCCESS', subject: message_builder.back_to_normal_text, ) ) end
notify_cancellation()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 56 def notify_cancellation if @definition.notify_cancellation || @definition.hipchat_notify_finished? request( build_payload( action: 'notify_cancellation', level: 'WARNING', subject: message_builder.failure_text, message: @instance.logs.reverse.detect{ |log| log.level == 'WARN' }.try!(:message), ) ) end end
notify_critical()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 80 def notify_critical request( build_payload( action: 'notify_critical', level: 'CRITICAL', subject: message_builder.failure_text, message: @instance.logs.last(2).first.message, ) ) end
notify_failure()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 69 def notify_failure request( build_payload( action: 'notify_failure', level: 'FAILURE', subject: message_builder.failure_text, message: @instance.logs.last(2).first.message, ) ) end
notify_finished()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 91 def notify_finished if @definition.hipchat_notify_finished? request( build_payload( action: 'notify_finished', level: 'SUCCESS', subject: message_builder.finished_text, ) ) end end
notify_launch()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 17 def notify_launch if @definition.hipchat_notify_finished? request( build_payload( action: 'notify_launch', level: 'INFO', subject: message_builder.launched_text, message: @instance.logs.reverse.detect{ |log| log.level == 'INFO' }.try!(:message), ) ) end end
notify_long_elapsed_time()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 113 def notify_long_elapsed_time request( build_payload( action: 'notify_long_elapsed_time', level: 'WARNING', subject: message_builder.long_elapsed_time_text, ) ) end
notify_retrying()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 30 def notify_retrying if @definition.hipchat_notify_finished? request( build_payload( action: 'notify_retrying', level: 'INFO', subject: message_builder.retrying_text, message: @instance.logs.last(2).first.message, ) ) end end
notify_skipping()
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 43 def notify_skipping if @definition.hipchat_notify_finished? request( build_payload( action: 'notify_skipping', level: 'INFO', subject: message_builder.skipping_text, message: @instance.logs.last(2).first.message, ) ) end end
Private Instance Methods
build_payload(action:, level:, subject:, message: nil)
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 153 def build_payload(action:, level:, subject:, message: nil) { action: action, level: level, subject: subject, message: message, job_instance: { url: message_builder.job_instance_path, id: @instance.id, script: @instance.script, finished_at: @instance.finished_at.try!(:iso8601), canceled_at: @instance.canceled_at.try!(:iso8601), error_at: @instance.error_at.try!(:iso8601), created_at: @instance.created_at.try!(:iso8601), }, job_definition: { url: Kuroko2::Engine.routes.url_helpers.job_definition_url( @definition, host: Kuroko2.config.url_host, protocol: Kuroko2.config.url_scheme, ), id: @definition.id, name: @definition.name, description: @definition.description, } } end
request(body)
click to toggle source
# File lib/autoload/kuroko2/workflow/notifier/webhook.rb, line 125 def request(body) return unless @definition.webhook_url.present? url = URI.parse(@definition.webhook_url) conn = Faraday.new(url: "#{url.scheme}://#{url.host}") do |faraday| faraday.port = url.port faraday.adapter Faraday.default_adapter end json = body.to_json response = conn.post do |req| req.url(url.path) req.headers['X-Kuroko2-Id'] = SecureRandom.uuid if @secret_token.present? req.headers['X-Kuroko2-Signature'] = "#{HASH_ALGORITHM}=#{OpenSSL::HMAC.hexdigest(HMAC_DIGEST, @secret_token, json)}" end req.headers['User-Agent'] = 'Kuroko2-Webhook' req.headers['Content-Type'] = 'application/json' req.body = json end unless response.success? Kuroko2.logger.fatal("Failure sending webhook: status=#{response.status} body=#{response.body}") end end