diff --git a/lib/gitlab/github_import/user_finder.rb b/lib/gitlab/github_import/user_finder.rb index 159a1d97cf1d7bad491ffcaea371888fa785458d..f0cbdc36dba4d419874b5ec1a7657b6fe3f067aa 100644 --- a/lib/gitlab/github_import/user_finder.rb +++ b/lib/gitlab/github_import/user_finder.rb @@ -129,8 +129,14 @@ def fetch_source_name_from_github(username) next source_name if source_name.present? end - user = client.user(username) - source_name = user.fetch(:name, username) + begin + user = client.user(username) + source_name = user.fetch(:name, username) + rescue ::Octokit::NotFound => error + log("GitHub user not found. #{error.message}", username: username) + + source_name = username + end cache_source_name(username, source_name) diff --git a/spec/lib/gitlab/github_import/user_finder_spec.rb b/spec/lib/gitlab/github_import/user_finder_spec.rb index 997e8e1e4d3f518fecf903f39a2b31fd7432c85d..40d560fb6f3176e0bd3497d70a33c4f3525facb9 100644 --- a/spec/lib/gitlab/github_import/user_finder_spec.rb +++ b/spec/lib/gitlab/github_import/user_finder_spec.rb @@ -166,7 +166,7 @@ end context 'when source user does not exist' do - it 'fetches the user source name from GitHub and create a new source user' do + it 'fetches the user source name from GitHub and creates a new source user' do user = { id: 7, login: 'kittens' } expect(client).to receive(:user).with('kittens').and_return({ name: 'Source name' }) @@ -177,6 +177,27 @@ source_user_identifier: '7' ) end + + context 'when GitHub user does not exist' do + before do + allow(client).to receive(:user).with('Copilot').and_raise(Octokit::NotFound) + end + + it 'creates a new source user, logs, and sets the `source_name` to be the username' do + user = { id: 7, login: 'Copilot' } + + expect(Gitlab::GithubImport::Logger).to receive(:info).with(hash_including( + message: include('GitHub user not found.'), + username: 'Copilot' + )) + expect { finder.source_user(user) }.to change { Import::SourceUser.count }.by(1) + expect(Import::SourceUser.last).to have_attributes( + source_name: 'Copilot', + source_username: 'Copilot', + source_user_identifier: '7' + ) + end + end end end