Commit b9b95a9f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve commit builds API endpoint RESTful behavior

1. Return 404 if commit is not found (RESTful resource not found)
2. Return an empty array if pipeline is not found (resource present, no
   associated builds found)
3. Return an empty array if pipeline found but no builds there (resource
   present, no associated builds)
parent 8e666187
...@@ -33,8 +33,10 @@ module API ...@@ -33,8 +33,10 @@ module API
get ':id/repository/commits/:sha/builds' do get ':id/repository/commits/:sha/builds' do
authorize_read_builds! authorize_read_builds!
return not_found! unless user_project.commit(params[:sha])
pipelines = user_project.pipelines.where(sha: params[:sha]) pipelines = user_project.pipelines.where(sha: params[:sha])
return not_found! if pipelines.empty? return [] if pipelines.empty?
builds = user_project.builds.where(pipeline: pipelines).order('id DESC') builds = user_project.builds.where(pipeline: pipelines).order('id DESC')
builds = filter_builds(builds, params[:scope]) builds = filter_builds(builds, params[:scope])
......
...@@ -63,6 +63,19 @@ describe API::API, api: true do ...@@ -63,6 +63,19 @@ describe API::API, api: true do
end end
describe 'GET /projects/:id/repository/commits/:sha/builds' do describe 'GET /projects/:id/repository/commits/:sha/builds' do
context 'when commit does not exist in repository' do
before do
get api("/projects/#{project.id}/repository/commits/1a271fd1/builds", api_user)
end
it 'responds with 404' do
expect(response).to have_http_status(404)
end
end
context 'when commit exists in repository' do
context 'when user is authorized' do
context 'when pipeline has builds' do
before do before do
create(:ci_pipeline, project: project, sha: project.commit.id) create(:ci_pipeline, project: project, sha: project.commit.id)
create(:ci_build, pipeline: pipeline) create(:ci_build, pipeline: pipeline)
...@@ -71,7 +84,6 @@ describe API::API, api: true do ...@@ -71,7 +84,6 @@ describe API::API, api: true do
get api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", api_user) get api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", api_user)
end end
context 'authorized user' do
it 'should return project builds for specific commit' do it 'should return project builds for specific commit' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
...@@ -79,11 +91,32 @@ describe API::API, api: true do ...@@ -79,11 +91,32 @@ describe API::API, api: true do
end end
end end
context 'unauthorized user' do context 'when pipeline has no builds' do
let(:api_user) { nil } before do
branch_head = project.commit('feature').id
get api("/projects/#{project.id}/repository/commits/#{branch_head}/builds", api_user)
end
it 'returns an empty array' do
expect(response).to have_http_status(200)
expect(json_response).to be_an Array
expect(json_response).to be_empty
end
end
end
context 'when user is not authorized' do
before do
create(:ci_pipeline, project: project, sha: project.commit.id)
create(:ci_build, pipeline: pipeline)
get api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", nil)
end
it 'should not return project builds' do it 'should not return project builds' do
expect(response).to have_http_status(401) expect(response).to have_http_status(401)
expect(json_response.except('message')).to be_empty
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment