Last active
February 18, 2018 18:54
-
-
Save vanwhale/f74879e0549b67eb17bb to your computer and use it in GitHub Desktop.
circleci-parallel-codeclimate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "codeclimate-test-reporter" | |
branch = ENV['CIRCLE_BRANCH'] | |
node_index = ENV['CIRCLE_NODE_INDEX'].to_i | |
node_total = ENV['CIRCLE_NODE_TOTAL'].to_i | |
artifacts_dir = ENV['CIRCLE_ARTIFACTS'] | |
coverage_dir = File.join("..", "..", "..", ENV['CIRCLE_ARTIFACTS'], "coverage") | |
filename = '.resultset.json' | |
SimpleCov.coverage_dir(coverage_dir) | |
# Only run on node0 | |
exit if !node_index.zero? | |
if node_total > 0 | |
# Copy coverage results from all nodes to circle artifacts directory | |
1.upto(node_total-1) do |i| | |
node = "node#{i}" | |
node_artifacts_dir = `ssh #{node} 'printf $CIRCLE_ARTIFACTS'` | |
from = File.join(node_artifacts_dir, 'coverage', filename) | |
to = File.join(coverage_dir, "#{i}#{filename}") | |
command = "scp #{node}:#{from} #{to}" | |
puts "running command: #{command}" | |
`#{command}` | |
end | |
end | |
# Merge coverage results from other nodes | |
# .resultset.json is a hidden file and thus ignored by the glob | |
files = Dir.glob(File.join(coverage_dir, "*#{filename}")) | |
files.each_with_index do |file, i| | |
resultset = JSON.load(File.read(file)) | |
resultset.each do |command_name, data| | |
result = SimpleCov::Result.from_hash(['command', i].join => data) | |
SimpleCov::ResultMerger.store_result(result) | |
end | |
end | |
merged_result = SimpleCov::ResultMerger.merged_result | |
merged_result.command_name = 'RSpec' | |
# Format merged result with html | |
html_formatter = SimpleCov::Formatter::HTMLFormatter.new | |
html_formatter.format(merged_result) | |
# Only submit coverage to codeclimate on master branch | |
exit if branch != 'master' | |
# Post merged coverage result to codeclimate | |
codeclimate_formatter = CodeClimate::TestReporter::Formatter.new | |
codeclimate_formatter.format(merged_result) |
Thanks for this! It was very helpful!
👍 nice.
Thanks for this!
I had to make a slight change to the script to get it to work with 0.4.8
version of codeclimate-test-reporter
. When formatting and posting final results to Code Climate I had to do as follows:
# Post merged coverage result to codeclimate
formatter = CodeClimate::TestReporter::Formatter.new
formatted_results = formatter.format(merged_result.to_hash)
CodeClimate::TestReporter::PostResults.new(formatted_results).post
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any reason to not move that to the top?