Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
55d76ff9
Commit
55d76ff9
authored
Oct 28, 2020
by
Eugenia Grieff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add service to import requirements from csv file
- Add tests
parent
1a6e58aa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
170 additions
and
0 deletions
+170
-0
ee/app/services/requirements_management/import_csv_service.rb
...pp/services/requirements_management/import_csv_service.rb
+64
-0
ee/spec/services/requirements_management/import_csv_service_spec.rb
...rvices/requirements_management/import_csv_service_spec.rb
+106
-0
No files found.
ee/app/services/requirements_management/import_csv_service.rb
0 → 100644
View file @
55d76ff9
# frozen_string_literal: true
module
RequirementsManagement
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
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
)
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
|
requirement_attributes
=
{
title:
row
[
:title
],
description:
row
[
:description
]
}
requirement
=
RequirementsManagement
::
CreateRequirementService
.
new
(
@project
,
@user
,
requirement_attributes
).
execute
if
requirement
.
persisted?
@results
[
:success
]
+=
1
else
@results
[
:error_lines
].
push
(
line_no
)
end
end
rescue
ArgumentError
,
CSV
::
MalformedCSVError
@results
[
:parse_error
]
=
true
end
def
email_results_to_user
Notify
.
import_requirements_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
end
end
end
ee/spec/services/requirements_management/import_csv_service_spec.rb
0 → 100644
View file @
55d76ff9
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
RequirementsManagement
::
ImportCsvService
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
subject
do
uploader
=
FileUploader
.
new
(
project
)
uploader
.
store!
(
file
)
described_class
.
new
(
user
,
project
,
uploader
).
execute
end
describe
'#execute'
do
context
'when user can create requirements'
do
before
do
project
.
add_reporter
(
user
)
stub_licensed_features
(
requirements:
true
)
end
context
'invalid file'
do
let
(
:file
)
{
fixture_file_upload
(
'spec/fixtures/banana_sample.gif'
)
}
it
'returns invalid file error'
do
expect
(
Notify
).
to
receive_message_chain
(
:import_requirements_csv_email
,
:deliver_later
)
expect
(
subject
[
:success
]).
to
eq
(
0
)
expect
(
subject
[
:parse_error
]).
to
eq
(
true
)
end
end
context
'comma delimited file'
do
let
(
:file
)
{
fixture_file_upload
(
'spec/fixtures/csv_comma.csv'
)
}
it
'imports CSV without errors'
do
expect
(
Notify
).
to
receive_message_chain
(
:import_requirements_csv_email
,
:deliver_later
)
expect
(
subject
[
:success
]).
to
eq
(
3
)
expect
(
subject
[
:error_lines
]).
to
eq
([])
expect
(
subject
[
:parse_error
]).
to
eq
(
false
)
end
it
'correctly sets the requirement attributes'
do
expect
{
subject
}.
to
change
{
project
.
requirements
.
count
}.
by
3
expect
(
project
.
requirements
.
reload
.
last
).
to
have_attributes
(
title:
'Title with quote"'
,
description:
'Description'
)
end
end
context
'tab delimited file with error row'
do
let
(
:file
)
{
fixture_file_upload
(
'spec/fixtures/csv_tab.csv'
)
}
it
'imports CSV with some error rows'
do
expect
(
Notify
).
to
receive_message_chain
(
:import_requirements_csv_email
,
:deliver_later
)
expect
(
subject
[
:success
]).
to
eq
(
2
)
expect
(
subject
[
:error_lines
]).
to
eq
([
3
])
expect
(
subject
[
:parse_error
]).
to
eq
(
false
)
end
it
'correctly sets the requirement attributes'
do
expect
{
subject
}.
to
change
{
project
.
requirements
.
count
}.
by
2
expect
(
project
.
requirements
.
reload
.
last
).
to
have_attributes
(
title:
'Hello'
,
description:
'World'
)
end
end
context
'semicolon delimited file with CRLF'
do
let
(
:file
)
{
fixture_file_upload
(
'spec/fixtures/csv_semicolon.csv'
)
}
it
'imports CSV with a blank row'
do
expect
(
Notify
).
to
receive_message_chain
(
:import_requirements_csv_email
,
:deliver_later
)
expect
(
subject
[
:success
]).
to
eq
(
3
)
expect
(
subject
[
:error_lines
]).
to
eq
([
4
])
expect
(
subject
[
:parse_error
]).
to
eq
(
false
)
end
it
'correctly sets the requirement attributes'
do
expect
{
subject
}.
to
change
{
project
.
requirements
.
count
}.
by
3
expect
(
project
.
requirements
.
reload
.
last
).
to
have_attributes
(
title:
'Hello'
,
description:
'World'
)
end
end
end
context
'when user cannot create requirements'
do
let
(
:file
)
{
fixture_file_upload
(
'spec/fixtures/csv_comma.csv'
)
}
it
'raises an exception'
do
expect
{
subject
}.
to
raise_error
(
Gitlab
::
Access
::
AccessDeniedError
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment