Commit 40e7846f3e25b7f679c9dda719c135fca1ef3d5b

Authored by Sebastian Ziebell
1 parent 2f0a75ab

Status code 404 returned when retrieving non existent branch (issue #2922)

Accessing a repository branch that does not exist returns a 404 error instead
of 200 now. Added a test.
lib/api/projects.rb
@@ -230,6 +230,7 @@ module Gitlab @@ -230,6 +230,7 @@ module Gitlab
230 # GET /projects/:id/repository/branches/:branch 230 # GET /projects/:id/repository/branches/:branch
231 get ":id/repository/branches/:branch" do 231 get ":id/repository/branches/:branch" do
232 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] } 232 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
  233 + error!("Branch does not exist", 404) if @branch.nil?
233 present @branch, with: Entities::RepoObject, project: user_project 234 present @branch, with: Entities::RepoObject, project: user_project
234 end 235 end
235 236
spec/requests/api/projects_spec.rb
@@ -109,6 +109,11 @@ describe Gitlab::API do @@ -109,6 +109,11 @@ describe Gitlab::API do
109 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1' 109 json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
110 json_response['protected'].should == false 110 json_response['protected'].should == false
111 end 111 end
  112 +
  113 + it "should return a 404 error if branch is not available" do
  114 + get api("/projects/#{project.id}/repository/branches/unknown", user)
  115 + response.status.should == 404
  116 + end
112 end 117 end
113 118
114 describe "PUT /projects/:id/repository/branches/:branch/protect" do 119 describe "PUT /projects/:id/repository/branches/:branch/protect" do