Commit 5b158b0b authored by Sean McGivern's avatar Sean McGivern Committed by Douglas Barbosa Alexandre

Merge branch 'fix/cross-project-ref-path' into 'master'

Fix cross-project references copy to include the project reference

Closes #25890

See merge request !8232
parent 4b19c1ac
...@@ -110,6 +110,28 @@ module GitlabMarkdownHelper ...@@ -110,6 +110,28 @@ module GitlabMarkdownHelper
end end
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 private
# Return +text+, truncated to +max_chars+ characters, excluding any HTML # Return +text+, truncated to +max_chars+ characters, excluding any HTML
...@@ -158,28 +180,6 @@ module GitlabMarkdownHelper ...@@ -158,28 +180,6 @@ module GitlabMarkdownHelper
end end
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 = {}) def markdown_toolbar_button(options = {})
data = options[:data].merge({ container: "body" }) data = options[:data].merge({ container: "body" })
content_tag :button, content_tag :button,
......
...@@ -91,8 +91,8 @@ class Commit ...@@ -91,8 +91,8 @@ class Commit
@link_reference_pattern ||= super("commit", /(?<commit>\h{7,40})/) @link_reference_pattern ||= super("commit", /(?<commit>\h{7,40})/)
end end
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
commit_reference(from_project, id) commit_reference(from_project, id, full: full)
end end
def reference_link_text(from_project = nil) def reference_link_text(from_project = nil)
...@@ -320,8 +320,8 @@ class Commit ...@@ -320,8 +320,8 @@ class Commit
private private
def commit_reference(from_project, referable_commit_id) def commit_reference(from_project, referable_commit_id, full: false)
reference = project.to_reference(from_project) reference = project.to_reference(from_project, full: full)
if reference.present? if reference.present?
"#{reference}#{self.class.reference_prefix}#{referable_commit_id}" "#{reference}#{self.class.reference_prefix}#{referable_commit_id}"
......
...@@ -89,8 +89,8 @@ class CommitRange ...@@ -89,8 +89,8 @@ class CommitRange
alias_method :id, :to_s alias_method :id, :to_s
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
project_reference = project.to_reference(from_project) project_reference = project.to_reference(from_project, full: full)
if project_reference.present? if project_reference.present?
project_reference + self.class.reference_prefix + self.id project_reference + self.class.reference_prefix + self.id
......
...@@ -17,7 +17,7 @@ module Referable ...@@ -17,7 +17,7 @@ module Referable
# Issue.last.to_reference(other_project) # => "cross-project#1" # Issue.last.to_reference(other_project) # => "cross-project#1"
# #
# Returns a String # Returns a String
def to_reference(_from_project = nil) def to_reference(_from_project = nil, full:)
'' ''
end end
......
...@@ -38,7 +38,7 @@ class ExternalIssue ...@@ -38,7 +38,7 @@ class ExternalIssue
@reference_pattern ||= %r{(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)} @reference_pattern ||= %r{(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)}
end end
def to_reference(_from_project = nil) def to_reference(_from_project = nil, full: nil)
id id
end end
......
...@@ -74,7 +74,7 @@ class Group < Namespace ...@@ -74,7 +74,7 @@ class Group < Namespace
end end
end end
def to_reference(_from_project = nil) def to_reference(_from_project = nil, full: nil)
"#{self.class.reference_prefix}#{name}" "#{self.class.reference_prefix}#{name}"
end end
......
...@@ -8,8 +8,4 @@ class GroupLabel < Label ...@@ -8,8 +8,4 @@ class GroupLabel < Label
def subject_foreign_key def subject_foreign_key
'group_id' 'group_id'
end end
def to_reference(source_project = nil, target_project = nil, format: :id)
super(source_project, target_project, format: format)
end
end end
...@@ -97,10 +97,10 @@ class Issue < ActiveRecord::Base ...@@ -97,10 +97,10 @@ class Issue < ActiveRecord::Base
end end
end end
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{iid}" reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project)}#{reference}" "#{project.to_reference(from_project, full: full)}#{reference}"
end end
def referenced_merge_requests(current_user = nil) def referenced_merge_requests(current_user = nil)
......
...@@ -146,17 +146,17 @@ class Label < ActiveRecord::Base ...@@ -146,17 +146,17 @@ 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, same_namespace_project) # => "gitlab-ce~1" # Label.first.to_reference(project, target_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: another_namespace_project) # => "gitlab-org/gitlab-ce~1"
# #
# Returns a String # 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) format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}" reference = "#{self.class.reference_prefix}#{format_reference}"
if source_project if from_project
"#{source_project.to_reference(target_project)}#{reference}" "#{from_project.to_reference(target_project, full: full)}#{reference}"
else else
reference reference
end end
......
...@@ -175,10 +175,10 @@ class MergeRequest < ActiveRecord::Base ...@@ -175,10 +175,10 @@ class MergeRequest < ActiveRecord::Base
work_in_progress?(title) ? title : "WIP: #{title}" work_in_progress?(title) ? title : "WIP: #{title}"
end end
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{iid}" reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project)}#{reference}" "#{project.to_reference(from_project, full: full)}#{reference}"
end end
def first_commit def first_commit
......
...@@ -118,11 +118,11 @@ class Milestone < ActiveRecord::Base ...@@ -118,11 +118,11 @@ class Milestone < ActiveRecord::Base
# Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-ce%1" # Milestone.first.to_reference(cross_namespace_project) # => "gitlab-org/gitlab-ce%1"
# Milestone.first.to_reference(same_namespace_project) # => "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) format_reference = milestone_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}" reference = "#{self.class.reference_prefix}#{format_reference}"
"#{project.to_reference(from_project)}#{reference}" "#{project.to_reference(from_project, full: full)}#{reference}"
end end
def reference_link_text(from_project = nil) def reference_link_text(from_project = nil)
......
...@@ -584,8 +584,8 @@ class Project < ActiveRecord::Base ...@@ -584,8 +584,8 @@ class Project < ActiveRecord::Base
end end
end end
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
if cross_namespace_reference?(from_project) if full || cross_namespace_reference?(from_project)
path_with_namespace path_with_namespace
elsif cross_project_reference?(from_project) elsif cross_project_reference?(from_project)
path path
......
...@@ -16,8 +16,8 @@ class ProjectLabel < Label ...@@ -16,8 +16,8 @@ class ProjectLabel < Label
'project_id' 'project_id'
end end
def to_reference(target_project = nil, format: :id) def to_reference(target_project = nil, format: :id, full: false)
super(project, target_project, format: format) super(project, target_project: target_project, format: format, full: full)
end end
private private
......
...@@ -64,11 +64,11 @@ class Snippet < ActiveRecord::Base ...@@ -64,11 +64,11 @@ class Snippet < ActiveRecord::Base
@link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/) @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
end end
def to_reference(from_project = nil) def to_reference(from_project = nil, full: false)
reference = "#{self.class.reference_prefix}#{id}" reference = "#{self.class.reference_prefix}#{id}"
if project.present? if project.present?
"#{project.to_reference(from_project)}#{reference}" "#{project.to_reference(from_project, full: full)}#{reference}"
else else
reference reference
end end
......
...@@ -336,7 +336,7 @@ class User < ActiveRecord::Base ...@@ -336,7 +336,7 @@ class User < ActiveRecord::Base
username username
end 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}" "#{self.class.reference_prefix}#{username}"
end end
......
---
title: Fix cross-project references copy to include the project reference
merge_request:
author:
...@@ -76,7 +76,7 @@ module Gitlab ...@@ -76,7 +76,7 @@ module Gitlab
if referable.respond_to?(:project) if referable.respond_to?(:project)
referable.to_reference(target_project) referable.to_reference(target_project)
else else
referable.to_reference(@source_project, target_project) referable.to_reference(@source_project, target_project: target_project)
end end
end end
......
...@@ -170,4 +170,14 @@ describe GitlabMarkdownHelper do ...@@ -170,4 +170,14 @@ describe GitlabMarkdownHelper do
expect(doc.content).to eq "@#{user.username}, can you look at this?..." expect(doc.content).to eq "@#{user.username}, can you look at this?..."
end end
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 end
...@@ -43,7 +43,7 @@ describe GroupLabel, models: true do ...@@ -43,7 +43,7 @@ describe GroupLabel, models: true do
let(:target_project) { build_stubbed(:empty_project, name: 'project-2', namespace: namespace) } let(:target_project) { build_stubbed(:empty_project, name: 'project-2', namespace: namespace) }
it 'returns a String reference to the object' do 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
end end
......
...@@ -30,6 +30,10 @@ describe Issue, models: true do ...@@ -30,6 +30,10 @@ describe Issue, models: true do
expect(issue.to_reference).to eq "#1" expect(issue.to_reference).to eq "#1"
end 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 it 'supports a cross-project reference' do
another_project = build(:project, name: 'another-project', namespace: project.namespace) another_project = build(:project, name: 'another-project', namespace: project.namespace)
expect(issue.to_reference(another_project)).to eq "sample-project#1" expect(issue.to_reference(another_project)).to eq "sample-project#1"
......
...@@ -153,6 +153,10 @@ describe MergeRequest, models: true do ...@@ -153,6 +153,10 @@ describe MergeRequest, models: true do
another_project = build(:project, name: 'another-project', namespace: project.namespace) another_project = build(:project, name: 'another-project', namespace: project.namespace)
expect(merge_request.to_reference(another_project)).to eq "sample-project!1" expect(merge_request.to_reference(another_project)).to eq "sample-project!1"
end 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 end
describe '#raw_diffs' do 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