2013-11-05 05:06:52 -05:00
|
|
|
module Files
|
2014-01-16 12:03:42 -05:00
|
|
|
class BaseService < ::BaseService
|
2015-08-11 08:33:31 -04:00
|
|
|
class ValidationError < StandardError; end
|
2013-11-05 05:06:52 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def execute
|
2015-12-18 04:03:34 -05:00
|
|
|
@source_project = params[:source_project] || @project
|
|
|
|
@source_branch = params[:source_branch]
|
2015-08-11 08:33:31 -04: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
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
# Create new branch if it different from source_branch
|
|
|
|
if different_branch?
|
2015-08-11 08:33:31 -04:00
|
|
|
create_target_branch
|
|
|
|
end
|
|
|
|
|
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?
|
|
|
|
@source_branch != @target_branch || @source_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
|
|
|
|
|
|
|
|
unless project.empty_repo?
|
2015-12-18 04:03:34 -05:00
|
|
|
unless @source_project.repository.branch_names.include?(@source_branch)
|
2016-07-06 09:26:59 -04:00
|
|
|
raise_error('You can only create or edit files when you are on a branch')
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
|
2015-12-18 04:03:34 -05:00
|
|
|
if different_branch?
|
2015-08-11 08:33:31 -04:00
|
|
|
if repository.branch_names.include?(@target_branch)
|
2016-07-06 09:26:59 -04:00
|
|
|
raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes')
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_target_branch
|
2015-12-18 04:03:34 -05:00
|
|
|
result = CreateBranchService.new(project, current_user).execute(@target_branch, @source_branch, source_project: @source_project)
|
2015-08-11 08:33:31 -04:00
|
|
|
|
|
|
|
unless result[:status] == :success
|
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
|