summaryrefslogtreecommitdiff
path: root/lib/api/branches.rb
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2017-10-14 11:32:24 +0200
committerJacopo <beschi.jacopo@gmail.com>2017-10-18 09:45:27 +0200
commit26ec874f77b00124615ff55d48ca79f7631a6c15 (patch)
tree9bb210ddef0a63672a6491b054fb4bd4074dd614 /lib/api/branches.rb
parent9ac5338b8eb361927ad068486398b92acb0c287e (diff)
downloadgitlab-ce-26ec874f77b00124615ff55d48ca79f7631a6c15.tar.gz
Get Project Branch API shows an helpful error message on invalid refname
In API v4 when requesting a branch with an invalid refname shows an helpful error message: 'The branch refname is invalid'.
Diffstat (limited to 'lib/api/branches.rb')
-rw-r--r--lib/api/branches.rb22
1 files changed, 14 insertions, 8 deletions
diff --git a/lib/api/branches.rb b/lib/api/branches.rb
index 61a2d688282..19152c9f395 100644
--- a/lib/api/branches.rb
+++ b/lib/api/branches.rb
@@ -8,6 +8,16 @@ module API
before { authorize! :download_code, user_project }
+ helpers do
+ def find_branch!(branch_name)
+ begin
+ user_project.repository.find_branch(branch_name) || not_found!('Branch')
+ rescue Gitlab::Git::CommandError
+ render_api_error!('The branch refname is invalid', 400)
+ end
+ end
+ end
+
params do
requires :id, type: String, desc: 'The ID of a project'
end
@@ -38,8 +48,7 @@ module API
user_project.repository.branch_exists?(params[:branch]) ? status(204) : status(404)
end
get do
- branch = user_project.repository.find_branch(params[:branch])
- not_found!('Branch') unless branch
+ branch = find_branch!(params[:branch])
present branch, with: Entities::Branch, project: user_project
end
@@ -60,8 +69,7 @@ module API
put ':id/repository/branches/:branch/protect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
authorize_admin_project
- branch = user_project.repository.find_branch(params[:branch])
- not_found!('Branch') unless branch
+ branch = find_branch!(params[:branch])
protected_branch = user_project.protected_branches.find_by(name: branch.name)
@@ -96,8 +104,7 @@ module API
put ':id/repository/branches/:branch/unprotect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
authorize_admin_project
- branch = user_project.repository.find_branch(params[:branch])
- not_found!("Branch") unless branch
+ branch = find_branch!(params[:branch])
protected_branch = user_project.protected_branches.find_by(name: branch.name)
protected_branch&.destroy
@@ -133,8 +140,7 @@ module API
delete ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
authorize_push_project
- branch = user_project.repository.find_branch(params[:branch])
- not_found!('Branch') unless branch
+ branch = find_branch!(params[:branch])
commit = user_project.repository.commit(branch.dereferenced_target)