Skip to content
Snippets Groups Projects
Commit 0c3f1214 authored by GitLab Bot's avatar GitLab Bot
Browse files

Add latest changes from gitlab-org/gitlab@master

parent ff67e3ed
No related branches found
No related tags found
No related merge requests found
Showing
with 295 additions and 42 deletions
Loading
Loading
@@ -23,10 +23,16 @@ class TodosFinder
 
NONE = '0'
 
TODO_TYPES = Set.new(%w(Issue MergeRequest Epic)).freeze
TODO_TYPES = Set.new(%w(Issue MergeRequest)).freeze
 
attr_accessor :current_user, :params
 
class << self
def todo_types
TODO_TYPES
end
end
def initialize(current_user, params = {})
@current_user = current_user
@params = params
Loading
Loading
@@ -124,7 +130,7 @@ class TodosFinder
end
 
def type?
type.present? && TODO_TYPES.include?(type)
type.present? && self.class.todo_types.include?(type)
end
 
def type
Loading
Loading
@@ -201,3 +207,5 @@ class TodosFinder
end
end
end
TodosFinder.prepend_if_ee('EE::TodosFinder')
Loading
Loading
@@ -55,6 +55,8 @@ class Group < Namespace
 
has_many :todos
 
has_one :import_export_upload
accepts_nested_attributes_for :variables, allow_destroy: true
 
validate :visibility_level_allowed_by_projects
Loading
Loading
Loading
Loading
@@ -5,6 +5,7 @@ class ImportExportUpload < ApplicationRecord
include ObjectStorage::BackgroundMove
 
belongs_to :project
belongs_to :group
 
# These hold the project Import/Export archives (.tar.gz files)
mount_uploader :import_file, ImportExportUploader
Loading
Loading
# frozen_string_literal: true
module Groups
module ImportExport
class ExportService
def initialize(group:, user:, params: {})
@group = group
@current_user = user
@params = params
@shared = @params[:shared] || Gitlab::ImportExport::Shared.new(@group)
end
def execute
save!
end
private
attr_accessor :shared
def save!
if savers.all?(&:save)
notify_success
else
cleanup_and_notify_error!
end
end
def savers
[tree_exporter, file_saver]
end
def tree_exporter
Gitlab::ImportExport::GroupTreeSaver.new(group: @group, current_user: @current_user, shared: @shared, params: @params)
end
def file_saver
Gitlab::ImportExport::Saver.new(exportable: @group, shared: @shared)
end
def cleanup_and_notify_error
FileUtils.rm_rf(shared.export_path)
notify_error
end
def cleanup_and_notify_error!
cleanup_and_notify_error
raise Gitlab::ImportExport::Error.new(shared.errors.to_sentence)
end
def notify_success
@shared.logger.info(
group_id: @group.id,
group_name: @group.name,
message: 'Group Import/Export: Export succeeded'
)
end
def notify_error
@shared.logger.error(
group_id: @group.id,
group_name: @group.name,
error: @shared.errors.join(', '),
message: 'Group Import/Export: Export failed'
)
end
end
end
end
Loading
Loading
@@ -24,7 +24,7 @@ module Projects
 
def save_all!
if save_exporters
Gitlab::ImportExport::Saver.save(project: project, shared: shared)
Gitlab::ImportExport::Saver.save(exportable: project, shared: shared)
notify_success
else
cleanup_and_notify_error!
Loading
Loading
Loading
Loading
@@ -179,3 +179,4 @@
- import_issues_csv
- project_daily_statistics
- create_evidence
- group_export
# frozen_string_literal: true
class GroupExportWorker
include ApplicationWorker
include ExceptionBacktrace
feature_category :source_code_management
def perform(current_user_id, group_id, params = {})
current_user = User.find(current_user_id)
group = Group.find(group_id)
::Groups::ImportExport::ExportService.new(group: group, user: current_user, params: params).execute
end
end
Loading
Loading
@@ -98,6 +98,7 @@
- [update_namespace_statistics, 1]
- [chaos, 2]
- [create_evidence, 2]
- [group_export, 1]
 
# EE-specific queues
- [analytics, 1]
Loading
Loading
# frozen_string_literal: true
class AddGroupIdToImportExportUploads < ActiveRecord::Migration[5.2]
DOWNTIME = false
def change
add_column :import_export_uploads, :group_id, :bigint
end
end
# frozen_string_literal: true
class AddGroupFkToImportExportUploads < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_foreign_key :import_export_uploads, :namespaces, column: :group_id, on_delete: :cascade
add_concurrent_index :import_export_uploads, :group_id, unique: true, where: 'group_id IS NOT NULL'
end
def down
remove_foreign_key_without_error(:import_export_uploads, column: :group_id)
remove_concurrent_index(:import_export_uploads, :group_id)
end
end
Loading
Loading
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
 
ActiveRecord::Schema.define(version: 2019_11_05_140942) do
ActiveRecord::Schema.define(version: 2019_11_11_115431) do
 
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
Loading
Loading
@@ -1874,6 +1874,8 @@ ActiveRecord::Schema.define(version: 2019_11_05_140942) do
t.integer "project_id"
t.text "import_file"
t.text "export_file"
t.bigint "group_id"
t.index ["group_id"], name: "index_import_export_uploads_on_group_id", unique: true, where: "(group_id IS NOT NULL)"
t.index ["project_id"], name: "index_import_export_uploads_on_project_id"
t.index ["updated_at"], name: "index_import_export_uploads_on_updated_at"
end
Loading
Loading
@@ -4288,6 +4290,7 @@ ActiveRecord::Schema.define(version: 2019_11_05_140942) do
add_foreign_key "group_group_links", "namespaces", column: "shared_group_id", on_delete: :cascade
add_foreign_key "group_group_links", "namespaces", column: "shared_with_group_id", on_delete: :cascade
add_foreign_key "identities", "saml_providers", name: "fk_aade90f0fc", on_delete: :cascade
add_foreign_key "import_export_uploads", "namespaces", column: "group_id", name: "fk_83319d9721", on_delete: :cascade
add_foreign_key "import_export_uploads", "projects", on_delete: :cascade
add_foreign_key "index_statuses", "projects", name: "fk_74b2492545", on_delete: :cascade
add_foreign_key "insights", "namespaces", on_delete: :cascade
Loading
Loading
Loading
Loading
@@ -15,7 +15,7 @@ module Gitlab
end
 
def storage_path
File.join(Settings.shared['path'], 'tmp/project_exports')
File.join(Settings.shared['path'], 'tmp/gitlab_exports')
end
 
def import_upload_path(filename:)
Loading
Loading
@@ -50,8 +50,8 @@ module Gitlab
'VERSION'
end
 
def export_filename(project:)
basename = "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_#{project.full_path.tr('/', '_')}"
def export_filename(exportable:)
basename = "#{Time.now.strftime('%Y-%m-%d_%H-%M-%3N')}_#{exportable.full_path.tr('/', '_')}"
 
"#{basename[0..FILENAME_LIMIT]}_export.tar.gz"
end
Loading
Loading
@@ -63,6 +63,14 @@ module Gitlab
def reset_tokens?
true
end
def group_filename
'group.json'
end
def group_config_file
Rails.root.join('lib/gitlab/import_export/group_import_export.yml')
end
end
end
 
Loading
Loading
Loading
Loading
@@ -3,7 +3,8 @@
module Gitlab
module ImportExport
class Config
def initialize
def initialize(config: Gitlab::ImportExport.config_file)
@config = config
@hash = parse_yaml
@hash.deep_symbolize_keys!
@ee_hash = @hash.delete(:ee) || {}
Loading
Loading
@@ -50,7 +51,7 @@ module Gitlab
end
 
def parse_yaml
YAML.load_file(Gitlab::ImportExport.config_file)
YAML.load_file(@config)
end
end
end
Loading
Loading
Loading
Loading
@@ -60,7 +60,7 @@ module Gitlab
def copy_archive
return if @archive_file
 
@archive_file = File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
@archive_file = File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @project))
 
download_or_copy_upload(@project.import_export_upload.import_file, @archive_file)
end
Loading
Loading
# Model relationships to be included in the group import/export
#
# This list _must_ only contain relationships that are available to both FOSS and
# Enterprise editions. EE specific relationships must be defined in the `ee` section further
# down below.
tree:
group:
- :milestones
- :badges
- labels:
- :priorities
- :boards
- members:
- :user
included_attributes:
excluded_attributes:
group:
- :runners_token
- :runners_token_encrypted
methods:
labels:
- :type
badges:
- :type
preloads:
# EE specific relationships and settings to include. All of this will be merged
# into the previous structures if EE is used.
ee:
tree:
group:
- :epics
# frozen_string_literal: true
module Gitlab
module ImportExport
class GroupTreeSaver
attr_reader :full_path
def initialize(group:, current_user:, shared:, params: {})
@params = params
@current_user = current_user
@shared = shared
@group = group
@full_path = File.join(@shared.export_path, ImportExport.group_filename)
end
def save
group_tree = serialize(@group, reader.group_tree)
tree_saver.save(group_tree, @shared.export_path, ImportExport.group_filename)
true
rescue => e
@shared.error(e)
false
end
private
def serialize(group, relations_tree)
group_tree = tree_saver.serialize(group, relations_tree)
group.descendants.each do |descendant|
group_tree['descendants'] = [] unless group_tree['descendants']
group_tree['descendants'] << serialize(descendant, relations_tree)
end
group_tree
rescue => e
@shared.error(e)
end
def reader
@reader ||= Gitlab::ImportExport::Reader.new(
shared: @shared,
config: Gitlab::ImportExport::Config.new(
config: Gitlab::ImportExport.group_config_file
).to_h
)
end
def tree_saver
@tree_saver ||= RelationTreeSaver.new
end
end
end
end
Loading
Loading
@@ -3,25 +3,20 @@
module Gitlab
module ImportExport
class ProjectTreeSaver
include Gitlab::ImportExport::CommandLineUtil
attr_reader :full_path
 
def initialize(project:, current_user:, shared:, params: {})
@params = params
@project = project
@params = params
@project = project
@current_user = current_user
@shared = shared
@full_path = File.join(@shared.export_path, ImportExport.project_filename)
@shared = shared
@full_path = File.join(@shared.export_path, ImportExport.project_filename)
end
 
def save
mkdir_p(@shared.export_path)
project_tree = serialize_project_tree
project_tree = tree_saver.serialize(@project, reader.project_tree)
fix_project_tree(project_tree)
project_tree_json = JSON.generate(project_tree)
File.write(full_path, project_tree_json)
tree_saver.save(project_tree, @shared.export_path, ImportExport.project_filename)
 
true
rescue => e
Loading
Loading
@@ -43,16 +38,6 @@ module Gitlab
RelationRenameService.add_new_associations(project_tree)
end
 
def serialize_project_tree
if Feature.enabled?(:export_fast_serialize, default_enabled: true)
Gitlab::ImportExport::FastHashSerializer
.new(@project, reader.project_tree)
.execute
else
@project.as_json(reader.project_tree)
end
end
def reader
@reader ||= Gitlab::ImportExport::Reader.new(shared: @shared)
end
Loading
Loading
@@ -74,6 +59,10 @@ module Gitlab
 
GroupMembersFinder.new(@project.group).execute.where.not(user_id: non_null_user_ids)
end
def tree_saver
@tree_saver ||= RelationTreeSaver.new
end
end
end
end
Loading
Loading
@@ -5,24 +5,31 @@ module Gitlab
class Reader
attr_reader :tree, :attributes_finder
 
def initialize(shared:)
@shared = shared
@attributes_finder = Gitlab::ImportExport::AttributesFinder.new(
config: ImportExport::Config.new.to_h)
def initialize(shared:, config: ImportExport::Config.new.to_h)
@shared = shared
@config = config
@attributes_finder = Gitlab::ImportExport::AttributesFinder.new(config: @config)
end
 
# Outputs a hash in the format described here: http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
# for outputting a project in JSON format, including its relations and sub relations.
def project_tree
attributes_finder.find_root(:project)
rescue => e
@shared.error(e)
false
tree_by_key(:project)
end
def group_tree
tree_by_key(:group)
end
 
def group_members_tree
attributes_finder.find_root(:group_members)
tree_by_key(:group_members)
end
def tree_by_key(key)
attributes_finder.find_root(key)
rescue => e
@shared.error(e)
false
end
end
end
Loading
Loading
Loading
Loading
@@ -8,7 +8,7 @@
# The behavior of these renamed relationships should be transient and it should
# only last one release until you completely remove the renaming from the list.
#
# When importing, this class will check the project hash and:
# When importing, this class will check the hash and:
# - if only the old relationship name is found, it will rename it with the new one
# - if only the new relationship name is found, it will do nothing
# - if it finds both, it will use the new relationship data
Loading
Loading
# frozen_string_literal: true
module Gitlab
module ImportExport
class RelationTreeSaver
include Gitlab::ImportExport::CommandLineUtil
def serialize(exportable, relations_tree)
if Feature.enabled?(:export_fast_serialize, default_enabled: true)
Gitlab::ImportExport::FastHashSerializer
.new(exportable, relations_tree)
.execute
else
exportable.as_json(relations_tree)
end
end
def save(tree, dir_path, filename)
mkdir_p(dir_path)
tree_json = JSON.generate(tree)
File.write(File.join(dir_path, filename), tree_json)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment