Sha256: 4502b8de88acc722b173470f50078911c8261344f4303cd3ae8a54c9189151af

Contents?: true

Size: 1.98 KB

Versions: 37

Compression:

Stored size: 1.98 KB

Contents

module KnapsackPro
  class Allocator
    def initialize(args)
      @test_files = args.fetch(:test_files)
      @ci_node_total = args.fetch(:ci_node_total)
      @ci_node_index = args.fetch(:ci_node_index)
      @repository_adapter = args.fetch(:repository_adapter)
    end

    def test_file_paths
      connection = KnapsackPro::Client::Connection.new(build_action)
      response = connection.call
      if connection.success?
        raise ArgumentError.new(response) if connection.errors?
        prepare_test_files(response)
      else
        KnapsackPro.logger.warn("Fallback mode started. We could not connect with Knapsack Pro API. Your tests will be executed based on directory names. Read more about fallback mode at https://github.com/KnapsackPro/knapsack_pro-ruby#what-happens-when-knapsack-pro-api-is-not-availablenot-reachable-temporarily")
        fallback_test_files
      end
    end

    private

    attr_reader :test_files,
      :ci_node_total,
      :ci_node_index,
      :repository_adapter

    def encrypted_test_files
      KnapsackPro::Crypto::Encryptor.call(test_files)
    end

    def encrypted_branch
      KnapsackPro::Crypto::BranchEncryptor.call(repository_adapter.branch)
    end

    def build_action
      KnapsackPro::Client::API::V1::BuildDistributions.subset(
        commit_hash: repository_adapter.commit_hash,
        branch: encrypted_branch,
        node_total: ci_node_total,
        node_index: ci_node_index,
        test_files: encrypted_test_files,
      )
    end

    def prepare_test_files(response)
      decrypted_test_files = KnapsackPro::Crypto::Decryptor.call(test_files, response['test_files'])
      KnapsackPro::TestFilePresenter.paths(decrypted_test_files)
    end

    def fallback_test_files
      test_flat_distributor = KnapsackPro::TestFlatDistributor.new(test_files, ci_node_total)
      test_files_for_node_index = test_flat_distributor.test_files_for_node(ci_node_index)
      KnapsackPro::TestFilePresenter.paths(test_files_for_node_index)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
knapsack_pro-1.17.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.16.1 lib/knapsack_pro/allocator.rb
knapsack_pro-1.16.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.15.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.14.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.13.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.12.1 lib/knapsack_pro/allocator.rb
knapsack_pro-1.12.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.11.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.10.1 lib/knapsack_pro/allocator.rb
knapsack_pro-1.10.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.9.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.8.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.7.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.6.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.5.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.4.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.3.0 lib/knapsack_pro/allocator.rb
knapsack_pro-1.2.1 lib/knapsack_pro/allocator.rb
knapsack_pro-1.2.0 lib/knapsack_pro/allocator.rb