Sha256: c287fc875b660a9595b80032a3bbca80f05695af162250519af69976fcc5622c

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "deep_merge"

module NeetoCompliance
  class SemaphoreciConfigVerifier < Base
    def local_copy
      ".semaphore/semaphore.yml"
    end

    def commons_copy
      NeetoCompliance::NeetoCommons.path.join "common_files/semaphore/semaphore.yml"
    end

    def semaphore_config_valid?
      deep_hash_comp(local_semaphore_config, common_semaphore_config)
    end

    def autofix_command
      "echo \"#{common_semaphore_config.to_yaml[4..-2]}\" > #{local_copy}"
    end

    def autofix_suggestion
      "ā†’ cp #{commons_copy} #{local_copy}\nā†’ And append the following configs\n #{app_specific_configs[app_name].to_yaml}".yellow
    end

    def valid?
      semaphore_config_valid?
    end

    private

      def local_semaphore_config
        YAML.load(File.open(local_copy), aliases: true)
      end

      def common_semaphore_config
        semaphore_config = YAML.load(File.open(commons_copy), aliases: true)
        semaphore_config.deep_merge!(app_specific_configs[app_name] || {})
      end

      def app_specific_configs
        {
          "neeto-kb-web" => {
            "global_job_config" => {
              "prologue" => { "commands" => ["sem-service start elasticsearch 7.9"] }
            }
          }
        }
      end

      def deep_hash_comp(hash1, hash2)
        return false if hash1.size != hash2.size

        hash1.all? do |key, value|
          if hash2[key].nil?
            false
          elsif value.class == Array
            array_comp(value.uniq, hash2[key]&.uniq)
          elsif value.class == Hash
            deep_hash_comp(value, hash2[key])
          else
            value == hash2[key]
          end
        end
      end

      def array_comp (array1, array2)
        return false if array1.size != array2.size

        array1.all? { |value| array2.include?(value) }
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neeto-compliance-1.0.58 lib/neeto_compliance/verifiers/semaphoreci_config_verifier.rb