debian-mirror-gitlab/app/services/files/base_service.rb

76 lines
2.3 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Files
class BaseService < ::BaseService
2015-09-25 12:07:36 +05:30
class ValidationError < StandardError; end
2014-09-02 18:07:02 +05:30
2015-09-25 12:07:36 +05:30
def execute
@source_project = params[:source_project] || @project
@source_branch = params[:source_branch]
2015-09-25 12:07:36 +05:30
@target_branch = params[:target_branch]
2015-09-25 12:07:36 +05:30
@commit_message = params[:commit_message]
@file_path = params[:file_path]
2016-08-24 12:49:21 +05:30
@previous_path = params[:previous_path]
2015-09-25 12:07:36 +05:30
@file_content = if params[:file_content_encoding] == 'base64'
Base64.decode64(params[:file_content])
else
params[:file_content]
end
2016-09-13 17:45:13 +05:30
@last_commit_sha = params[:last_commit_sha]
2015-09-25 12:07:36 +05:30
# Validate parameters
validate
# Create new branch if it different from source_branch
if different_branch?
2015-09-25 12:07:36 +05:30
create_target_branch
end
2015-10-24 18:46:33 +05:30
if commit
2015-09-25 12:07:36 +05:30
success
else
2016-08-24 12:49:21 +05:30
error('Something went wrong. Your changes were not committed')
2015-09-25 12:07:36 +05:30
end
rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex
2015-09-25 12:07:36 +05:30
error(ex.message)
2014-09-02 18:07:02 +05:30
end
private
def different_branch?
@source_branch != @target_branch || @source_project != @project
2015-09-25 12:07:36 +05:30
end
def raise_error(message)
raise ValidationError.new(message)
end
def validate
2016-08-24 12:49:21 +05:30
allowed = ::Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(@target_branch)
2015-09-25 12:07:36 +05:30
unless allowed
raise_error("You are not allowed to push into this branch")
end
unless project.empty_repo?
unless @source_project.repository.branch_names.include?(@source_branch)
2016-08-24 12:49:21 +05:30
raise_error('You can only create or edit files when you are on a branch')
2015-09-25 12:07:36 +05:30
end
if different_branch?
2015-09-25 12:07:36 +05:30
if repository.branch_names.include?(@target_branch)
2016-08-24 12:49:21 +05:30
raise_error('Branch with such name already exists. You need to switch to this branch in order to make changes')
2015-09-25 12:07:36 +05:30
end
end
end
end
def create_target_branch
result = CreateBranchService.new(project, current_user).execute(@target_branch, @source_branch, source_project: @source_project)
2015-09-25 12:07:36 +05:30
unless result[:status] == :success
raise_error("Something went wrong when we tried to create #{@target_branch} for you: #{result[:message]}")
2015-09-25 12:07:36 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
end