diff options
author | Robert Speicher <rspeicher@gmail.com> | 2019-06-06 17:16:12 +0000 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2019-06-06 17:16:12 +0000 |
commit | 5a6fdc09f8ec887f98ae0c46667feaeaefbb0898 (patch) | |
tree | a42f14e9a8604020379f1c5e2f7cc02d1fde0c37 /app/services | |
parent | 2fe47c1092fb25f8ef62152bca5502ff4efc441d (diff) | |
parent | 2a01b33e6ac27e84d842f3040d7adf799f7cf258 (diff) | |
download | gitlab-ce-5a6fdc09f8ec887f98ae0c46667feaeaefbb0898.tar.gz |
Merge branch 'create-base-class-for-auto-merge-architecture' into 'master'
Create BaseService for Auto Merge architecture
See merge request gitlab-org/gitlab-ce!29120
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/auto_merge/base_service.rb | 52 | ||||
-rw-r--r-- | app/services/auto_merge/merge_when_pipeline_succeeds_service.rb | 34 |
2 files changed, 57 insertions, 29 deletions
diff --git a/app/services/auto_merge/base_service.rb b/app/services/auto_merge/base_service.rb new file mode 100644 index 00000000000..058105db3a4 --- /dev/null +++ b/app/services/auto_merge/base_service.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module AutoMerge + class BaseService < ::BaseService + include Gitlab::Utils::StrongMemoize + + def execute(merge_request) + merge_request.merge_params.merge!(params) + merge_request.auto_merge_enabled = true + merge_request.merge_user = current_user + merge_request.auto_merge_strategy = strategy + + return :failed unless merge_request.save + + yield if block_given? + + strategy.to_sym + end + + def cancel(merge_request) + if cancel_auto_merge(merge_request) + yield if block_given? + + success + else + error("Can't cancel the automatic merge", 406) + end + end + + private + + def strategy + strong_memoize(:strategy) do + self.class.name.demodulize.remove('Service').underscore + end + end + + def cancel_auto_merge(merge_request) + merge_request.auto_merge_enabled = false + merge_request.merge_user = nil + + merge_request.merge_params&.except!( + 'should_remove_source_branch', + 'commit_message', + 'squash_commit_message', + 'auto_merge_strategy' + ) + + merge_request.save + end + end +end diff --git a/app/services/auto_merge/merge_when_pipeline_succeeds_service.rb b/app/services/auto_merge/merge_when_pipeline_succeeds_service.rb index d0586468859..c41073a73e9 100644 --- a/app/services/auto_merge/merge_when_pipeline_succeeds_service.rb +++ b/app/services/auto_merge/merge_when_pipeline_succeeds_service.rb @@ -1,32 +1,12 @@ # frozen_string_literal: true module AutoMerge - class MergeWhenPipelineSucceedsService < BaseService + class MergeWhenPipelineSucceedsService < AutoMerge::BaseService def execute(merge_request) - return :failed unless merge_request.actual_head_pipeline - - if merge_request.actual_head_pipeline.active? - merge_request.merge_params.merge!(params) - - unless merge_request.auto_merge_enabled? - merge_request.auto_merge_enabled = true - merge_request.merge_user = @current_user - merge_request.auto_merge_strategy = AutoMergeService::STRATEGY_MERGE_WHEN_PIPELINE_SUCCEEDS - - SystemNoteService.merge_when_pipeline_succeeds(merge_request, @project, @current_user, merge_request.diff_head_commit) + super do + if merge_request.saved_change_to_auto_merge_enabled? + SystemNoteService.merge_when_pipeline_succeeds(merge_request, project, current_user, merge_request.diff_head_commit) end - - return :failed unless merge_request.save - - :merge_when_pipeline_succeeds - elsif merge_request.actual_head_pipeline.success? - # This can be triggered when a user clicks the auto merge button while - # the tests finish at about the same time - merge_request.merge_async(current_user.id, merge_params) - - :success - else - :failed end end @@ -38,12 +18,8 @@ module AutoMerge end def cancel(merge_request) - if merge_request.reset_auto_merge + super do SystemNoteService.cancel_merge_when_pipeline_succeeds(merge_request, @project, @current_user) - - success - else - error("Can't cancel the automatic merge", 406) end end |