create_pipeline_service.rb 4.9 KB
Newer Older
K
Kamil Trzcinski 已提交
1 2
module Ci
  class CreatePipelineService < BaseService
3
    attr_reader :pipeline
4

5
    def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil)
6
      @pipeline = Ci::Pipeline.new(
7
        source: source,
8 9 10 11 12 13
        project: project,
        ref: ref,
        sha: sha,
        before_sha: before_sha,
        tag: tag?,
        trigger_requests: Array(trigger_request),
14 15
        user: current_user,
        pipeline_schedule: schedule
16 17
      )

18 19 20
      result = validate(current_user || trigger_request.trigger.owner,
                        ignore_skip_ci: ignore_skip_ci,
                        save_on_errors: save_on_errors)
21

22 23 24
      return result if result

      begin
S
init  
Shinya Maeda 已提交
25 26
        Ci::Pipeline.transaction do
          pipeline.save!
L
Lin Jen-Shin 已提交
27

S
init  
Shinya Maeda 已提交
28 29 30 31 32 33 34 35
          yield(pipeline) if block_given?

          Ci::CreatePipelineStagesService
            .new(project, current_user)
            .execute(pipeline)
        end
      rescue ActiveRecord::RecordInvalid => e
        return error("Failed to persist the pipeline: #{e}")
L
Lin Jen-Shin 已提交
36 37
      end

S
init  
Shinya Maeda 已提交
38 39
      update_merge_requests_head_pipeline

L
Lin Jen-Shin 已提交
40 41 42 43 44 45 46 47 48 49
      cancel_pending_pipelines if project.auto_cancel_pending_pipelines?

      pipeline_created_counter.increment(source: source)

      pipeline.tap(&:process!)
    end

    private

    def validate(triggering_user, ignore_skip_ci:, save_on_errors:)
50
      unless project.builds_enabled?
51
        return error('Pipeline is disabled')
52 53
      end

54 55
      unless allowed_to_trigger_pipeline?(triggering_user)
        if can?(triggering_user, :create_pipeline, project)
56
          return error("Insufficient permissions for protected ref '#{ref}'")
57
        else
58
          return error('Insufficient permissions to create a new pipeline')
59
        end
K
Kamil Trzcinski 已提交
60 61
      end

62
      unless branch? || tag?
63
        return error('Reference not found')
64 65
      end

66
      unless commit
67
        return error('Commit not found')
K
Kamil Trzcinski 已提交
68 69
      end

70
      unless pipeline.config_processor
71
        unless pipeline.ci_yaml_file
72
          return error("Missing #{pipeline.ci_yaml_file_path} file")
73
        end
74
        return error(pipeline.yaml_errors, save: save_on_errors)
75
      end
K
Kamil Trzcinski 已提交
76

77
      if !ignore_skip_ci && skip_ci?
78
        pipeline.skip if save_on_errors
S
Shinya Maeda 已提交
79
        raise InsufficientConditionError, pipeline
80
      end
K
Kamil Trzcinski 已提交
81

82
      unless pipeline.has_stage_seeds?
83
        return error('No stages / jobs for this pipeline.')
K
Kamil Trzcinski 已提交
84 85 86
      end
    end

87
    def allowed_to_trigger_pipeline?(triggering_user)
L
Lin Jen-Shin 已提交
88 89 90
      if triggering_user
        allowed_to_create?(triggering_user)
      else # legacy triggers don't have a corresponding user
L
Lin Jen-Shin 已提交
91
        !project.protected_for?(ref)
L
Lin Jen-Shin 已提交
92
      end
L
Lin Jen-Shin 已提交
93 94
    end

95 96 97
    def allowed_to_create?(triggering_user)
      access = Gitlab::UserAccess.new(triggering_user, project: project)

98
      can?(triggering_user, :create_pipeline, project) &&
99
        if branch?
100
          access.can_update_branch?(ref)
101 102 103
        elsif tag?
          access.can_create_tag?(ref)
        else
104
          true # Allow it for now and we'll reject when we check ref existence
105 106 107
        end
    end

108
    def update_merge_requests_head_pipeline
109
      return unless pipeline.latest?
110

111 112
      MergeRequest.where(source_project: @pipeline.project, source_branch: @pipeline.ref)
        .update_all(head_pipeline_id: @pipeline.id)
113 114
    end

115
    def skip_ci?
116 117
      return false unless pipeline.git_commit_message
      pipeline.git_commit_message =~ /\[(ci[ _-]skip|skip[ _-]ci)\]/i
K
Kamil Trzcinski 已提交
118 119
    end

120
    def cancel_pending_pipelines
121
      Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines) do |cancelables|
122
        cancelables.find_each do |cancelable|
123
          cancelable.auto_cancel_running(pipeline)
124 125 126 127
        end
      end
    end

128 129 130 131 132 133 134 135
    def auto_cancelable_pipelines
      project.pipelines
        .where(ref: pipeline.ref)
        .where.not(id: pipeline.id)
        .where.not(sha: project.repository.sha_from_ref(pipeline.ref))
        .created_or_pending
    end

K
Kamil Trzcinski 已提交
136
    def commit
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
      @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?
157 158 159 160
      return @is_branch if defined?(@is_branch)

      @is_branch =
        project.repository.ref_exists?(Gitlab::Git::BRANCH_REF_PREFIX + ref)
161 162 163
    end

    def tag?
164 165 166 167
      return @is_tag if defined?(@is_tag)

      @is_tag =
        project.repository.ref_exists?(Gitlab::Git::TAG_REF_PREFIX + ref)
168 169 170
    end

    def ref
171
      @ref ||= Gitlab::Git.ref_name(origin_ref)
172 173 174 175 176 177 178 179
    end

    def valid_sha?
      origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
    end

    def error(message, save: false)
      pipeline.errors.add(:base, message)
180
      pipeline.drop if save
181
      pipeline
K
Kamil Trzcinski 已提交
182
    end
183 184

    def pipeline_created_counter
B
Ben Kochie 已提交
185
      @pipeline_created_counter ||= Gitlab::Metrics.counter(:pipelines_created_total, "Counter of pipelines created")
186
    end
K
Kamil Trzcinski 已提交
187
  end
K
Kamil Trzcinski 已提交
188
end