Class: OverridesTracker::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/overrides_tracker/api.rb

Constant Summary collapse

API_HOST =
ENV['OVERRIDES_TRACKER_DEVELOPMENT'] ? 'localhost:3000' : 'www.overrides.io'
API_PROTOCOL =
ENV['OVERRIDES_TRACKER_DEVELOPMENT'] ? 'http' : 'https'
API_DOMAIN =
"#{API_PROTOCOL}://#{API_HOST}"
API_BASE =
"#{API_DOMAIN}/api/v1"

Class Method Summary collapse

Class Method Details

.build_client(uri) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/overrides_tracker/api.rb', line 68

def self.build_client(uri)
  client = Net::HTTP.new(uri.host, uri.port)
  client.use_ssl = true if uri.port == 443
  client.verify_mode = OpenSSL::SSL::VERIFY_NONE

  client
end

.disable_net_blockers!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/overrides_tracker/api.rb', line 76

def self.disable_net_blockers!
  begin
    require 'webmock'

    allow = WebMock::Config.instance.allow || []
    WebMock::Config.instance.allow = [*allow].push API_HOST
  rescue LoadError
  end

  begin
    require 'vcr'

    VCR.send(VCR.version.major < 2 ? :config : :configure) do |c|
      c.ignore_hosts API_HOST
    end
  rescue LoadError
  end
end

.find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name, file_hash) ⇒ Object



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

def self.find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name, file_hash)
  uri = URI(API_DOMAIN)
  client = build_client(uri)    

  form_data = [['api_token', api_token], ['branch_name', branch_name], ['build_provider_id', last_commit_id],
  ['build_name', last_commit_name], ['file_hash', file_hash]]
  
  request = Net::HTTP::Post.new('/api/v1/builds/find_or_create')
  request.set_form form_data

  begin
    response = client.request(request)
    if response.code == '404'
      false
    else
      true
    end
  rescue SocketError => e
    puts 'Failed to report to the Overrides API.'.red
    false
  end
end

.report_build(api_token, branch_name, last_commit_id, last_commit_name, file_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/overrides_tracker/api.rb', line 12

def self.report_build(api_token, branch_name, last_commit_id, last_commit_name, file_path)
  disable_net_blockers!

  puts '  '
  puts '==========='
  puts 'Sending report to Overrides.io...'

  file_hash = Digest::SHA256.hexdigest(File.read(file_path))
  
  if find_or_report_build(api_token, branch_name, last_commit_id, last_commit_name, file_hash)
    puts 'Success.'.green
    true
  else  
    file = File.open(file_path)
    form_data = [['api_token', api_token], ['branch_name', branch_name], ['build_provider_id', last_commit_id],
    ['build_name', last_commit_name], ['result_file', file], ['file_hash', file_hash]]
    
    uri = URI(API_DOMAIN)
    client = build_client(uri)
    request = Net::HTTP::Post.new('/api/v1/builds')
    request.set_form form_data, 'multipart/form-data'
    
    begin
      response = client.request(request)
      puts 'Success.'.green
      true
    rescue SocketError => e
      puts 'Failed to report to the Overrides API.'.red
      false
    end
  end   
end