codacy-coverage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed371108f2d9c6b2f61a19b7ea7aba4e9557e194
4
+ data.tar.gz: 0727932b29163f3e42e9754cfbc56d21beb74bdf
5
+ SHA512:
6
+ metadata.gz: a6adaf3d319cc860ac86d290900db388031ddf20f7fbb1b0b1925aa2d34493ac272661d274e9e75c7ba87f1d20de6b4db6892d8d5c3f4a6eddcea0f0ecbc1716
7
+ data.tar.gz: e562e27817edb1926519cc1c51ed38411be78a640c5b80338a6af2a189d6f9fba17da643c27985dfc36e3f44206c4a2f7ccbd7cc8c93601e231dbc6fdbe1c956
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .idea/
20
+ .idea_modules/
21
+
22
+ .DS_Store
23
+ /vendor/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '>= 10.4'
6
+ gem 'rspec', '>= 3.2'
7
+ gem 'simplecov', :require => false
8
+ gem 'rest-client'
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nuno Teixeira"]
6
+ gem.email = ["nuno@codacy.com"]
7
+ gem.description = "Post code coverage results to Codacy."
8
+ gem.summary = "Post code coverage results to Codacy."
9
+ gem.homepage = "https://codacy.com"
10
+ gem.license = "MIT"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "codacy-coverage"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = '0.0.1'
18
+
19
+ gem.required_ruby_version = '>= 1.8.7'
20
+
21
+ gem.add_dependency 'simplecov', '~> 0.10.0'
22
+
23
+ gem.add_development_dependency 'bundler', '~> 1.7'
24
+ end
@@ -0,0 +1,6 @@
1
+ require "codacy/configuration"
2
+ require "codacy/reporter"
3
+ require "codacy/client"
4
+ require "codacy/formatter"
5
+ require "codacy/git"
6
+ require "codacy/parser"
@@ -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
@@ -0,0 +1,40 @@
1
+ require 'logger'
2
+ require 'tmpdir'
3
+
4
+ module Codacy
5
+ module Configuration
6
+
7
+ def self.logger
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)
18
+
19
+ log
20
+ end
21
+
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
39
+ end
40
+ end
@@ -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
@@ -0,0 +1,30 @@
1
+ module Codacy
2
+ class Git
3
+ def self.commit_id
4
+
5
+ commit = ENV['TRAVIS_COMMIT'] ||
6
+ ENV['DRONE_COMMIT'] ||
7
+ ENV['GIT_COMMIT'] ||
8
+ ENV['CIRCLE_SHA1'] ||
9
+ ENV['CI_COMMIT_ID'] ||
10
+ ENV['WERCKER_GIT_COMMIT'] ||
11
+ git_commit
12
+
13
+ commit
14
+ end
15
+
16
+ private
17
+
18
+ def self.git_commit
19
+ git("log -1 --pretty=format:'%H'")
20
+ end
21
+
22
+ def self.git_dir
23
+ return Dir.pwd
24
+ end
25
+
26
+ def self.git(command)
27
+ `git --git-dir="#{git_dir}/.git" #{command}`
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ module Codacy
2
+ module Parser
3
+
4
+ def self.parse_file(simplecov_result)
5
+ project_dir = Dir.pwd
6
+
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 = v.compact.length == 0 ? 0 : ((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
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ require 'simplecov'
2
+
3
+ module Codacy
4
+ module Reporter
5
+ def self.start
6
+ SimpleCov.formatter = Codacy::Formatter
7
+ SimpleCov.start
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Codacy::Git do
4
+ describe 'commit hash' do
5
+ it 'recognizes CI environment variable.' do
6
+ expected_git_hash = "123456789"
7
+
8
+ original_val = ENV['TRAVIS_COMMIT']
9
+ ENV['TRAVIS_COMMIT'] = expected_git_hash
10
+
11
+ result = Codacy::Git.commit_id
12
+
13
+ ENV['TRAVIS_COMMIT'] = original_val
14
+
15
+ expect(result).to eq expected_git_hash
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+
3
+ class InceptionFormatter
4
+ def format(result)
5
+ Codacy::Formatter.new.format(result)
6
+ end
7
+ end
8
+
9
+ SimpleCov.formatter = InceptionFormatter
10
+ SimpleCov.start
11
+
12
+ require 'codacy-coverage'
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codacy-coverage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nuno Teixeira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ description: Post code coverage results to Codacy.
42
+ email:
43
+ - nuno@codacy.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - codacy-coverage.gemspec
51
+ - lib/codacy-coverage.rb
52
+ - lib/codacy/client.rb
53
+ - lib/codacy/configuration.rb
54
+ - lib/codacy/formatter.rb
55
+ - lib/codacy/git.rb
56
+ - lib/codacy/parser.rb
57
+ - lib/codacy/reporter.rb
58
+ - ruby-codacy-coverage.iml
59
+ - spec/codacy/git_spec.rb
60
+ - spec/spec_helper.rb
61
+ homepage: https://codacy.com
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: 1.8.7
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.14
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Post code coverage results to Codacy.
85
+ test_files:
86
+ - spec/codacy/git_spec.rb
87
+ - spec/spec_helper.rb