Sha256: 4fdab0a9c6b5ac07f8f9d29c87b838baef19c5683b0a0bb36f7b402e0b797045

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "json"
require "net/http"
require "uri"

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 = URI(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

13 entries across 13 versions & 1 rubygems

Version Path
mihari-3.12.0 lib/mihari/emitters/webhook.rb
mihari-3.11.0 lib/mihari/emitters/webhook.rb
mihari-3.10.1 lib/mihari/emitters/webhook.rb
mihari-3.10.0 lib/mihari/emitters/webhook.rb
mihari-3.9.2 lib/mihari/emitters/webhook.rb
mihari-3.9.1 lib/mihari/emitters/webhook.rb
mihari-3.9.0 lib/mihari/emitters/webhook.rb
mihari-3.8.1 lib/mihari/emitters/webhook.rb
mihari-3.8.0 lib/mihari/emitters/webhook.rb
mihari-3.7.2 lib/mihari/emitters/webhook.rb
mihari-3.7.1 lib/mihari/emitters/webhook.rb
mihari-3.7.0 lib/mihari/emitters/webhook.rb
mihari-3.6.1 lib/mihari/emitters/webhook.rb