Skip to content

Commit 3efa705

Browse files
committed
Add Ci and Git classes
1 parent 25b50bb commit 3efa705

File tree

8 files changed

+171
-73
lines changed

8 files changed

+171
-73
lines changed

lib/code_climate/test_reporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def self.configured_branch
2626
end
2727

2828
def self.current_branch
29-
`git symbolic-ref --short HEAD`.strip
29+
Git.branch_from_git_or_ci
3030
end
3131

3232
def self.show_warnings?

lib/code_climate/test_reporter/ci.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module CodeClimate
2+
module TestReporter
3+
class Ci
4+
5+
def self.service_data
6+
if ENV['TRAVIS']
7+
{
8+
name: "travis-ci",
9+
branch: ENV['TRAVIS_BRANCH'],
10+
build_identifier: ENV['TRAVIS_JOB_ID'],
11+
pull_request: ENV['TRAVIS_PULL_REQUEST']
12+
}
13+
elsif ENV['CIRCLECI']
14+
{
15+
name: "circlci",
16+
build_identifier: ENV['CIRCLE_BUILD_NUM'],
17+
branch: ENV['CIRCLE_BRANCH'],
18+
commit_sha: ENV['CIRCLE_SHA1']
19+
}
20+
elsif ENV['SEMAPHORE']
21+
{
22+
name: "semaphore",
23+
branch: ENV['BRANCH_NAME'],
24+
build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
25+
}
26+
elsif ENV['JENKINS_URL']
27+
{
28+
name: "jenkins",
29+
build_identifier: ENV['BUILD_NUMBER'],
30+
build_url: ENV['BUILD_URL'],
31+
branch: ENV['GIT_BRANCH'],
32+
commit_sha: ENV['GIT_COMMIT']
33+
}
34+
elsif ENV['TDDIUM']
35+
{
36+
name: "tddium",
37+
build_identifier: ENV['TDDIUM_SESSION_ID'],
38+
worker_id: ENV['TDDIUM_TID']
39+
}
40+
else
41+
{}
42+
end
43+
end
44+
45+
end
46+
end
47+
end

lib/code_climate/test_reporter/formatter.rb

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def format(result)
1919
File.open(file_path, "w") { |file| file.write(payload.to_json) }
2020
else
2121
client = Client.new
22-
computed_branch = compute_branch(payload)
23-
print "Sending report to #{client.host} for branch #{computed_branch}... "
22+
print "Sending report to #{client.host} for branch #{Git.branch_from_git_or_ci}... "
2423
client.post_results(payload)
2524
end
2625

@@ -64,11 +63,7 @@ def to_payload(result)
6463
covered_strength: round(result.covered_strength, 2),
6564
line_counts: totals,
6665
partial: partial?,
67-
git: {
68-
head: `git log -1 --pretty=format:'%H'`,
69-
committed_at: committed_at,
70-
branch: git_branch,
71-
},
66+
git: Git.info,
7267
environment: {
7368
test_framework: result.command_name.downcase,
7469
pwd: Dir.pwd,
@@ -80,46 +75,6 @@ def to_payload(result)
8075
}
8176
end
8277

83-
def ci_service_data
84-
if ENV['TRAVIS']
85-
{
86-
name: "travis-ci",
87-
branch: ENV['TRAVIS_BRANCH'],
88-
build_identifier: ENV['TRAVIS_JOB_ID'],
89-
pull_request: ENV['TRAVIS_PULL_REQUEST']
90-
}
91-
elsif ENV['CIRCLECI']
92-
{
93-
name: "circlci",
94-
build_identifier: ENV['CIRCLE_BUILD_NUM'],
95-
branch: ENV['CIRCLE_BRANCH'],
96-
commit_sha: ENV['CIRCLE_SHA1']
97-
}
98-
elsif ENV['SEMAPHORE']
99-
{
100-
name: "semaphore",
101-
branch: ENV['BRANCH_NAME'],
102-
build_identifier: ENV['SEMAPHORE_BUILD_NUMBER']
103-
}
104-
elsif ENV['JENKINS_URL']
105-
{
106-
name: "jenkins",
107-
build_identifier: ENV['BUILD_NUMBER'],
108-
build_url: ENV['BUILD_URL'],
109-
branch: ENV['GIT_BRANCH'],
110-
commit_sha: ENV['GIT_COMMIT']
111-
}
112-
elsif ENV['TDDIUM']
113-
{
114-
name: "tddium",
115-
build_identifier: ENV['TDDIUM_SESSION_ID'],
116-
worker_id: ENV['TDDIUM_TID']
117-
}
118-
else
119-
{}
120-
end
121-
end
122-
12378
def calculate_blob_id(path)
12479
content = File.open(path, "rb") {|f| f.read }
12580
header = "blob #{content.length}\0"
@@ -132,39 +87,21 @@ def short_filename(filename)
13287
filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
13388
end
13489

135-
def committed_at
136-
committed_at = `git log -1 --pretty=format:'%ct'`
137-
committed_at.to_i.zero? ? nil : committed_at.to_i
138-
end
139-
140-
def git_branch
141-
branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
142-
branch = [branch].flatten.first
143-
branch ? branch.gsub("* ","") : nil
144-
end
145-
14690
def tddium?
14791
ci_service_data && ci_service_data[:name] == "tddium"
14892
end
14993

150-
def compute_branch(payload)
151-
git_branch = payload[:git][:branch]
152-
ci_branch = payload[:ci_service][:branch]
153-
154-
if ci_branch.to_s.strip.size > 0
155-
ci_branch.sub(/^origin\//, "")
156-
elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
157-
git_branch.sub(/^origin\//, "")
158-
else
159-
"master"
160-
end
161-
end
162-
16394
# Convert to Float before rounding.
16495
# Fixes [#7] possible segmentation fault when calling #round on a Rational
16596
def round(numeric, precision)
16697
Float(numeric).round(precision)
16798
end
99+
100+
private
101+
102+
def ci_service_data
103+
@ci_service_data ||= Ci.service_data
104+
end
168105
end
169106
end
170107
end

lib/code_climate/test_reporter/git.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module CodeClimate
2+
module TestReporter
3+
class Git
4+
5+
class << self
6+
def info
7+
{
8+
head: `git log -1 --pretty=format:'%H'`,
9+
committed_at: committed_at,
10+
branch: branch_from_git,
11+
}
12+
end
13+
14+
def branch_from_git_or_ci
15+
git_branch = branch_from_git
16+
ci_branch = Ci.service_data[:branch]
17+
18+
if ci_branch.to_s.strip.size > 0
19+
ci_branch.sub(/^origin\//, "")
20+
elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(")
21+
git_branch.sub(/^origin\//, "")
22+
else
23+
"master"
24+
end
25+
end
26+
27+
private
28+
29+
def committed_at
30+
committed_at = `git log -1 --pretty=format:'%ct'`
31+
committed_at.to_i.zero? ? nil : committed_at.to_i
32+
end
33+
34+
def branch_from_git
35+
branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" }
36+
branch = [branch].flatten.first
37+
branch ? branch.gsub("* ","") : nil
38+
end
39+
end
40+
end
41+
end
42+
end

lib/codeclimate-test-reporter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
require "code_climate/test_reporter/client"
44
require "code_climate/test_reporter/formatter"
55
require "code_climate/test_reporter/configuration"
6+
require "code_climate/test_reporter/git"
7+
require "code_climate/test_reporter/ci"
68

spec/lib/ci_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
module CodeClimate::TestReporter
4+
describe Ci do
5+
6+
describe '.service_data' do
7+
before :each do
8+
ENV['SEMAPHORE'] = 'yes?'
9+
ENV['BRANCH_NAME'] = 'master'
10+
ENV['SEMAPHORE_BUILD_NUMBER'] = '1234'
11+
end
12+
13+
after :each do
14+
ENV.delete('SEMAPHORE')
15+
ENV.delete('BRANCH_NAME')
16+
ENV.delete('SEMAPHORE_BUILD_NUMBER')
17+
end
18+
19+
it 'returns a hash of CI environment info' do
20+
expected_semaphore_hash = {
21+
name: 'semaphore',
22+
branch: 'master',
23+
build_identifier: '1234'
24+
}
25+
26+
expect(Ci.service_data).to include expected_semaphore_hash
27+
end
28+
end
29+
30+
end
31+
end

spec/lib/formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module CodeClimate::TestReporter
6161
"pwd" => Dir.pwd,
6262
"rails_root" => nil,
6363
"simplecov_root" => Dir.pwd,
64-
"gem_version" => "0.0.11"
64+
"gem_version" => VERSION
6565
},
6666
"ci_service" => {}
6767
}

spec/lib/git_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
3+
module CodeClimate::TestReporter
4+
describe Git do
5+
describe '.info' do
6+
it 'returns a hash with git information.' do
7+
expected_git_hash = {
8+
head: `git log -1 --pretty=format:'%H'`,
9+
committed_at: `git log -1 --pretty=format:'%ct'`.to_i,
10+
branch: Git.send(:branch_from_git)
11+
}
12+
13+
expect(Git.info).to include expected_git_hash
14+
end
15+
end
16+
17+
describe 'branch_from_git_or_ci' do
18+
it 'returns the branch from ci' do
19+
allow(Ci).to receive(:service_data).and_return({branch: 'ci-branch'})
20+
21+
expect(Git.branch_from_git_or_ci).to eq 'ci-branch'
22+
end
23+
24+
it 'returns the branch from git if there is no ci branch' do
25+
allow(Ci).to receive(:service_data).and_return({})
26+
27+
expect(Git.branch_from_git_or_ci).to eq Git.send(:branch_from_git)
28+
end
29+
30+
it 'returns master otherwise' do
31+
allow(Ci).to receive(:service_data).and_return({})
32+
allow(Git).to receive(:branch_from_git).and_return(nil)
33+
34+
expect(Git.branch_from_git_or_ci).to eq 'master'
35+
end
36+
end
37+
38+
end
39+
end

0 commit comments

Comments
 (0)