2016-05-09 19:26:13 -04:00
|
|
|
module Ci
|
|
|
|
class CreatePipelineService < BaseService
|
|
|
|
def execute
|
2016-05-18 18:01:42 -04:00
|
|
|
pipeline = project.ci_commits.new(params)
|
2016-05-18 14:02:10 -04:00
|
|
|
|
2016-05-09 19:26:13 -04:00
|
|
|
unless ref_names.include?(params[:ref])
|
2016-05-18 14:02:10 -04:00
|
|
|
pipeline.errors.add(:base, 'Reference not found')
|
|
|
|
return pipeline
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
if commit
|
|
|
|
pipeline.sha = commit.id
|
|
|
|
else
|
2016-05-18 14:02:10 -04:00
|
|
|
pipeline.errors.add(:base, 'Commit not found')
|
|
|
|
return pipeline
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless can?(current_user, :create_pipeline, project)
|
2016-05-18 14:02:10 -04:00
|
|
|
pipeline.errors.add(:base, 'Insufficient permissions to create a new pipeline')
|
|
|
|
return pipeline
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
unless pipeline.config_processor
|
|
|
|
pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
|
|
|
|
return pipeline
|
|
|
|
end
|
2016-05-14 20:47:16 -04:00
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
pipeline.save!
|
2016-05-09 19:26:13 -04:00
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
unless pipeline.create_builds(current_user)
|
|
|
|
pipeline.errors.add(:base, 'No builds for this pipeline.')
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
pipeline.update_state!
|
2016-05-09 19:26:13 -04:00
|
|
|
pipeline
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ref_names
|
|
|
|
@ref_names ||= project.repository.ref_names
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
|
|
|
@commit ||= project.commit(params[:ref])
|
|
|
|
end
|
|
|
|
end
|
2016-05-12 14:08:18 -04:00
|
|
|
end
|