Commit d8c4556d authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor code reponsible for creating builds

This removes duplications and extracts method that builds build-jobs
without persisting those objects, to a separate method.
parent a21d084d
...@@ -66,10 +66,7 @@ module Ci ...@@ -66,10 +66,7 @@ module Ci
# We use around_transition to create builds for next stage as soon as possible, before the `after_*` is executed # We use around_transition to create builds for next stage as soon as possible, before the `after_*` is executed
around_transition any => [:success, :failed, :canceled] do |build, block| around_transition any => [:success, :failed, :canceled] do |build, block|
block.call block.call
if build.commit build.commit.create_next_builds(build) if build.commit
build.commit.create_next_builds(build)
build.commit.save
end
end end
after_transition any => [:success, :failed, :canceled] do |build| after_transition any => [:success, :failed, :canceled] do |build|
......
...@@ -89,13 +89,22 @@ module Ci ...@@ -89,13 +89,22 @@ module Ci
trigger_requests.any? trigger_requests.any?
end end
def create_builds(user, trigger_request = nil) def build_builds_for_stage(stage, user, status, trigger_request)
CreateBuildsService.new(self).execute(stage, user, status, trigger_request)
end
def build_builds(user, status = 'success', trigger_request = nil)
return unless config_processor return unless config_processor
config_processor.stages.any? do |stage| config_processor.stages.any? do |stage|
CreateBuildsService.new(self).execute(stage, user, 'success', trigger_request).present? build_builds_for_stage(stage, user, status, trigger_request).present?
end end
end end
def create_builds(user, trigger_request = nil)
build_builds(user, 'success', trigger_request)
save!
end
def create_next_builds(build) def create_next_builds(build)
return unless config_processor return unless config_processor
...@@ -112,9 +121,11 @@ module Ci ...@@ -112,9 +121,11 @@ module Ci
prior_status = prior_builds.status prior_status = prior_builds.status
# create builds for next stages based # create builds for next stages based
next_stages.any? do |stage| have_builds = next_stages.any? do |stage|
CreateBuildsService.new(self).execute(stage, build.user, prior_status, build.trigger_request).present? build_builds_for_stage(stage, build.user, prior_status, build.trigger_request).present?
end end
save! if have_builds
end end
def retried def retried
......
...@@ -15,7 +15,6 @@ module Ci ...@@ -15,7 +15,6 @@ module Ci
) )
if ci_commit.create_builds(nil, trigger_request) if ci_commit.create_builds(nil, trigger_request)
ci_commit.save
trigger_request trigger_request
end end
end end
......
...@@ -25,7 +25,7 @@ class CreateCommitBuildsService ...@@ -25,7 +25,7 @@ class CreateCommitBuildsService
# Create builds for commit and # Create builds for commit and
# skip saving pipeline when there are no builds # skip saving pipeline when there are no builds
unless commit.create_builds(user) unless commit.build_builds(user)
# Save object when there are yaml errors # Save object when there are yaml errors
unless commit.yaml_errors.present? unless commit.yaml_errors.present?
commit.errors.add(:base, 'No builds created') commit.errors.add(:base, 'No builds created')
......
...@@ -34,10 +34,7 @@ feature 'Merge request created from fork' do ...@@ -34,10 +34,7 @@ feature 'Merge request created from fork' do
ref: merge_request.source_branch) ref: merge_request.source_branch)
end end
background do background { pipeline.create_builds(user) }
pipeline.create_builds(user)
pipeline.save
end
scenario 'user visits a pipelines page', js: true do scenario 'user visits a pipelines page', js: true do
visit_merge_request(merge_request) visit_merge_request(merge_request)
......
...@@ -55,15 +55,11 @@ describe Ci::Commit, models: true do ...@@ -55,15 +55,11 @@ describe Ci::Commit, models: true do
let!(:commit) { FactoryGirl.create :ci_commit, project: project, ref: 'master', tag: false } let!(:commit) { FactoryGirl.create :ci_commit, project: project, ref: 'master', tag: false }
def create_builds(trigger_request = nil) def create_builds(trigger_request = nil)
if commit.create_builds(nil, trigger_request) commit.create_builds(nil, trigger_request)
commit.save
end
end end
def create_next_builds def create_next_builds
if commit.create_next_builds(commit.builds.order(:id).last) commit.create_next_builds(commit.builds.order(:id).last)
commit.save
end
end end
it 'creates builds' do it 'creates builds' do
......
...@@ -22,7 +22,6 @@ describe Ci::API::API do ...@@ -22,7 +22,6 @@ describe Ci::API::API do
it "should start a build" do it "should start a build" do
commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master') commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master')
commit.create_builds(nil) commit.create_builds(nil)
commit.save
build = commit.builds.first build = commit.builds.first
post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin } post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
...@@ -59,7 +58,6 @@ describe Ci::API::API do ...@@ -59,7 +58,6 @@ describe Ci::API::API do
it "returns options" do it "returns options" do
commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master') commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master')
commit.create_builds(nil) commit.create_builds(nil)
commit.save
post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin } post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
...@@ -70,7 +68,6 @@ describe Ci::API::API do ...@@ -70,7 +68,6 @@ describe Ci::API::API do
it "returns variables" do it "returns variables" do
commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master') commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master')
commit.create_builds(nil) commit.create_builds(nil)
commit.save
project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value") project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value")
post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin } post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
...@@ -90,7 +87,6 @@ describe Ci::API::API do ...@@ -90,7 +87,6 @@ describe Ci::API::API do
trigger_request = FactoryGirl.create(:ci_trigger_request_with_variables, commit: commit, trigger: trigger) trigger_request = FactoryGirl.create(:ci_trigger_request_with_variables, commit: commit, trigger: trigger)
commit.create_builds(nil, trigger_request) commit.create_builds(nil, trigger_request)
commit.save
project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value") project.variables << Ci::Variable.new(key: "SECRET_KEY", value: "secret_value")
post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin } post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
...@@ -109,7 +105,6 @@ describe Ci::API::API do ...@@ -109,7 +105,6 @@ describe Ci::API::API do
it "returns dependent builds" do it "returns dependent builds" do
commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master') commit = FactoryGirl.create(:ci_commit, project: project, ref: 'master')
commit.create_builds(nil, nil) commit.create_builds(nil, nil)
commit.save
commit.builds.where(stage: 'test').each(&:success) commit.builds.where(stage: 'test').each(&:success)
post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin } post ci_api("/builds/register"), token: runner.token, info: { platform: :darwin }
......
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