2014-01-16 13:36:51 -05:00
|
|
|
require_relative "base_service"
|
2013-12-10 07:15:08 -05:00
|
|
|
|
2013-11-05 05:06:52 -05:00
|
|
|
module Files
|
2014-01-16 12:03:42 -05:00
|
|
|
class CreateService < BaseService
|
2013-11-05 05:06:52 -05:00
|
|
|
def execute
|
|
|
|
allowed = if project.protected_branch?(ref)
|
|
|
|
can?(current_user, :push_code_to_protected_branches, project)
|
|
|
|
else
|
|
|
|
can?(current_user, :push_code, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless allowed
|
|
|
|
return error("You are not allowed to create file in this branch")
|
|
|
|
end
|
|
|
|
|
|
|
|
unless repository.branch_names.include?(ref)
|
|
|
|
return error("You can only create files if you are on top of a branch")
|
|
|
|
end
|
|
|
|
|
2013-11-20 03:03:50 -05:00
|
|
|
file_name = File.basename(path)
|
|
|
|
file_path = path
|
2013-11-05 05:06:52 -05:00
|
|
|
|
|
|
|
unless file_name =~ Gitlab::Regex.path_regex
|
2013-12-16 03:57:50 -05:00
|
|
|
return error("Your changes could not be committed, because file name contains not allowed characters")
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
|
2014-02-05 04:39:55 -05:00
|
|
|
blob = repository.blob_at_branch(ref, file_path)
|
2013-11-05 05:06:52 -05:00
|
|
|
|
|
|
|
if blob
|
2013-12-16 03:57:50 -05:00
|
|
|
return error("Your changes could not be committed, because file with such name exists")
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
|
2013-11-11 07:58:39 -05:00
|
|
|
new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, file_path)
|
2013-11-05 05:06:52 -05:00
|
|
|
created_successfully = new_file_action.commit!(
|
|
|
|
params[:content],
|
2014-01-16 11:10:35 -05:00
|
|
|
params[:commit_message],
|
|
|
|
params[:encoding]
|
2013-11-05 05:06:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if created_successfully
|
|
|
|
success
|
|
|
|
else
|
2013-12-16 03:57:50 -05:00
|
|
|
error("Your changes could not be committed, because the file has been changed")
|
2013-11-05 05:06:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|