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 ...@@ -5,8 +5,23 @@ FactoryBot.define do
skip_create skip_create
transient do transient do
project { raise ArgumentError.new("project is required") } files { { 'foo.txt' => 'content' } }
service { raise ArgumentError.new("this factory cannot be used without specifying a trait") } 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 { nil }
tag_message { nil } tag_message { nil }
...@@ -27,47 +42,18 @@ FactoryBot.define do ...@@ -27,47 +42,18 @@ FactoryBot.define do
end end
end end
initialize_with do
commit
end
trait :files do trait :files do
transient do transient do
files { raise ArgumentError.new("files is required") } files { raise ArgumentError.new("files is required") }
message { 'Add files' } message { 'Add files' }
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: files.map do |path, content|
{ action: :create, file_path: path, content: content }
end
)
end
end end
trait :package do trait :package do
transient do transient do
path { raise ArgumentError.new("path is required") } path { raise ArgumentError.new("path is required") }
message { 'Add package' } message { 'Add package' }
end files { { "#{path}/b.go" => "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" } }
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" }
]
)
end end
end end
...@@ -75,39 +61,22 @@ FactoryBot.define do ...@@ -75,39 +61,22 @@ FactoryBot.define do
transient do transient do
name { nil } name { nil }
message { 'Add module' } message { 'Add module' }
host_prefix { "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" }
url do url { name ? "#{host_prefix}/#{name}" : host_prefix }
v = "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" path { name.to_s + '/' }
if name
v + '/' + name
else
v
end
end
path do files do
if name {
name + '/' "#{path}go.mod" => "module #{url}\n",
else "#{path}a.go" => "package a\nfunc Hi() { println(\"Hello world!\") }\n"
'' }
end
end end
end end
end
service do initialize_with do
Files::MultiService.new( commit
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
end end
end end
end end
...@@ -15,7 +15,7 @@ FactoryBot.define do ...@@ -15,7 +15,7 @@ FactoryBot.define do
mod { create :go_module } mod { create :go_module }
type { :commit } type { :commit }
commit { raise ArgumentError.new("commit is required") } commit { mod.project.repository.head_commit }
name { nil } name { nil }
semver { nil } semver { nil }
ref { nil } ref { nil }
...@@ -23,16 +23,49 @@ FactoryBot.define do ...@@ -23,16 +23,49 @@ FactoryBot.define do
params { OpenStruct.new(mod: mod, type: type, commit: commit, name: name, semver: semver, ref: ref) } params { OpenStruct.new(mod: mod, type: type, commit: commit, name: name, semver: semver, ref: ref) }
trait :tagged do trait :tagged do
name { raise ArgumentError.new("name is required") }
ref { mod.project.repository.find_tag(name) } ref { mod.project.repository.find_tag(name) }
commit { ref.dereferenced_target } 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) } params { OpenStruct.new(mod: mod, type: :ref, commit: commit, semver: name, ref: ref) }
end end
trait :pseudo do trait :pseudo do
transient 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 end
type { :pseudo } type { :pseudo }
......
...@@ -5,7 +5,8 @@ FactoryBot.define do ...@@ -5,7 +5,8 @@ FactoryBot.define do
initialize_with { new(attributes[:project], attributes[:name], attributes[:path]) } initialize_with { new(attributes[:project], attributes[:name], attributes[:path]) }
skip_create skip_create
project project { create :project, :repository }
path { '' } path { '' }
name { "#{Settings.build_gitlab_go_url}/#{project.full_path}#{path.empty? ? '' : '/'}#{path}" } name { "#{Settings.build_gitlab_go_url}/#{project.full_path}#{path.empty? ? '' : '/'}#{path}" }
end 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