Commit 629c7f77 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '213929-refactor-go-factories' into 'master'

Refactor GO factories

See merge request gitlab-org/gitlab!35301
parents 87fe437f 3aa0c69d
......@@ -5,8 +5,23 @@ FactoryBot.define do
skip_create
transient do
project { raise ArgumentError.new("project is required") }
service { raise ArgumentError.new("this factory cannot be used without specifying a trait") }
files { { 'foo.txt' => 'content' } }
message { 'Message' }
project { create(:project, :repository) }
service do
Files::MultiService.new(
project,
project.owner,
commit_message: message,
start_branch: project.repository.root_ref || 'master',
branch_name: project.repository.root_ref || 'master',
actions: files.map do |path, content|
{ action: :create, file_path: path, content: content }
end
)
end
tag { nil }
tag_message { nil }
......@@ -27,47 +42,18 @@ FactoryBot.define do
end
end
initialize_with do
commit
end
trait :files do
transient do
files { raise ArgumentError.new("files is required") }
message { 'Add files' }
end
service do
Files::MultiService.new(
project,
project.owner,
commit_message: message,
start_branch: project.repository.root_ref || 'master',
branch_name: project.repository.root_ref || 'master',
actions: files.map do |path, content|
{ action: :create, file_path: path, content: content }
end
)
end
end
trait :package do
transient do
path { raise ArgumentError.new("path is required") }
message { 'Add package' }
end
service do
Files::MultiService.new(
project,
project.owner,
commit_message: message,
start_branch: project.repository.root_ref || 'master',
branch_name: project.repository.root_ref || 'master',
actions: [
{ action: :create, file_path: path + '/b.go', content: "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" }
]
)
files { { "#{path}/b.go" => "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" } }
end
end
......@@ -75,39 +61,22 @@ FactoryBot.define do
transient do
name { nil }
message { 'Add module' }
host_prefix { "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" }
url do
v = "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}"
url { name ? "#{host_prefix}/#{name}" : host_prefix }
path { name.to_s + '/' }
if name
v + '/' + name
else
v
end
end
path do
if name
name + '/'
else
''
files do
{
"#{path}go.mod" => "module #{url}\n",
"#{path}a.go" => "package a\nfunc Hi() { println(\"Hello world!\") }\n"
}
end
end
end
service do
Files::MultiService.new(
project,
project.owner,
commit_message: message,
start_branch: project.repository.root_ref || 'master',
branch_name: project.repository.root_ref || 'master',
actions: [
{ action: :create, file_path: path + 'go.mod', content: "module #{url}\n" },
{ action: :create, file_path: path + 'a.go', content: "package a\nfunc Hi() { println(\"Hello world!\") }\n" }
]
)
end
initialize_with do
commit
end
end
end
......@@ -15,7 +15,7 @@ FactoryBot.define do
mod { create :go_module }
type { :commit }
commit { raise ArgumentError.new("commit is required") }
commit { mod.project.repository.head_commit }
name { nil }
semver { nil }
ref { nil }
......@@ -23,16 +23,49 @@ FactoryBot.define do
params { OpenStruct.new(mod: mod, type: type, commit: commit, name: name, semver: semver, ref: ref) }
trait :tagged do
name { raise ArgumentError.new("name is required") }
ref { mod.project.repository.find_tag(name) }
commit { ref.dereferenced_target }
name do
# This provides a sane default value, but in reality the caller should
# specify `name:`
# Find 'latest' semver tag (does not actually use semver precedence rules)
mod.project.repository.tags
.filter { |t| Packages::SemVer.match?(t.name, prefixed: true) }
.map { |t| Packages::SemVer.parse(t.name, prefixed: true) }
.max { |a, b| "#{a}" <=> "#{b}" }
.to_s
end
params { OpenStruct.new(mod: mod, type: :ref, commit: commit, semver: name, ref: ref) }
end
trait :pseudo do
transient do
prefix { raise ArgumentError.new("prefix is required") }
prefix do
# This provides a sane default value, but in reality the caller should
# specify `prefix:`
# This does not take into account that `commit` may be before the
# latest tag.
# Find 'latest' semver tag (does not actually use semver precedence rules)
v = mod.project.repository.tags
.filter { |t| Packages::SemVer.match?(t.name, prefixed: true) }
.map { |t| Packages::SemVer.parse(t.name, prefixed: true) }
.max { |a, b| "#{a}" <=> "#{b}" }
# Default if no semver tags exist
next 'v0.0.0' unless v
# Valid pseudo-versions are:
# vX.0.0-yyyymmddhhmmss-sha1337beef0, when no earlier tagged commit exists for X
# vX.Y.Z-pre.0.yyyymmddhhmmss-sha1337beef0, when most recent prior tag is vX.Y.Z-pre
# vX.Y.(Z+1)-0.yyyymmddhhmmss-sha1337beef0, when most recent prior tag is vX.Y.Z
v = v.with(patch: v.patch + 1) unless v.prerelease
"#{v}.0"
end
end
type { :pseudo }
......
......@@ -5,7 +5,8 @@ FactoryBot.define do
initialize_with { new(attributes[:project], attributes[:name], attributes[:path]) }
skip_create
project
project { create :project, :repository }
path { '' }
name { "#{Settings.build_gitlab_go_url}/#{project.full_path}#{path.empty? ? '' : '/'}#{path}" }
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment