Commit 7588d827 authored by Max Woolf's avatar Max Woolf

Merge branch 'refactor-version-index' into 'master'

Refactor to inline version_index to its only use

See merge request gitlab-org/gitlab!63851
parents 58fc35d5 b1e4d55d
...@@ -80,12 +80,6 @@ module MergeRequestsHelper ...@@ -80,12 +80,6 @@ module MergeRequestsHelper
diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff.id, start_sha: start_sha) diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff.id, start_sha: start_sha)
end end
def version_index(merge_request_diff)
return if @merge_request_diffs.empty?
@merge_request_diffs.size - @merge_request_diffs.index(merge_request_diff)
end
def merge_params(merge_request) def merge_params(merge_request)
{ {
auto_merge_strategy: AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS, auto_merge_strategy: AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS,
......
...@@ -6,13 +6,13 @@ class MergeRequestDiffEntity < Grape::Entity ...@@ -6,13 +6,13 @@ class MergeRequestDiffEntity < Grape::Entity
include MergeRequestsHelper include MergeRequestsHelper
expose :version_index do |merge_request_diff| expose :version_index do |merge_request_diff|
@merge_request_diffs = options[:merge_request_diffs] merge_request_diffs = options[:merge_request_diffs]
diff = options[:merge_request_diff] diff = options[:merge_request_diff]
next unless @merge_request_diffs.include?(diff) next unless merge_request_diffs.include?(diff)
next unless @merge_request_diffs.size > 1 next unless merge_request_diffs.size > 1
version_index(merge_request_diff) merge_request_diffs.size - merge_request_diffs.index(merge_request_diff)
end end
expose :created_at expose :created_at
......
...@@ -341,6 +341,11 @@ FactoryBot.define do ...@@ -341,6 +341,11 @@ FactoryBot.define do
create(:diff_note_on_merge_request, noteable: mr, project: mr.source_project) create(:diff_note_on_merge_request, noteable: mr, project: mr.source_project)
end end
end end
factory :merge_request_with_multiple_diffs do
after(:create) do |mr|
mr.merge_request_diffs.create!(head_commit_sha: '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9')
end
end
factory :labeled_merge_request do factory :labeled_merge_request do
transient do transient do
......
...@@ -10,7 +10,9 @@ RSpec.describe MergeRequestDiffEntity do ...@@ -10,7 +10,9 @@ RSpec.describe MergeRequestDiffEntity do
let(:merge_request_diffs) { merge_request.merge_request_diffs } let(:merge_request_diffs) { merge_request.merge_request_diffs }
let(:merge_request_diff) { merge_request_diffs.first } let(:merge_request_diff) { merge_request_diffs.first }
let(:entity) do let(:entity) { initialize_entity(merge_request, merge_request_diff) }
def initialize_entity(merge_request, merge_request_diff)
described_class.new( described_class.new(
merge_request_diff, merge_request_diff,
request: request, request: request,
...@@ -70,6 +72,22 @@ RSpec.describe MergeRequestDiffEntity do ...@@ -70,6 +72,22 @@ RSpec.describe MergeRequestDiffEntity do
it_behaves_like 'version_index is nil' it_behaves_like 'version_index is nil'
end end
context 'when @merge_request_diffs.size > 1' do
let(:merge_request) { create(:merge_request_with_multiple_diffs) }
it 'returns difference between size and diff index' do
expect(merge_request_diffs.size).to eq(2)
# diff index: 0
subject = initialize_entity(merge_request, merge_request_diffs.first)
expect(subject.as_json[:version_index]).to eq(2)
# diff index: 1
subject = initialize_entity(merge_request, merge_request_diffs.last)
expect(subject.as_json[:version_index]).to eq(1)
end
end
end end
describe '#short_commit_sha' do describe '#short_commit_sha' do
......
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