2016-04-18 03:39:07 -04:00
module Commits
class ChangeService < :: BaseService
2017-03-01 06:00:37 -05:00
ValidationError = Class . new ( StandardError )
ChangeError = Class . new ( StandardError )
2016-04-18 03:39:07 -04:00
def execute
2017-01-06 10:29:13 -05:00
@start_project = params [ :start_project ] || @project
@start_branch = params [ :start_branch ]
2016-04-18 03:39:07 -04:00
@target_branch = params [ :target_branch ]
@commit = params [ :commit ]
2017-02-16 19:24:56 -05:00
check_push_permissions
2016-04-18 03:39:07 -04:00
commit
rescue Repository :: CommitError , Gitlab :: Git :: Repository :: InvalidBlobName , GitHooksService :: PreReceiveError ,
ValidationError , ChangeError = > ex
error ( ex . message )
end
2016-09-15 10:30:27 -04:00
private
2016-04-18 03:39:07 -04:00
def commit
raise NotImplementedError
end
2016-09-15 10:30:27 -04:00
def commit_change ( action )
raise NotImplementedError unless repository . respond_to? ( action )
2017-02-16 19:24:56 -05:00
validate_target_branch if different_branch?
2016-09-15 10:30:27 -04:00
2017-02-16 19:24:56 -05:00
repository . public_send (
action ,
current_user ,
@commit ,
@target_branch ,
start_project : @start_project ,
start_branch_name : @start_branch )
2016-12-07 06:50:08 -05:00
2017-02-16 19:24:56 -05:00
success
2017-03-03 08:23:49 -05:00
rescue Repository :: CreateTreeError
2017-03-02 19:11:23 -05:00
error_msg = " Sorry, we cannot #{ action . to_s . dasherize } this #{ @commit . change_type_title ( current_user ) } automatically.
2016-10-05 11:12:46 -04:00
A #{action.to_s.dasherize} may have already been performed with this #{@commit.change_type_title(current_user)}, or a more recent commit may have updated some of its content."
2017-03-02 19:11:23 -05:00
raise ChangeError , error_msg
2016-09-15 10:30:27 -04:00
end
2016-04-18 03:39:07 -04:00
def check_push_permissions
2016-07-18 04:16:56 -04:00
allowed = :: Gitlab :: UserAccess . new ( current_user , project : project ) . can_push_to_branch? ( @target_branch )
2016-04-18 03:39:07 -04:00
unless allowed
raise ValidationError . new ( 'You are not allowed to push into this branch' )
end
true
end
2016-07-18 04:16:56 -04:00
2017-02-16 19:24:56 -05:00
def validate_target_branch
2017-03-02 19:11:23 -05:00
result = ValidateNewBranchService . new ( @project , current_user )
. execute ( @target_branch )
2016-04-18 03:39:07 -04:00
if result [ :status ] == :error
raise ChangeError , " There was an error creating the source branch: #{ result [ :message ] } "
end
end
2017-02-16 19:24:56 -05:00
def different_branch?
@start_branch != @target_branch || @start_project != @project
end
2016-04-18 03:39:07 -04:00
end
end