Commit 677718fd authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'fix/cross-project-labels-project-reference' into 'master'

Fix cross-project label ref used with invalid project

When cross-project label reference was used with invalid project reference, `nil` was assigned to project variable which later caused an exception.

Closes #15168

See merge request !3811
parents 140eba0c 35d8bc44
...@@ -4,6 +4,7 @@ v 8.8.0 (unreleased) ...@@ -4,6 +4,7 @@ v 8.8.0 (unreleased)
v 8.7.0 (unreleased) v 8.7.0 (unreleased)
- The number of InfluxDB points stored per UDP packet can now be configured - The number of InfluxDB points stored per UDP packet can now be configured
- Fix error when cross-project label reference used with non-existent project
- Transactions for /internal/allowed now have an "action" tag set - Transactions for /internal/allowed now have an "action" tag set
- Method instrumentation now uses Module#prepend instead of aliasing methods - Method instrumentation now uses Module#prepend instead of aliasing methods
- Repository.clean_old_archives is now instrumented - Repository.clean_old_archives is now instrumented
......
...@@ -18,9 +18,7 @@ module Banzai ...@@ -18,9 +18,7 @@ module Banzai
def references_in(text, pattern = Label.reference_pattern) def references_in(text, pattern = Label.reference_pattern)
text.gsub(pattern) do |match| text.gsub(pattern) do |match|
project = project_from_ref($~[:project]) label = find_label($~[:project], $~[:label_id], $~[:label_name])
params = label_params($~[:label_id].to_i, $~[:label_name])
label = project.labels.find_by(params)
if label if label
yield match, label.id, $~[:project], $~ yield match, label.id, $~[:project], $~
...@@ -30,18 +28,12 @@ module Banzai ...@@ -30,18 +28,12 @@ module Banzai
end end
end end
def url_for_object(label, project) def find_label(project_ref, label_id, label_name)
h = Gitlab::Routing.url_helpers project = project_from_ref(project_ref)
h.namespace_project_issues_url(project.namespace, project, label_name: label.name, return unless project
only_path: context[:only_path])
end
def object_link_text(object, matches) label_params = label_params(label_id, label_name)
if context[:project] == object.project project.labels.find_by(label_params)
LabelsHelper.render_colored_label(object)
else
LabelsHelper.render_colored_cross_project_label(object)
end
end end
# Parameters to pass to `Label.find_by` based on the given arguments # Parameters to pass to `Label.find_by` based on the given arguments
...@@ -55,7 +47,21 @@ module Banzai ...@@ -55,7 +47,21 @@ module Banzai
if name if name
{ name: name.tr('"', '') } { name: name.tr('"', '') }
else else
{ id: id } { id: id.to_i }
end
end
def url_for_object(label, project)
h = Gitlab::Routing.url_helpers
h.namespace_project_issues_url(project.namespace, project, label_name: label.name,
only_path: context[:only_path])
end
def object_link_text(object, matches)
if context[:project] == object.project
LabelsHelper.render_colored_label(object)
else
LabelsHelper.render_colored_cross_project_label(object)
end end
end end
end end
......
...@@ -178,6 +178,7 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do ...@@ -178,6 +178,7 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do
end end
describe 'cross project label references' do describe 'cross project label references' do
context 'valid project referenced' do
let(:another_project) { create(:empty_project, :public) } let(:another_project) { create(:empty_project, :public) }
let(:project_name) { another_project.name_with_namespace } let(:project_name) { another_project.name_with_namespace }
let(:label) { create(:label, project: another_project, color: '#00ff00') } let(:label) { create(:label, project: another_project, color: '#00ff00') }
...@@ -201,4 +202,13 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do ...@@ -201,4 +202,13 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do
expect(result.css('a').first.text).to eq "#{label.name} in #{project_name}" expect(result.css('a').first.text).to eq "#{label.name} in #{project_name}"
end end
end end
context 'project that does not exist referenced' do
let(:result) { reference_filter('aaa/bbb~ccc') }
it 'does not link reference' do
expect(result.to_html).to eq 'aaa/bbb~ccc'
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