From 48b17e991a960c006da278d4c1f534b1db81d070 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Wed, 4 Apr 2018 16:40:58 +0200 Subject: Add helper for accessing lfs_objects for project This makes accessing LFS Objects for a project easier project.lfs_storage_project.lfs_objects` becomes project.all_lfs_objects This will make the refactor in https://gitlab.com/gitlab-org/gitlab-ce/issues/39769 easier to deal with. --- app/controllers/projects/lfs_api_controller.rb | 2 +- app/models/project.rb | 10 ++++++++++ lib/gitlab/checks/lfs_integrity.rb | 3 +-- lib/gitlab/import_export/lfs_restorer.rb | 2 +- lib/gitlab/import_export/lfs_saver.rb | 4 +--- spec/models/project_spec.rb | 16 ++++++++++++++++ 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/controllers/projects/lfs_api_controller.rb b/app/controllers/projects/lfs_api_controller.rb index c77f10ef1dd..ee4ed674110 100644 --- a/app/controllers/projects/lfs_api_controller.rb +++ b/app/controllers/projects/lfs_api_controller.rb @@ -41,7 +41,7 @@ class Projects::LfsApiController < Projects::GitHttpClientController def existing_oids @existing_oids ||= begin - storage_project.lfs_objects.where(oid: objects.map { |o| o['oid'].to_s }).pluck(:oid) + project.all_lfs_objects.where(oid: objects.map { |o| o['oid'].to_s }).pluck(:oid) end end diff --git a/app/models/project.rb b/app/models/project.rb index 714a15ade9c..32289106f28 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1066,6 +1066,16 @@ class Project < ActiveRecord::Base end end + # This will return all `lfs_objects` that are accessible to the project. + # So this might be `self.lfs_objects` if the project is not part of a fork + # network, or it is the base of the fork network. + # + # TODO: refactor this to get the correct lfs objects when implementing + # https://gitlab.com/gitlab-org/gitlab-ce/issues/39769 + def all_lfs_objects + lfs_storage_project.lfs_objects + end + def personal? !group end diff --git a/lib/gitlab/checks/lfs_integrity.rb b/lib/gitlab/checks/lfs_integrity.rb index f7276a380dc..f0e5773ec3c 100644 --- a/lib/gitlab/checks/lfs_integrity.rb +++ b/lib/gitlab/checks/lfs_integrity.rb @@ -15,8 +15,7 @@ module Gitlab return false unless new_lfs_pointers.present? - existing_count = @project.lfs_storage_project - .lfs_objects + existing_count = @project.all_lfs_objects .where(oid: new_lfs_pointers.map(&:lfs_oid)) .count diff --git a/lib/gitlab/import_export/lfs_restorer.rb b/lib/gitlab/import_export/lfs_restorer.rb index 4f144d5c8e6..b28c3c161b7 100644 --- a/lib/gitlab/import_export/lfs_restorer.rb +++ b/lib/gitlab/import_export/lfs_restorer.rb @@ -28,7 +28,7 @@ module Gitlab lfs_object = LfsObject.find_or_initialize_by(oid: oid, size: size) lfs_object.file = File.open(path) unless lfs_object.file&.exists? - @project.lfs_storage_project.lfs_objects << lfs_object + @project.all_lfs_objects << lfs_object end def lfs_file_paths diff --git a/lib/gitlab/import_export/lfs_saver.rb b/lib/gitlab/import_export/lfs_saver.rb index 2e3b9ea200d..29410e2331c 100644 --- a/lib/gitlab/import_export/lfs_saver.rb +++ b/lib/gitlab/import_export/lfs_saver.rb @@ -9,9 +9,7 @@ module Gitlab end def save - return true if @project.lfs_objects.empty? - - @project.lfs_storage_project.lfs_objects.each do |lfs_object| + @project.all_lfs_objects.each do |lfs_object| save_lfs_object(lfs_object) end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 0e560be9eaa..8bd62dcdccb 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2022,6 +2022,22 @@ describe Project do expect(forked_project.lfs_storage_project).to eq forked_project end end + + describe '#all_lfs_objects' do + let(:lfs_object) { create(:lfs_object) } + + before do + project.lfs_objects << lfs_object + end + + it 'returns the lfs object for a project' do + expect(project.all_lfs_objects).to contain_exactly(lfs_object) + end + + it 'returns the lfs object for a fork' do + expect(forked_project.all_lfs_objects).to contain_exactly(lfs_object) + end + end end describe '#pushes_since_gc' do -- cgit v1.2.1