Class: Ci::RetryBuildService

Inherits:
BaseService show all
Defined in:
app/services/ci/retry_build_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Class Method Details

.clone_accessorsObject


5
6
7
8
9
10
11
# File 'app/services/ci/retry_build_service.rb', line 5

def self.clone_accessors
  %i[pipeline project ref tag options name
     allow_failure stage stage_id stage_idx trigger_request
     yaml_variables when environment coverage_regex
     description tag_list protected needs_attributes
     resource_group scheduling_type].freeze
end

Instance Method Details

#execute(build) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/ci/retry_build_service.rb', line 13

def execute(build)
  build.ensure_scheduling_type!

  reprocess!(build).tap do |new_build|
    mark_subsequent_stages_as_processable(build)

    Gitlab::OptimisticLocking.retry_lock(new_build, &:enqueue)

    MergeRequests::AddTodoWhenBuildFailsService
      .new(project, current_user)
      .close(new_build)
  end
end

#reprocess!(build) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/ci/retry_build_service.rb', line 28

def reprocess!(build)
  unless can?(current_user, :update_build, build)
    raise Gitlab::Access::AccessDeniedError
  end

  attributes = self.class.clone_accessors.map do |attribute|
    [attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
  end.to_h

  attributes[:user] = current_user

  Ci::Build.transaction do
    # mark all other builds of that name as retried
    build.pipeline.builds.latest
      .where(name: build.name)
      .update_all(retried: true, processed: true)

    create_build!(attributes).tap do
      # mark existing object as retried/processed without a reload
      build.retried = true
      build.processed = true
    end
  end
end