summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorRubén Dávila <ruben@gitlab.com>2015-11-25 19:20:40 -0500
committerRubén Dávila <ruben@gitlab.com>2015-12-03 09:39:15 -0500
commit5145706c82613d64462fe736850d09799224cd77 (patch)
tree81744d6a01be18d3c89c6cb726f8496777b99e4e /app/services
parentb5103a83a8574936721250997e75ab9a6855d00a (diff)
downloadgitlab-ce-5145706c82613d64462fe736850d09799224cd77.tar.gz
Run custom Git hooks when creating or deleting branches through the UI. #1156
Diffstat (limited to 'app/services')
-rw-r--r--app/services/create_branch_service.rb5
-rw-r--r--app/services/delete_branch_service.rb4
-rw-r--r--app/services/files/base_service.rb2
-rw-r--r--app/services/git_hooks_service.rb32
4 files changed, 39 insertions, 4 deletions
diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb
index cf7ae4345f3..de18f3bc556 100644
--- a/app/services/create_branch_service.rb
+++ b/app/services/create_branch_service.rb
@@ -13,8 +13,7 @@ class CreateBranchService < BaseService
return error('Branch already exists')
end
- repository.add_branch(branch_name, ref)
- new_branch = repository.find_branch(branch_name)
+ new_branch = repository.add_branch(current_user, branch_name, ref)
if new_branch
push_data = build_push_data(project, current_user, new_branch)
@@ -27,6 +26,8 @@ class CreateBranchService < BaseService
else
error('Invalid reference name')
end
+ rescue GitHooksService::PreReceiveError
+ error('Branch creation was rejected by Git hook')
end
def success(branch)
diff --git a/app/services/delete_branch_service.rb b/app/services/delete_branch_service.rb
index b19b112a0c4..22bf9dd935e 100644
--- a/app/services/delete_branch_service.rb
+++ b/app/services/delete_branch_service.rb
@@ -24,7 +24,7 @@ class DeleteBranchService < BaseService
return error('You dont have push access to repo', 405)
end
- if repository.rm_branch(branch_name)
+ if repository.rm_branch(current_user, branch_name)
push_data = build_push_data(branch)
EventCreateService.new.push(project, current_user, push_data)
@@ -35,6 +35,8 @@ class DeleteBranchService < BaseService
else
error('Failed to remove branch')
end
+ rescue GitHooksService::PreReceiveError
+ error('Branch deletion was rejected by Git hook')
end
def error(message, return_code = 400)
diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb
index 008833eed80..f50aaf2eb52 100644
--- a/app/services/files/base_service.rb
+++ b/app/services/files/base_service.rb
@@ -26,7 +26,7 @@ module Files
else
error("Something went wrong. Your changes were not committed")
end
- rescue Repository::CommitError, Repository::PreReceiveError, ValidationError => ex
+ rescue Repository::CommitError, GitHooksService::PreReceiveError, ValidationError => ex
error(ex.message)
end
diff --git a/app/services/git_hooks_service.rb b/app/services/git_hooks_service.rb
new file mode 100644
index 00000000000..53f1fdef796
--- /dev/null
+++ b/app/services/git_hooks_service.rb
@@ -0,0 +1,32 @@
+class GitHooksService
+ PreReceiveError = Class.new(StandardError)
+
+ def execute(user, repo_path, oldrev, newrev, ref)
+ @repo_path = repo_path
+ @user = Gitlab::ShellEnv.gl_id(user)
+ @oldrev = oldrev
+ @newrev = newrev
+ @ref = ref
+
+ pre_status = run_hook('pre-receive')
+
+ if pre_status
+ yield
+
+ run_hook('post-receive')
+ end
+ end
+
+ private
+
+ def run_hook(name)
+ hook = Gitlab::Git::Hook.new(name, @repo_path)
+ status = hook.trigger(@user, @oldrev, @newrev, @ref)
+
+ if !status && (name != 'post-receive')
+ raise PreReceiveError.new("Git operation was rejected by #{name} hook")
+ end
+
+ status
+ end
+end