Sha256: 6fffe6d9b827266d213d2bb7d6d009f277c8f426ecc5e683ed2a36e1a792ad7e

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Jiminy
  module Reporting
    class YAMLFileCommentPresenter
      require "open-uri"
      require "yaml"
      require "jiminy/github_apiable"

      include GithubAPIable

      INSTANCE_SEPARATOR = "\n"

      def initialize(source_filepath:, pr_number:)
        @source_filepath = source_filepath
        @pr_number = pr_number
      end

      def comment_body
        @_comment_body ||= build_comment_body
      end

      alias to_s comment_body

      private

        attr_reader :source_filepath, :pr_number

        def instances
          @_instances ||= YAML.safe_load(file_content).map do |hash|
            options = hash.values.first.transform_keys!(&:to_sym)
            NPlusOne.new(**options)
          end
        end

        def file_content
          @_file_content ||= file_content_for_filepath(source_filepath)
        end

        def file_content_for_filepath(source_filepath)
          return file_content_for_remote_file(source_filepath) if source_filepath.start_with?("https://")

          file_content_for_local_file(source_filepath)
        end

        def file_content_for_remote_file(source_filepath)
          URI.parse(source_filepath).open({ "Circle-Token" => ENV["CIRCLE_CI_API_TOKEN"] }).read
        end

        def file_content_for_local_file(source_filepath)
          File.read(source_filepath)
        end

        def build_comment_body
          Array(instances).map do |instance|
            file = file_from_instance(instance) || next
            instance.blob_url = file.blob_url
            instance.to_markdown
          end.compact.join(INSTANCE_SEPARATOR)
        end

        def file_from_instance(instance)
          client.pull_request_files(env_config.repo_path, pr_number).detect do |file|
            file.filename == instance.file
          end
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jiminy-0.4.0 lib/jiminy/reporting/yaml_file_comment_presenter.rb
jiminy-0.3.0 lib/jiminy/reporting/yaml_file_comment_presenter.rb
jiminy-0.2.0 lib/jiminy/reporting/yaml_file_comment_presenter.rb
jiminy-0.1.1 lib/jiminy/reporting/yaml_file_comment_presenter.rb
jiminy-0.1.0 lib/jiminy/reporting/yaml_file_comment_presenter.rb