Commit 410dc9d1 authored by drew's avatar drew Committed by Grzegorz Bizon

Add PipelinePresenter#related_merge_request_text

parent 48b2f366
......@@ -746,6 +746,10 @@ module Ci
end
end
def all_merge_requests_by_recency
all_merge_requests.order(id: :desc)
end
def detailed_status(current_user)
Gitlab::Ci::Status::Pipeline::Factory
.new(self, current_user)
......
......@@ -48,15 +48,35 @@ module Ci
def ref_text
if pipeline.detached_merge_request_pipeline?
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch}").html_safe % { link_to_merge_request: link_to_merge_request, link_to_merge_request_source_branch: link_to_merge_request_source_branch }
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch}")
.html_safe % {
link_to_merge_request: link_to_merge_request,
link_to_merge_request_source_branch: link_to_merge_request_source_branch
}
elsif pipeline.merge_request_pipeline?
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch} into %{link_to_merge_request_target_branch}").html_safe % { link_to_merge_request: link_to_merge_request, link_to_merge_request_source_branch: link_to_merge_request_source_branch, link_to_merge_request_target_branch: link_to_merge_request_target_branch }
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch} into %{link_to_merge_request_target_branch}")
.html_safe % {
link_to_merge_request: link_to_merge_request,
link_to_merge_request_source_branch: link_to_merge_request_source_branch,
link_to_merge_request_target_branch: link_to_merge_request_target_branch
}
elsif pipeline.ref && pipeline.ref_exists?
_("for %{link_to_pipeline_ref}")
.html_safe % { link_to_pipeline_ref: link_to_pipeline_ref }
elsif pipeline.ref
if pipeline.ref_exists?
_("for %{link_to_pipeline_ref}").html_safe % { link_to_pipeline_ref: link_to_pipeline_ref }
else
_("for %{ref}").html_safe % { ref: content_tag(:span, pipeline.ref, class: 'ref-name') }
_("for %{ref}").html_safe % { ref: plain_ref_name }
end
end
def all_related_merge_request_text
if all_related_merge_requests.none?
'No related merge requests found.'
else
_("%{count} related %{pluralized_subject}: %{links}" % {
count: all_related_merge_requests.count,
pluralized_subject: 'merge request'.pluralize(all_related_merge_requests.count),
links: all_related_merge_request_links.join(', ')
}).html_safe
end
end
......@@ -84,10 +104,30 @@ module Ci
private
def plain_ref_name
content_tag(:span, pipeline.ref, class: 'ref-name')
end
def merge_request_presenter
return unless pipeline.triggered_by_merge_request?
strong_memoize(:merge_request_presenter) do
if pipeline.triggered_by_merge_request?
pipeline.merge_request.present(current_user: current_user)
end
end
end
@merge_request_presenter ||= pipeline.merge_request.present(current_user: current_user)
def all_related_merge_request_links
all_related_merge_requests.map do |merge_request|
mr_path = project_merge_request_path(merge_request.project, merge_request)
link_to "#{merge_request.to_reference} #{merge_request.title}", mr_path, class: 'mr-iid'
end
end
def all_related_merge_requests
strong_memoize(:all_related_merge_requests) do
pipeline.ref ? pipeline.all_merge_requests_by_recency.to_a : []
end
end
end
end
......
---
title: Use MR links in PipelinePresenter#ref_text for branch pipelines
merge_request: 17947
author:
type: changed
......@@ -223,6 +223,9 @@ msgid_plural "%{count} pending comments"
msgstr[0] ""
msgstr[1] ""
msgid "%{count} related %{pluralized_subject}: %{links}"
msgstr ""
msgid "%{duration}ms"
msgstr ""
......
......@@ -176,6 +176,44 @@ describe Ci::PipelinePresenter do
end
end
describe '#all_related_merge_request_text' do
subject { presenter.all_related_merge_request_text }
context 'with zero related merge requests (branch pipeline)' do
it { is_expected.to eq('No related merge requests found.') }
end
context 'with one related merge request' do
let!(:mr_1) { create(:merge_request, project: project, source_project: project) }
it {
is_expected.to eq("1 related merge request: " \
"<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
}
end
context 'with two related merge requests' do
let!(:mr_1) { create(:merge_request, project: project, source_project: project, target_branch: 'staging') }
let!(:mr_2) { create(:merge_request, project: project, source_project: project, target_branch: 'feature') }
it {
is_expected.to eq("2 related merge requests: " \
"<a class=\"mr-iid\" href=\"#{merge_request_path(mr_2)}\">#{mr_2.to_reference} #{mr_2.title}</a>, " \
"<a class=\"mr-iid\" href=\"#{merge_request_path(mr_1)}\">#{mr_1.to_reference} #{mr_1.title}</a>")
}
end
end
describe '#all_related_merge_requests' do
it 'memoizes the returned relation' do
query_count = ActiveRecord::QueryRecorder.new do
2.times { presenter.send(:all_related_merge_requests).count }
end.count
expect(query_count).to eq(1)
end
end
describe '#link_to_merge_request' do
subject { presenter.link_to_merge_request }
......
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