Commit 6c47dae3 authored by James Fargher's avatar James Fargher

Make GlRepository the single source of truth for initialising Repository

Moves default "current" repository access to GlRepository rather than in
each specific container.
parent 15ee88e6
...@@ -896,12 +896,12 @@ class Project < ApplicationRecord ...@@ -896,12 +896,12 @@ class Project < ApplicationRecord
end end
def repository def repository
@repository ||= Repository.new(full_path, self, shard: repository_storage, disk_path: disk_path) @repository ||= Gitlab::GlRepository::PROJECT.repository_for(self)
end end
def design_repository def design_repository
strong_memoize(:design_repository) do strong_memoize(:design_repository) do
DesignManagement::Repository.new(self) Gitlab::GlRepository::DESIGN.repository_for(self)
end end
end end
......
...@@ -275,7 +275,7 @@ class Snippet < ApplicationRecord ...@@ -275,7 +275,7 @@ class Snippet < ApplicationRecord
override :repository override :repository
def repository def repository
@repository ||= Repository.new(full_path, self, shard: repository_storage, disk_path: disk_path, repo_type: Gitlab::GlRepository::SNIPPET) @repository ||= Gitlab::GlRepository::SNIPPET.repository_for(self)
end end
override :repository_size_checker override :repository_size_checker
......
...@@ -180,7 +180,7 @@ class Wiki ...@@ -180,7 +180,7 @@ class Wiki
override :repository override :repository
def repository def repository
@repository ||= Repository.new(full_path, container, shard: repository_storage, disk_path: disk_path, repo_type: Gitlab::GlRepository::WIKI) @repository ||= Gitlab::GlRepository::WIKI.repository_for(container)
end end
def repository_storage def repository_storage
......
...@@ -12,7 +12,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -12,7 +12,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_identifier) { "group-#{expected_id}-wiki" } let(:expected_identifier) { "group-#{expected_id}-wiki" }
let(:expected_suffix) { '.wiki' } let(:expected_suffix) { '.wiki' }
let(:expected_container) { group } let(:expected_container) { group }
let(:expected_repository) { expected_container.wiki.repository } let(:expected_repository) { ::Repository.new(group.wiki.full_path, group, shard: group.wiki.repository_storage, disk_path: group.wiki.disk_path, repo_type: Gitlab::GlRepository::WIKI) }
end end
end end
end end
......
...@@ -7,19 +7,19 @@ module Gitlab ...@@ -7,19 +7,19 @@ module Gitlab
PROJECT = RepoType.new( PROJECT = RepoType.new(
name: :project, name: :project,
access_checker_class: Gitlab::GitAccessProject, access_checker_class: Gitlab::GitAccessProject,
repository_resolver: -> (project) { project&.repository } repository_resolver: -> (project) { ::Repository.new(project.full_path, project, shard: project.repository_storage, disk_path: project.disk_path) }
).freeze ).freeze
WIKI = RepoType.new( WIKI = RepoType.new(
name: :wiki, name: :wiki,
access_checker_class: Gitlab::GitAccessWiki, access_checker_class: Gitlab::GitAccessWiki,
repository_resolver: -> (container) { container&.wiki&.repository }, repository_resolver: -> (container) { ::Repository.new(container.wiki.full_path, container, shard: container.wiki.repository_storage, disk_path: container.wiki.disk_path, repo_type: WIKI) },
project_resolver: -> (container) { container.is_a?(Project) ? container : nil }, project_resolver: -> (container) { container.is_a?(Project) ? container : nil },
suffix: :wiki suffix: :wiki
).freeze ).freeze
SNIPPET = RepoType.new( SNIPPET = RepoType.new(
name: :snippet, name: :snippet,
access_checker_class: Gitlab::GitAccessSnippet, access_checker_class: Gitlab::GitAccessSnippet,
repository_resolver: -> (snippet) { snippet&.repository }, repository_resolver: -> (snippet) { ::Repository.new(snippet.full_path, snippet, shard: snippet.repository_storage, disk_path: snippet.disk_path, repo_type: SNIPPET) },
container_class: Snippet, container_class: Snippet,
project_resolver: -> (snippet) { snippet&.project }, project_resolver: -> (snippet) { snippet&.project },
guest_read_ability: :read_snippet guest_read_ability: :read_snippet
......
...@@ -57,6 +57,8 @@ module Gitlab ...@@ -57,6 +57,8 @@ module Gitlab
end end
def repository_for(container) def repository_for(container)
return unless container
repository_resolver.call(container) repository_resolver.call(container)
end end
......
...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -17,7 +17,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_identifier) { "project-#{expected_id}" } let(:expected_identifier) { "project-#{expected_id}" }
let(:expected_suffix) { '' } let(:expected_suffix) { '' }
let(:expected_container) { project } let(:expected_container) { project }
let(:expected_repository) { expected_container.repository } let(:expected_repository) { ::Repository.new(project.full_path, project, shard: project.repository_storage, disk_path: project.disk_path, repo_type: Gitlab::GlRepository::PROJECT) }
end end
it 'knows its type' do it 'knows its type' do
...@@ -46,7 +46,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -46,7 +46,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_identifier) { "wiki-#{expected_id}" } let(:expected_identifier) { "wiki-#{expected_id}" }
let(:expected_suffix) { '.wiki' } let(:expected_suffix) { '.wiki' }
let(:expected_container) { project } let(:expected_container) { project }
let(:expected_repository) { expected_container.wiki.repository } let(:expected_repository) { ::Repository.new(project.wiki.full_path, project, shard: project.wiki.repository_storage, disk_path: project.wiki.disk_path, repo_type: Gitlab::GlRepository::WIKI) }
end end
it 'knows its type' do it 'knows its type' do
...@@ -75,7 +75,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -75,7 +75,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_id) { personal_snippet.id } let(:expected_id) { personal_snippet.id }
let(:expected_identifier) { "snippet-#{expected_id}" } let(:expected_identifier) { "snippet-#{expected_id}" }
let(:expected_suffix) { '' } let(:expected_suffix) { '' }
let(:expected_repository) { personal_snippet.repository } let(:expected_repository) { ::Repository.new(personal_snippet.full_path, personal_snippet, shard: personal_snippet.repository_storage, disk_path: personal_snippet.disk_path, repo_type: Gitlab::GlRepository::SNIPPET) }
let(:expected_container) { personal_snippet } let(:expected_container) { personal_snippet }
end end
...@@ -104,7 +104,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -104,7 +104,7 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_id) { project_snippet.id } let(:expected_id) { project_snippet.id }
let(:expected_identifier) { "snippet-#{expected_id}" } let(:expected_identifier) { "snippet-#{expected_id}" }
let(:expected_suffix) { '' } let(:expected_suffix) { '' }
let(:expected_repository) { project_snippet.repository } let(:expected_repository) { ::Repository.new(project_snippet.full_path, project_snippet, shard: project_snippet.repository_storage, disk_path: project_snippet.disk_path, repo_type: Gitlab::GlRepository::SNIPPET) }
let(:expected_container) { project_snippet } let(:expected_container) { project_snippet }
end end
...@@ -133,10 +133,14 @@ RSpec.describe Gitlab::GlRepository::RepoType do ...@@ -133,10 +133,14 @@ RSpec.describe Gitlab::GlRepository::RepoType do
let(:expected_identifier) { "design-#{project.id}" } let(:expected_identifier) { "design-#{project.id}" }
let(:expected_id) { project.id } let(:expected_id) { project.id }
let(:expected_suffix) { '.design' } let(:expected_suffix) { '.design' }
let(:expected_repository) { project.design_repository } let(:expected_repository) { ::DesignManagement::Repository.new(project) }
let(:expected_container) { project } let(:expected_container) { project }
end end
it 'uses the design access checker' do
expect(described_class.access_checker_class).to eq(::Gitlab::GitAccessDesign)
end
it 'knows its type' do it 'knows its type' do
aggregate_failures do aggregate_failures do
expect(described_class).to be_design expect(described_class).to be_design
......
...@@ -31,15 +31,4 @@ RSpec.describe ::Gitlab::GlRepository do ...@@ -31,15 +31,4 @@ RSpec.describe ::Gitlab::GlRepository do
expect { described_class.parse("project-foo") }.to raise_error(ArgumentError) expect { described_class.parse("project-foo") }.to raise_error(ArgumentError)
end end
end end
describe 'DESIGN' do
it 'uses the design access checker' do
expect(described_class::DESIGN.access_checker_class).to eq(::Gitlab::GitAccessDesign)
end
it 'builds a design repository' do
expect(described_class::DESIGN.repository_resolver.call(create(:project)))
.to be_a(::DesignManagement::Repository)
end
end
end end
...@@ -1179,7 +1179,7 @@ RSpec.describe API::Internal::Base do ...@@ -1179,7 +1179,7 @@ RSpec.describe API::Internal::Base do
let(:gl_repository) { "snippet-#{personal_snippet.id}" } let(:gl_repository) { "snippet-#{personal_snippet.id}" }
it 'does not try to notify that project moved' do it 'does not try to notify that project moved' do
allow(Gitlab::GlRepository).to receive(:parse).and_return([personal_snippet, nil, Gitlab::GlRepository::PROJECT]) allow(Gitlab::GlRepository).to receive(:parse).and_return([personal_snippet, nil, Gitlab::GlRepository::SNIPPET])
expect(Gitlab::Checks::ProjectMoved).not_to receive(:fetch_message) expect(Gitlab::Checks::ProjectMoved).not_to receive(:fetch_message)
......
...@@ -17,5 +17,9 @@ RSpec.shared_examples 'a repo type' do ...@@ -17,5 +17,9 @@ RSpec.shared_examples 'a repo type' do
it 'finds the repository for the repo type' do it 'finds the repository for the repo type' do
expect(described_class.repository_for(expected_container)).to eq(expected_repository) expect(described_class.repository_for(expected_container)).to eq(expected_repository)
end end
it 'returns nil when container is nil' do
expect(described_class.repository_for(nil)).to eq(nil)
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