debian-mirror-gitlab/app/services/import_csv/base_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.1 KiB
Ruby
Raw Normal View History

2023-04-23 21:23:45 +05:30
# frozen_string_literal: true
module ImportCsv
class BaseService
2023-05-27 22:25:52 +05:30
include Gitlab::Utils::StrongMemoize
2023-04-23 21:23:45 +05:30
def initialize(user, project, csv_io)
@user = user
@project = project
@csv_io = csv_io
@results = { success: 0, error_lines: [], parse_error: false }
end
2023-05-27 22:25:52 +05:30
PreprocessError = Class.new(StandardError)
2023-04-23 21:23:45 +05:30
def execute
process_csv
email_results_to_user
results
end
def email_results_to_user
raise NotImplementedError
end
private
attr_reader :user, :project, :csv_io, :results
def attributes_for(row)
raise NotImplementedError
end
def validate_headers_presence!(headers)
raise NotImplementedError
end
def create_object_class
raise NotImplementedError
end
2023-05-27 22:25:52 +05:30
def validate_structure!
header_line = csv_data.lines.first
2023-06-20 00:43:36 +05:30
raise CSV::MalformedCSVError.new('File is empty, no headers found', 1) if header_line.blank?
2023-05-27 22:25:52 +05:30
validate_headers_presence!(header_line)
detect_col_sep
end
def preprocess!
# any logic can be added in subclasses if needed
# hence just a no-op rather than NotImplementedError
end
2023-04-23 21:23:45 +05:30
def process_csv
2023-05-27 22:25:52 +05:30
validate_structure!
preprocess!
2023-04-23 21:23:45 +05:30
with_csv_lines.each do |row, line_no|
attributes = attributes_for(row)
if create_object(attributes)&.persisted?
results[:success] += 1
else
results[:error_lines].push(line_no)
end
end
2023-05-27 22:25:52 +05:30
rescue ArgumentError, CSV::MalformedCSVError => e
2023-04-23 21:23:45 +05:30
results[:parse_error] = true
2023-05-27 22:25:52 +05:30
results[:error_lines].push(e.line_number) if e.respond_to?(:line_number)
rescue PreprocessError
results[:parse_error] = false
2023-04-23 21:23:45 +05:30
end
def with_csv_lines
CSV.new(
csv_data,
2023-05-27 22:25:52 +05:30
col_sep: detect_col_sep,
2023-04-23 21:23:45 +05:30
headers: true,
header_converters: :symbol
).each.with_index(2)
end
2023-05-27 22:25:52 +05:30
def csv_data
@csv_io.open(&:read).force_encoding(Encoding::UTF_8)
end
strong_memoize_attr :csv_data
def detect_col_sep
header = csv_data.lines.first
2023-04-23 21:23:45 +05:30
if header.include?(",")
","
elsif header.include?(";")
";"
elsif header.include?("\t")
"\t"
else
raise CSV::MalformedCSVError.new('Invalid CSV format', 1)
end
end
2023-05-27 22:25:52 +05:30
strong_memoize_attr :detect_col_sep
2023-04-23 21:23:45 +05:30
def create_object(attributes)
# NOTE: CSV imports are performed by workers, so we do not have a request context in order
# to create a SpamParams object to pass to the issuable create service.
spam_params = nil
# default_params can be extracted into a method if we need
# to support creation of objects that belongs to groups.
default_params = { container: project,
current_user: user,
params: attributes,
spam_params: spam_params }
create_service = create_object_class.new(**default_params.merge(extra_create_service_params))
create_service.execute_without_rate_limiting
end
# Overidden in subclasses to support specific parameters
def extra_create_service_params
{}
end
end
end