summaryrefslogtreecommitdiff
path: root/app/services/create_commit_builds_service.rb
blob: 0b66b854deabd245b02259f8cb9cd7e3c424176d (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class CreateCommitBuildsService
  def execute(project, user, params)
    return unless project.builds_enabled?

    before_sha = params[:checkout_sha] || params[:before]
    sha = params[:checkout_sha] || params[:after]
    origin_ref = params[:ref]

    ref = Gitlab::Git.ref_name(origin_ref)
    tag = Gitlab::Git.tag_ref?(origin_ref)

    # Skip branch removal
    if sha == Gitlab::Git::BLANK_SHA
      return false
    end

    @pipeline = Ci::Pipeline.new(
      project: project,
      sha: sha,
      ref: ref,
      before_sha: before_sha,
      tag: tag,
      user: user)

    ##
    # Skip creating pipeline if no gitlab-ci.yml is found
    #
    unless @pipeline.ci_yaml_file
      return false
    end

    ##
    # Skip creating builds for commits that have [ci skip]
    # but save pipeline object
    #
    if @pipeline.skip_ci?
      return save_pipeline!
    end

    ##
    # Skip creating builds when CI config is invalid
    # but save pipeline object
    #
    unless @pipeline.config_processor
      return save_pipeline!
    end

    ##
    # Skip creating pipeline object if there are no builds for it.
    #
    unless @pipeline.create_builds(user)
      @pipeline.errors.add(:base, 'No builds created')
      return false
    end

    save_pipeline!
  end

  private

  ##
  # Create a new pipeline and touch object to calculate status
  #
  def save_pipeline!
    @pipeline.save!
    @pipeline.touch
    @pipeline
  end
end