class SimpleCov::S3::Codeclimate

Constants

VERSION

Public Class Methods

ensured(task_to_invoke) { || ... } click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 12
def self.ensured(task_to_invoke, &_block)
  Rake::Task[task_to_invoke].invoke
ensure
  begin
    yield
  rescue => exception
    puts exception.inspect
    puts exception.backtrace
  end
end
new(opts) click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 23
def initialize(opts)
  @connection = Fog::Storage.new(opts[:fog])
  @shared_secret = opts[:shared_secret]
  @bucket_name = opts[:bucket_name]
  @subfolder = opts[:subfolder] || ''
  @build_units = opts[:build_units].to_i
  @build_unit = opts[:build_unit]
  @build_id = opts[:build_id]
  @project_name = opts[:project_name] || 'unknown'
  @project_name = 'unknown' if @project_name.empty?
  @history_limit = opts[:history_limit] || 15
end

Public Instance Methods

pull_merge_and_push_full() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 47
def pull_merge_and_push_full
  json_files_size = json_files.size
  if json_files_size < @build_units
    puts "Expected to see #{@build_units} files"
    puts "currently only #{json_files_size}"
    require 'pp'
    pp json_files.map(&:key)
    puts 'Assuming some other build unit will be the last to exit, and merge/post full coverage'
    return false
  end
  puts "Coverage combined from #{json_files_size} units"
  results = []
  included_units = []
  json_files.each do |file|
    parsed = JSON.parse(file.body)
    included_units << parsed.delete('BUILD_UNIT')
    results << SimpleCov::Result.new(parsed)
  end
  puts "Included units: #{included_units.sort_by(&:to_s).inspect}"
  merged = {}
  results.each do |result|
    merged = result.original_result.merge_resultset(merged)
  end
  result = SimpleCov::Result.new(merged)
  CodeClimate::TestReporter::Formatter.new.format(result)
  cleanup_files
end
push_partial() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 36
def push_partial
  result = SimpleCov::ResultMerger.merged_result
  data_to_push = result.original_result.merge('BUILD_UNIT' => @build_unit).to_json
  put_file("#{@project_name}-#{commit_time}-coverageJSON/#{hashed_build_id}/#{@build_unit}", data_to_push)
rescue => exception
  puts exception.inspect
  puts exception.backtrace
  puts 'SOMEHTING WENT WRONG, PUSHING EMPTY COVERAGE FILE'
  put_file("#{@project_name}-#{commit_time}-coverageJSON/#{hashed_build_id}/#{@build_unit}", {}.to_json)
end

Private Instance Methods

cleanup_files() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 77
def cleanup_files
  json_files.each(&:destroy)
  all_files = @connection.directories.get(@bucket_name, prefix: @project_name).files
  return unless all_files.size > @history_limit
  all_files.sort_by(&:last_modified)[0, all_files.size - @history_limit].each do |file|
    puts "Cleaning up #{file.key}"
    file.destroy
  end
end
commit_time() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 97
def commit_time
  @commit_time ||= DateTime.parse(`git show | grep Date`.gsub('Date:', '').strip).strftime('%s')
end
hashed_build_id() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 101
def hashed_build_id
  @hashed_build_id ||= begin
    Digest::SHA1.hexdigest(@build_id + @shared_secret)
  end
end
json_files() click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 92
def json_files
  expected_dir = "#{@subfolder}/#{@project_name}-#{commit_time}-coverageJSON/#{hashed_build_id}/"
  @connection.directories.get(@bucket_name, prefix: expected_dir).files
end
put_file(path, data) click to toggle source
# File lib/simplecov_s3_codeclimate.rb, line 87
def put_file(path, data)
  puts "Putting to #{path} data size #{data.size}"
  @connection.directories.get(@bucket_name).files.create(key: "#{@subfolder}/#{path}", body: data, content_type: 'text/html')
end