Commit 895fe163 authored by James Lopez's avatar James Lopez

Merge branch '12079-productivity-analytics-part-1' into 'master'

Backport EE changes for productivity analytics.

See merge request gitlab-org/gitlab-ce!32575
parents f8f8ed47 f089a230
...@@ -197,7 +197,7 @@ class MergeRequestDiff < ApplicationRecord ...@@ -197,7 +197,7 @@ class MergeRequestDiff < ApplicationRecord
def lines_count def lines_count
strong_memoize(:lines_count) do strong_memoize(:lines_count) do
diffs.diff_files.sum(&:line_count) raw_diffs(limits: false).line_count
end end
end end
...@@ -222,6 +222,10 @@ class MergeRequestDiff < ApplicationRecord ...@@ -222,6 +222,10 @@ class MergeRequestDiff < ApplicationRecord
commits.last commits.last
end end
def last_commit
commits.first
end
def base_commit def base_commit
return unless base_commit_sha return unless base_commit_sha
......
...@@ -81,6 +81,12 @@ module Gitlab ...@@ -81,6 +81,12 @@ module Gitlab
end end
end end
def line_count
populate!
@line_count
end
def decorate! def decorate!
collection = each_with_index do |element, i| collection = each_with_index do |element, i|
@array[i] = yield(element) @array[i] = yield(element)
......
...@@ -74,6 +74,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -74,6 +74,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
end end
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
context 'when limiting is disabled' do context 'when limiting is disabled' do
let(:limits) { false } let(:limits) { false }
...@@ -100,6 +105,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -100,6 +105,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
expect(subject.size).to eq(3) expect(subject.size).to eq(3)
end end
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
end end
end end
...@@ -120,6 +130,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -120,6 +130,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('0+') } it { is_expected.to eq('0+') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq 1000 }
end
it { expect(subject.size).to eq(0) } it { expect(subject.size).to eq(0) }
context 'when limiting is disabled' do context 'when limiting is disabled' do
...@@ -139,6 +155,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -139,6 +155,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('3') } it { is_expected.to eq('3') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(3) } it { expect(subject.size).to eq(3) }
end end
end end
...@@ -164,6 +186,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -164,6 +186,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('10+') } it { is_expected.to eq('10+') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq 10 }
end
it { expect(subject.size).to eq(10) } it { expect(subject.size).to eq(10) }
context 'when limiting is disabled' do context 'when limiting is disabled' do
...@@ -183,6 +211,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -183,6 +211,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('11') } it { is_expected.to eq('11') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(11) } it { expect(subject.size).to eq(11) }
end end
end end
...@@ -204,6 +238,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -204,6 +238,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('3+') } it { is_expected.to eq('3+') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq 120 }
end
it { expect(subject.size).to eq(3) } it { expect(subject.size).to eq(3) }
context 'when limiting is disabled' do context 'when limiting is disabled' do
...@@ -223,6 +263,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -223,6 +263,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('11') } it { is_expected.to eq('11') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(11) } it { expect(subject.size).to eq(11) }
end end
end end
...@@ -248,6 +294,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -248,6 +294,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('10') } it { is_expected.to eq('10') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(10) } it { expect(subject.size).to eq(10) }
end end
end end
...@@ -270,6 +322,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -270,6 +322,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('9+') } it { is_expected.to eq('9+') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(9) } it { expect(subject.size).to eq(9) }
context 'when limiting is disabled' do context 'when limiting is disabled' do
...@@ -289,6 +347,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -289,6 +347,12 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('10') } it { is_expected.to eq('10') }
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq file_count * line_count }
end
it { expect(subject.size).to eq(10) } it { expect(subject.size).to eq(10) }
end end
end end
...@@ -316,6 +380,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do ...@@ -316,6 +380,11 @@ describe Gitlab::Git::DiffCollection, :seed_helper do
subject { super().real_size } subject { super().real_size }
it { is_expected.to eq('0')} it { is_expected.to eq('0')}
end end
describe '#line_count' do
subject { super().line_count }
it { is_expected.to eq 0 }
end
end end
describe '#each' do describe '#each' do
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
require 'spec_helper' require 'spec_helper'
describe MergeRequest::Metrics do describe MergeRequest::Metrics do
subject { described_class.new }
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:merge_request) } it { is_expected.to belong_to(:merge_request) }
it { is_expected.to belong_to(:latest_closed_by).class_name('User') } it { is_expected.to belong_to(:latest_closed_by).class_name('User') }
......
...@@ -400,6 +400,18 @@ describe MergeRequestDiff do ...@@ -400,6 +400,18 @@ describe MergeRequestDiff do
end end
end end
describe '#first_commit' do
it 'returns first commit' do
expect(diff_with_commits.first_commit.sha).to eq(diff_with_commits.merge_request_diff_commits.last.sha)
end
end
describe '#last_commit' do
it 'returns last commit' do
expect(diff_with_commits.last_commit.sha).to eq(diff_with_commits.merge_request_diff_commits.first.sha)
end
end
describe '#commits_by_shas' do describe '#commits_by_shas' do
let(:commit_shas) { diff_with_commits.commit_shas } let(:commit_shas) { diff_with_commits.commit_shas }
...@@ -489,7 +501,7 @@ describe MergeRequestDiff do ...@@ -489,7 +501,7 @@ describe MergeRequestDiff do
subject { diff_with_commits } subject { diff_with_commits }
it 'returns sum of all changed lines count in diff files' do it 'returns sum of all changed lines count in diff files' do
expect(subject.lines_count).to eq 109 expect(subject.lines_count).to eq 189
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment