Commit 2d1c67c2 authored by Shinya Maeda's avatar Shinya Maeda Committed by Ash McKenzie

Expose merge train information

Exposed data will be rendered in merge request widgets
parent 4eb4ff3e
...@@ -14,6 +14,10 @@ class MergeTrain < ApplicationRecord ...@@ -14,6 +14,10 @@ class MergeTrain < ApplicationRecord
all_in_train(merge_request).first all_in_train(merge_request).first
end end
def total_count_in_train(merge_request)
all_in_train(merge_request).count
end
def joined_merge_requests(merge_request) def joined_merge_requests(merge_request)
MergeRequest.joins(:merge_train) MergeRequest.joins(:merge_train)
.where('merge_requests.target_project_id = ?', merge_request.target_project_id) .where('merge_requests.target_project_id = ?', merge_request.target_project_id)
...@@ -25,6 +29,10 @@ class MergeTrain < ApplicationRecord ...@@ -25,6 +29,10 @@ class MergeTrain < ApplicationRecord
self.class.all_in_train(merge_request).where('merge_trains.id > ?', id) self.class.all_in_train(merge_request).where('merge_trains.id > ?', id)
end end
def index
self.class.all_in_train(merge_request).where('merge_trains.id < ?', id).count
end
def first_in_train? def first_in_train?
!follower_in_train? !follower_in_train?
end end
......
...@@ -136,7 +136,15 @@ module EE ...@@ -136,7 +136,15 @@ module EE
end end
expose :merge_trains_enabled?, as: :merge_trains_enabled do |merge_request| expose :merge_trains_enabled?, as: :merge_trains_enabled do |merge_request|
merge_request.target_project.merge_trains_enabled? merge_trains_enabled?
end
expose :merge_trains_count, if: -> (*) { merge_trains_enabled? } do |merge_request|
MergeTrain.total_count_in_train(merge_request)
end
expose :merge_train_index, if: -> (merge_request) { merge_request.on_train? } do |merge_request|
merge_request.merge_train.index
end end
expose :can_push_to_source_branch do |merge_request| expose :can_push_to_source_branch do |merge_request|
...@@ -176,5 +184,9 @@ module EE ...@@ -176,5 +184,9 @@ module EE
object.base_pipeline&.present(current_user: current_user) object.base_pipeline&.present(current_user: current_user)
&.downloadable_path_for_report_type(file_type) &.downloadable_path_for_report_type(file_type)
end end
def merge_trains_enabled?
object.target_project.merge_trains_enabled?
end
end end
end end
...@@ -63,6 +63,32 @@ describe MergeTrain do ...@@ -63,6 +63,32 @@ describe MergeTrain do
end end
end end
describe '.total_count_in_train' do
subject { described_class.total_count_in_train(merge_request) }
let!(:merge_request) { create_merge_request_on_train }
it 'returns the merge request' do
is_expected.to eq(1)
end
context 'when the other merge request is on the merge train' do
let!(:merge_request_2) { create_merge_request_on_train(source_branch: 'improve/awesome') }
it 'returns the merge request' do
is_expected.to eq(2)
end
end
context 'when the merge request is not on merge train' do
let(:merge_request) { create(:merge_request) }
it 'returns empty array' do
is_expected.to be(0)
end
end
end
describe '#all_next' do describe '#all_next' do
subject { merge_train.all_next } subject { merge_train.all_next }
...@@ -114,6 +140,22 @@ describe MergeTrain do ...@@ -114,6 +140,22 @@ describe MergeTrain do
end end
end end
describe '#index' do
subject { merge_train.index }
let(:merge_train) { merge_request.merge_train }
let!(:merge_request) { create_merge_request_on_train }
it { is_expected.to eq(0) }
context 'when the merge train is at the second queue' do
let(:merge_train) { merge_request_2.merge_train }
let!(:merge_request_2) { create_merge_request_on_train(source_branch: 'improve/awesome') }
it { is_expected.to eq(1) }
end
end
def create_merge_request_on_train(target_project: project, target_branch: 'master', source_project: project, source_branch: 'feature') def create_merge_request_on_train(target_project: project, target_branch: 'master', source_project: project, source_branch: 'feature')
create(:merge_request, create(:merge_request,
:on_train, :on_train,
......
...@@ -195,7 +195,36 @@ describe MergeRequestWidgetEntity do ...@@ -195,7 +195,36 @@ describe MergeRequestWidgetEntity do
expect(subject.as_json).to include(:pipeline_id) expect(subject.as_json).to include(:pipeline_id)
end end
it 'has merge trains flag' do describe 'Merge Trains' do
let!(:merge_train) { create(:merge_train, merge_request: merge_request) }
before do
stub_licensed_features(merge_pipelines: true, merge_trains: true)
project.update!(merge_trains_enabled: true, merge_pipelines_enabled: true)
end
it 'has merge train entity' do
expect(subject.as_json).to include(:merge_trains_enabled) expect(subject.as_json).to include(:merge_trains_enabled)
expect(subject.as_json).to include(:merge_trains_count)
expect(subject.as_json).to include(:merge_train_index)
end
context 'when the merge train feature is disabled' do
before do
project.update!(merge_trains_enabled: false)
end
it 'does not have merge trains count' do
expect(subject.as_json).not_to include(:merge_trains_count)
end
end
context 'when the merge request is not on a merge train' do
let!(:merge_train) { }
it 'does not have merge train index' do
expect(subject.as_json).not_to include(:merge_train_index)
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