summaryrefslogtreecommitdiff
path: root/app/services/ci/create_pipeline_service.rb
blob: a7751b8effcb9b9e7271b7fe8ae54fb944ae49ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Ci
  class CreatePipelineService < BaseService
    def execute
      pipeline = project.pipelines.new(params)

      unless ref_names.include?(params[:ref])
        pipeline.errors.add(:base, 'Reference not found')
        return pipeline
      end

      unless commit
        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

      begin
        Ci::Pipeline.transaction do
          pipeline.sha = commit.id

          unless pipeline.config_processor
            pipeline.errors.add(:base, pipeline.yaml_errors || 'Missing .gitlab-ci.yml file')
            raise ActiveRecord::Rollback
          end

          pipeline.save!
          pipeline.create_builds(current_user)
        end
      rescue
        pipeline.errors.add(:base, 'The pipeline could not be created. Please try again.')
      end

      pipeline
    end

    private

    def ref_names
      @ref_names ||= project.repository.ref_names
    end

    def commit
      @commit ||= project.commit(params[:ref])
    end
  end
end