Gracefully handle orphaned write deploy keys in /internal/post_receive

On GitLab.com, there are write deploy keys with no associated users.
Pushes with these deploy keys end with an Error 500 since we attempt
to look up redirect message. If there is no user, don't attempt
to display a redirect message.

Closes #41466
This commit is contained in:
Stan Hu 2017-12-24 09:03:58 -08:00
parent 0c8d7e8460
commit 36bdf3dc9a
3 changed files with 21 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
title: Gracefully handle orphaned write deploy keys in /internal/post_receive
merge_request:
author:
type: fixed

View File

@ -190,9 +190,12 @@ module API
project = Gitlab::GlRepository.parse(params[:gl_repository]).first
user = identify(params[:identifier])
redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id)
if redirect_message
output[:redirected_message] = redirect_message
# A user is not guaranteed to be returned; an orphaned write deploy
# key could be used
if user
redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id)
output[:redirected_message] = redirect_message if redirect_message
end
output

View File

@ -784,6 +784,16 @@ describe API::Internal do
expect(json_response["redirected_message"]).to eq(project_moved.redirect_message)
end
end
context 'with an orphaned write deploy key' do
it 'does not try to notify that project moved' do
allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(nil)
post api("/internal/post_receive"), valid_params
expect(response).to have_gitlab_http_status(200)
end
end
end
describe 'POST /internal/pre_receive' do