summaryrefslogtreecommitdiff
path: root/app/services/files
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-11 14:29:43 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-11 14:29:43 +0200
commitc0bf026ed8253e9bf749546e40f968dbc20aaac1 (patch)
treeadb3d8f276395f8593fdac5a6153a2a51dced654 /app/services/files
parent7f3228ec74725c02efb0e23ff8380eaa03739891 (diff)
parent5daf44b7c86e0e2641a902b1da8b01d91fa3dbfa (diff)
downloadgitlab-ce-c0bf026ed8253e9bf749546e40f968dbc20aaac1.tar.gz
Merge branch 'master' of gitlab.com:gitlab-org/gitlab-ce
Diffstat (limited to 'app/services/files')
-rw-r--r--app/services/files/base_service.rb77
-rw-r--r--app/services/files/create_service.rb44
-rw-r--r--app/services/files/delete_service.rb33
-rw-r--r--app/services/files/update_service.rb36
4 files changed, 103 insertions, 87 deletions
diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb
index d7b40ee8906..bd245100955 100644
--- a/app/services/files/base_service.rb
+++ b/app/services/files/base_service.rb
@@ -1,80 +1,17 @@
module Files
class BaseService < ::BaseService
- class ValidationError < StandardError; end
+ attr_reader :ref, :path
- def execute
- @current_branch = params[:current_branch]
- @target_branch = params[:target_branch]
- @commit_message = params[:commit_message]
- @file_path = params[:file_path]
- @file_content = if params[:file_content_encoding] == 'base64'
- Base64.decode64(params[:file_content])
- else
- params[:file_content]
- end
-
- # Validate parameters
- validate
-
- # Create new branch if it different from current_branch
- if @target_branch != @current_branch
- create_target_branch
- end
-
- if sha = commit
- after_commit(sha, @target_branch)
- success
- else
- error("Something went wrong. Your changes were not committed")
- end
- rescue ValidationError => ex
- error(ex.message)
+ def initialize(project, user, params, ref, path = nil)
+ @project, @current_user, @params = project, user, params.dup
+ @ref = ref
+ @path = path
end
private
- def after_commit(sha, branch)
- PostCommitService.new(project, current_user).execute(sha, branch)
- end
-
- def current_branch
- @current_branch ||= params[:current_branch]
- end
-
- def target_branch
- @target_branch ||= params[:target_branch]
- end
-
- def raise_error(message)
- raise ValidationError.new(message)
- end
-
- def validate
- allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(@target_branch)
-
- unless allowed
- raise_error("You are not allowed to push into this branch")
- end
-
- unless project.empty_repo?
- unless repository.branch_names.include?(@current_branch)
- raise_error("You can only create files if you are on top of a branch")
- end
-
- if @current_branch != @target_branch
- if repository.branch_names.include?(@target_branch)
- raise_error("Branch with such name already exists. You need to switch to this branch in order to make changes")
- end
- end
- end
- end
-
- def create_target_branch
- result = CreateBranchService.new(project, current_user).execute(@target_branch, @current_branch)
-
- unless result[:status] == :success
- raise_error("Something went wrong when we tried to create #{@target_branch} for you")
- end
+ def repository
+ project.repository
end
end
end
diff --git a/app/services/files/create_service.rb b/app/services/files/create_service.rb
index 91d715b2d63..23833aa78ec 100644
--- a/app/services/files/create_service.rb
+++ b/app/services/files/create_service.rb
@@ -1,30 +1,52 @@
require_relative "base_service"
module Files
- class CreateService < Files::BaseService
- def commit
- repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch)
- end
+ class CreateService < BaseService
+ def execute
+ allowed = Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref)
- def validate
- super
+ unless allowed
+ return error("You are not allowed to create file in this branch")
+ end
- file_name = File.basename(@file_path)
+ file_name = File.basename(path)
+ file_path = path
unless file_name =~ Gitlab::Regex.file_name_regex
- raise_error(
+ return error(
'Your changes could not be committed, because the file name ' +
Gitlab::Regex.file_name_regex_message
)
end
- unless project.empty_repo?
- blob = repository.blob_at_branch(@current_branch, @file_path)
+ if project.empty_repo?
+ # everything is ok because repo does not have a commits yet
+ else
+ unless repository.branch_names.include?(ref)
+ return error("You can only create files if you are on top of a branch")
+ end
+
+ blob = repository.blob_at_branch(ref, file_path)
if blob
- raise_error("Your changes could not be committed, because file with such name exists")
+ return error("Your changes could not be committed, because file with such name exists")
end
end
+
+
+ new_file_action = Gitlab::Satellite::NewFileAction.new(current_user, project, ref, file_path)
+ created_successfully = new_file_action.commit!(
+ params[:content],
+ params[:commit_message],
+ params[:encoding],
+ params[:new_branch]
+ )
+
+ if created_successfully
+ success
+ else
+ error("Your changes could not be committed, because the file has been changed")
+ end
end
end
end
diff --git a/app/services/files/delete_service.rb b/app/services/files/delete_service.rb
index 27c881c3430..1497a0f883b 100644
--- a/app/services/files/delete_service.rb
+++ b/app/services/files/delete_service.rb
@@ -1,9 +1,36 @@
require_relative "base_service"
module Files
- class DeleteService < Files::BaseService
- def commit
- repository.remove_file(current_user, @file_path, @commit_message, @target_branch)
+ class DeleteService < BaseService
+ def execute
+ allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref)
+
+ unless allowed
+ return error("You are not allowed to push into 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
+
+ blob = repository.blob_at_branch(ref, path)
+
+ unless blob
+ return error("You can only edit text files")
+ end
+
+ delete_file_action = Gitlab::Satellite::DeleteFileAction.new(current_user, project, ref, path)
+
+ deleted_successfully = delete_file_action.commit!(
+ nil,
+ params[:commit_message]
+ )
+
+ if deleted_successfully
+ success
+ else
+ error("Your changes could not be committed, because the file has been changed")
+ end
end
end
end
diff --git a/app/services/files/update_service.rb b/app/services/files/update_service.rb
index a20903c6f02..0724d3ae634 100644
--- a/app/services/files/update_service.rb
+++ b/app/services/files/update_service.rb
@@ -1,9 +1,39 @@
require_relative "base_service"
module Files
- class UpdateService < Files::BaseService
- def commit
- repository.commit_file(current_user, @file_path, @file_content, @commit_message, @target_branch)
+ class UpdateService < BaseService
+ def execute
+ allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(ref)
+
+ unless allowed
+ return error("You are not allowed to push into 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
+
+ blob = repository.blob_at_branch(ref, path)
+
+ unless blob
+ return error("You can only edit text files")
+ end
+
+ edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path)
+ edit_file_action.commit!(
+ params[:content],
+ params[:commit_message],
+ params[:encoding],
+ params[:new_branch]
+ )
+
+ success
+ rescue Gitlab::Satellite::CheckoutFailed => ex
+ error("Your changes could not be committed because ref '#{ref}' could not be checked out", 400)
+ rescue Gitlab::Satellite::CommitFailed => ex
+ error("Your changes could not be committed. Maybe there was nothing to commit?", 409)
+ rescue Gitlab::Satellite::PushFailed => ex
+ error("Your changes could not be committed. Maybe the file was changed by another process?", 409)
end
end
end