Skip to content

Commit c7e7198

Browse files
committed
Add CircleCI support!
1 parent 0219ead commit c7e7198

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

bin/slather

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Clamp do
1212
parameter "[xcodeproj]", "Path to the xcodeproj", :attribute_name => :xcodeproj_path
1313

1414
option ["--travis", "-t"], :flag, "Indicate that the builds are running on Travis CI"
15+
option ["--circleci"], :flag, "Indicate that the builds are running on CircleCI"
1516

1617
option ["--coveralls", "-c"], :flag, "Post coverage results to coveralls"
1718
option ["--simple-output", "-s"], :flag, "Output coverage results to the terminal"
@@ -57,6 +58,8 @@ Clamp do
5758
def setup_service_name
5859
if travis?
5960
project.ci_service = :travis_ci
61+
elsif circleci?
62+
project.ci_service = :circleci
6063
end
6164
end
6265

lib/slather/coverage_service/coveralls.rb

+40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ def travis_job_id
1111
ENV['TRAVIS_JOB_ID']
1212
end
1313
private :travis_job_id
14+
15+
def circleci_job_id
16+
ENV['CIRCLE_BUILD_NUM']
17+
end
18+
private :circleci_job_id
19+
20+
def circleci_pull_request
21+
ENV['CI_PULL_REQUEST']
22+
end
23+
private :circleci_pull_request
24+
25+
def circleci_git_info
26+
{
27+
:head => {
28+
:id => (ENV['CIRCLE_SHA1'] || ""),
29+
:author_name => (ENV['CIRCLE_USERNAME'] || ""),
30+
:message => (`git log --format=%B -n 1 HEAD`.chomp || "")
31+
},
32+
:branch => (ENV['CIRCLE_BRANCH'] || "")
33+
}
34+
end
35+
private :circleci_git_info
1436

1537
def coveralls_coverage_data
1638
if ci_service == :travis_ci || ci_service == :travis_pro
@@ -32,6 +54,24 @@ def coveralls_coverage_data
3254
else
3355
raise StandardError, "Environment variable `TRAVIS_JOB_ID` not set. Is this running on a travis build?"
3456
end
57+
elsif ci_service == :circleci
58+
if circleci_job_id
59+
coveralls_hash = {
60+
:service_job_id => circleci_job_id,
61+
:service_name => "circleci",
62+
:repo_token => ci_access_token,
63+
:source_files => coverage_files.map(&:as_json),
64+
:git => circleci_git_info
65+
}
66+
67+
if circleci_pull_request != nil && circleci_pull_request.length > 0
68+
coveralls_hash[:service_pull_request] = circleci_pull_request
69+
end
70+
71+
coveralls_hash.to_json
72+
else
73+
raise StandardError, "Environment variable `CIRCLE_BUILD_NUM` not set. Is this running on a circleci build?"
74+
end
3575
else
3676
raise StandardError, "No support for ci named #{ci_service}"
3777
end

spec/slather/coverage_service/coveralls_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@
5454
expect { fixtures_project.send(:coveralls_coverage_data) }.to raise_error(StandardError)
5555
end
5656
end
57+
58+
context "coverage_service is :circleci" do
59+
before(:each) { fixtures_project.ci_service = :circleci }
60+
61+
it "should return valid json for coveralls coverage data" do
62+
fixtures_project.stub(:circleci_job_id).and_return("9182")
63+
fixtures_project.stub(:ci_access_token).and_return("abc123")
64+
fixtures_project.stub(:circleci_pull_request).and_return("1")
65+
fixtures_project.stub(:circleci_git_info).and_return({ :head => { :id => "ababa123", :author_name => "bwayne", :message => "hello" }, :branch => "master" })
66+
expect(fixtures_project.send(:coveralls_coverage_data)).to be_json_eql("{\"service_job_id\":\"9182\",\"service_name\":\"circleci\",\"repo_token\":\"abc123\",\"service_pull_request\":\"1\",\"git\":{\"head\":{\"id\":\"ababa123\",\"author_name\":\"bwayne\",\"message\":\"hello\"},\"branch\":\"master\"}}").excluding("source_files")
67+
expect(fixtures_project.send(:coveralls_coverage_data)).to be_json_eql(fixtures_project.send(:coverage_files).map(&:as_json).to_json).at_path("source_files")
68+
end
69+
70+
it "should raise an error if there is no CIRCLE_BUILD_NUM" do
71+
fixtures_project.stub(:circleci_job_id).and_return(nil)
72+
expect { fixtures_project.send(:coveralls_coverage_data) }.to raise_error(StandardError)
73+
end
74+
end
5775

5876
it "should raise an error if it does not recognize the ci_service" do
5977
fixtures_project.ci_service = :jenkins_ci

0 commit comments

Comments
 (0)