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
|
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
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-02 09:16:24 +00:00
|
|
|
def status
|
2018-07-05 12:24:53 +00:00
|
|
|
@already_added_projects = find_already_added_projects
|
|
|
|
already_added_import_urls = @already_added_projects.pluck(:import_url)
|
2018-07-02 09:16:24 +00:00
|
|
|
|
2018-07-05 12:24:53 +00:00
|
|
|
@pending_repositories = repositories.to_a.reject do |repository|
|
|
|
|
already_added_import_urls.include?(repository[:url])
|
|
|
|
end
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-02 09:16:24 +00: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 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?
|
2018-07-09 11:59:07 +00:00
|
|
|
session[:manifest_import_repositories] = manifest.projects
|
|
|
|
session[:manifest_import_group_id] = group.id
|
2018-07-02 09:16:24 +00:00
|
|
|
|
|
|
|
redirect_to status_import_manifest_path
|
|
|
|
else
|
|
|
|
@errors = manifest.errors
|
|
|
|
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def jobs
|
2018-07-05 12:24:53 +00:00
|
|
|
render json: find_jobs
|
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?
|
|
|
|
render json: ProjectSerializer.new.represent(project)
|
|
|
|
else
|
|
|
|
render json: { errors: project_save_error(project) }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2018-07-09 11:59:07 +00:00
|
|
|
@group ||= Group.find_by(id: session[:manifest_import_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
|
|
|
|
|
|
|
def repositories
|
2018-07-09 11:59:07 +00:00
|
|
|
@repositories ||= session[:manifest_import_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-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-05 12:24:53 +00:00
|
|
|
def find_already_added_projects
|
|
|
|
group.all_projects
|
|
|
|
.where(import_type: 'manifest')
|
|
|
|
.where(creator_id: current_user)
|
2020-01-24 12:09:01 +00:00
|
|
|
.with_import_state
|
2018-07-02 09:16:24 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
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
|