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.



29
30
31
32
# File 'app/models/code_climate/coverage_report.rb', line 29

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

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



34
35
36
# File 'app/models/code_climate/coverage_report.rb', line 34

def project
  @project
end

#test_runObject (readonly)

Returns the value of attribute test_run.



34
35
36
# File 'app/models/code_climate/coverage_report.rb', line 34

def test_run
  @test_run
end

Class Method Details

.publish!(test_run) ⇒ Object



25
26
27
# File 'app/models/code_climate/coverage_report.rb', line 25

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

Instance Method Details

#blob_id_of(filename) ⇒ Object



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

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

#ci_serviceObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/code_climate/coverage_report.rb', line 129

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/code_climate/coverage_report.rb', line 45

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



107
108
109
110
111
112
113
# File 'app/models/code_climate/coverage_report.rb', line 107

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

#committed_atObject



144
145
146
147
148
149
150
151
# File 'app/models/code_climate/coverage_report.rb', line 144

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



89
90
91
# File 'app/models/code_climate/coverage_report.rb', line 89

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

#covered_strengthObject



93
94
95
# File 'app/models/code_climate/coverage_report.rb', line 93

def covered_strength
  test_run.covered_strength.round(2)
end

#environmentObject



117
118
119
120
121
122
123
124
125
# File 'app/models/code_climate/coverage_report.rb', line 117

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



97
98
99
100
101
102
103
# File 'app/models/code_climate/coverage_report.rb', line 97

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



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

def publish!
  code_climate = CodeClimate::TestReporter::Client.new
  code_climate.post_results code_climate_payload
end

#repo_tokenObject



62
63
64
# File 'app/models/code_climate/coverage_report.rb', line 62

def repo_token
  project.code_climate_repo_token
end

#run_atObject



85
86
87
# File 'app/models/code_climate/coverage_report.rb', line 85

def run_at
  test_run.completed_at.to_i
end

#short_filename(filename) ⇒ Object



156
157
158
# File 'app/models/code_climate/coverage_report.rb', line 156

def short_filename(filename)
  filename
end

#source_filesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/code_climate/coverage_report.rb', line 68

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