2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-02 05:16:24 -04:00
|
|
|
class Import::ManifestController < Import::BaseController
|
2018-07-06 08:06:44 -04:00
|
|
|
before_action :whitelist_query_limiting, only: [:create]
|
2018-07-05 08:46:39 -04:00
|
|
|
before_action :verify_import_enabled
|
2018-07-05 08:24:53 -04:00
|
|
|
before_action :ensure_import_vars, only: [:create, :status]
|
2018-07-02 05:16:24 -04:00
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-02 05:16:24 -04:00
|
|
|
def status
|
2018-07-05 08:24:53 -04:00
|
|
|
@already_added_projects = find_already_added_projects
|
|
|
|
already_added_import_urls = @already_added_projects.pluck(:import_url)
|
2018-07-02 05:16:24 -04:00
|
|
|
|
2018-07-05 08:24:53 -04:00
|
|
|
@pending_repositories = repositories.to_a.reject do |repository|
|
|
|
|
already_added_import_urls.include?(repository[:url])
|
|
|
|
end
|
2018-07-02 05:16:24 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-02 05:16:24 -04:00
|
|
|
|
|
|
|
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 05:54:46 -04:00
|
|
|
render :new && return
|
2018-07-02 05:16:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
manifest = Gitlab::ManifestImport::Manifest.new(params[:manifest].tempfile)
|
|
|
|
|
|
|
|
if manifest.valid?
|
2018-07-09 07:59:07 -04:00
|
|
|
session[:manifest_import_repositories] = manifest.projects
|
|
|
|
session[:manifest_import_group_id] = group.id
|
2018-07-02 05:16:24 -04:00
|
|
|
|
|
|
|
redirect_to status_import_manifest_path
|
|
|
|
else
|
|
|
|
@errors = manifest.errors
|
|
|
|
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2018-07-05 08:24:53 -04:00
|
|
|
render json: find_jobs
|
2018-07-02 05:16:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-07-05 08:24:53 -04:00
|
|
|
repository = repositories.find do |project|
|
2018-07-02 05:16:24 -04:00
|
|
|
project[:id] == params[:repo_id].to_i
|
|
|
|
end
|
|
|
|
|
2018-07-06 09:21:22 -04:00
|
|
|
project = Gitlab::ManifestImport::ProjectCreator.new(repository, group, current_user).execute
|
2018-07-02 05:16:24 -04:00
|
|
|
|
|
|
|
if project.persisted?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-07-05 08:24:53 -04:00
|
|
|
def ensure_import_vars
|
|
|
|
unless group && repositories.present?
|
2018-07-02 05:16:24 -04:00
|
|
|
redirect_to(new_import_manifest_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-02 05:16:24 -04:00
|
|
|
def group
|
2018-07-09 07:59:07 -04:00
|
|
|
@group ||= Group.find_by(id: session[:manifest_import_group_id])
|
2018-07-05 08:24:53 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-05 08:24:53 -04:00
|
|
|
|
|
|
|
def repositories
|
2018-07-09 07:59:07 -04:00
|
|
|
@repositories ||= session[:manifest_import_repositories]
|
2018-07-05 08:24:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_jobs
|
|
|
|
find_already_added_projects.to_json(only: [:id], methods: [:import_status])
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-05 08:24:53 -04:00
|
|
|
def find_already_added_projects
|
|
|
|
group.all_projects
|
|
|
|
.where(import_type: 'manifest')
|
|
|
|
.where(creator_id: current_user)
|
2020-01-24 07:09:01 -05:00
|
|
|
.with_import_state
|
2018-07-02 05:16:24 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-05 08:46:39 -04:00
|
|
|
|
|
|
|
def verify_import_enabled
|
|
|
|
render_404 unless manifest_import_enabled?
|
|
|
|
end
|
2018-07-06 08:06:44 -04:00
|
|
|
|
|
|
|
def whitelist_query_limiting
|
2019-09-18 10:02:45 -04:00
|
|
|
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/48939')
|
2018-07-06 08:06:44 -04:00
|
|
|
end
|
2018-07-02 05:16:24 -04:00
|
|
|
end
|