2018-09-23 19:44:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-02 09:16:24 +00:00
|
|
|
class Import::ManifestController < Import::BaseController
|
2020-08-19 00:10:34 +00:00
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2018-07-06 12:06:44 +00:00
|
|
|
before_action :whitelist_query_limiting, only: [:create]
|
2018-07-05 12:46:39 +00:00
|
|
|
before_action :verify_import_enabled
|
2018-07-05 12:24:53 +00:00
|
|
|
before_action :ensure_import_vars, only: [:create, :status]
|
2018-07-02 09:16:24 +00:00
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
2020-08-19 00:10:34 +00:00
|
|
|
super
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload
|
|
|
|
group = Group.find(params[:group_id])
|
|
|
|
|
|
|
|
unless can?(current_user, :create_projects, group)
|
|
|
|
@errors = ["You don't have enough permissions to create projects in the selected group"]
|
|
|
|
|
2018-07-05 09:54:46 +00:00
|
|
|
render :new && return
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
manifest = Gitlab::ManifestImport::Manifest.new(params[:manifest].tempfile)
|
|
|
|
|
|
|
|
if manifest.valid?
|
2020-09-25 09:09:40 +00:00
|
|
|
manifest_import_metadata.save(manifest.projects, group.id)
|
2018-07-02 09:16:24 +00:00
|
|
|
|
|
|
|
redirect_to status_import_manifest_path
|
|
|
|
else
|
|
|
|
@errors = manifest.errors
|
|
|
|
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-19 00:10:34 +00:00
|
|
|
def realtime_changes
|
|
|
|
super
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-07-05 12:24:53 +00:00
|
|
|
repository = repositories.find do |project|
|
2018-07-02 09:16:24 +00:00
|
|
|
project[:id] == params[:repo_id].to_i
|
|
|
|
end
|
|
|
|
|
2018-07-06 13:21:22 +00:00
|
|
|
project = Gitlab::ManifestImport::ProjectCreator.new(repository, group, current_user).execute
|
2018-07-02 09:16:24 +00:00
|
|
|
|
|
|
|
if project.persisted?
|
2020-08-19 00:10:34 +00:00
|
|
|
render json: ProjectSerializer.new.represent(project, serializer: :import)
|
2018-07-02 09:16:24 +00:00
|
|
|
else
|
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-19 00:10:34 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
override :importable_repos
|
|
|
|
def importable_repos
|
|
|
|
already_added_projects_names = already_added_projects.pluck(:import_url)
|
|
|
|
|
|
|
|
repositories.reject { |repo| already_added_projects_names.include?(repo[:url]) }
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
override :incompatible_repos
|
|
|
|
def incompatible_repos
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
override :provider_name
|
|
|
|
def provider_name
|
|
|
|
:manifest
|
|
|
|
end
|
|
|
|
|
|
|
|
override :provider_url
|
|
|
|
def provider_url
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
override :extra_representation_opts
|
|
|
|
def extra_representation_opts
|
|
|
|
{ group_full_path: group.full_path }
|
|
|
|
end
|
|
|
|
|
2018-07-02 09:16:24 +00:00
|
|
|
private
|
|
|
|
|
2018-07-05 12:24:53 +00:00
|
|
|
def ensure_import_vars
|
|
|
|
unless group && repositories.present?
|
2018-07-02 09:16:24 +00:00
|
|
|
redirect_to(new_import_manifest_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-02 09:16:24 +00:00
|
|
|
def group
|
2020-09-25 09:09:40 +00:00
|
|
|
@group ||= Group.find_by(id: manifest_import_metadata.group_id)
|
2018-07-05 12:24:53 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-05 12:24:53 +00:00
|
|
|
|
2020-09-25 09:09:40 +00:00
|
|
|
def manifest_import_metadata
|
|
|
|
@manifest_import_status ||= Gitlab::ManifestImport::Metadata.new(current_user, fallback: session)
|
|
|
|
end
|
|
|
|
|
2018-07-05 12:24:53 +00:00
|
|
|
def repositories
|
2020-09-25 09:09:40 +00:00
|
|
|
@repositories ||= manifest_import_metadata.repositories
|
2018-07-05 12:24:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_jobs
|
|
|
|
find_already_added_projects.to_json(only: [:id], methods: [:import_status])
|
|
|
|
end
|
|
|
|
|
2018-07-05 12:46:39 +00:00
|
|
|
def verify_import_enabled
|
|
|
|
render_404 unless manifest_import_enabled?
|
|
|
|
end
|
2018-07-06 12:06:44 +00:00
|
|
|
|
|
|
|
def whitelist_query_limiting
|
2019-09-18 14:02:45 +00:00
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/48939')
|
2018-07-06 12:06:44 +00:00
|
|
|
end
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|