2016-06-02 11:05:42 +05:30
|
|
|
module Ci
|
|
|
|
class CreatePipelineService < BaseService
|
|
|
|
def execute
|
2016-06-16 23:09:34 +05:30
|
|
|
pipeline = project.pipelines.new(params)
|
2016-08-24 12:49:21 +05:30
|
|
|
pipeline.user = current_user
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
unless ref_names.include?(params[:ref])
|
|
|
|
pipeline.errors.add(:base, 'Reference not found')
|
|
|
|
return pipeline
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
if commit
|
|
|
|
pipeline.sha = commit.id
|
|
|
|
else
|
2016-06-02 11:05:42 +05:30
|
|
|
pipeline.errors.add(:base, 'Commit not found')
|
|
|
|
return pipeline
|
|
|
|
end
|
|
|
|
|
|
|
|
unless can?(current_user, :create_pipeline, project)
|
|
|
|
pipeline.errors.add(:base, 'Insufficient permissions to create a new pipeline')
|
|
|
|
return pipeline
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
unless pipeline.config_processor
|
|
|
|
pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
|
|
|
|
return pipeline
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
pipeline.save!
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
unless pipeline.create_builds(current_user)
|
|
|
|
pipeline.errors.add(:base, 'No builds for this pipeline.')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
pipeline.save
|
2016-06-02 11:05:42 +05:30
|
|
|
pipeline
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ref_names
|
|
|
|
@ref_names ||= project.repository.ref_names
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
|
|
|
@commit ||= project.commit(params[:ref])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|