Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ Code Climate account if you are in the test coverage private beta.

Please contact hello@codeclimate.com if you need any assistance setting this up.

## Configuration
By default the reporter will run on every branch, and log messages to $stderr with a log level of Logger::INFO.
These can be overridden with a configure block.

*Note that the configuration block must come before TestReporter.start.*

```ruby
CodeClimate::TestReporter.configure do |config|
config.branch = :master

# set a custom level
config.logger.level = Logger::WARN

# use a custom logger
config.logger = MyCoolLogger.new
end

CodeClimate::TestReporter.start
```


## Help! Your gem is raising a ...

### VCR::Errors::UnhandledHTTPRequestError
Expand Down
37 changes: 34 additions & 3 deletions lib/code_climate/test_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,44 @@ def self.start
::SimpleCov.add_filter 'vendor'
::SimpleCov.formatter = Formatter
::SimpleCov.start("test_frameworks")
else
puts("Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.")
end
end

def self.run?
!!ENV["CODECLIMATE_REPO_TOKEN"]
environment_variable_set? && run_on_current_branch?
end

def self.environment_variable_set?
environment_variable_set = !!ENV["CODECLIMATE_REPO_TOKEN"]
unless environment_variable_set
logger.info("Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.")
end

environment_variable_set
end

def self.run_on_current_branch?
return true if configured_branch.nil?

run_on_current_branch = !!(current_branch =~ /#{configured_branch}/i)
unless run_on_current_branch
logger.info("Not reporting to Code Climate because #{configured_branch} is set as the reporting branch.")
end

run_on_current_branch
end

def self.configured_branch
configuration.branch
end

def self.current_branch
Git.branch_from_git_or_ci
end

def self.logger
CodeClimate::TestReporter.configuration.logger
end

end
end
47 changes: 47 additions & 0 deletions lib/code_climate/test_reporter/ci.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module CodeClimate
module TestReporter
class Ci

def self.service_data
if ENV['TRAVIS']
{
name: "travis-ci",
branch: ENV['TRAVIS_BRANCH'],
build_identifier: ENV['TRAVIS_JOB_ID'],
pull_request: ENV['TRAVIS_PULL_REQUEST']
}
elsif ENV['CIRCLECI']
{
name: "circlci",
build_identifier: ENV['CIRCLE_BUILD_NUM'],
branch: ENV['CIRCLE_BRANCH'],
commit_sha: ENV['CIRCLE_SHA1']
}
elsif ENV['SEMAPHORE']
{
name: "semaphore",
branch: ENV['BRANCH_NAME'],
build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
}
elsif ENV['JENKINS_URL']
{
name: "jenkins",
build_identifier: ENV['BUILD_NUMBER'],
build_url: ENV['BUILD_URL'],
branch: ENV['GIT_BRANCH'],
commit_sha: ENV['GIT_COMMIT']
}
elsif ENV['TDDIUM']
{
name: "tddium",
build_identifier: ENV['TDDIUM_SESSION_ID'],
worker_id: ENV['TDDIUM_TID']
}
else
{}
end
end

end
end
end
37 changes: 37 additions & 0 deletions lib/code_climate/test_reporter/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module CodeClimate
module TestReporter
@@configuration = nil

def self.configure
@@configuration = Configuration.new

if block_given?
yield configuration
end

configuration
end

def self.configuration
@@configuration || configure
end

class Configuration
attr_accessor :branch, :logger

def logger
@logger ||= default_logger
end

private

def default_logger
log = Logger.new($stderr)
log.level = Logger::INFO

log
end
end

end
end
79 changes: 8 additions & 71 deletions lib/code_climate/test_reporter/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def format(result)
File.open(file_path, "w") { |file| file.write(payload.to_json) }
else
client = Client.new
computed_branch = compute_branch(payload)
print "Sending report to #{client.host} for branch #{computed_branch}... "
print "Sending report to #{client.host} for branch #{Git.branch_from_git_or_ci}... "
client.post_results(payload)
end

Expand Down Expand Up @@ -64,11 +63,7 @@ def to_payload(result)
covered_strength: round(result.covered_strength, 2),
line_counts: totals,
partial: partial?,
git: {
head: `git log -1 --pretty=format:'%H'`,
committed_at: committed_at,
branch: git_branch,
},
git: Git.info,
environment: {
test_framework: result.command_name.downcase,
pwd: Dir.pwd,
Expand All @@ -80,46 +75,6 @@ def to_payload(result)
}
end

def ci_service_data
if ENV['TRAVIS']
{
name: "travis-ci",
branch: ENV['TRAVIS_BRANCH'],
build_identifier: ENV['TRAVIS_JOB_ID'],
pull_request: ENV['TRAVIS_PULL_REQUEST']
}
elsif ENV['CIRCLECI']
{
name: "circlci",
build_identifier: ENV['CIRCLE_BUILD_NUM'],
branch: ENV['CIRCLE_BRANCH'],
commit_sha: ENV['CIRCLE_SHA1']
}
elsif ENV['SEMAPHORE']
{
name: "semaphore",
branch: ENV['BRANCH_NAME'],
build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
}
elsif ENV['JENKINS_URL']
{
name: "jenkins",
build_identifier: ENV['BUILD_NUMBER'],
build_url: ENV['BUILD_URL'],
branch: ENV['GIT_BRANCH'],
commit_sha: ENV['GIT_COMMIT']
}
elsif ENV['TDDIUM']
{
name: "tddium",
build_identifier: ENV['TDDIUM_SESSION_ID'],
worker_id: ENV['TDDIUM_TID']
}
else
{}
end
end

def calculate_blob_id(path)
content = File.open(path, "rb") {|f| f.read }
header = "blob #{content.length}\0"
Expand All @@ -132,39 +87,21 @@ def short_filename(filename)
filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
end

def committed_at
committed_at = `git log -1 --pretty=format:'%ct'`
committed_at.to_i.zero? ? nil : committed_at.to_i
end

def git_branch
branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
branch = [branch].flatten.first
branch ? branch.gsub("* ","") : nil
end

def tddium?
ci_service_data && ci_service_data[:name] == "tddium"
end

def compute_branch(payload)
git_branch = payload[:git][:branch]
ci_branch = payload[:ci_service][:branch]

if ci_branch.to_s.strip.size > 0
ci_branch.sub(/^origin\//, "")
elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
git_branch.sub(/^origin\//, "")
else
"master"
end
end

# Convert to Float before rounding.
# Fixes [#7] possible segmentation fault when calling #round on a Rational
def round(numeric, precision)
Float(numeric).round(precision)
end

private

def ci_service_data
@ci_service_data ||= Ci.service_data
end
end
end
end
42 changes: 42 additions & 0 deletions lib/code_climate/test_reporter/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module CodeClimate
module TestReporter
class Git

class << self
def info
{
head: `git log -1 --pretty=format:'%H'`,
committed_at: committed_at,
branch: branch_from_git,
}
end

def branch_from_git_or_ci
git_branch = branch_from_git
ci_branch = Ci.service_data[:branch]

if ci_branch.to_s.strip.size > 0
ci_branch.sub(/^origin\//, "")
elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
git_branch.sub(/^origin\//, "")
else
"master"
end
end

private

def committed_at
committed_at = `git log -1 --pretty=format:'%ct'`
committed_at.to_i.zero? ? nil : committed_at.to_i
end

def branch_from_git
branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
branch = [branch].flatten.first
branch ? branch.gsub("* ","") : nil
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/codeclimate-test-reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
require "code_climate/test_reporter/version"
require "code_climate/test_reporter/client"
require "code_climate/test_reporter/formatter"
require "code_climate/test_reporter/configuration"
require "code_climate/test_reporter/git"
require "code_climate/test_reporter/ci"

31 changes: 31 additions & 0 deletions spec/lib/ci_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

module CodeClimate::TestReporter
describe Ci do

describe '.service_data' do
before :each do
ENV['SEMAPHORE'] = 'yes?'
ENV['BRANCH_NAME'] = 'master'
ENV['SEMAPHORE_BUILD_NUMBER'] = '1234'
end

after :each do
ENV.delete('SEMAPHORE')
ENV.delete('BRANCH_NAME')
ENV.delete('SEMAPHORE_BUILD_NUMBER')
end

it 'returns a hash of CI environment info' do
expected_semaphore_hash = {
name: 'semaphore',
branch: 'master',
build_identifier: '1234'
}

expect(Ci.service_data).to include expected_semaphore_hash
end
end

end
end
Loading