create_pipeline_service.rb 5.0 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
      result = validate(current_user,
19 20
                        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

Z
Zeger-Jan van de Weg 已提交
70 71 72 73
      unless pipeline.detect_ci_yaml_file
        return error("Missing #{pipeline.ci_yaml_file_path} file")
      end

74
      unless pipeline.config_processor
75
        return error(pipeline.yaml_errors, save: save_on_errors)
76
      end
K
Kamil Trzcinski 已提交
77

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

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

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

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

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

109
    def update_merge_requests_head_pipeline
110
      return unless pipeline.latest?
111

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

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

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

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

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

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

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

    def ref
172
      @ref ||= Gitlab::Git.ref_name(origin_ref)
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)
180 181 182 183 184 185 186 187
      pipeline.tap do
        pipeline.errors.add(:base, message)

        if save
          pipeline.drop
          update_merge_requests_head_pipeline
        end
      end
K
Kamil Trzcinski 已提交
188
    end
189 190

    def pipeline_created_counter
B
Ben Kochie 已提交
191
      @pipeline_created_counter ||= Gitlab::Metrics.counter(:pipelines_created_total, "Counter of pipelines created")
192
    end
K
Kamil Trzcinski 已提交
193
  end
K
Kamil Trzcinski 已提交
194
end