Commit cd56c73b authored by Imre Farkas's avatar Imre Farkas

Merge branch 'jprovazn-second-source-create-update' into 'master'

Update route namespace when route is created/updated

See merge request gitlab-org/gitlab!77757
parents 74ccb590 697fc373
...@@ -190,5 +190,10 @@ module Routable ...@@ -190,5 +190,10 @@ module Routable
route || build_route(source: self) route || build_route(source: self)
route.path = build_full_path route.path = build_full_path
route.name = build_full_name route.name = build_full_name
route.namespace = if is_a?(Namespace)
self
elsif is_a?(Project)
self.project_namespace
end
end end
end end
...@@ -43,7 +43,7 @@ class Namespace < ApplicationRecord ...@@ -43,7 +43,7 @@ class Namespace < ApplicationRecord
has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :project_statistics has_many :project_statistics
has_one :namespace_settings, inverse_of: :namespace, class_name: 'NamespaceSetting', autosave: true has_one :namespace_settings, inverse_of: :namespace, class_name: 'NamespaceSetting', autosave: true
has_one :namespace_route, foreign_key: :namespace_id, autosave: true, inverse_of: :namespace, class_name: 'Route' has_one :namespace_route, foreign_key: :namespace_id, autosave: false, inverse_of: :namespace, class_name: 'Route'
has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace' has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace'
has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner' has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner'
......
...@@ -141,6 +141,11 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do ...@@ -141,6 +141,11 @@ RSpec.describe Group, 'Routable', :with_clean_rails_cache do
end end
end end
it 'creates route with namespace referencing group' do
expect(group.route).not_to be_nil
expect(group.route.namespace).to eq(group)
end
describe '.where_full_path_in' do describe '.where_full_path_in' do
context 'without any paths' do context 'without any paths' do
it 'returns an empty relation' do it 'returns an empty relation' do
...@@ -208,30 +213,20 @@ RSpec.describe Project, 'Routable', :with_clean_rails_cache do ...@@ -208,30 +213,20 @@ RSpec.describe Project, 'Routable', :with_clean_rails_cache do
it_behaves_like 'routable resource with parent' do it_behaves_like 'routable resource with parent' do
let_it_be(:record) { project } let_it_be(:record) { project }
end end
it 'creates route with namespace referencing project namespace' do
expect(project.route).not_to be_nil
expect(project.route.namespace).to eq(project.project_namespace)
end
end end
RSpec.describe Namespaces::ProjectNamespace, 'Routable', :with_clean_rails_cache do RSpec.describe Namespaces::ProjectNamespace, 'Routable', :with_clean_rails_cache do
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:project_namespace) do
# For now we create only project namespace w/o project, otherwise same path
# would be used for project and project namespace.
# This can be removed when route is created automatically for project namespaces.
# https://gitlab.com/gitlab-org/gitlab/-/issues/346448
create(:project_namespace, project: nil, parent: group,
visibility_level: Gitlab::VisibilityLevel::PUBLIC,
path: 'foo', name: 'foo').tap do |project_namespace|
Route.create!(source: project_namespace, path: project_namespace.full_path,
name: project_namespace.full_name)
end
end
# we have couple of places where we use generic Namespace, in that case
# we don't want to include ProjectNamespace routes yet
it 'ignores project namespace when searching for generic namespace' do
redirect_route = create(:redirect_route, source: project_namespace)
expect(Namespace.find_by_full_path(project_namespace.full_path)).to be_nil it 'skips route creation for the resource' do
expect(Namespace.find_by_full_path(redirect_route.path, follow_redirects: true)).to be_nil expect do
described_class.create!(project: nil, parent: group, visibility_level: Gitlab::VisibilityLevel::PUBLIC, path: 'foo', name: 'foo')
end.not_to change { Route.count }
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