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
61e32695
Commit
61e32695
authored
Aug 09, 2018
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor maven packages code and add more specs
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
60c64b76
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
221 additions
and
33 deletions
+221
-33
ee/app/finders/packages/package_file_finder.rb
ee/app/finders/packages/package_file_finder.rb
+23
-0
ee/app/models/packages/package_file.rb
ee/app/models/packages/package_file.rb
+2
-2
ee/app/services/packages/create_package_file_service.rb
ee/app/services/packages/create_package_file_service.rb
+17
-15
ee/lib/api/maven_packages.rb
ee/lib/api/maven_packages.rb
+25
-16
ee/spec/finders/packages/maven_package_finder_spec.rb
ee/spec/finders/packages/maven_package_finder_spec.rb
+21
-0
ee/spec/finders/packages/package_file_finder_spec.rb
ee/spec/finders/packages/package_file_finder_spec.rb
+21
-0
ee/spec/services/packages/create_maven_package_service_spec.rb
...ec/services/packages/create_maven_package_service_spec.rb
+74
-0
ee/spec/services/packages/create_package_file_service_spec.rb
...pec/services/packages/create_package_file_service_spec.rb
+38
-0
No files found.
ee/app/finders/packages/package_file_finder.rb
0 → 100644
View file @
61e32695
# frozen_string_literal: true
class
Packages::PackageFileFinder
attr_reader
:package
,
:file_name
def
initialize
(
package
,
file_name
)
@package
=
package
@file_name
=
file_name
end
def
execute
package_files
.
last
end
def
execute!
package_files
.
last!
end
private
def
package_files
package
.
package_files
.
where
(
file_name:
file_name
)
end
end
ee/app/models/packages/package_file.rb
View file @
61e32695
...
...
@@ -3,8 +3,8 @@ class Packages::PackageFile < ActiveRecord::Base
belongs_to
:package
validates
:package
,
presence:
true
validates
:file
,
presence:
true
validates
:file_name
,
presence:
true
mount_uploader
:file
,
Packages
::
PackageFileUploader
scope
:recent
,
->
{
reorder
(
id: :desc
)
}
end
ee/app/services/packages/create_package_file_service.rb
View file @
61e32695
# frozen_string_literal: true
class
Packages::CreatePackageFileService
module
Packages
class
CreatePackageFileService
attr_reader
:package
,
:params
def
initialize
(
package
,
params
)
...
...
@@ -17,4 +18,5 @@ class Packages::CreatePackageFileService
file_md5:
params
[
:file_md5
]
)
end
end
end
ee/lib/api/maven_packages.rb
View file @
61e32695
...
...
@@ -43,6 +43,17 @@ module API
[
file_name
,
nil
]
end
end
def
verify_package_file
(
package_file
,
uploaded_file
)
stored_sha1
=
Digest
::
SHA256
.
hexdigest
(
package_file
.
file_sha1
)
expected_sha1
=
uploaded_file
.
sha256
if
stored_sha1
==
expected_sha1
no_content!
else
conflict!
end
end
end
params
do
...
...
@@ -64,7 +75,8 @@ module API
package
=
::
Packages
::
MavenPackageFinder
.
new
(
user_project
,
params
[
:path
]).
execute!
package_file
=
package
.
package_files
.
recent
.
find_by!
(
file_name:
file_name
)
package_file
=
::
Packages
::
PackageFileFinder
.
new
(
package
,
file_name
).
execute!
case
format
when
'md5'
...
...
@@ -142,23 +154,20 @@ module API
version:
version
}
package
=
::
Packages
::
CreateMavenPackageService
.
new
(
user_project
,
current_user
,
package_params
).
execute
package
=
::
Packages
::
CreateMavenPackageService
.
new
(
user_project
,
current_user
,
package_params
).
execute
end
if
format
# TODO: Extract in separate method
if
format
==
'sha1'
package_file
=
package
.
package_files
.
recent
.
find_by!
(
file_name:
file_name
)
stored_sha1
=
Digest
::
SHA256
.
hexdigest
(
package_file
.
file_sha1
)
expected_sha1
=
uploaded_file
.
sha256
case
format
when
'sha1'
# After uploading a file, Maven tries to upload a sha1 and md5 version of it.
# Since we store md5/sha1 in database we simply need to validate our hash
# against one uploaded by Maven. We do this for `sha1` format.
package_file
=
::
Packages
::
PackageFileFinder
.
new
(
package
,
file_name
).
execute!
if
stored_sha1
==
expected_sha1
no_content!
else
conflict!
end
end
else
verify_package_file
(
package_file
,
uploaded_file
)
when
nil
file_params
=
{
file:
uploaded_file
,
size:
params
[
'file.size'
],
...
...
ee/spec/finders/packages/maven_package_finder_spec.rb
0 → 100644
View file @
61e32695
# frozen_string_literal: true
require
'spec_helper'
describe
Packages
::
MavenPackageFinder
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:package
)
{
create
(
:maven_package
,
project:
project
)
}
describe
'#execute!'
do
it
'returns a package'
do
finder
=
described_class
.
new
(
project
,
package
.
maven_metadatum
.
path
)
expect
(
finder
.
execute!
).
to
eq
(
package
)
end
it
'raises an error'
do
finder
=
described_class
.
new
(
project
,
'com/example/my-app/1.0-SNAPSHOT'
)
expect
{
finder
.
execute!
}.
to
raise_error
(
ActiveRecord
::
RecordNotFound
)
end
end
end
ee/spec/finders/packages/package_file_finder_spec.rb
0 → 100644
View file @
61e32695
# frozen_string_literal: true
require
'spec_helper'
describe
Packages
::
PackageFileFinder
do
let
(
:package
)
{
create
(
:maven_package
)
}
let
(
:package_file
)
{
package
.
package_files
.
first
}
describe
'#execute!'
do
it
'returns a package file'
do
finder
=
described_class
.
new
(
package
,
package_file
.
file_name
)
expect
(
finder
.
execute!
).
to
eq
(
package_file
)
end
it
'raises an error'
do
finder
=
described_class
.
new
(
package
,
'unknown.jpg'
)
expect
{
finder
.
execute!
}.
to
raise_error
(
ActiveRecord
::
RecordNotFound
)
end
end
end
ee/spec/services/packages/create_maven_package_service_spec.rb
0 → 100644
View file @
61e32695
# frozen_string_literal: true
require
'spec_helper'
describe
Packages
::
CreateMavenPackageService
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:app_name
)
{
'my-app'
.
freeze
}
let
(
:version
)
{
'1.0-SNAPSHOT'
.
freeze
}
let
(
:path
)
{
"my/company/app/
#{
app_name
}
"
}
let
(
:path_with_version
)
{
"
#{
path
}
/
#{
version
}
"
}
describe
'#execute'
do
context
'with version'
do
let
(
:params
)
do
{
path:
path_with_version
,
name:
path
,
version:
version
}
end
it
'creates a new package with metadatum'
do
package
=
described_class
.
new
(
project
,
user
,
params
).
execute
expect
(
package
).
to
be_valid
expect
(
package
.
name
).
to
eq
(
path
)
expect
(
package
.
version
).
to
eq
(
version
)
expect
(
package
.
maven_metadatum
).
to
be_valid
expect
(
package
.
maven_metadatum
.
path
).
to
eq
(
path_with_version
)
expect
(
package
.
maven_metadatum
.
app_group
).
to
eq
(
'my.company.app'
)
expect
(
package
.
maven_metadatum
.
app_name
).
to
eq
(
app_name
)
expect
(
package
.
maven_metadatum
.
app_version
).
to
eq
(
version
)
end
end
context
'without version'
do
let
(
:params
)
do
{
path:
path
,
name:
path
,
version:
nil
}
end
it
'creates a new package with metadatum'
do
package
=
described_class
.
new
(
project
,
user
,
params
).
execute
expect
(
package
).
to
be_valid
expect
(
package
.
name
).
to
eq
(
path
)
expect
(
package
.
version
).
to
be
nil
expect
(
package
.
maven_metadatum
).
to
be_valid
expect
(
package
.
maven_metadatum
.
path
).
to
eq
(
path
)
expect
(
package
.
maven_metadatum
.
app_group
).
to
eq
(
'my.company.app'
)
expect
(
package
.
maven_metadatum
.
app_name
).
to
eq
(
app_name
)
expect
(
package
.
maven_metadatum
.
app_version
).
to
be
nil
end
end
context
'path is missing'
do
let
(
:params
)
do
{
name:
path
,
version:
version
}
end
it
'raises an error'
do
service
=
described_class
.
new
(
project
,
user
,
params
)
expect
{
service
.
execute
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
end
end
end
end
ee/spec/services/packages/create_package_file_service_spec.rb
0 → 100644
View file @
61e32695
# frozen_string_literal: true
require
'spec_helper'
describe
Packages
::
CreatePackageFileService
do
let
(
:package
)
{
create
(
:maven_package
)
}
describe
'#execute'
do
context
'with valid params'
do
let
(
:params
)
do
{
file:
Tempfile
.
new
,
file_name:
'foo.jar'
}
end
it
'creates a new package file'
do
package_file
=
described_class
.
new
(
package
,
params
).
execute
expect
(
package_file
).
to
be_valid
expect
(
package_file
.
file_name
).
to
eq
(
'foo.jar'
)
end
end
context
'file is missing'
do
let
(
:params
)
do
{
file_name:
'foo.jar'
}
end
it
'raises an error'
do
service
=
described_class
.
new
(
package
,
params
)
expect
{
service
.
execute
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
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