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

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 de25604f
......@@ -110,6 +110,28 @@ module GitlabMarkdownHelper
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 @@ module GitlabMarkdownHelper
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 @@ class Commit
@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 @@ class Commit
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 @@ class CommitRange
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 @@ class ExternalIssue
@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 @@ class Group < Namespace
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 @@ class Issue < ActiveRecord::Base
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 @@ class Label < ActiveRecord::Base
#
# 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 @@ class MergeRequest < ActiveRecord::Base
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 @@ class Milestone < ActiveRecord::Base
# 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 @@ class Project < ActiveRecord::Base
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 @@ class ProjectLabel < Label
'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 @@ class Snippet < ActiveRecord::Base
@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 @@ class User < ActiveRecord::Base
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 @@ module Gitlab
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 @@ describe GitlabMarkdownHelper do
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 @@ describe GroupLabel, models: true do
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 @@ describe Issue, models: true do
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"
......
......@@ -153,6 +153,10 @@ describe MergeRequest, models: true do
another_project = build(:project, name: 'another-project', namespace: project.namespace)
expect(merge_request.to_reference(another_project)).to eq "sample-project!1"
end
it 'returns a String reference with the full path' do
expect(merge_request.to_reference(full: true)).to eq(project.path_with_namespace + '!1')
end
end
describe '#raw_diffs' do
......
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