Class: AutoMergeService

Inherits:
BaseService show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/auto_merge_service.rb

Constant Summary collapse

STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS =
'merge_when_pipeline_succeeds'
STRATEGIES =
[STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS].freeze

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

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

.all_strategies_ordered_by_preferenceObject


10
11
12
# File 'app/services/auto_merge_service.rb', line 10

def all_strategies_ordered_by_preference
  STRATEGIES
end

.get_service_class(strategy) ⇒ Object


14
15
16
17
18
# File 'app/services/auto_merge_service.rb', line 14

def get_service_class(strategy)
  return unless all_strategies_ordered_by_preference.include?(strategy)

  "::AutoMerge::#{strategy.camelize}Service".constantize
end

Instance Method Details

#abort(merge_request, reason) ⇒ Object


51
52
53
54
55
56
# File 'app/services/auto_merge_service.rb', line 51

def abort(merge_request, reason)
  return error("Can't abort the automatic merge", 406) unless merge_request.auto_merge_enabled?

  strategy = merge_request.auto_merge_strategy
  get_service_instance(merge_request, strategy).abort(merge_request, reason)
end

#available_strategies(merge_request) ⇒ Object


58
59
60
61
62
# File 'app/services/auto_merge_service.rb', line 58

def available_strategies(merge_request)
  self.class.all_strategies_ordered_by_preference.select do |strategy|
    get_service_instance(merge_request, strategy).available_for?(merge_request)
  end
end

#cancel(merge_request) ⇒ Object


44
45
46
47
48
49
# File 'app/services/auto_merge_service.rb', line 44

def cancel(merge_request)
  return error("Can't cancel the automatic merge", 406) unless merge_request.auto_merge_enabled?

  strategy = merge_request.auto_merge_strategy
  get_service_instance(merge_request, strategy).cancel(merge_request)
end

#execute(merge_request, strategy = nil) ⇒ Object


21
22
23
24
25
26
27
28
# File 'app/services/auto_merge_service.rb', line 21

def execute(merge_request, strategy = nil)
  strategy ||= preferred_strategy(merge_request)
  service = get_service_instance(merge_request, strategy)

  return :failed unless service&.available_for?(merge_request)

  service.execute(merge_request)
end

#preferred_strategy(merge_request) ⇒ Object


64
65
66
# File 'app/services/auto_merge_service.rb', line 64

def preferred_strategy(merge_request)
  available_strategies(merge_request).first
end

#process(merge_request) ⇒ Object


37
38
39
40
41
42
# File 'app/services/auto_merge_service.rb', line 37

def process(merge_request)
  return unless merge_request.auto_merge_enabled?

  strategy = merge_request.auto_merge_strategy
  get_service_instance(merge_request, strategy).process(merge_request)
end

#update(merge_request) ⇒ Object


30
31
32
33
34
35
# File 'app/services/auto_merge_service.rb', line 30

def update(merge_request)
  return :failed unless merge_request.auto_merge_enabled?

  strategy = merge_request.auto_merge_strategy
  get_service_instance(merge_request, strategy).update(merge_request)
end