1# frozen_string_literal: true 2 3FactoryBot.define do 4 factory :go_module_commit, class: 'Commit' do 5 skip_create 6 7 transient do 8 files { { 'foo.txt' => 'content' } } 9 message { 'Message' } 10 # rubocop: disable FactoryBot/InlineAssociation 11 # We need a persisted project so we can create commits and tags 12 # in `commit` otherwise linting this factory with `build` strategy 13 # will fail. 14 project { create(:project, :repository) } 15 # rubocop: enable FactoryBot/InlineAssociation 16 17 service do 18 Files::MultiService.new( 19 project, 20 project.owner, 21 commit_message: message, 22 start_branch: project.repository.root_ref || 'master', 23 branch_name: project.repository.root_ref || 'master', 24 actions: files.map do |path, content| 25 { action: :create, file_path: path, content: content } 26 end 27 ) 28 end 29 30 tag { nil } 31 tag_message { nil } 32 33 commit do 34 r = service.execute 35 36 raise "operation failed: #{r}" unless r[:status] == :success 37 38 commit = project.repository.commit_by(oid: r[:result]) 39 40 if tag 41 r = Tags::CreateService.new(project, project.owner).execute(tag, commit.sha, tag_message) 42 43 raise "operation failed: #{r}" unless r[:status] == :success 44 end 45 46 commit 47 end 48 end 49 50 trait :files do 51 transient do 52 message { 'Add files' } 53 end 54 end 55 56 trait :package do 57 transient do 58 path { 'pkg' } 59 message { 'Add package' } 60 files { { "#{path}/b.go" => "package b\nfunc Bye() { println(\"Goodbye world!\") }\n" } } 61 end 62 end 63 64 trait :module do 65 transient do 66 name { nil } 67 message { 'Add module' } 68 host_prefix { "#{::Gitlab.config.gitlab.host}/#{project.path_with_namespace}" } 69 70 url { name ? "#{host_prefix}/#{name}" : host_prefix } 71 path { "#{name}/" } 72 73 files do 74 { 75 "#{path}go.mod" => "module #{url}\n", 76 "#{path}a.go" => "package a\nfunc Hi() { println(\"Hello world!\") }\n" 77 } 78 end 79 end 80 end 81 82 initialize_with do 83 commit 84 end 85 end 86end 87