diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-09-25 16:22:00 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-09-25 16:22:00 +0200 |
commit | 8f47d484dab12df982655c3c05305bce7624914d (patch) | |
tree | bd4eef75a1714a22377962092f6f0103df49feed /app/services/ci | |
parent | 1209f4f671c290ce6c577c0ff16ad7f9ea8b6271 (diff) | |
download | gitlab-ce-8f47d484dab12df982655c3c05305bce7624914d.tar.gz |
Extract pipeline chain builder classes from service
Diffstat (limited to 'app/services/ci')
-rw-r--r-- | app/services/ci/create_pipeline_service.rb | 118 |
1 files changed, 24 insertions, 94 deletions
diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb index d20de9b16a4..1a5bf7142e0 100644 --- a/app/services/ci/create_pipeline_service.rb +++ b/app/services/ci/create_pipeline_service.rb @@ -2,6 +2,9 @@ module Ci class CreatePipelineService < BaseService attr_reader :pipeline + SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Validate, + Gitlab::Ci::Pipeline::Chain::Skip] + def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil) @pipeline = Ci::Pipeline.new( source: source, @@ -16,11 +19,23 @@ module Ci protected: project.protected_for?(ref) ) - result = validate_project_and_git_items || - validate_pipeline(ignore_skip_ci: ignore_skip_ci, - save_on_errors: save_on_errors) + command = OpenStruct.new(ignore_skip_ci: ignore_skip_ci, + save_incompleted: save_on_errors, + trigger_request: trigger_request, + schedule: schedule, + project: project, + current_user: current_user) + + sequence = SEQUENCE.map { |chain| chain.new(pipeline, command) } + + done = sequence.any? do |chain| + chain.perform! + chain.break? + end - return result if result + update_merge_requests_head_pipeline if pipeline.persisted? + + return pipeline if done begin Ci::Pipeline.transaction do @@ -36,10 +51,8 @@ module Ci return error("Failed to persist the pipeline: #{e}") end - update_merge_requests_head_pipeline - + update_merge_requests_head_pipeline if pipeline.persisted? cancel_pending_pipelines if project.auto_cancel_pending_pipelines? - pipeline_created_counter.increment(source: source) pipeline.tap(&:process!) @@ -47,65 +60,12 @@ module Ci private - def validate_project_and_git_items - unless project.builds_enabled? - return error('Pipeline is disabled') - end - - unless allowed_to_trigger_pipeline? - if can?(current_user, :create_pipeline, project) - return error("Insufficient permissions for protected ref '#{ref}'") - else - return error('Insufficient permissions to create a new pipeline') - end - end - - unless branch? || tag? - return error('Reference not found') - end - - unless commit - return error('Commit not found') - end - end - - def validate_pipeline(ignore_skip_ci:, save_on_errors:) - unless pipeline.config_processor - unless pipeline.ci_yaml_file - return error("Missing #{pipeline.ci_yaml_file_path} file") - end - return error(pipeline.yaml_errors, save: save_on_errors) - end - - if !ignore_skip_ci && skip_ci? - pipeline.skip if save_on_errors - return pipeline - end - - unless pipeline.has_stage_seeds? - return error('No stages / jobs for this pipeline.') - end - end - - def allowed_to_trigger_pipeline? - if current_user - allowed_to_create? - else # legacy triggers don't have a corresponding user - !project.protected_for?(ref) - end + def commit + @commit ||= project.commit(origin_sha || origin_ref) end - def allowed_to_create? - return unless can?(current_user, :create_pipeline, project) - - access = Gitlab::UserAccess.new(current_user, project: project) - if branch? - access.can_update_branch?(ref) - elsif tag? - access.can_create_tag?(ref) - else - true # Allow it for now and we'll reject when we check ref existence - end + def sha + commit.try(:id) end def update_merge_requests_head_pipeline @@ -115,11 +75,6 @@ module Ci .update_all(head_pipeline_id: @pipeline.id) end - def skip_ci? - return false unless pipeline.git_commit_message - pipeline.git_commit_message =~ /\[(ci[ _-]skip|skip[ _-]ci)\]/i - end - def cancel_pending_pipelines Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines) do |cancelables| cancelables.find_each do |cancelable| @@ -136,13 +91,6 @@ module Ci .created_or_pending end - def commit - @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 @@ -156,13 +104,6 @@ module Ci params[:ref] end - def branch? - return @is_branch if defined?(@is_branch) - - @is_branch = - project.repository.ref_exists?(Gitlab::Git::BRANCH_REF_PREFIX + ref) - end - def tag? return @is_tag if defined?(@is_tag) @@ -178,17 +119,6 @@ module Ci origin_sha && origin_sha != Gitlab::Git::BLANK_SHA end - def error(message, save: false) - pipeline.tap do - pipeline.errors.add(:base, message) - - if save - pipeline.drop - update_merge_requests_head_pipeline - end - end - end - def pipeline_created_counter @pipeline_created_counter ||= Gitlab::Metrics.counter(:pipelines_created_total, "Counter of pipelines created") end |