Commit acd877c7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add cross project references support for label model

parent 65ba4da9
...@@ -70,16 +70,20 @@ class Label < ActiveRecord::Base ...@@ -70,16 +70,20 @@ class Label < ActiveRecord::Base
# #
# Label.first.to_reference # => "~1" # Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\"" # Label.first.to_reference(format: :name) # => "~\"bug\""
# Label.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
# #
# Returns a String # Returns a String
def to_reference(_from_project = nil, format: :id) def to_reference(from_project = nil, format: :id)
if format == :name && !name.include?('"') reference = label_format_reference(format)
%(#{self.class.reference_prefix}"#{name}")
if cross_project_reference?(from_project)
project.to_reference + reference
else else
"#{self.class.reference_prefix}#{id}" reference
end end
end end
def open_issues_count def open_issues_count
issues.opened.count issues.opened.count
end end
...@@ -95,4 +99,16 @@ class Label < ActiveRecord::Base ...@@ -95,4 +99,16 @@ class Label < ActiveRecord::Base
def template? def template?
template template
end end
private
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
if format == :name && !name.include?('"')
%(#{self.class.reference_prefix}"#{name}")
else
"#{self.class.reference_prefix}#{id}"
end
end
end end
...@@ -59,7 +59,6 @@ describe Label, models: true do ...@@ -59,7 +59,6 @@ describe Label, models: true do
context 'using id' do context 'using id' do
it 'returns a String reference to the object' do it 'returns a String reference to the object' do
expect(label.to_reference).to eq "~#{label.id}" expect(label.to_reference).to eq "~#{label.id}"
expect(label.to_reference(double('project'))).to eq "~#{label.id}"
end end
end end
...@@ -73,5 +72,30 @@ describe Label, models: true do ...@@ -73,5 +72,30 @@ describe Label, models: true do
expect(label.to_reference(format: :name)).to eq "~#{label.id}" expect(label.to_reference(format: :name)).to eq "~#{label.id}"
end end
end end
context 'using invalid format' do
it 'raises error' do
expect { label.to_reference(format: :invalid) }
.to raise_error StandardError, /Unknown format/
end
end
context 'cross project reference' do
let(:project) { create(:project) }
context 'using name' do
it 'returns cross reference with label name' do
expect(label.to_reference(project, format: :name))
.to eq %Q(#{label.project.to_reference}~"#{label.name}")
end
end
context 'using id' do
it 'returns cross reference with label id' do
expect(label.to_reference(project, format: :id))
.to eq %Q(#{label.project.to_reference}~#{label.id})
end
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