Commit af2f56f8 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor ci commit pipeline to prevent implicit saves

parent 3f4ac2ff
...@@ -13,7 +13,7 @@ module Ci ...@@ -13,7 +13,7 @@ module Ci
validate :valid_commit_sha validate :valid_commit_sha
# Invalidate object and save if when touched # Invalidate object and save if when touched
after_touch :update_state after_touch :update_state!
def self.truncate_sha(sha) def self.truncate_sha(sha)
sha[0...8] sha[0...8]
...@@ -135,10 +135,10 @@ module Ci ...@@ -135,10 +135,10 @@ module Ci
@config_processor ||= begin @config_processor ||= begin
Ci::GitlabCiYamlProcessor.new(ci_yaml_file, project.path_with_namespace) Ci::GitlabCiYamlProcessor.new(ci_yaml_file, project.path_with_namespace)
rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e rescue Ci::GitlabCiYamlProcessor::ValidationError, Psych::SyntaxError => e
save_yaml_error(e.message) self.yaml_errors = (e.message)
nil nil
rescue rescue
save_yaml_error("Undefined error") self.yaml_errors = 'Undefined error'
nil nil
end end
end end
...@@ -159,9 +159,7 @@ module Ci ...@@ -159,9 +159,7 @@ module Ci
git_commit_message =~ /(\[ci skip\])/ if git_commit_message git_commit_message =~ /(\[ci skip\])/ if git_commit_message
end end
private def update_state!
def update_state
statuses.reload statuses.reload
self.status = if yaml_errors.blank? self.status = if yaml_errors.blank?
statuses.latest.status || 'skipped' statuses.latest.status || 'skipped'
...@@ -173,11 +171,5 @@ module Ci ...@@ -173,11 +171,5 @@ module Ci
self.duration = statuses.latest.duration self.duration = statuses.latest.duration
save save
end end
def save_yaml_error(error)
return if self.yaml_errors?
self.yaml_errors = error
update_state
end
end end
end end
...@@ -8,7 +8,9 @@ module Ci ...@@ -8,7 +8,9 @@ module Ci
return pipeline return pipeline
end end
unless commit if commit
pipeline.sha = commit.id
else
pipeline.errors.add(:base, 'Commit not found') pipeline.errors.add(:base, 'Commit not found')
return pipeline return pipeline
end end
...@@ -18,22 +20,18 @@ module Ci ...@@ -18,22 +20,18 @@ module Ci
return pipeline return pipeline
end end
begin unless pipeline.config_processor
Ci::Commit.transaction do pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
pipeline.sha = commit.id return pipeline
end
unless pipeline.config_processor pipeline.save!
pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
raise ActiveRecord::Rollback
end
pipeline.save! unless pipeline.create_builds(current_user)
pipeline.create_builds(current_user) pipeline.errors.add(:base, 'No builds for this pipeline.')
end
rescue
pipeline.errors.add(:base, 'The pipeline could not be created. Please try again.')
end end
pipeline.update_state!
pipeline pipeline
end end
......
...@@ -34,7 +34,7 @@ class CreateCommitBuildsService ...@@ -34,7 +34,7 @@ class CreateCommitBuildsService
commit.create_builds(user) commit.create_builds(user)
end end
commit.touch commit.update_state!
commit commit
end end
end end
...@@ -81,7 +81,7 @@ describe CreateCommitBuildsService, services: true do ...@@ -81,7 +81,7 @@ describe CreateCommitBuildsService, services: true do
expect(commit.yaml_errors).not_to be_nil expect(commit.yaml_errors).not_to be_nil
end end
describe :ci_skip? do context 'when commit contains a [ci skip] directive' do
let(:message) { "some message[ci skip]" } let(:message) { "some message[ci skip]" }
before do before do
......
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