Merge branch '22203-reduce-duplication-in-commits-cherry_pick_service' into 'master'

Reduce duplication in `Commits::{CherryPickService,RevertService}`

## What does this MR do?

This factorize similar code from `Commits::{CherryPickService,RevertService}`.

## Why was this MR needed?

To get rid of flay offense.

## What are the relevant issue numbers?

Closes #22203

See merge request !6366
This commit is contained in:
Robert Speicher 2016-09-15 17:34:36 +00:00
commit d2370422c5
3 changed files with 21 additions and 27 deletions

View file

@ -16,11 +16,29 @@ module Commits
error(ex.message)
end
private
def commit
raise NotImplementedError
end
private
def commit_change(action)
raise NotImplementedError unless repository.respond_to?(action)
into = @create_merge_request ? @commit.public_send("#{action}_branch_name") : @target_branch
tree_id = repository.public_send("check_#{action}_content", @commit, @target_branch)
if tree_id
create_target_branch(into) if @create_merge_request
repository.public_send(action, current_user, @commit, into, tree_id)
success
else
error_msg = "Sorry, we cannot #{action.to_s.dasherize} this #{@commit.change_type_title} automatically.
It may have already been #{action.to_s.dasherize}, or a more recent commit may have updated some of its content."
raise ChangeError, error_msg
end
end
def check_push_permissions
allowed = ::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(@target_branch)

View file

@ -1,19 +1,7 @@
module Commits
class CherryPickService < ChangeService
def commit
cherry_pick_into = @create_merge_request ? @commit.cherry_pick_branch_name : @target_branch
cherry_pick_tree_id = repository.check_cherry_pick_content(@commit, @target_branch)
if cherry_pick_tree_id
create_target_branch(cherry_pick_into) if @create_merge_request
repository.cherry_pick(current_user, @commit, cherry_pick_into, cherry_pick_tree_id)
success
else
error_msg = "Sorry, we cannot cherry-pick this #{@commit.change_type_title} automatically.
It may have already been cherry-picked, or a more recent commit may have updated some of its content."
raise ChangeError, error_msg
end
commit_change(:cherry_pick)
end
end
end

View file

@ -1,19 +1,7 @@
module Commits
class RevertService < ChangeService
def commit
revert_into = @create_merge_request ? @commit.revert_branch_name : @target_branch
revert_tree_id = repository.check_revert_content(@commit, @target_branch)
if revert_tree_id
create_target_branch(revert_into) if @create_merge_request
repository.revert(current_user, @commit, revert_into, revert_tree_id)
success
else
error_msg = "Sorry, we cannot revert this #{@commit.change_type_title} automatically.
It may have already been reverted, or a more recent commit may have updated some of its content."
raise ChangeError, error_msg
end
commit_change(:revert)
end
end
end