2013-11-05 05:06:52 -05:00
|
|
|
module Files
|
2014-01-16 12:03:42 -05:00
|
|
|
class BaseService < ::BaseService
|
2017-03-01 06:00:37 -05:00
|
|
|
ValidationError = Class.new(StandardError)
|
2013-11-05 05:06:52 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def execute
|
2017-01-06 10:29:13 -05:00
|
|
|
@start_project = params[:start_project] || @project
|
|
|
|
@start_branch = params[:start_branch]
|
2016-12-08 02:23:41 -05:00
|
|
|
@target_branch = params[:target_branch]
|
2015-12-18 04:03:34 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
@commit_message = params[:commit_message]
|
|
|
|
@file_path = params[:file_path]
|
2016-07-04 06:32:57 -04:00
|
|
|
@previous_path = params[:previous_path]
|
2015-08-11 08:33:31 -04:00
|
|
|
@file_content = if params[:file_content_encoding] == 'base64'
|
|
|
|
Base64.decode64(params[:file_content])
|
|
|
|
else
|
|
|
|
params[:file_content]
|
|
|
|
end
|
2016-08-07 23:29:23 -04:00
|
|
|
@last_commit_sha = params[:last_commit_sha]
|
2016-09-08 11:40:07 -04:00
|
|
|
@author_email = params[:author_email]
|
|
|
|
@author_name = params[:author_name]
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2016-07-01 12:46:37 -04:00
|
|
|
# Validate parameters
|
2015-08-11 08:33:31 -04:00
|
|
|
validate
|
|
|
|
|
2017-01-06 10:29:13 -05:00
|
|
|
# Create new branch if it different from start_branch
|
2016-11-22 05:14:41 -05:00
|
|
|
validate_target_branch if different_branch?
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
result = commit
|
|
|
|
if result
|
|
|
|
success(result: result)
|
2015-08-11 08:33:31 -04:00
|
|
|
else
|
2016-07-06 09:26:59 -04:00
|
|
|
error('Something went wrong. Your changes were not committed')
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
2015-12-18 04:03:34 -05:00
|
|
|
rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex
|
2015-08-11 08:33:31 -04:00
|
|
|
error(ex.message)
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
def different_branch?
|
2017-01-06 10:29:13 -05:00
|
|
|
@start_branch != @target_branch || @start_project != @project
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
|
2016-08-29 19:58:32 -04:00
|
|
|
def file_has_changed?
|
|
|
|
return false unless @last_commit_sha && last_commit
|
|
|
|
|
|
|
|
@last_commit_sha != last_commit.sha
|
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def raise_error(message)
|
|
|
|
raise ValidationError.new(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
2016-07-18 04:16:56 -04:00
|
|
|
allowed = ::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(@target_branch)
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
unless allowed
|
|
|
|
raise_error("You are not allowed to push into this branch")
|
|
|
|
end
|
|
|
|
|
2017-03-01 13:08:51 -05:00
|
|
|
if !@start_project.empty_repo? && !@start_project.repository.branch_exists?(@start_branch)
|
|
|
|
raise ValidationError, 'You can only create or edit files when you are on a branch'
|
|
|
|
end
|
|
|
|
|
|
|
|
if !project.empty_repo? && different_branch? && repository.branch_exists?(@branch_name)
|
2017-03-01 15:29:33 -05:00
|
|
|
raise ValidationError, "A branch called #{@branch_name} already exists. Switch to that branch in order to make changes"
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-14 15:02:10 -05:00
|
|
|
def validate_target_branch
|
|
|
|
result = ValidateNewBranchService.new(project, current_user).
|
|
|
|
execute(@target_branch)
|
2015-08-11 08:33:31 -04:00
|
|
|
|
2016-11-14 15:02:10 -05:00
|
|
|
if result[:status] == :error
|
2015-12-18 04:03:34 -05:00
|
|
|
raise_error("Something went wrong when we tried to create #{@target_branch} for you: #{result[:message]}")
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
2015-08-11 04:28:42 -04:00
|
|
|
end
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
end
|