From 118d12405a8ed1e8251c74a08044e47cd17998c2 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 12 Mar 2019 16:22:32 -0700 Subject: [PATCH] Fix 500 error caused by CODEOWNERS with no matches Including a CODEOWNERS file with lines without any matching username or e-mail regular expressions would cause an Error 500. Don't attempt a database query if there is nothing to query. Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/10282 --- changelogs/unreleased/sh-fix-blank-codeowners-ce.yml | 5 +++++ lib/gitlab/user_extractor.rb | 6 +++++- spec/lib/gitlab/user_extractor_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/sh-fix-blank-codeowners-ce.yml diff --git a/changelogs/unreleased/sh-fix-blank-codeowners-ce.yml b/changelogs/unreleased/sh-fix-blank-codeowners-ce.yml new file mode 100644 index 00000000000..05ea5869eb1 --- /dev/null +++ b/changelogs/unreleased/sh-fix-blank-codeowners-ce.yml @@ -0,0 +1,5 @@ +--- +title: Fix 500 error caused by CODEOWNERS with no matches +merge_request: 26072 +author: +type: fixed diff --git a/lib/gitlab/user_extractor.rb b/lib/gitlab/user_extractor.rb index 874599688bb..076781fdd96 100644 --- a/lib/gitlab/user_extractor.rb +++ b/lib/gitlab/user_extractor.rb @@ -17,7 +17,11 @@ module Gitlab def users return User.none unless @text.present? - @users ||= User.from_union(union_relations) + relations = union_relations + + return User.none unless relations.any? + + @users ||= User.from_union(relations) end def usernames diff --git a/spec/lib/gitlab/user_extractor_spec.rb b/spec/lib/gitlab/user_extractor_spec.rb index fcc05ab3a0c..6e2bb81fbda 100644 --- a/spec/lib/gitlab/user_extractor_spec.rb +++ b/spec/lib/gitlab/user_extractor_spec.rb @@ -48,6 +48,14 @@ describe Gitlab::UserExtractor do it 'includes all mentioned usernames' do expect(extractor.matches[:usernames]).to contain_exactly('user-1', 'user-2', 'user-4') end + + context 'input has no matching e-mail or usernames' do + it 'returns an empty list of users' do + extractor = described_class.new('My test') + + expect(extractor.users).to be_empty + end + end end describe '#references' do