Class: Hiptest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli_options, reporter = nil) ⇒ Client

Returns a new instance of Client.



36
37
38
39
40
# File 'lib/hiptest-publisher/client.rb', line 36

def initialize(cli_options, reporter = nil)
  @cli_options = cli_options
  @reporter = reporter || NullReporter.new
  @async_options = { max_attempts: 200, sleep_time_between_attemps: 5 }
end

Instance Attribute Details

#async_options=(value) ⇒ Object (writeonly)

Sets the attribute async_options

Parameters:

  • value

    the value to set the attribute async_options to.



34
35
36
# File 'lib/hiptest-publisher/client.rb', line 34

def async_options=(value)
  @async_options = value
end

#cli_optionsObject (readonly)

Returns the value of attribute cli_options.



33
34
35
# File 'lib/hiptest-publisher/client.rb', line 33

def cli_options
  @cli_options
end

Instance Method Details

#available_test_runsObject



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hiptest-publisher/client.rb', line 117

def available_test_runs
  @available_test_runs ||= begin
    response = send_get_request("#{base_publication_path}/test_runs")
    if response.code_type == Net::HTTPNotFound
      :api_not_available
    else
      json_response = JSON.parse(response.body)
      json_response["test_runs"]
    end
  end
end

#fetch_projectObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hiptest-publisher/client.rb', line 94

def fetch_project
  cached = export_cache.cache_for(url)

  unless cached.nil?
    @reporter.with_status_message I18n.t(:using_cached_data) do
      return cached
    end
  end

  content = @reporter.with_status_message I18n.t(:fetching_data) do
    break fetch_project_export if use_synchronous_fetch?

    begin
      fetch_project_export_asynchronously
    rescue AsyncExportUnavailable
      fetch_project_export
    end
  end

  export_cache.cache(url, content)
  content
end

#global_failure_urlObject



52
53
54
# File 'lib/hiptest-publisher/client.rb', line 52

def global_failure_url
  "#{cli_options.site}/report_global_failure/#{cli_options.token}/#{cli_options.test_run_id}/"
end

#project_export_filtersObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hiptest-publisher/client.rb', line 56

def project_export_filters
  mapping = {
    filter_on_scenario_ids: 'filter_scenario_ids',
    filter_on_folder_ids: 'filter_folder_ids',
    filter_on_scenario_name: 'filter_scenario_name',
    filter_on_folder_name: 'filter_folder_name',
    filter_on_tags: 'filter_tags'
  }

  options = []

  mapping.each do |key, filter_name|
    value = @cli_options[key]
    next if value.nil? || value.empty?

    if [:filter_on_scenario_ids, :filter_on_folder_ids, :filter_on_tags].include?(key)
      value = value.split(',').map(&:strip).map{ |s| ERB::Util.url_encode(s) }.join(',')
    else
      value = ERB::Util.url_encode(value)
    end

    options << "#{filter_name}=#{value}"
    if [:filter_on_folder_ids, :filter_on_folder_name].include?(key) && @cli_options[:not_recursive]
      options << "not_recursive=true"
    end
  end


  return options.empty? ? '' : "?#{options.join('&')}"
end

#push_resultsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hiptest-publisher/client.rb', line 129

def push_results
  # Code from: https://github.com/nicksieger/multipart-post
  uploaded = {}
  Dir.glob(cli_options.push.gsub('\\', '/')).each do |filename|
    uploaded["file-#{filename.normalize}"] = filename
  end

  if cli_options.global_failure_on_missing_reports && uploaded.empty?
    return send_post_request(global_failure_url)
  end

  send_multipart_request(url, uploaded)
end

#test_run_export_filterObject



87
88
89
90
91
92
# File 'lib/hiptest-publisher/client.rb', line 87

def test_run_export_filter
  value = @cli_options.filter_on_status
  return '' if value.nil? || value.empty?

  return "?filter_status=#{value}"
end

#urlObject



42
43
44
45
46
47
48
49
50
# File 'lib/hiptest-publisher/client.rb', line 42

def url
  if cli_options.push?
    "#{cli_options.site}/import_test_results/#{cli_options.token}/#{cli_options.push_format}#{push_query_parameters}"
  elsif test_run_id
    "#{base_publication_path}/test_run/#{test_run_id}#{test_run_export_filter}"
  else
    "#{base_publication_path}/#{cli_options.leafless_export ? 'leafless_tests' : 'project'}#{project_export_filters}"
  end
end