debian-mirror-gitlab/app/controllers/import/manifest_controller.rb

113 lines
2.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
class Import::ManifestController < Import::BaseController
2020-10-24 23:57:45 +05:30
extend ::Gitlab::Utils::Override
2021-04-29 21:17:54 +05:30
before_action :disable_query_limiting, only: [:create]
2018-11-18 11:00:15 +05:30
before_action :verify_import_enabled
before_action :ensure_import_vars, only: [:create, :status]
def new
end
2022-07-16 23:28:13 +05:30
# We need to re-expose controller's internal method 'status' as action.
# rubocop:disable Lint/UselessMethodDefinition
2018-11-18 11:00:15 +05:30
def status
2020-10-24 23:57:45 +05:30
super
2018-11-18 11:00:15 +05:30
end
2022-07-16 23:28:13 +05:30
# rubocop:enable Lint/UselessMethodDefinition
2018-11-18 11:00:15 +05:30
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"]
render :new && return
end
manifest = Gitlab::ManifestImport::Manifest.new(params[:manifest].tempfile)
if manifest.valid?
2021-01-03 14:25:43 +05:30
manifest_import_metadata.save(manifest.projects, group.id)
2018-11-18 11:00:15 +05:30
redirect_to status_import_manifest_path
else
@errors = manifest.errors
render :new
end
end
def create
2021-11-11 11:23:49 +05:30
repository = importable_repos.find do |project|
2018-11-18 11:00:15 +05:30
project[:id] == params[:repo_id].to_i
end
project = Gitlab::ManifestImport::ProjectCreator.new(repository, group, current_user).execute
if project.persisted?
2020-10-24 23:57:45 +05:30
render json: ProjectSerializer.new.represent(project, serializer: :import)
2018-11-18 11:00:15 +05:30
else
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
end
end
2020-10-24 23:57:45 +05:30
protected
override :importable_repos
def importable_repos
2021-11-11 11:23:49 +05:30
@importable_repos ||= manifest_import_metadata.repositories
2020-10-24 23:57:45 +05:30
end
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-11-18 11:00:15 +05:30
private
def ensure_import_vars
2021-11-11 11:23:49 +05:30
unless group && importable_repos.present?
2018-11-18 11:00:15 +05:30
redirect_to(new_import_manifest_path)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def group
2021-01-03 14:25:43 +05:30
@group ||= Group.find_by(id: manifest_import_metadata.group_id)
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2021-01-03 14:25:43 +05:30
def manifest_import_metadata
@manifest_import_status ||= Gitlab::ManifestImport::Metadata.new(current_user, fallback: session)
end
2018-11-18 11:00:15 +05:30
def find_jobs
find_already_added_projects.to_json(only: [:id], methods: [:import_status])
end
def verify_import_enabled
render_404 unless manifest_import_enabled?
end
2021-04-29 21:17:54 +05:30
def disable_query_limiting
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/23147')
2018-11-18 11:00:15 +05:30
end
end