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

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 08481a71
...@@ -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, full: false) def to_reference(from = nil, full_path: false)
reference = "#{self.class.reference_prefix}#{iid}" reference = "#{self.class.reference_prefix}#{iid}"
"#{project.to_reference(from_project, full: full)}#{reference}" "#{project.to_reference(from, full_path: full_path)}#{reference}"
end end
def referenced_merge_requests(current_user = nil) def referenced_merge_requests(current_user = nil)
......
...@@ -591,20 +591,10 @@ class Project < ActiveRecord::Base ...@@ -591,20 +591,10 @@ class Project < ActiveRecord::Base
end end
end end
def to_reference(from_project = nil, from_group = nil) def to_reference(from = nil, full_path: false)
if from_group.nil? return path_with_namespace if full_path
if cross_namespace_reference?(from_project)
path_with_namespace path_from(from)
elsif cross_project_reference?(from_project)
path
elsif self == from_project
nil
else
path_with_namespace
end
else
path
end
end end
def to_human_reference(from_project = nil) def to_human_reference(from_project = nil)
...@@ -1299,21 +1289,33 @@ class Project < ActiveRecord::Base ...@@ -1299,21 +1289,33 @@ class Project < ActiveRecord::Base
private 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 # Check if a reference is being done cross-project
# def cross_project_reference?(from)
# from_project - Refering Project object return true if from.is_a?(Namespace)
def cross_project_reference?(from_project)
from_project && self != from_project from && self != from
end end
def pushes_since_gc_redis_key def pushes_since_gc_redis_key
"projects/#{id}/pushes_since_gc" "projects/#{id}/pushes_since_gc"
end end
def cross_namespace_reference?(from_project)
from_project && namespace != from_project.namespace
end
def default_branch_protected? def default_branch_protected?
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_FULL || current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_FULL ||
current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE current_application_settings.default_branch_protection == Gitlab::Access::PROTECTION_DEV_CAN_MERGE
......
...@@ -34,7 +34,13 @@ ...@@ -34,7 +34,13 @@
= note_count = note_count
.issue-info .issue-info
#{issue.to_reference(@project, @group)} &middot; - if controller_name == "dashboard"
#{issue.to_reference(full_path: true)}
- else
#{issue.to_reference(@group || @project)}
&middot;
opened #{time_ago_with_tooltip(issue.created_at, placement: 'bottom')} opened #{time_ago_with_tooltip(issue.created_at, placement: 'bottom')}
by #{link_to_member(@project, issue.author, avatar: false)} by #{link_to_member(@project, issue.author, avatar: false)}
- if issue.milestone - 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 @@ describe Issue, models: true do ...@@ -23,19 +23,37 @@ describe Issue, models: true do
end end
describe '#to_reference' do describe '#to_reference' do
let(:project) { build(:empty_project, name: 'sample-project') } let(:namespace) { build(:namespace, path: 'sample-namespace') }
let(:issue) { build(:issue, iid: 1, project: project) } 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 context 'when full_path is true' do
expect(issue.to_reference).to eq "#{project.namespace.name}/sample-project#1" 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 end
it 'supports a project reference' do context 'when same project argument' do
expect(issue.to_reference(project)).to eq "#1" it 'returns issue id' do
expect(issue.to_reference(project)).to eq("#1")
end
end end
it 'returns a String reference with the full path' do context 'when cross namespace project argument' do
expect(issue.to_reference(full: true)).to eq(project.path_with_namespace + '#1') 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 end
it 'supports a cross-project reference' do it 'supports a cross-project reference' do
...@@ -43,9 +61,35 @@ describe Issue, models: true do ...@@ -43,9 +61,35 @@ describe Issue, models: true do
expect(issue.to_reference(another_project)).to eq "sample-project#1" expect(issue.to_reference(another_project)).to eq "sample-project#1"
end end
it 'supports a group reference' do context 'when same namespace / cross-project argument' do
group = build(:group, name: 'sample-group') let(:another_project) { create(:empty_project, namespace: namespace) }
expect(issue.to_reference(nil, group)).to eq("sample-project#1")
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
end end
......
...@@ -282,13 +282,22 @@ describe Project, models: true do ...@@ -282,13 +282,22 @@ describe Project, models: true do
end end
describe '#to_reference' do 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(: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 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 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
end end
...@@ -314,9 +323,26 @@ describe Project, models: true do ...@@ -314,9 +323,26 @@ describe Project, models: true do
end end
end end
context 'when group argument' do context 'when different namespace / cross-project argument' do
it 'returns path to the project' do let(:another_namespace) { create(:namespace, path: 'another-namespace', owner: owner) }
expect(project.to_reference(nil, namespace)).to eq 'sample-project' 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 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