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
619d8693
Commit
619d8693
authored
Jun 10, 2020
by
Ethan Reesor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move Go models to Packages::Go
parent
8132dd48
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
219 additions
and
206 deletions
+219
-206
ee/app/finders/packages/go/module_finder.rb
ee/app/finders/packages/go/module_finder.rb
+1
-1
ee/app/finders/packages/go/version_finder.rb
ee/app/finders/packages/go/version_finder.rb
+1
-1
ee/app/models/packages/go/module.rb
ee/app/models/packages/go/module.rb
+93
-0
ee/app/models/packages/go/module_version.rb
ee/app/models/packages/go/module_version.rb
+115
-0
ee/app/models/packages/go_module.rb
ee/app/models/packages/go_module.rb
+0
-89
ee/app/models/packages/go_module_version.rb
ee/app/models/packages/go_module_version.rb
+0
-111
ee/changelogs/unreleased/relocate-go-models.yml
ee/changelogs/unreleased/relocate-go-models.yml
+5
-0
ee/spec/factories/go_module_versions.rb
ee/spec/factories/go_module_versions.rb
+1
-1
ee/spec/factories/go_modules.rb
ee/spec/factories/go_modules.rb
+1
-1
ee/spec/models/packages/go/module_spec.rb
ee/spec/models/packages/go/module_spec.rb
+1
-1
ee/spec/models/packages/go/module_version_spec.rb
ee/spec/models/packages/go/module_version_spec.rb
+1
-1
No files found.
ee/app/finders/packages/go/module_finder.rb
View file @
619d8693
...
...
@@ -22,7 +22,7 @@ module Packages
module_project_path
=
module_path
.
shift
(
project_path
.
length
)
return
unless
module_project_path
==
project_path
Packages
::
GoModule
.
new
(
@project
,
@module_name
,
module_path
.
join
(
'/'
))
Packages
::
Go
::
Module
.
new
(
@project
,
@module_name
,
module_path
.
join
(
'/'
))
end
end
end
...
...
ee/app/finders/packages/go/version_finder.rb
View file @
619d8693
...
...
@@ -24,7 +24,7 @@ module Packages
if
pseudo_version?
target
semver
=
parse_semver
(
target
)
commit
=
pseudo_version_commit
(
@mod
.
project
,
semver
)
Packages
::
GoModuleVersion
.
new
(
@mod
,
:pseudo
,
commit
,
name:
target
,
semver:
semver
)
Packages
::
Go
::
ModuleVersion
.
new
(
@mod
,
:pseudo
,
commit
,
name:
target
,
semver:
semver
)
else
@mod
.
version_by
(
ref:
target
)
end
...
...
ee/app/models/packages/go/module.rb
0 → 100644
View file @
619d8693
# frozen_string_literal: true
module
Packages
module
Go
class
Module
include
Gitlab
::
Utils
::
StrongMemoize
attr_reader
:project
,
:name
,
:path
def
initialize
(
project
,
name
,
path
)
@project
=
project
@name
=
name
@path
=
path
end
def
versions
strong_memoize
(
:versions
)
{
Packages
::
Go
::
VersionFinder
.
new
(
self
).
execute
}
end
def
version_by
(
ref:
nil
,
commit:
nil
)
raise
ArgumentError
.
new
'no filter specified'
unless
ref
||
commit
raise
ArgumentError
.
new
'ref and commit are mutually exclusive'
if
ref
&&
commit
if
commit
return
version_by_sha
(
commit
)
if
commit
.
is_a?
String
return
version_by_commit
(
commit
)
end
return
version_by_name
(
ref
)
if
ref
.
is_a?
String
version_by_ref
(
ref
)
end
def
path_valid?
(
major
)
m
=
/\/v(\d+)$/i
.
match
(
@name
)
case
major
when
0
,
1
m
.
nil?
else
!
m
.
nil?
&&
m
[
1
].
to_i
==
major
end
end
def
gomod_valid?
(
gomod
)
if
Feature
.
enabled?
(
:go_proxy_disable_gomod_validation
,
@project
)
return
gomod
&
.
start_with?
(
"module "
)
end
gomod
&
.
split
(
"
\n
"
,
2
)
&
.
first
==
"module
#{
@name
}
"
end
private
def
version_by_name
(
name
)
# avoid a Gitaly call if possible
if
strong_memoized?
(
:versions
)
v
=
versions
.
find
{
|
v
|
v
.
name
==
ref
}
return
v
if
v
end
ref
=
@project
.
repository
.
find_tag
(
name
)
||
@project
.
repository
.
find_branch
(
name
)
return
unless
ref
version_by_ref
(
ref
)
end
def
version_by_ref
(
ref
)
# reuse existing versions
if
strong_memoized?
(
:versions
)
v
=
versions
.
find
{
|
v
|
v
.
ref
==
ref
}
return
v
if
v
end
commit
=
ref
.
dereferenced_target
semver
=
Packages
::
SemVer
.
parse
(
ref
.
name
,
prefixed:
true
)
Packages
::
Go
::
ModuleVersion
.
new
(
self
,
:ref
,
commit
,
ref:
ref
,
semver:
semver
)
end
def
version_by_sha
(
sha
)
commit
=
@project
.
commit_by
(
oid:
sha
)
return
unless
ref
version_by_commit
(
commit
)
end
def
version_by_commit
(
commit
)
Packages
::
Go
::
ModuleVersion
.
new
(
self
,
:commit
,
commit
)
end
end
end
end
ee/app/models/packages/go/module_version.rb
0 → 100644
View file @
619d8693
# frozen_string_literal: true
module
Packages
module
Go
class
ModuleVersion
include
Gitlab
::
Utils
::
StrongMemoize
VALID_TYPES
=
%i[ref commit pseudo]
.
freeze
attr_reader
:mod
,
:type
,
:ref
,
:commit
delegate
:major
,
to: :@semver
,
allow_nil:
true
delegate
:minor
,
to: :@semver
,
allow_nil:
true
delegate
:patch
,
to: :@semver
,
allow_nil:
true
delegate
:prerelease
,
to: :@semver
,
allow_nil:
true
delegate
:build
,
to: :@semver
,
allow_nil:
true
def
initialize
(
mod
,
type
,
commit
,
name:
nil
,
semver:
nil
,
ref:
nil
)
raise
ArgumentError
.
new
(
"invalid type '
#{
type
}
'"
)
unless
VALID_TYPES
.
include?
type
raise
ArgumentError
.
new
(
"mod is required"
)
unless
mod
raise
ArgumentError
.
new
(
"commit is required"
)
unless
commit
if
type
==
:ref
raise
ArgumentError
.
new
(
"ref is required"
)
unless
ref
elsif
type
==
:pseudo
raise
ArgumentError
.
new
(
"name is required"
)
unless
name
raise
ArgumentError
.
new
(
"semver is required"
)
unless
semver
end
@mod
=
mod
@type
=
type
@commit
=
commit
@name
=
name
if
name
@semver
=
semver
if
semver
@ref
=
ref
if
ref
end
def
name
@name
||
@ref
&
.
name
end
def
full_name
"
#{
mod
.
name
}
@
#{
name
||
commit
.
sha
}
"
end
def
gomod
strong_memoize
(
:gomod
)
do
if
strong_memoized?
(
:blobs
)
blob_at
(
@mod
.
path
+
'/go.mod'
)
elsif
@mod
.
path
.
empty?
@mod
.
project
.
repository
.
blob_at
(
@commit
.
sha
,
'go.mod'
)
&
.
data
else
@mod
.
project
.
repository
.
blob_at
(
@commit
.
sha
,
@mod
.
path
+
'/go.mod'
)
&
.
data
end
end
end
def
archive
suffix_len
=
@mod
.
path
==
''
?
0
:
@mod
.
path
.
length
+
1
Zip
::
OutputStream
.
write_buffer
do
|
zip
|
files
.
each
do
|
file
|
zip
.
put_next_entry
"
#{
full_name
}
/
#{
file
[
suffix_len
...
]
}
"
zip
.
write
blob_at
(
file
)
end
end
end
def
files
strong_memoize
(
:files
)
do
ls_tree
.
filter
{
|
e
|
!
excluded
.
any?
{
|
n
|
e
.
start_with?
n
}
}
end
end
def
excluded
strong_memoize
(
:excluded
)
do
ls_tree
.
filter
{
|
f
|
f
.
end_with?
(
'/go.mod'
)
&&
f
!=
@mod
.
path
+
'/go.mod'
}
.
map
{
|
f
|
f
[
0
..-
7
]
}
end
end
def
valid?
@mod
.
path_valid?
(
major
)
&&
@mod
.
gomod_valid?
(
gomod
)
end
private
def
blob_at
(
path
)
return
if
path
.
nil?
||
path
.
empty?
path
=
path
[
1
..
]
if
path
.
start_with?
'/'
blobs
.
find
{
|
x
|
x
.
path
==
path
}
&
.
data
end
def
blobs
strong_memoize
(
:blobs
)
{
@mod
.
project
.
repository
.
batch_blobs
(
files
.
map
{
|
x
|
[
@commit
.
sha
,
x
]
})
}
end
def
ls_tree
strong_memoize
(
:ls_tree
)
do
path
=
if
@mod
.
path
.
empty?
'.'
else
@mod
.
path
end
@mod
.
project
.
repository
.
gitaly_repository_client
.
search_files_by_name
(
@commit
.
sha
,
path
)
end
end
end
end
end
ee/app/models/packages/go_module.rb
deleted
100644 → 0
View file @
8132dd48
# frozen_string_literal: true
class
Packages::GoModule
include
Gitlab
::
Utils
::
StrongMemoize
attr_reader
:project
,
:name
,
:path
def
initialize
(
project
,
name
,
path
)
@project
=
project
@name
=
name
@path
=
path
end
def
versions
strong_memoize
(
:versions
)
{
Packages
::
Go
::
VersionFinder
.
new
(
self
).
execute
}
end
def
version_by
(
ref:
nil
,
commit:
nil
)
raise
ArgumentError
.
new
'no filter specified'
unless
ref
||
commit
raise
ArgumentError
.
new
'ref and commit are mutually exclusive'
if
ref
&&
commit
if
commit
return
version_by_sha
(
commit
)
if
commit
.
is_a?
String
return
version_by_commit
(
commit
)
end
return
version_by_name
(
ref
)
if
ref
.
is_a?
String
version_by_ref
(
ref
)
end
def
path_valid?
(
major
)
m
=
/\/v(\d+)$/i
.
match
(
@name
)
case
major
when
0
,
1
m
.
nil?
else
!
m
.
nil?
&&
m
[
1
].
to_i
==
major
end
end
def
gomod_valid?
(
gomod
)
if
Feature
.
enabled?
(
:go_proxy_disable_gomod_validation
,
@project
)
return
gomod
&
.
start_with?
(
"module "
)
end
gomod
&
.
split
(
"
\n
"
,
2
)
&
.
first
==
"module
#{
@name
}
"
end
private
def
version_by_name
(
name
)
# avoid a Gitaly call if possible
if
strong_memoized?
(
:versions
)
v
=
versions
.
find
{
|
v
|
v
.
name
==
ref
}
return
v
if
v
end
ref
=
@project
.
repository
.
find_tag
(
name
)
||
@project
.
repository
.
find_branch
(
name
)
return
unless
ref
version_by_ref
(
ref
)
end
def
version_by_ref
(
ref
)
# reuse existing versions
if
strong_memoized?
(
:versions
)
v
=
versions
.
find
{
|
v
|
v
.
ref
==
ref
}
return
v
if
v
end
commit
=
ref
.
dereferenced_target
semver
=
Packages
::
SemVer
.
parse
(
ref
.
name
,
prefixed:
true
)
Packages
::
GoModuleVersion
.
new
(
self
,
:ref
,
commit
,
ref:
ref
,
semver:
semver
)
end
def
version_by_sha
(
sha
)
commit
=
@project
.
commit_by
(
oid:
sha
)
return
unless
ref
version_by_commit
(
commit
)
end
def
version_by_commit
(
commit
)
Packages
::
GoModuleVersion
.
new
(
self
,
:commit
,
commit
)
end
end
ee/app/models/packages/go_module_version.rb
deleted
100644 → 0
View file @
8132dd48
# frozen_string_literal: true
class
Packages::GoModuleVersion
include
Gitlab
::
Utils
::
StrongMemoize
VALID_TYPES
=
%i[ref commit pseudo]
.
freeze
attr_reader
:mod
,
:type
,
:ref
,
:commit
delegate
:major
,
to: :@semver
,
allow_nil:
true
delegate
:minor
,
to: :@semver
,
allow_nil:
true
delegate
:patch
,
to: :@semver
,
allow_nil:
true
delegate
:prerelease
,
to: :@semver
,
allow_nil:
true
delegate
:build
,
to: :@semver
,
allow_nil:
true
def
initialize
(
mod
,
type
,
commit
,
name:
nil
,
semver:
nil
,
ref:
nil
)
raise
ArgumentError
.
new
(
"invalid type '
#{
type
}
'"
)
unless
VALID_TYPES
.
include?
type
raise
ArgumentError
.
new
(
"mod is required"
)
unless
mod
raise
ArgumentError
.
new
(
"commit is required"
)
unless
commit
if
type
==
:ref
raise
ArgumentError
.
new
(
"ref is required"
)
unless
ref
elsif
type
==
:pseudo
raise
ArgumentError
.
new
(
"name is required"
)
unless
name
raise
ArgumentError
.
new
(
"semver is required"
)
unless
semver
end
@mod
=
mod
@type
=
type
@commit
=
commit
@name
=
name
if
name
@semver
=
semver
if
semver
@ref
=
ref
if
ref
end
def
name
@name
||
@ref
&
.
name
end
def
full_name
"
#{
mod
.
name
}
@
#{
name
||
commit
.
sha
}
"
end
def
gomod
strong_memoize
(
:gomod
)
do
if
strong_memoized?
(
:blobs
)
blob_at
(
@mod
.
path
+
'/go.mod'
)
elsif
@mod
.
path
.
empty?
@mod
.
project
.
repository
.
blob_at
(
@commit
.
sha
,
'go.mod'
)
&
.
data
else
@mod
.
project
.
repository
.
blob_at
(
@commit
.
sha
,
@mod
.
path
+
'/go.mod'
)
&
.
data
end
end
end
def
archive
suffix_len
=
@mod
.
path
==
''
?
0
:
@mod
.
path
.
length
+
1
Zip
::
OutputStream
.
write_buffer
do
|
zip
|
files
.
each
do
|
file
|
zip
.
put_next_entry
"
#{
full_name
}
/
#{
file
[
suffix_len
...
]
}
"
zip
.
write
blob_at
(
file
)
end
end
end
def
files
strong_memoize
(
:files
)
do
ls_tree
.
filter
{
|
e
|
!
excluded
.
any?
{
|
n
|
e
.
start_with?
n
}
}
end
end
def
excluded
strong_memoize
(
:excluded
)
do
ls_tree
.
filter
{
|
f
|
f
.
end_with?
(
'/go.mod'
)
&&
f
!=
@mod
.
path
+
'/go.mod'
}
.
map
{
|
f
|
f
[
0
..-
7
]
}
end
end
def
valid?
@mod
.
path_valid?
(
major
)
&&
@mod
.
gomod_valid?
(
gomod
)
end
private
def
blob_at
(
path
)
return
if
path
.
nil?
||
path
.
empty?
path
=
path
[
1
..
]
if
path
.
start_with?
'/'
blobs
.
find
{
|
x
|
x
.
path
==
path
}
&
.
data
end
def
blobs
strong_memoize
(
:blobs
)
{
@mod
.
project
.
repository
.
batch_blobs
(
files
.
map
{
|
x
|
[
@commit
.
sha
,
x
]
})
}
end
def
ls_tree
strong_memoize
(
:ls_tree
)
do
path
=
if
@mod
.
path
.
empty?
'.'
else
@mod
.
path
end
@mod
.
project
.
repository
.
gitaly_repository_client
.
search_files_by_name
(
@commit
.
sha
,
path
)
end
end
end
ee/changelogs/unreleased/relocate-go-models.yml
0 → 100644
View file @
619d8693
---
title
:
Relocate Go models
merge_request
:
34338
author
:
Ethan Reesor (@firelizzard)
type
:
other
ee/spec/factories/go_module_versions.rb
View file @
619d8693
# frozen_string_literal: true
FactoryBot
.
define
do
factory
:go_module_version
,
class:
'Packages::GoModuleVersion'
do
factory
:go_module_version
,
class:
'Packages::Go
::
ModuleVersion'
do
skip_create
initialize_with
do
...
...
ee/spec/factories/go_modules.rb
View file @
619d8693
# frozen_string_literal: true
FactoryBot
.
define
do
factory
:go_module
,
class:
'Packages::GoModule'
do
factory
:go_module
,
class:
'Packages::Go
::
Module'
do
initialize_with
{
new
(
attributes
[
:project
],
attributes
[
:name
],
attributes
[
:path
])
}
skip_create
...
...
ee/spec/models/packages/go
_
module_spec.rb
→
ee/spec/models/packages/go
/
module_spec.rb
View file @
619d8693
...
...
@@ -2,7 +2,7 @@
require
'spec_helper'
RSpec
.
describe
Packages
::
GoModule
,
type: :model
do
RSpec
.
describe
Packages
::
Go
::
Module
,
type: :model
do
before
do
stub_feature_flags
(
go_proxy_disable_gomod_validation:
false
)
end
...
...
ee/spec/models/packages/go
_
module_version_spec.rb
→
ee/spec/models/packages/go
/
module_version_spec.rb
View file @
619d8693
...
...
@@ -2,7 +2,7 @@
require
'spec_helper'
RSpec
.
describe
Packages
::
GoModuleVersion
,
type: :model
do
RSpec
.
describe
Packages
::
Go
::
ModuleVersion
,
type: :model
do
let_it_be
(
:user
)
{
create
:user
}
let_it_be
(
:project
)
{
create
:project_empty_repo
,
creator:
user
,
path:
'my-go-lib'
}
let_it_be
(
:mod
)
{
create
:go_module
,
project:
project
}
...
...
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