Add cross project references support for label model
This commit is contained in:
parent
65ba4da925
commit
acd877c73e
2 changed files with 45 additions and 5 deletions
|
@ -70,16 +70,20 @@ class Label < ActiveRecord::Base
|
|||
#
|
||||
# Label.first.to_reference # => "~1"
|
||||
# Label.first.to_reference(format: :name) # => "~\"bug\""
|
||||
# Label.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
|
||||
#
|
||||
# Returns a String
|
||||
def to_reference(_from_project = nil, format: :id)
|
||||
if format == :name && !name.include?('"')
|
||||
%(#{self.class.reference_prefix}"#{name}")
|
||||
def to_reference(from_project = nil, format: :id)
|
||||
reference = label_format_reference(format)
|
||||
|
||||
if cross_project_reference?(from_project)
|
||||
project.to_reference + reference
|
||||
else
|
||||
"#{self.class.reference_prefix}#{id}"
|
||||
reference
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def open_issues_count
|
||||
issues.opened.count
|
||||
end
|
||||
|
@ -95,4 +99,16 @@ class Label < ActiveRecord::Base
|
|||
def template?
|
||||
template
|
||||
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
|
||||
|
|
|
@ -59,7 +59,6 @@ describe Label, models: true do
|
|||
context 'using id' do
|
||||
it 'returns a String reference to the object' do
|
||||
expect(label.to_reference).to eq "~#{label.id}"
|
||||
expect(label.to_reference(double('project'))).to eq "~#{label.id}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -73,5 +72,30 @@ describe Label, models: true do
|
|||
expect(label.to_reference(format: :name)).to eq "~#{label.id}"
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue