diff options
author | Rémy Coutable <remy@rymai.me> | 2017-10-18 10:38:16 +0000 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-10-18 10:38:16 +0000 |
commit | 220a5c375972462a3c583a106c5ca4c8a2138267 (patch) | |
tree | 365c8941f9400dcb0210483aa5c1bca2225827a5 /lib/api | |
parent | be3bddf90d81b84243b74c3013202aad1c4ab3a9 (diff) | |
parent | 26ec874f77b00124615ff55d48ca79f7631a6c15 (diff) | |
download | gitlab-ce-220a5c375972462a3c583a106c5ca4c8a2138267.tar.gz |
Merge branch '37032-get-project-branch-invalid-name-message' into 'master'
Get Project Branch API shows an helpful error message on invalid refname
Closes #37032
See merge request gitlab-org/gitlab-ce!14884
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/branches.rb | 22 |
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) |