1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# frozen_string_literal: true
module Import
module GitlabProjects
class CreateProjectFromUploadedFileService
include ActiveModel::Validations
include ::Services::ReturnServiceResponses
validate :required_params_presence
def initialize(current_user, params = {})
@current_user = current_user
@params = params.dup
end
def execute
return error(errors.full_messages.first) unless valid?
return error(project.errors.full_messages&.first) unless project.saved?
success(project)
rescue StandardError => e
error(e.message)
end
private
attr_reader :current_user, :params
def error(message)
super(message, :bad_request)
end
def project
@project ||= ::Projects::GitlabProjectsImportService.new(
current_user,
project_params,
params[:override]
).execute
end
def project_params
{
name: params[:name],
path: params[:path],
namespace_id: params[:namespace].id,
file: params[:file],
overwrite: params[:overwrite],
import_type: 'gitlab_project'
}
end
def required_params
[:path, :namespace, :file]
end
def required_params_presence
required_params
.select { |key| params[key].blank? }
.each do |missing_parameter|
errors.add(:base, "Parameter '#{missing_parameter}' is required")
end
end
end
end
end
|