diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-06 18:08:08 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-06 18:08:08 +0000 |
commit | 83731155d997ae24c7e0cd5ffa6f0dba41bec6dc (patch) | |
tree | 31f785012137fda4ac9a470f4f07c961b42d0299 /lib | |
parent | 57a37ce99f297cddae12cb4d982b6d572f932bb4 (diff) | |
download | gitlab-ce-83731155d997ae24c7e0cd5ffa6f0dba41bec6dc.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/background_migration/user_mentions/models/snippet.rb | 43 | ||||
-rw-r--r-- | lib/gitlab/background_migration/user_mentions/models/snippet_user_mention.rb | 18 | ||||
-rw-r--r-- | lib/gitlab/import_export.rb | 12 | ||||
-rw-r--r-- | lib/gitlab/import_export/importer.rb | 8 | ||||
-rw-r--r-- | lib/gitlab/import_export/snippet_repo_restorer.rb | 48 | ||||
-rw-r--r-- | lib/gitlab/import_export/snippet_repo_saver.rb | 21 | ||||
-rw-r--r-- | lib/gitlab/import_export/snippets_repo_restorer.rb | 36 | ||||
-rw-r--r-- | lib/gitlab/import_export/snippets_repo_saver.rb | 34 |
9 files changed, 159 insertions, 63 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index ca6e6de3342..02b3fe7e03e 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -45,7 +45,7 @@ module API before do Gitlab::ApplicationContext.push( - user: -> { current_user }, + user: -> { @current_user }, project: -> { @project }, namespace: -> { @group }, caller_id: route.origin diff --git a/lib/gitlab/background_migration/user_mentions/models/snippet.rb b/lib/gitlab/background_migration/user_mentions/models/snippet.rb deleted file mode 100644 index cdbada76429..00000000000 --- a/lib/gitlab/background_migration/user_mentions/models/snippet.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true -# rubocop:disable Style/Documentation - -module Gitlab - module BackgroundMigration - module UserMentions - module Models - class Snippet < ActiveRecord::Base - include Concerns::IsolatedMentionable - include Concerns::MentionableMigrationMethods - include CacheMarkdownField - - attr_mentionable :title, pipeline: :single_line - attr_mentionable :description - cache_markdown_field :title, pipeline: :single_line - cache_markdown_field :description - - self.table_name = 'snippets' - self.inheritance_column = :_type_disabled - - belongs_to :author, class_name: "User" - belongs_to :project - - def self.user_mention_model - Gitlab::BackgroundMigration::UserMentions::Models::SnippetUserMention - end - - def user_mention_model - self.class.user_mention_model - end - - def user_mention_resource_id - id - end - - def user_mention_note_id - 'NULL' - end - end - end - end - end -end diff --git a/lib/gitlab/background_migration/user_mentions/models/snippet_user_mention.rb b/lib/gitlab/background_migration/user_mentions/models/snippet_user_mention.rb deleted file mode 100644 index a856a53626e..00000000000 --- a/lib/gitlab/background_migration/user_mentions/models/snippet_user_mention.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true -# rubocop:disable Style/Documentation - -module Gitlab - module BackgroundMigration - module UserMentions - module Models - class SnippetUserMention < ActiveRecord::Base - self.table_name = 'snippet_user_mentions' - - def self.resource_foreign_key - :snippet_id - end - end - end - end - end -end diff --git a/lib/gitlab/import_export.rb b/lib/gitlab/import_export.rb index 1033e6c4e05..52102b6f508 100644 --- a/lib/gitlab/import_export.rb +++ b/lib/gitlab/import_export.rb @@ -42,6 +42,18 @@ module Gitlab "project.wiki.bundle" end + def snippet_repo_bundle_dir + 'snippets' + end + + def snippets_repo_bundle_path(absolute_path) + File.join(absolute_path, ::Gitlab::ImportExport.snippet_repo_bundle_dir) + end + + def snippet_repo_bundle_filename_for(snippet) + "#{snippet.hexdigest}.bundle" + end + def config_file Rails.root.join('lib/gitlab/import_export/project/import_export.yml') end diff --git a/lib/gitlab/import_export/importer.rb b/lib/gitlab/import_export/importer.rb index 4eeecc14067..4b761eb86ae 100644 --- a/lib/gitlab/import_export/importer.rb +++ b/lib/gitlab/import_export/importer.rb @@ -35,7 +35,7 @@ module Gitlab def restorers [repo_restorer, wiki_restorer, project_tree, avatar_restorer, - uploads_restorer, lfs_restorer, statistics_restorer] + uploads_restorer, lfs_restorer, statistics_restorer, snippets_repo_restorer] end def import_file @@ -79,6 +79,12 @@ module Gitlab Gitlab::ImportExport::LfsRestorer.new(project: project, shared: shared) end + def snippets_repo_restorer + Gitlab::ImportExport::SnippetsRepoRestorer.new(project: project, + shared: shared, + user: current_user) + end + def statistics_restorer Gitlab::ImportExport::StatisticsRestorer.new(project: project, shared: shared) end diff --git a/lib/gitlab/import_export/snippet_repo_restorer.rb b/lib/gitlab/import_export/snippet_repo_restorer.rb new file mode 100644 index 00000000000..079681dfac5 --- /dev/null +++ b/lib/gitlab/import_export/snippet_repo_restorer.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module Gitlab + module ImportExport + class SnippetRepoRestorer < RepoRestorer + attr_reader :snippet + + def initialize(snippet:, user:, shared:, path_to_bundle:) + @snippet = snippet + @user = user + @repository = snippet.repository + @path_to_bundle = path_to_bundle.to_s + @shared = shared + end + + def restore + if File.exist?(path_to_bundle) + create_repository_from_bundle + else + create_repository_from_db + end + + true + rescue => e + shared.error(e) + false + end + + private + + def create_repository_from_bundle + repository.create_from_bundle(path_to_bundle) + snippet.track_snippet_repository + end + + def create_repository_from_db + snippet.create_repository + + commit_attrs = { + branch_name: 'master', + message: 'Initial commit' + } + + repository.create_file(@user, snippet.file_name, snippet.content, commit_attrs) + end + end + end +end diff --git a/lib/gitlab/import_export/snippet_repo_saver.rb b/lib/gitlab/import_export/snippet_repo_saver.rb new file mode 100644 index 00000000000..cab96c78232 --- /dev/null +++ b/lib/gitlab/import_export/snippet_repo_saver.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Gitlab + module ImportExport + class SnippetRepoSaver < RepoSaver + def initialize(project:, shared:, repository:) + @project = project + @shared = shared + @repository = repository + end + + private + + def bundle_full_path + File.join(shared.export_path, + ::Gitlab::ImportExport.snippet_repo_bundle_dir, + ::Gitlab::ImportExport.snippet_repo_bundle_filename_for(repository.container)) + end + end + end +end diff --git a/lib/gitlab/import_export/snippets_repo_restorer.rb b/lib/gitlab/import_export/snippets_repo_restorer.rb new file mode 100644 index 00000000000..8fe83225812 --- /dev/null +++ b/lib/gitlab/import_export/snippets_repo_restorer.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Gitlab + module ImportExport + class SnippetsRepoRestorer + def initialize(project:, shared:, user:) + @project = project + @shared = shared + @user = user + end + + def restore + return true unless Feature.enabled?(:version_snippets, @user) + return true unless Dir.exist?(snippets_repo_bundle_path) + + @project.snippets.find_each.all? do |snippet| + Gitlab::ImportExport::SnippetRepoRestorer.new(snippet: snippet, + user: @user, + shared: @shared, + path_to_bundle: snippet_repo_bundle_path(snippet)) + .restore + end + end + + private + + def snippet_repo_bundle_path(snippet) + File.join(snippets_repo_bundle_path, ::Gitlab::ImportExport.snippet_repo_bundle_filename_for(snippet)) + end + + def snippets_repo_bundle_path + @snippets_repo_bundle_path ||= ::Gitlab::ImportExport.snippets_repo_bundle_path(@shared.export_path) + end + end + end +end diff --git a/lib/gitlab/import_export/snippets_repo_saver.rb b/lib/gitlab/import_export/snippets_repo_saver.rb new file mode 100644 index 00000000000..85e094c0d15 --- /dev/null +++ b/lib/gitlab/import_export/snippets_repo_saver.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Gitlab + module ImportExport + class SnippetsRepoSaver + include Gitlab::ImportExport::CommandLineUtil + + def initialize(current_user:, project:, shared:) + @project = project + @shared = shared + @current_user = current_user + end + + def save + return true unless Feature.enabled?(:version_snippets, @current_user) + + create_snippets_repo_directory + + @project.snippets.find_each.all? do |snippet| + Gitlab::ImportExport::SnippetRepoSaver.new(project: @project, + shared: @shared, + repository: snippet.repository) + .save + end + end + + private + + def create_snippets_repo_directory + mkdir_p(::Gitlab::ImportExport.snippets_repo_bundle_path(@shared.export_path)) + end + end + end +end |