Commit dcc67ac1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Return empty array when commit has no statuses in API

This makes API endpoint for Commit Statuses return empty array instead
of 404 when commit exists, but has no statuses.

Closes #3080
parent 17251cf4
...@@ -18,10 +18,12 @@ module API ...@@ -18,10 +18,12 @@ module API
# Examples: # Examples:
# GET /projects/:id/repository/commits/:sha/statuses # GET /projects/:id/repository/commits/:sha/statuses
get ':id/repository/commits/:sha/statuses' do get ':id/repository/commits/:sha/statuses' do
authorize! :read_commit_status, user_project authorize!(:read_commit_status, user_project)
sha = params[:sha]
ci_commit = user_project.ci_commit(sha) ci_commit = user_project.ci_commit(params[:sha])
not_found! 'Commit' unless ci_commit not_found!('Commit') unless user_project.commit(params[:sha])
return [] unless ci_commit
statuses = ci_commit.statuses statuses = ci_commit.statuses
statuses = statuses.latest unless parse_boolean(params[:all]) statuses = statuses.latest unless parse_boolean(params[:all])
statuses = statuses.where(ref: params[:ref]) if params[:ref].present? statuses = statuses.where(ref: params[:ref]) if params[:ref].present?
......
...@@ -2,15 +2,18 @@ require 'spec_helper' ...@@ -2,15 +2,18 @@ require 'spec_helper'
describe API::CommitStatus, api: true do describe API::CommitStatus, api: true do
include ApiHelpers include ApiHelpers
let!(:project) { create(:project) } let!(:project) { create(:project) }
let(:commit) { project.repository.commit } let(:commit) { project.repository.commit }
let!(:ci_commit) { project.ensure_ci_commit(commit.id) }
let(:commit_status) { create(:commit_status, commit: ci_commit) } let(:commit_status) { create(:commit_status, commit: ci_commit) }
let(:guest) { create_user(ProjectMember::GUEST) } let(:guest) { create_user(ProjectMember::GUEST) }
let(:reporter) { create_user(ProjectMember::REPORTER) } let(:reporter) { create_user(ProjectMember::REPORTER) }
let(:developer) { create_user(ProjectMember::DEVELOPER) } let(:developer) { create_user(ProjectMember::DEVELOPER) }
describe "GET /projects/:id/repository/commits/:sha/statuses" do describe "GET /projects/:id/repository/commits/:sha/statuses" do
context 'ci commit exists' do
let!(:ci_commit) { project.ensure_ci_commit(commit.id) }
it_behaves_like 'a paginated resources' do it_behaves_like 'a paginated resources' do
let(:request) { get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter) } let(:request) { get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter) }
end end
...@@ -18,13 +21,29 @@ describe API::CommitStatus, api: true do ...@@ -18,13 +21,29 @@ describe API::CommitStatus, api: true do
context "reporter user" do context "reporter user" do
let(:statuses_id) { json_response.map { |status| status['id'] } } let(:statuses_id) { json_response.map { |status| status['id'] } }
before do let!(:status1) do
@status1 = create(:commit_status, commit: ci_commit, status: 'running') create(:commit_status, commit: ci_commit, status: 'running')
@status2 = create(:commit_status, commit: ci_commit, name: 'coverage', status: 'pending') end
@status3 = create(:commit_status, commit: ci_commit, name: 'coverage', ref: 'develop', status: 'running', allow_failure: true)
@status4 = create(:commit_status, commit: ci_commit, name: 'coverage', status: 'success') let!(:status2) do
@status5 = create(:commit_status, commit: ci_commit, ref: 'develop', status: 'success') create(:commit_status, commit: ci_commit, name: 'coverage', status: 'pending')
@status6 = create(:commit_status, commit: ci_commit, status: 'success') end
let!(:status3) do
create(:commit_status, commit: ci_commit, name: 'coverage', ref: 'develop',
status: 'running', allow_failure: true)
end
let!(:status4) do
create(:commit_status, commit: ci_commit, name: 'coverage', status: 'success')
end
let!(:status5) do
create(:commit_status, commit: ci_commit, ref: 'develop', status: 'success')
end
let!(:status6) do
create(:commit_status, commit: ci_commit, status: 'success')
end end
it "should return latest commit statuses" do it "should return latest commit statuses" do
...@@ -32,7 +51,7 @@ describe API::CommitStatus, api: true do ...@@ -32,7 +51,7 @@ describe API::CommitStatus, api: true do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(statuses_id).to contain_exactly(@status3.id, @status4.id, @status5.id, @status6.id) expect(statuses_id).to contain_exactly(status3.id, status4.id, status5.id, status6.id)
json_response.sort_by!{ |status| status['id'] } json_response.sort_by!{ |status| status['id'] }
expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false]) expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false])
end end
...@@ -42,7 +61,7 @@ describe API::CommitStatus, api: true do ...@@ -42,7 +61,7 @@ describe API::CommitStatus, api: true do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(statuses_id).to contain_exactly(@status1.id, @status2.id, @status3.id, @status4.id, @status5.id, @status6.id) expect(statuses_id).to contain_exactly(status1.id, status2.id, status3.id, status4.id, status5.id, status6.id)
end end
it "should return latest commit statuses for specific ref" do it "should return latest commit statuses for specific ref" do
...@@ -50,7 +69,7 @@ describe API::CommitStatus, api: true do ...@@ -50,7 +69,7 @@ describe API::CommitStatus, api: true do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(statuses_id).to contain_exactly(@status3.id, @status5.id) expect(statuses_id).to contain_exactly(status3.id, status5.id)
end end
it "should return latest commit statuses for specific name" do it "should return latest commit statuses for specific name" do
...@@ -58,7 +77,20 @@ describe API::CommitStatus, api: true do ...@@ -58,7 +77,20 @@ describe API::CommitStatus, api: true do
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(statuses_id).to contain_exactly(@status3.id, @status4.id) expect(statuses_id).to contain_exactly(status3.id, status4.id)
end
end
end
context 'ci commit does not exist' do
before do
get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", reporter)
end
it 'returns empty array' do
expect(response.status).to eq 200
expect(json_response).to be_an Array
expect(json_response).to be_empty
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