Class: KnapsackPro::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_pro/report.rb

Class Method Summary collapse

Class Method Details

.create_build_subset(test_files) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/knapsack_pro/report.rb', line 45

def self.create_build_subset(test_files)
  repository_adapter = KnapsackPro::RepositoryAdapterInitiator.call
  test_files = KnapsackPro::Utils.unsymbolize(test_files)
  encrypted_test_files = KnapsackPro::Crypto::Encryptor.call(test_files)
  encrypted_branch = KnapsackPro::Crypto::BranchEncryptor.call(repository_adapter.branch)
  action = KnapsackPro::Client::API::V1::BuildSubsets.create(
    commit_hash: repository_adapter.commit_hash,
    branch: encrypted_branch,
    node_total: KnapsackPro::Config::Env.ci_node_total,
    node_index: KnapsackPro::Config::Env.ci_node_index,
    test_files: encrypted_test_files,
  )
  connection = KnapsackPro::Client::Connection.new(action)
  response = connection.call
  if connection.success?
    raise ArgumentError.new(response) if connection.errors?
    KnapsackPro.logger.debug('Saved time execution report on API server.')
  end
end

.saveObject



3
4
5
6
7
8
9
10
11
# File 'lib/knapsack_pro/report.rb', line 3

def self.save
  test_files = KnapsackPro.tracker.to_a

  if test_files.empty?
    KnapsackPro.logger.warn("No test files were executed on this CI node. When you use knapsack_pro regular mode then probably reason might be very narrowed tests list - you run only tests with specified tag and there are fewer test files with the tag than node total number.")
  end

  create_build_subset(test_files)
end

.save_node_queue_to_apiObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/knapsack_pro/report.rb', line 30

def self.save_node_queue_to_api
  test_files = []
  Dir.glob("#{queue_path}/*.json").each do |file|
    report = JSON.parse(File.read(file))
    test_files += report
  end

  if test_files.empty?
    KnapsackPro.logger.warn("No test files were executed on this CI node. When you use knapsack_pro queue mode then probably reason might be that CI node was started after the test files from the queue were already executed by other CI nodes. That is why this CI node has no test files to execute.")
    KnapsackPro.logger.warn("Another reason might be when your CI node failed in a way that prevented knapsack_pro to save time execution data to Knapsack Pro API and you have just tried to retry failed CI node but instead you got no test files to execute. In that case knapsack_pro don't know what testes should be executed here.")
  end

  create_build_subset(test_files)
end

.save_subset_queue_to_fileObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/knapsack_pro/report.rb', line 13

def self.save_subset_queue_to_file
  test_files = KnapsackPro.tracker.to_a
  KnapsackPro.tracker.reset!

  subset_queue_id = KnapsackPro::Config::Env.subset_queue_id

  FileUtils.mkdir_p(queue_path)

  subset_queue_file_name = "#{subset_queue_id}.json"
  report_path = File.join(queue_path, subset_queue_file_name)
  report_json = JSON.pretty_generate(test_files)

  File.open(report_path, 'w+') do |f|
    f.write(report_json)
  end
end