Skip to content
Snippets Groups Projects
Verified Commit 778793d5 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin :two: Committed by GitLab
Browse files

Fix 500 error in Commits API when the repository is empty

Contributes to #452488

**Problem**

Gitaly started returning an internal error, when the user requests
commits of the empty repository:
gitaly!6590. Rails
doesn't handle it and returns a 500 error.

**Solution**

Add a temporary handler for this error, before the Gitaly code is
updated.

Changelog: fixed
parent a47e17e5
No related branches found
No related tags found
1 merge request!148405Fix 500 error in Commits API when the repository is empty
......@@ -171,6 +171,11 @@ def commits(ref = nil, opts = {})
commits = Commit.decorate(commits, container) if commits.present?
CommitCollection.new(container, commits, ref)
rescue Gitlab::Git::CommandError => e
# Temporary fix to address a new Gitaly internal error: https://gitlab.com/gitlab-org/gitlab/-/issues/452488
return CommitCollection.new(container, [], ref) if e.message.include?('listing commits failed')
raise
end
def commits_between(from, to, limit: nil)
......
......@@ -386,6 +386,26 @@ def expect_to_raise_storage_error
end
end
context 'when Gitaly raises a CommandError error' do
let(:error_message) { 'Boom' }
before do
expect(Gitlab::Git::Commit).to receive(:where).and_raise(Gitlab::Git::CommandError, error_message)
end
it 're-raises an error' do
expect { repository.commits('master', limit: 60) }.to raise_error(Gitlab::Git::CommandError, error_message)
end
context 'when error contains "listing commits failed" message' do
let(:error_message) { 'listing commits failed' }
it 'returns an empty collection' do
expect(repository.commits('master', limit: 60).to_a).to eq([])
end
end
end
context 'when ref is passed' do
it 'returns every commit from the specified ref' do
expect(repository.commits('master', limit: 60).size).to eq(37)
......
......@@ -90,6 +90,16 @@
it_behaves_like 'project commits'
context 'when repository does not have commits' do
let_it_be(:project) { create(:project, :empty_repo) }
it 'returns an empty array' do
get api("/projects/#{project_id}/repository/commits", user)
expect(json_response).to eq([])
end
end
context "since optional parameter" do
it "returns project commits since provided parameter" do
commits = project.repository.commits("master", limit: 2)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment