Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit 978ce30

Browse files
committed
working version
1 parent e40f348 commit 978ce30

File tree

12 files changed

+156
-47
lines changed

12 files changed

+156
-47
lines changed

Gemfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ gemspec
55
gem 'rake', '>= 10.4'
66
gem 'rspec', '>= 3.2'
77
gem 'simplecov', :require => false
8-
gem 'truthy', '>= 1'
9-
gem 'vcr', '>= 2.9'
108
gem 'webmock', '>= 1.20'
11-
9+
gem 'rest-client'

bin/codacy-coverage

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/codacy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
require "codacy/configuration"
2+
require "codacy/client"
3+
require "codacy/formatter"
24
require "codacy/git"
3-
require "codacy/version"
5+
require "codacy/parser"

lib/codacy/cli.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/codacy/client.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'json'
2+
require 'rest_client'
3+
4+
module Codacy
5+
module ClientAPI
6+
def self.post_results(parsed_result)
7+
logger.info('Preparing payload...')
8+
logger.debug(parsed_result)
9+
10+
project_token = ENV['CODACY_PROJECT_TOKEN']
11+
codacy_base_api = ENV['CODACY_BASE_API_URL'] || 'https://api.codacy.com'
12+
commit = Codacy::Git.commit_id
13+
url = codacy_base_api + '/2.0/coverage/' + commit + '/ruby'
14+
15+
result = parsed_result.to_json
16+
17+
if project_token.to_s == ''
18+
logger.error 'Could not find Codacy API token, make sure you have it configured in your environment.'
19+
false
20+
elsif commit.to_s == ''
21+
logger.error 'Could not find the current commit, make sure that you are running inside a git project.'
22+
false
23+
else
24+
logger.info('Posting ' + result.to_s.bytes.length.to_s + ' bytes to ' + url)
25+
response = send_request(url, result, project_token)
26+
27+
logger.info(response)
28+
response.to_s.include? 'success'
29+
end
30+
end
31+
32+
def self.send_request(url, request, project_token, redirects = 3)
33+
RestClient.post(
34+
url,
35+
request,
36+
'project_token' => project_token,
37+
:content_type => :json
38+
) do |resp, req, result, &block|
39+
if [301, 302, 307].include? resp.code and redirects > 0
40+
redirected_url = resp.headers[:location]
41+
send_request(redirected_url, req, project_token, redirects - 1)
42+
else
43+
resp.return!(req, result, &block)
44+
end
45+
end
46+
end
47+
48+
def self.logger
49+
Codacy::Configuration.logger
50+
end
51+
end
52+
end

lib/codacy/configuration.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
require 'logger'
2+
require 'tmpdir'
23

34
module Codacy
45
module Configuration
56

67
def self.logger
7-
log = Logger.new($stdout)
8-
log.level = Logger::INFO
8+
log_filename = self.temp_dir + 'codacy-coverage_' + Date.today.to_s + '.log'
9+
log_file = File.open(log_filename, 'a')
10+
11+
logger_file = Logger.new(log_file)
12+
logger_file.level = Logger::DEBUG
13+
14+
logger_stdout = Logger.new(STDOUT)
15+
logger_stdout.level = Logger::INFO
16+
17+
log = MultiLogger.new(logger_stdout, logger_file)
918

1019
log
1120
end
1221

22+
def self.temp_dir
23+
directory_name = Dir.tmpdir + "/codacy-coverage/"
24+
Dir.mkdir(directory_name) unless File.exists?(directory_name)
25+
directory_name
26+
end
27+
28+
class MultiLogger
29+
def initialize(*targets)
30+
@targets = targets
31+
end
32+
33+
%w(log debug info warn error fatal unknown).each do |m|
34+
define_method(m) do |*args|
35+
@targets.map { |t| t.send(m, *args) }
36+
end
37+
end
38+
end
1339
end
1440
end

lib/codacy/formatter.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Codacy
2+
class Formatter
3+
4+
def format(result)
5+
parse_result = Codacy::Parser.parse_file(result)
6+
Codacy::ClientAPI.post_results(parse_result)
7+
rescue => ex
8+
logger.fatal(ex)
9+
false
10+
end
11+
12+
private
13+
14+
def logger
15+
Codacy::Configuration.logger
16+
end
17+
18+
end
19+
end

lib/codacy/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def self.git_commit
2020
end
2121

2222
def self.git_dir
23-
return '.'
23+
return Dir.pwd
2424
end
2525

2626
def self.git(command)

lib/codacy/parser.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
module Codacy
22
module Parser
33

4+
def self.parse_file(simplecov_result)
5+
project_dir = Dir.pwd
46

7+
logger.info("Parsing simplecov result to Codacy format...")
8+
logger.debug(simplecov_result.original_result)
9+
10+
file_reports = simplecov_result.original_result.map do |k, v|
11+
file_dir = k.sub(project_dir, "").sub("/", "")
12+
coverage_lines = v.each_with_index.map do |covered, lineNr|
13+
if !covered.nil? && covered > 0
14+
[(lineNr + 1).to_s, covered]
15+
else
16+
nil
17+
end
18+
end.compact
19+
lines_covered = ((coverage_lines.length.to_f / v.compact.length) * 100).round
20+
Hash[
21+
[['filename', file_dir],
22+
['total', lines_covered],
23+
['coverage', Hash[coverage_lines]]]
24+
]
25+
end
26+
27+
total = simplecov_result.source_files.covered_percent.round
28+
29+
Hash[[['total', total], ['fileReports', file_reports]]]
30+
end
31+
32+
33+
private
34+
35+
def self.logger
36+
Codacy::Configuration.logger
37+
end
538
end
639
end

lib/codacy/version.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)