Module: Codacy::ClientAPI

Defined in:
lib/codacy/client.rb

Class Method Summary collapse

Class Method Details

.create_url(codacy_base_api, commit, final: false, partial: false) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/codacy/client.rb', line 54

def self.create_url(codacy_base_api, commit, final: false, partial: false)
  commit = commit.gsub(/[^[:alnum:]]/, "")
  if final
    codacy_base_api + '/2.0/commit/' + commit + '/coverageFinal'
  else
    query_params = partial ? '?partial=true' : ''
    codacy_base_api + '/2.0/coverage/' + commit + '/ruby' + query_params
  end
end

.get_parametersObject



83
84
85
86
87
88
# File 'lib/codacy/client.rb', line 83

def self.get_parameters
  project_token = ENV['CODACY_PROJECT_TOKEN']
  codacy_base_api = ENV['CODACY_BASE_API_URL'] || 'https://api.codacy.com'
  commit = Codacy::Git.commit_id
  return project_token, codacy_base_api, commit
end

.loggerObject



50
51
52
# File 'lib/codacy/client.rb', line 50

def self.logger
  Codacy::Configuration.logger
end

.notify_finalObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/codacy/client.rb', line 64

def self.notify_final
  project_token, codacy_base_api, commit = get_parameters
  url = create_url(codacy_base_api, commit, final: true)

  if project_token.to_s == ''
    puts 'Could not find Codacy API token, make sure you have it configured in your environment.'
    false
  elsif commit.to_s == ''
    puts 'Could not find the current commit, make sure that you are running inside a git project.'
    false
  else
    puts 'Posting Coverage Final Notice to ' + url
    response = send_request(url, '{}', project_token)

    puts response
    response.to_s.include? 'success'
  end
end

.post_results(parsed_result, partial: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codacy/client.rb', line 8

def self.post_results(parsed_result, partial: false)
  logger.info('Preparing payload...')
  logger.debug(parsed_result)

  project_token, codacy_base_api, commit = get_parameters
  url = create_url(codacy_base_api, commit, partial: partial)

  result = parsed_result.to_json

  if project_token.to_s == ''
    logger.error 'Could not find Codacy API token, make sure you have it configured in your environment.'
    false
  elsif commit.to_s == ''
    logger.error 'Could not find the current commit, make sure that you are running inside a git project.'
    false
  else
    logger.info('Posting ' + result.to_s.length.to_s + ' bytes to ' + url)
    response = send_request(url, result, project_token)

    logger.info(response)
    response.to_s.include? 'success'
  end
end

.send_request(url, content, project_token, redirects = 3) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/codacy/client.rb', line 32

def self.send_request(url, content, project_token, redirects = 3)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri)
  http.use_ssl = uri.scheme == "https"
  request["project_token"] = project_token
  request["Content-Type"] = "application/json"
  request.body = content
  response = http.request(request)

  if [301, 302, 307].include? response.code.to_i and redirects > 0
    redirected_url = response.headers[:location]
    send_request(redirected_url, content, project_token, redirects - 1)
  else
    response.body
  end
end