Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.0 on 2022-01-18 at 14:00 UTC+1 (a few minutes of down time)

Commit ac662684 authored by Berna Castro's avatar Berna Castro Committed by Oswaldo Ferreira
Browse files

Refactor Project#to_reference and make full_path a keyword argument

Refactor overall code and fix failing specs

Fix Project#to_reference

Fix wrong spaces and update changelog

Refactor #to_reference for Project & Issue

Fix and improves Project#to_reference
parent 313571f8ef32
......@@ -97,10 +97,10 @@ def self.sort(method, excluded_labels: [])
end
end
def to_reference(from_project = nil, full: false)
def to_reference(from = nil, full_path: false)
reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project, full: full)}#{reference}"
"#{project.to_reference(from, full_path: full_path)}#{reference}"
end
def referenced_merge_requests(current_user = nil)
......
......@@ -591,20 +591,10 @@ def to_param
end
end
def to_reference(from_project = nil, from_group = nil)
if from_group.nil?
if cross_namespace_reference?(from_project)
path_with_namespace
elsif cross_project_reference?(from_project)
path
elsif self == from_project
nil
else
path_with_namespace
end
else
path
end
def to_reference(from = nil, full_path: false)
return path_with_namespace if full_path
path_from(from)
end
def to_human_reference(from_project = nil)
......@@ -1299,21 +1289,33 @@ def environments_recently_updated_on_branch(branch)
private
def path_from(from)
if cross_namespace_reference?(from)
path_with_namespace
elsif cross_project_reference?(from)
path
end
end
def cross_namespace_reference?(from)
if from.is_a?(Project)
from && namespace != from.namespace
else
from && namespace != from
end
end
# Check if a reference is being done cross-project
#
# from_project - Refering Project object
def cross_project_reference?(from_project)
from_project && self != from_project
def cross_project_reference?(from)
return true if from.is_a?(Namespace)
from && self != from
end
def pushes_since_gc_redis_key
"projects/#{id}/pushes_since_gc"
end
def cross_namespace_reference?(from_project)
from_project && namespace != from_project.namespace
end
def default_branch_protected?
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
......
......@@ -34,7 +34,13 @@
= note_count
.issue-info
#{issue.to_reference(@project, @group)} ·
- if controller_name == "dashboard"
#{issue.to_reference(full_path: true)}
- else
#{issue.to_reference(@group || @project)}
·
opened #{time_ago_with_tooltip(issue.created_at, placement: 'bottom')}
by #{link_to_member(@project, issue.author, avatar: false)}
- if issue.milestone
......
---
title: Don't group issues by project on group-level and dashboard issue indexes.
merge_request: 8111
author: Bernardo Castro
......@@ -23,19 +23,37 @@
end
describe '#to_reference' do
let(:project) { build(:empty_project, name: 'sample-project') }
let(:issue) { build(:issue, iid: 1, project: project) }
let(:namespace) { build(:namespace, path: 'sample-namespace') }
let(:project) { build(:empty_project, name: 'sample-project', namespace: namespace) }
let(:issue) { build(:issue, iid: 1, project: project) }
let(:group) { create(:group, name: 'Group', path: 'sample-group') }
context 'when nil argument' do
it 'returns issue id' do
expect(issue.to_reference).to eq "#1"
end
end
it 'returns a String reference to the object' do
expect(issue.to_reference).to eq "#{project.namespace.name}/sample-project#1"
context 'when full_path is true' do
it 'returns complete path to the issue' do
expect(issue.to_reference(full_path: true)).to eq 'sample-namespace/sample-project#1'
expect(issue.to_reference(project, full_path: true)).to eq 'sample-namespace/sample-project#1'
expect(issue.to_reference(group, full_path: true)).to eq 'sample-namespace/sample-project#1'
end
end
it 'supports a project reference' do
expect(issue.to_reference(project)).to eq "#1"
context 'when same project argument' do
it 'returns issue id' do
expect(issue.to_reference(project)).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')
context 'when cross namespace project argument' do
let(:another_namespace_project) { create(:empty_project, name: 'another-project') }
it 'returns complete path to the issue' do
expect(issue.to_reference(another_namespace_project)).to eq 'sample-namespace/sample-project#1'
end
end
it 'supports a cross-project reference' do
......@@ -43,9 +61,35 @@
expect(issue.to_reference(another_project)).to eq "sample-project#1"
end
it 'supports a group reference' do
group = build(:group, name: 'sample-group')
expect(issue.to_reference(nil, group)).to eq("sample-project#1")
context 'when same namespace / cross-project argument' do
let(:another_project) { create(:empty_project, namespace: namespace) }
it 'returns path to the issue with the project name' do
expect(issue.to_reference(another_project)).to eq 'sample-project#1'
end
end
context 'when different namespace / cross-project argument' do
let(:another_namespace) { create(:namespace, path: 'another-namespace') }
let(:another_project) { create(:empty_project, path: 'another-project', namespace: another_namespace) }
it 'returns full path to the issue' do
expect(issue.to_reference(another_project)).to eq 'sample-namespace/sample-project#1'
end
end
context 'when argument is a namespace' do
context 'with same project path' do
it 'returns path to the issue with the project name' do
expect(issue.to_reference(namespace)).to eq 'sample-project#1'
end
end
context 'with different project path' do
it 'returns full path to the issue' do
expect(issue.to_reference(group)).to eq 'sample-namespace/sample-project#1'
end
end
end
end
......
......@@ -282,13 +282,22 @@
end
describe '#to_reference' do
let(:owner) { create(:user, name: 'Gitlab') }
let(:owner) { create(:user, name: 'Gitlab') }
let(:namespace) { create(:namespace, path: 'sample-namespace', owner: owner) }
let(:project) { create(:empty_project, path: 'sample-project', namespace: namespace) }
let(:project) { create(:empty_project, path: 'sample-project', namespace: namespace) }
let(:group) { create(:group, name: 'Group', path: 'sample-group', owner: owner) }
context 'when nil argument' do
it 'returns nil' do
expect(project.to_reference).to be_nil
end
end
context 'when full_path is true' do
it 'returns complete path to the project' do
expect(project.to_reference).to eq 'sample-namespace/sample-project'
expect(project.to_reference(full_path: true)).to eq 'sample-namespace/sample-project'
expect(project.to_reference(project, full_path: true)).to eq 'sample-namespace/sample-project'
expect(project.to_reference(group, full_path: true)).to eq 'sample-namespace/sample-project'
end
end
......@@ -314,9 +323,26 @@
end
end
context 'when group argument' do
it 'returns path to the project' do
expect(project.to_reference(nil, namespace)).to eq 'sample-project'
context 'when different namespace / cross-project argument' do
let(:another_namespace) { create(:namespace, path: 'another-namespace', owner: owner) }
let(:another_project) { create(:empty_project, path: 'another-project', namespace: another_namespace) }
it 'returns full path to the project' do
expect(project.to_reference(another_project)).to eq 'sample-namespace/sample-project'
end
end
context 'when argument is a namespace' do
context 'with same project path' do
it 'returns path to the project' do
expect(project.to_reference(namespace)).to eq 'sample-project'
end
end
context 'with different project path' do
it 'returns full path to the project' do
expect(project.to_reference(group)).to eq 'sample-namespace/sample-project'
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