Commit 2ffe160d authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'rymai/gitlab-ce-3007-fix-mr-label-links' into 'master'

Fix label links for a merge request pointing to issues list

_Originally opened at !2150._

---

I have added a type parameter to the `link_to_label` helper method that is set to `'merge_request'` when in the context of a merge request.

I have also improved the specs to actually test the output of the method instead of its implementation.

Fixes #3007.

See merge request !2700
parents 42e0e357 6e79ce2e
...@@ -13,6 +13,7 @@ v 8.5.0 (unreleased) ...@@ -13,6 +13,7 @@ v 8.5.0 (unreleased)
set it up set it up
- Fix diff comments loaded by AJAX to load comment with diff in discussion tab - Fix diff comments loaded by AJAX to load comment with diff in discussion tab
- Whitelist raw "abbr" elements when parsing Markdown (Benedict Etzel) - Whitelist raw "abbr" elements when parsing Markdown (Benedict Etzel)
- Fix label links for a merge request pointing to issues list
- Don't vendor minified JS - Don't vendor minified JS
- Display 404 error on group not found - Display 404 error on group not found
- Track project import failure - Track project import failure
......
...@@ -7,6 +7,8 @@ module LabelsHelper ...@@ -7,6 +7,8 @@ module LabelsHelper
# project - Project object which will be used as the context for the label's # project - Project object which will be used as the context for the label's
# link. If omitted, defaults to `@project`, or the label's own # link. If omitted, defaults to `@project`, or the label's own
# project. # project.
# type - The type of item the link will point to (:issue or
# :merge_request). If omitted, defaults to :issue.
# block - An optional block that will be passed to `link_to`, forming the # block - An optional block that will be passed to `link_to`, forming the
# body of the link element. If omitted, defaults to # body of the link element. If omitted, defaults to
# `render_colored_label`. # `render_colored_label`.
...@@ -23,14 +25,19 @@ module LabelsHelper ...@@ -23,14 +25,19 @@ module LabelsHelper
# # Force the generated link to use a provided project # # Force the generated link to use a provided project
# link_to_label(label, project: Project.last) # link_to_label(label, project: Project.last)
# #
# # Force the generated link to point to merge requests instead of issues
# link_to_label(label, type: :merge_request)
#
# # Customize link body with a block # # Customize link body with a block
# link_to_label(label) { "My Custom Label Text" } # link_to_label(label) { "My Custom Label Text" }
# #
# Returns a String # Returns a String
def link_to_label(label, project: nil, &block) def link_to_label(label, project: nil, type: :issue, &block)
project ||= @project || label.project project ||= @project || label.project
link = namespace_project_issues_path(project.namespace, project, link = send("namespace_project_#{type.to_s.pluralize}_path",
label_name: label.name) project.namespace,
project,
label_name: label.name)
if block_given? if block_given?
link_to link, &block link_to link, &block
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
- if merge_request.labels.any? - if merge_request.labels.any?
   
- merge_request.labels.each do |label| - merge_request.labels.each do |label|
= link_to_label(label, project: merge_request.project) = link_to_label(label, project: merge_request.project, type: 'merge_request')
- if merge_request.tasks? - if merge_request.tasks?
   
%span.task-status %span.task-status
......
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
.value.issuable-show-labels .value.issuable-show-labels
- if issuable.labels.any? - if issuable.labels.any?
- issuable.labels.each do |label| - issuable.labels.each do |label|
= link_to_label(label) = link_to_label(label, type: issuable.to_ability_name)
- else - else
.light None .light None
.selectbox .selectbox
...@@ -129,4 +129,4 @@ ...@@ -129,4 +129,4 @@
:javascript :javascript
new Subscription("#{toggle_subscription_path(issuable)}"); new Subscription("#{toggle_subscription_path(issuable)}");
new IssuableContext(); new IssuableContext();
\ No newline at end of file
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe LabelsHelper do describe LabelsHelper do
describe 'link_to_label' do describe 'link_to_label' do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:label) { create(:label, project: project) } let(:label) { create(:label, project: project) }
context 'with @project set' do context 'with @project set' do
before do before do
...@@ -11,34 +11,31 @@ describe LabelsHelper do ...@@ -11,34 +11,31 @@ describe LabelsHelper do
end end
it 'uses the instance variable' do it 'uses the instance variable' do
expect(label).not_to receive(:project) expect(link_to_label(label)).to match %r{<a href="/#{@project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
link_to_label(label)
end end
end end
context 'without @project set' do context 'without @project set' do
it "uses the label's project" do it "uses the label's project" do
expect(label).to receive(:project).and_return(project) expect(link_to_label(label)).to match %r{<a href="/#{label.project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
link_to_label(label)
end end
end end
context 'with a named project argument' do context 'with a project argument' do
it 'uses the provided project' do let(:another_project) { double('project', namespace: 'foo3', to_param: 'bar3') }
arg = double('project')
expect(arg).to receive(:namespace).and_return('foo')
expect(arg).to receive(:to_param).and_return('foo')
link_to_label(label, project: arg) it 'links to merge requests page' do
expect(link_to_label(label, project: another_project)).to match %r{<a href="/foo3/bar3/issues\?label_name=#{label.name}">.*</a>}
end end
end
it 'takes precedence over other types' do context 'with a type argument' do
@project = project ['issue', :issue, 'merge_request', :merge_request].each do |type|
expect(@project).not_to receive(:namespace) context "set to #{type}" do
expect(label).not_to receive(:project) it 'links to correct page' do
expect(link_to_label(label, type: type)).to match %r{<a href="/#{label.project.to_reference}/#{type.to_s.pluralize}\?label_name=#{label.name}">.*</a>}
arg = double('project', namespace: 'foo', to_param: 'foo') end
link_to_label(label, project: arg) 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