Class: Ci::CreateBuildsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ci/create_builds_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(commit) ⇒ CreateBuildsService

Returns a new instance of CreateBuildsService


3
4
5
# File 'app/services/ci/create_builds_service.rb', line 3

def initialize(commit)
  @commit = commit
end

Instance Method Details

#execute(stage, user, status, trigger_request = nil) ⇒ Object


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
# File 'app/services/ci/create_builds_service.rb', line 7

def execute(stage, user, status, trigger_request = nil)
  builds_attrs = config_processor.builds_for_stage_and_ref(stage, @commit.ref, @commit.tag, trigger_request)

  # check when to create next build
  builds_attrs = builds_attrs.select do |build_attrs|
    case build_attrs[:when]
    when 'on_success'
      status == 'success'
    when 'on_failure'
      status == 'failed'
    when 'always'
      %w(success failed).include?(status)
    end
  end

  builds_attrs.map do |build_attrs|
    # don't create the same build twice
    unless @commit.builds.find_by(ref: @commit.ref, tag: @commit.tag,
                                  trigger_request: trigger_request, name: build_attrs[:name])
      build_attrs.slice!(:name,
                         :commands,
                         :tag_list,
                         :options,
                         :allow_failure,
                         :stage,
                         :stage_idx)

      build_attrs.merge!(ref: @commit.ref,
                         tag: @commit.tag,
                         trigger_request: trigger_request,
                         user: user,
                         project: @commit.project)

      @commit.builds.create!(build_attrs)
    end
  end
end