Commit 112f4705 authored by James Lopez's avatar James Lopez
Browse files

Fix cross-project references copy to include the project reference

Also added relevant specs and refactored to_references in a bunch of places to be more consistent.
parent 240c2d3d929b
......@@ -110,6 +110,28 @@ def render_wiki_content(wiki_page)
end
end
# Returns the text necessary to reference `entity` across projects
#
# project - Project to reference
# entity - Object that responds to `to_reference`
#
# Examples:
#
# cross_project_reference(project, project.issues.first)
# # => 'namespace1/project1#123'
#
# cross_project_reference(project, project.merge_requests.first)
# # => 'namespace1/project1!345'
#
# Returns a String
def cross_project_reference(project, entity)
if entity.respond_to?(:to_reference)
entity.to_reference(project, full: true)
else
''
end
end
private
# Return +text+, truncated to +max_chars+ characters, excluding any HTML
......@@ -158,28 +180,6 @@ def truncate_if_block(node, truncated)
end
end
# Returns the text necessary to reference `entity` across projects
#
# project - Project to reference
# entity - Object that responds to `to_reference`
#
# Examples:
#
# cross_project_reference(project, project.issues.first)
# # => 'namespace1/project1#123'
#
# cross_project_reference(project, project.merge_requests.first)
# # => 'namespace1/project1!345'
#
# Returns a String
def cross_project_reference(project, entity)
if entity.respond_to?(:to_reference)
entity.to_reference(project)
else
''
end
end
def markdown_toolbar_button(options = {})
data = options[:data].merge({ container: "body" })
content_tag :button,
......
......@@ -91,8 +91,8 @@ def self.link_reference_pattern
@link_reference_pattern ||= super("commit", /(?<commit>\h{7,40})/)
end
def to_reference(from_project = nil)
commit_reference(from_project, id)
def to_reference(from_project = nil, full: false)
commit_reference(from_project, id, full: full)
end
def reference_link_text(from_project = nil)
......@@ -320,8 +320,8 @@ def diffs(diff_options = nil)
private
def commit_reference(from_project, referable_commit_id)
reference = project.to_reference(from_project)
def commit_reference(from_project, referable_commit_id, full: false)
reference = project.to_reference(from_project, full: full)
if reference.present?
"#{reference}#{self.class.reference_prefix}#{referable_commit_id}"
......
......@@ -89,8 +89,8 @@ def to_s
alias_method :id, :to_s
def to_reference(from_project = nil)
project_reference = project.to_reference(from_project)
def to_reference(from_project = nil, full: false)
project_reference = project.to_reference(from_project, full: full)
if project_reference.present?
project_reference + self.class.reference_prefix + self.id
......
......@@ -17,7 +17,7 @@ module Referable
# Issue.last.to_reference(other_project) # => "cross-project#1"
#
# Returns a String
def to_reference(_from_project = nil)
def to_reference(_from_project = nil, full:)
''
end
......
......@@ -38,7 +38,7 @@ def self.reference_pattern
@reference_pattern ||= %r{(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)}
end
def to_reference(_from_project = nil)
def to_reference(_from_project = nil, full: nil)
id
end
......
......@@ -80,7 +80,7 @@ def select_for_project_authorization
end
end
def to_reference(_from_project = nil)
def to_reference(_from_project = nil, full: nil)
"#{self.class.reference_prefix}#{name}"
end
......
......@@ -8,8 +8,4 @@ class GroupLabel < Label
def subject_foreign_key
'group_id'
end
def to_reference(source_project = nil, target_project = nil, format: :id)
super(source_project, target_project, format: format)
end
end
......@@ -97,10 +97,10 @@ def self.sort(method, excluded_labels: [])
end
end
def to_reference(from_project = nil)
def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project)}#{reference}"
"#{project.to_reference(from_project, full: full)}#{reference}"
end
def referenced_merge_requests(current_user = nil)
......
......@@ -146,17 +146,17 @@ def title=(value)
#
# Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\""
# Label.first.to_reference(project, same_namespace_project) # => "gitlab-ce~1"
# Label.first.to_reference(project, another_namespace_project) # => "gitlab-org/gitlab-ce~1"
# Label.first.to_reference(project, target_project: same_namespace_project) # => "gitlab-ce~1"
# Label.first.to_reference(project, target_project: another_namespace_project) # => "gitlab-org/gitlab-ce~1"
#
# Returns a String
#
def to_reference(source_project = nil, target_project = nil, format: :id)
def to_reference(from_project = nil, target_project: nil, format: :id, full: false)
format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
if source_project
"#{source_project.to_reference(target_project)}#{reference}"
if from_project
"#{from_project.to_reference(target_project, full: full)}#{reference}"
else
reference
end
......
......@@ -175,10 +175,10 @@ def self.wip_title(title)
work_in_progress?(title) ? title : "WIP: #{title}"
end
def to_reference(from_project = nil)
def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project)}#{reference}"
"#{project.to_reference(from_project, full: full)}#{reference}"
end
def first_commit
......
......@@ -118,11 +118,11 @@ def self.upcoming_ids_by_projects(projects)
# Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-ce%1"
# Milestone.first.to_reference(same_namespace_project) # => "gitlab-ce%1"
#
def to_reference(from_project = nil, format: :iid)
def to_reference(from_project = nil, format: :iid, full: false)
format_reference = milestone_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
"#{project.to_reference(from_project)}#{reference}"
"#{project.to_reference(from_project, full: full)}#{reference}"
end
def reference_link_text(from_project = nil)
......
......@@ -589,8 +589,8 @@ def to_param
end
end
def to_reference(from_project = nil)
if cross_namespace_reference?(from_project)
def to_reference(from_project = nil, full: false)
if full || cross_namespace_reference?(from_project)
path_with_namespace
elsif cross_project_reference?(from_project)
path
......
......@@ -16,8 +16,8 @@ def subject_foreign_key
'project_id'
end
def to_reference(target_project = nil, format: :id)
super(project, target_project, format: format)
def to_reference(target_project = nil, format: :id, full: false)
super(project, target_project: target_project, format: format, full: full)
end
private
......
......@@ -64,11 +64,11 @@ def self.link_reference_pattern
@link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
end
def to_reference(from_project = nil)
def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{id}"
if project.present?
"#{project.to_reference(from_project)}#{reference}"
"#{project.to_reference(from_project, full: full)}#{reference}"
else
reference
end
......
......@@ -332,7 +332,7 @@ def to_param
username
end
def to_reference(_from_project = nil, _target_project = nil)
def to_reference(_from_project = nil, target_project: nil, full: nil)
"#{self.class.reference_prefix}#{username}"
end
......
---
title: Fix cross-project references copy to include the project reference
merge_request:
author:
......@@ -76,7 +76,7 @@ def build_cross_reference(referable, target_project)
if referable.respond_to?(:project)
referable.to_reference(target_project)
else
referable.to_reference(@source_project, target_project)
referable.to_reference(@source_project, target_project: target_project)
end
end
......
......@@ -170,4 +170,14 @@
expect(doc.content).to eq "@#{user.username}, can you look at this?..."
end
end
describe '#cross_project_reference' do
it 'shows the full MR reference' do
expect(helper.cross_project_reference(project, merge_request)).to include(project.path_with_namespace)
end
it 'shows the full issue reference' do
expect(helper.cross_project_reference(project, issue)).to include(project.path_with_namespace)
end
end
end
......@@ -43,7 +43,7 @@
let(:target_project) { build_stubbed(:empty_project, name: 'project-2', namespace: namespace) }
it 'returns a String reference to the object' do
expect(label.to_reference(source_project, target_project)).to eq %(project-1~#{label.id})
expect(label.to_reference(source_project, target_project: target_project)).to eq %(project-1~#{label.id})
end
end
......
......@@ -30,6 +30,10 @@
expect(issue.to_reference).to eq "#1"
end
it 'returns a String reference with the full path' do
expect(issue.to_reference(full: true)).to eq(project.path_with_namespace + '#1')
end
it 'supports a cross-project reference' do
another_project = build(:project, name: 'another-project', namespace: project.namespace)
expect(issue.to_reference(another_project)).to eq "sample-project#1"
......
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