diff options
author | Francisco Javier López <fjlopez@gitlab.com> | 2018-04-06 15:23:49 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-04-06 15:23:49 +0000 |
commit | f20912df033d07c46b0989012244d96d0a12b66d (patch) | |
tree | 6207b8face17f9b7166ba1a5e047032e3927e53e /lib | |
parent | 44f4a674e2a87d104f700265d835aba000c589f0 (diff) | |
download | gitlab-ce-f20912df033d07c46b0989012244d96d0a12b66d.tar.gz |
Extend API for importing a project export with overwrite support
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/project_import.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/import_export/importer.rb | 42 | ||||
-rw-r--r-- | lib/gitlab/import_export/project_tree_restorer.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/import_export/statistics_restorer.rb | 17 |
4 files changed, 60 insertions, 5 deletions
diff --git a/lib/api/project_import.rb b/lib/api/project_import.rb index 303b58a5942..bc5152e539f 100644 --- a/lib/api/project_import.rb +++ b/lib/api/project_import.rb @@ -26,6 +26,7 @@ module API requires :path, type: String, desc: 'The new project path and name' requires :file, type: File, desc: 'The project export file to be imported' optional :namespace, type: String, desc: "The ID or name of the namespace that the project will be imported into. Defaults to the current user's namespace." + optional :overwrite, type: Boolean, default: false, desc: 'If there is a project in the same namespace and with the same name overwrite it' optional :override_params, type: Hash, desc: 'New project params to override values in the export' do @@ -50,7 +51,8 @@ module API project_params = { path: import_params[:path], namespace_id: namespace.id, - file: import_params[:file]['tempfile'] + file: import_params[:file]['tempfile'], + overwrite: import_params[:overwrite] } override_params = import_params.delete(:override_params) diff --git a/lib/gitlab/import_export/importer.rb b/lib/gitlab/import_export/importer.rb index c490bf059d2..63cab07324a 100644 --- a/lib/gitlab/import_export/importer.rb +++ b/lib/gitlab/import_export/importer.rb @@ -1,6 +1,9 @@ module Gitlab module ImportExport class Importer + include Gitlab::Allowable + include Gitlab::Utils::StrongMemoize + def self.imports_repository? true end @@ -13,12 +16,14 @@ module Gitlab end def execute - if import_file && check_version! && restorers.all?(&:restore) + if import_file && check_version! && restorers.all?(&:restore) && overwrite_project project_tree.restored_project else raise Projects::ImportService::Error.new(@shared.errors.join(', ')) end - + rescue => e + raise Projects::ImportService::Error.new(e.message) + ensure remove_import_file end @@ -26,7 +31,7 @@ module Gitlab def restorers [repo_restorer, wiki_restorer, project_tree, avatar_restorer, - uploads_restorer, lfs_restorer] + uploads_restorer, lfs_restorer, statistics_restorer] end def import_file @@ -69,6 +74,10 @@ module Gitlab Gitlab::ImportExport::LfsRestorer.new(project: project_tree.restored_project, shared: @shared) end + def statistics_restorer + Gitlab::ImportExport::StatisticsRestorer.new(project: project_tree.restored_project, shared: @shared) + end + def path_with_namespace File.join(@project.namespace.full_path, @project.path) end @@ -84,6 +93,33 @@ module Gitlab def remove_import_file FileUtils.rm_rf(@archive_file) end + + def overwrite_project + project = project_tree.restored_project + + return unless can?(@current_user, :admin_namespace, project.namespace) + + if overwrite_project? + ::Projects::OverwriteProjectService.new(project, @current_user) + .execute(project_to_overwrite) + end + + true + end + + def original_path + @project.import_data&.data&.fetch('original_path', nil) + end + + def overwrite_project? + original_path.present? && project_to_overwrite.present? + end + + def project_to_overwrite + strong_memoize(:project_to_overwrite) do + Project.find_by_full_path("#{@project.namespace.full_path}/#{original_path}") + end + end end end end diff --git a/lib/gitlab/import_export/project_tree_restorer.rb b/lib/gitlab/import_export/project_tree_restorer.rb index 2c315207298..d5590dde40f 100644 --- a/lib/gitlab/import_export/project_tree_restorer.rb +++ b/lib/gitlab/import_export/project_tree_restorer.rb @@ -92,7 +92,7 @@ module Gitlab end def override_params - return {} unless params = @project.import_data&.data&.fetch('override_params') + return {} unless params = @project.import_data&.data&.fetch('override_params', nil) @override_params ||= params.select do |key, _value| Project.column_names.include?(key.to_s) && diff --git a/lib/gitlab/import_export/statistics_restorer.rb b/lib/gitlab/import_export/statistics_restorer.rb new file mode 100644 index 00000000000..bcdd9c12c85 --- /dev/null +++ b/lib/gitlab/import_export/statistics_restorer.rb @@ -0,0 +1,17 @@ +module Gitlab + module ImportExport + class StatisticsRestorer + def initialize(project:, shared:) + @project = project + @shared = shared + end + + def restore + @project.statistics.refresh! + rescue => e + @shared.error(e) + false + end + end + end +end |