2016-05-09 23:26:13 +00:00
|
|
|
module Ci
|
|
|
|
class CreatePipelineService < BaseService
|
2016-08-11 13:22:35 +00:00
|
|
|
attr_reader :pipeline
|
2016-05-18 18:02:10 +00:00
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
def execute(ignore_skip_ci: false, save_on_errors: true, trigger_request: nil)
|
|
|
|
@pipeline = Ci::Pipeline.new(
|
|
|
|
project: project,
|
|
|
|
ref: ref,
|
|
|
|
sha: sha,
|
|
|
|
before_sha: before_sha,
|
|
|
|
tag: tag?,
|
|
|
|
trigger_requests: Array(trigger_request),
|
|
|
|
user: current_user
|
|
|
|
)
|
|
|
|
|
|
|
|
unless project.builds_enabled?
|
|
|
|
return error('Pipeline is disabled')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless trigger_request || can?(current_user, :create_pipeline, project)
|
|
|
|
return error('Insufficient permissions to create a new pipeline')
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
unless branch? || tag?
|
|
|
|
return error('Reference not found')
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
unless commit
|
|
|
|
return error('Commit not found')
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
2016-06-02 10:39:15 +00:00
|
|
|
unless pipeline.config_processor
|
2016-08-11 13:22:35 +00:00
|
|
|
unless pipeline.ci_yaml_file
|
|
|
|
return error('Missing .gitlab-ci.yml file')
|
|
|
|
end
|
|
|
|
return error(pipeline.yaml_errors, save: save_on_errors)
|
2016-06-02 10:39:15 +00:00
|
|
|
end
|
2016-05-15 00:47:16 +00:00
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
if !ignore_skip_ci && skip_ci?
|
2016-08-11 18:54:02 +00:00
|
|
|
pipeline.skip if save_on_errors
|
|
|
|
return pipeline
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
2016-05-09 23:26:13 +00:00
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
unless pipeline.config_builds_attributes.present?
|
|
|
|
return error('No builds for this pipeline.')
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
2016-11-24 14:58:31 +00:00
|
|
|
Ci::Pipeline.transaction do
|
|
|
|
pipeline.save
|
|
|
|
|
|
|
|
Ci::CreatePipelineBuildsService
|
|
|
|
.new(project, current_user)
|
|
|
|
.execute(pipeline)
|
|
|
|
end
|
|
|
|
|
|
|
|
pipeline.tap(&:process!)
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
def skip_ci?
|
|
|
|
pipeline.git_commit_message =~ /\[(ci skip|skip ci)\]/i if pipeline.git_commit_message
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
2016-08-11 13:22:35 +00:00
|
|
|
@commit ||= project.commit(origin_sha || origin_ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
commit.try(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_sha
|
|
|
|
params[:checkout_sha] || params[:before] || Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_sha
|
|
|
|
params[:checkout_sha] || params[:after]
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_ref
|
|
|
|
params[:ref]
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch?
|
|
|
|
project.repository.ref_exists?(Gitlab::Git::BRANCH_REF_PREFIX + ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag?
|
|
|
|
project.repository.ref_exists?(Gitlab::Git::TAG_REF_PREFIX + ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
|
|
|
Gitlab::Git.ref_name(origin_ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_sha?
|
|
|
|
origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(message, save: false)
|
|
|
|
pipeline.errors.add(:base, message)
|
2016-08-11 18:54:02 +00:00
|
|
|
pipeline.drop if save
|
2016-08-11 13:22:35 +00:00
|
|
|
pipeline
|
2016-05-09 23:26:13 +00:00
|
|
|
end
|
|
|
|
end
|
2016-05-12 18:08:18 +00:00
|
|
|
end
|