diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-11-19 11:27:35 +0300 |
commit | 7e9c479f7de77702622631cff2628a9c8dcbc627 (patch) | |
tree | c8f718a08e110ad7e1894510980d2155a6549197 /app/services/issues/import_csv_service.rb | |
parent | e852b0ae16db4052c1c567d9efa4facc81146e88 (diff) |
Add latest changes from gitlab-org/gitlab@13-6-stable-eev13.6.0-rc42
Diffstat (limited to 'app/services/issues/import_csv_service.rb')
-rw-r--r-- | app/services/issues/import_csv_service.rb | 61 |
1 files changed, 11 insertions, 50 deletions
diff --git a/app/services/issues/import_csv_service.rb b/app/services/issues/import_csv_service.rb index 60790ba3547..bce3ecc8bef 100644 --- a/app/services/issues/import_csv_service.rb +++ b/app/services/issues/import_csv_service.rb @@ -1,64 +1,25 @@ # frozen_string_literal: true module Issues - class ImportCsvService - def initialize(user, project, csv_io) - @user = user - @project = project - @csv_io = csv_io - @results = { success: 0, error_lines: [], parse_error: false } - end - + class ImportCsvService < Issuable::ImportCsv::BaseService def execute - process_csv - email_results_to_user - - @results - end - - private - - def process_csv - csv_data = @csv_io.open(&:read).force_encoding(Encoding::UTF_8) + record_import_attempt - csv_parsing_params = { - col_sep: detect_col_sep(csv_data.lines.first), - headers: true, - header_converters: :symbol - } - - CSV.new(csv_data, csv_parsing_params).each.with_index(2) do |row, line_no| - issue_attributes = { - title: row[:title], - description: row[:description] - } - - issue = Issues::CreateService.new(@project, @user, issue_attributes).execute - - if issue.persisted? - @results[:success] += 1 - else - @results[:error_lines].push(line_no) - end - end - rescue ArgumentError, CSV::MalformedCSVError - @results[:parse_error] = true + super end def email_results_to_user Notify.import_issues_csv_email(@user.id, @project.id, @results).deliver_later end - def detect_col_sep(header) - if header.include?(",") - "," - elsif header.include?(";") - ";" - elsif header.include?("\t") - "\t" - else - raise CSV::MalformedCSVError - end + private + + def create_issuable_class + Issues::CreateService + end + + def record_import_attempt + Issues::CsvImport.create!(user: @user, project: @project) end end end |