Sha256: e8468e7924b34e35335ce1f4a7d63de3529fbd82dc0bc60669df1ffe87a49fdb

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Engines
  module Custom
    class Extractor
      def initialize
        @filenames = ENV.fetch('CODEMONITOR_CUSTOMS', nil)
      end

      def call(provider)
        provider.emit(metrics)
      end

      def requirements?
        custom_files.length.positive?
      end

      private

      def custom_files
        return Dir.glob('./.codemonitor/*.rb') if @filenames.nil?

        raise 'Forbidden access to parent folder' unless @filenames.match(/\.\./).nil?

        includes = @filenames.split(',').reject do |filename|
          filename.start_with?('-')
        end.map do |filename|
          "./.codemonitor/#{filename}.rb"
        end

        excludes = @filenames.split(',').filter do |filename|
          filename.start_with?('-')
        end.map do |filename|
          "./.codemonitor/#{filename.gsub(/^-/, '')}.rb"
        end

        raise 'Mixed included and excluded custom paths is not allowed' if includes.size > 0 && excludes.size > 0

        return Dir.glob(includes) if includes.size > 0

        Dir.glob('./.codemonitor/*.rb') - Dir.glob(excludes)
      end

      def metrics
        custom_files.map do |file|
          values = begin
            eval File.read(file)
          rescue SyntaxError => e
            raise "Unable to execute the custom codemonitor script `#{file}` file"
          end

          raise "Malformed return value from `#{file}` file. It must be a hash of metrics" unless values.is_a?(Hash)

          values
        end.reduce({}, :merge)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
codemonitor-0.6.4 engines/custom/extractor.rb
codemonitor-0.6.3 engines/custom/extractor.rb
codemonitor-0.6.2 engines/custom/extractor.rb
codemonitor-0.6.1 engines/custom/extractor.rb