Sha256: 2e7a297976ddda8232c534050797b6c8904a419208b395f89a2e9818293fc5e0

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Mihari
  module Emitters
    class Webhook < Base
      # @return [Boolean]
      def valid?
        webhook_url?
      end

      def emit(title:, description:, artifacts:, source:, tags:)
        return if artifacts.empty?

        uri = Addressable::URI.parse(Mihari.config.webhook_url)
        data = {
          title: title,
          description: description,
          artifacts: artifacts.map(&:data),
          source: source,
          tags: tags
        }

        if use_json_body?
          Net::HTTP.post(uri, data.to_json, "Content-Type" => "application/json")
        else
          Net::HTTP.post_form(uri, data)
        end
      end

      private

      def configuration_keys
        %w[webhook_url]
      end

      #
      # Webhook URL
      #
      # @return [String, nil]
      #
      def webhook_url
        @webhook_url ||= Mihari.config.webhook_url
      end

      #
      # Check whether a webhook URL is set or not
      #
      # @return [<Type>] <description>
      #
      def webhook_url?
        !webhook_url.nil?
      end

      #
      # Check whether to use JSON body or NOT
      #
      # @return [<Type>] <description>
      #
      def use_json_body?
        @use_json_body ||= Mihari.config.webhook_use_json_body
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mihari-4.2.0 lib/mihari/emitters/webhook.rb
mihari-4.1.2 lib/mihari/emitters/webhook.rb
mihari-4.1.1 lib/mihari/emitters/webhook.rb
mihari-4.1.0 lib/mihari/emitters/webhook.rb
mihari-4.0.0 lib/mihari/emitters/webhook.rb