Class: CodeClimate::CoverageReport

Inherits:
Object
  • Object
show all
Defined in:
app/models/code_climate/coverage_report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_run) ⇒ CoverageReport

Returns a new instance of CoverageReport.



31
32
33
34
# File 'app/models/code_climate/coverage_report.rb', line 31

def initialize(test_run)
  @project = test_run.project
  @test_run = test_run
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



36
37
38
# File 'app/models/code_climate/coverage_report.rb', line 36

def project
  @project
end

#test_runObject (readonly)

Returns the value of attribute test_run.



36
37
38
# File 'app/models/code_climate/coverage_report.rb', line 36

def test_run
  @test_run
end

Class Method Details

.publish!(test_run) ⇒ Object



27
28
29
# File 'app/models/code_climate/coverage_report.rb', line 27

def self.publish!(test_run)
  self.new(test_run).publish!
end

Instance Method Details

#blob_id_of(filename) ⇒ Object



168
169
170
171
# File 'app/models/code_climate/coverage_report.rb', line 168

def blob_id_of(filename)
  blob = project.repo.find_file(filename, commit: test_run.sha)
  blob && blob.oid
end

#ci_serviceObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/code_climate/coverage_report.rb', line 135

def ci_service
  case project.ci_server
  when Houston::Adapters::CIServer::JenkinsAdapter::Job
    {
      name:             "jenkins",
      build_identifier: test_run.results_url[/job\/#{project.slug}\/(\d+)/, 1],
      build_url:        test_run.results_url,
      branch:           test_run.branch,
      commit_sha:       test_run.sha
    }
  else
    {}
  end
end

#code_climate_payloadObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/code_climate/coverage_report.rb', line 51

def code_climate_payload
  {
    repo_token:         repo_token,
    source_files:       source_files,
    run_at:             run_at,
    covered_percent:    covered_percent,
    covered_strength:   covered_strength,
    line_counts:        line_counts,
    partial:            false,
    git:                commit_info,
    environment:        environment,
    ci_service:         ci_service
  }
end

#commit_infoObject



113
114
115
116
117
118
119
# File 'app/models/code_climate/coverage_report.rb', line 113

def commit_info
  {
    head:             test_run.sha,
    committed_at:     committed_at.to_i,
    branch:           test_run.branch,
  }
end

#committed_atObject



150
151
152
153
154
155
156
157
# File 'app/models/code_climate/coverage_report.rb', line 150

def committed_at
  # NB: CodeClimate actually uses committed_at
  return test_run.commit.authored_at if test_run.commit

  project.repo.native_commit(test_run.sha).committed_at
rescue Houston::Adapters::VersionControl::CommitNotFound
  nil
end

#covered_percentObject



95
96
97
# File 'app/models/code_climate/coverage_report.rb', line 95

def covered_percent
  (100 * test_run.covered_percent).round(2)
end

#covered_strengthObject



99
100
101
# File 'app/models/code_climate/coverage_report.rb', line 99

def covered_strength
  test_run.covered_strength.round(2)
end

#environmentObject



123
124
125
126
127
128
129
130
131
# File 'app/models/code_climate/coverage_report.rb', line 123

def environment
  {
    test_framework:   "rspec",  # result.command_name.downcase
    pwd:              Dir.pwd,  # Dir.pwd
    rails_root:       nil,      # (Rails.root.to_s rescue nil)
    simplecov_root:   Dir.pwd,  # ::SimpleCov.root
    gem_version:      CodeClimate::TestReporter::VERSION
  }
end

#line_countsObject



103
104
105
106
107
108
109
# File 'app/models/code_climate/coverage_report.rb', line 103

def line_counts
  @line_counts ||= source_files.each_with_object(Hash.new(0)) do |file, totals|
    totals[:total]   += file[:line_counts][:total]
    totals[:covered] += file[:line_counts][:covered]
    totals[:missed]  += file[:line_counts][:missed]
  end
end

#publish!Object



38
39
40
41
42
43
44
45
# File 'app/models/code_climate/coverage_report.rb', line 38

def publish!
  code_climate = CodeClimate::TestReporter::Client.new
  code_climate.post_results code_climate_payload
rescue RuntimeError
  # https://github.com/codeclimate/ruby-test-reporter/blob/v0.4.8/lib/code_climate/test_reporter/client.rb#L72
  $!.extend CodeClimate::ServerError if $!.message =~ /HTTP Error: 5\d\d/
  raise
end

#repo_tokenObject



68
69
70
# File 'app/models/code_climate/coverage_report.rb', line 68

def repo_token
  project.code_climate_repo_token
end

#run_atObject



91
92
93
# File 'app/models/code_climate/coverage_report.rb', line 91

def run_at
  test_run.completed_at.to_i
end

#short_filename(filename) ⇒ Object



162
163
164
# File 'app/models/code_climate/coverage_report.rb', line 162

def short_filename(filename)
  filename
end

#source_filesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/code_climate/coverage_report.rb', line 74

def source_files
  @source_files ||= test_run.coverage_detail.map do |file|
    {
      name:             short_filename(file.filename),
      blob_id:          blob_id_of(file.filename),
      coverage:         file.coverage.to_json,
      covered_percent:  file.covered_percent.round(2),
      covered_strength: file.covered_strength.round(2),
      line_counts: {
        total:          file.lines.count,
        covered:        file.covered_lines.count,
        missed:         file.missed_lines.count
      }
    }
  end
end