Commit 8e811f2c authored by Kamil Trzcinski's avatar Kamil Trzcinski Committed by Grzegorz Bizon

Update CreateCommitBuildsService to pass tests

parent c6bce7e6
...@@ -36,7 +36,8 @@ module Ci ...@@ -36,7 +36,8 @@ module Ci
:stage, :stage,
:stage_idx) :stage_idx)
build_attrs.merge!(ref: @commit.ref, build_attrs.merge!(commit: @commit,
ref: @commit.ref,
tag: @commit.tag, tag: @commit.tag,
trigger_request: trigger_request, trigger_request: trigger_request,
user: user, user: user,
......
class CreateCommitBuildsService class CreateCommitBuildsService
def execute(project, user, params) def execute(project, user, params)
return false unless project.builds_enabled? return unless project.builds_enabled?
before_sha = params[:checkout_sha] || params[:before] before_sha = params[:checkout_sha] || params[:before]
sha = params[:checkout_sha] || params[:after] sha = params[:checkout_sha] || params[:after]
origin_ref = params[:ref] origin_ref = params[:ref]
unless origin_ref && sha.present?
return false
end
ref = Gitlab::Git.ref_name(origin_ref) ref = Gitlab::Git.ref_name(origin_ref)
tag = Gitlab::Git.tag_ref?(origin_ref) tag = Gitlab::Git.tag_ref?(origin_ref)
# Skip branch removal
if sha == Gitlab::Git::BLANK_SHA
return false
end
commit = Ci::Commit.new(project: project, sha: sha, ref: ref, before_sha: before_sha, tag: tag) commit = Ci::Commit.new(project: project, sha: sha, ref: ref, before_sha: before_sha, tag: tag)
# Skip creating ci_commit when no gitlab-ci.yml is found
unless commit.ci_yaml_file unless commit.ci_yaml_file
return false commit.errors.add(:base, 'No .gitlab-ci.yml file found')
return commit
end end
# Make object as skipped
if commit.skip_ci?
commit.status = 'skipped'
commit.save
return commit
end
# Skip creating builds for commits that have [ci skip] # Create builds for commit and
unless commit.skip_ci? # skip saving pipeline when there are no builds
# Create builds for commit and unless commit.create_builds(user)
# skip saving pipeline when there are no builds # Save object when there are yaml errors
return false unless commit.create_builds(user) unless commit.yaml_errors.present?
commit.errors.add(:base, 'No builds created')
return commit
end
end end
# Create a new ci_commit # Create a new ci_commit
commit.save! commit.save!
commit.touch
commit commit
end end
end end
...@@ -351,8 +351,8 @@ describe Ci::Commit, models: true do ...@@ -351,8 +351,8 @@ describe Ci::Commit, models: true do
end end
describe '#update_state!' do describe '#update_state!' do
it 'execute update_state! after touching object' do it 'execute update_state after touching object' do
expect(commit).to receive(:update_state!).and_return(true) expect(commit).to receive(:update_state).and_return(true)
commit.touch commit.touch
end end
...@@ -360,7 +360,7 @@ describe Ci::Commit, models: true do ...@@ -360,7 +360,7 @@ describe Ci::Commit, models: true do
let(:commit_status) { build :commit_status, commit: commit } let(:commit_status) { build :commit_status, commit: commit }
it 'execute update_state after saving dependent object' do it 'execute update_state after saving dependent object' do
expect(commit).to receive(:update_state!).and_return(true) expect(commit).to receive(:update_state).and_return(true)
commit_status.save commit_status.save
end end
end end
......
...@@ -60,7 +60,7 @@ describe CreateCommitBuildsService, services: true do ...@@ -60,7 +60,7 @@ describe CreateCommitBuildsService, services: true do
after: '31das312', after: '31das312',
commits: [{ message: 'Message' }] commits: [{ message: 'Message' }]
) )
expect(result).to be_falsey expect(result).not_to be_persisted
expect(Ci::Commit.count).to eq(0) expect(Ci::Commit.count).to eq(0)
end end
...@@ -174,7 +174,7 @@ describe CreateCommitBuildsService, services: true do ...@@ -174,7 +174,7 @@ describe CreateCommitBuildsService, services: true do
context 'when there are no jobs for this pipeline' do context 'when there are no jobs for this pipeline' do
before do before do
config = YAML.dump({ test: { deploy: 'ls', only: ['feature'] } }) config = YAML.dump({ test: { script: 'ls', only: ['feature'] } })
stub_ci_commit_yaml_file(config) stub_ci_commit_yaml_file(config)
end end
...@@ -184,9 +184,9 @@ describe CreateCommitBuildsService, services: true do ...@@ -184,9 +184,9 @@ describe CreateCommitBuildsService, services: true do
before: '00000000', before: '00000000',
after: '31das312', after: '31das312',
commits: [{ message: 'some msg' }]) commits: [{ message: 'some msg' }])
expect(result).not_to be_persisted
expect(result).to be false
expect(Ci::Build.all).to be_empty expect(Ci::Build.all).to be_empty
expect(Ci::Commit.count).to eq(0)
end end
end end
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