diff --git a/lib/modulesync/git_service.rb b/lib/modulesync/git_service.rb index 7146ea75..df606bc9 100644 --- a/lib/modulesync/git_service.rb +++ b/lib/modulesync/git_service.rb @@ -5,24 +5,6 @@ class Error < StandardError; end module GitService class MissingCredentialsError < Error; end - def self.instantiate(type:, endpoint:, token:) - raise MissingCredentialsError, <<~MESSAGE if token.nil? - A token is required to use services from #{type}: - Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options. - MESSAGE - - case type - when :github - require 'modulesync/git_service/github' - ModuleSync::GitService::GitHub.new(token, endpoint) - when :gitlab - require 'modulesync/git_service/gitlab' - ModuleSync::GitService::GitLab.new(token, endpoint) - else - raise NotImplementedError, "Unable to manage a PR/MR for Git service: '#{type}'" - end - end - def self.configuration_for(sourcecode:) type = type_for(sourcecode: sourcecode) diff --git a/lib/modulesync/git_service/factory.rb b/lib/modulesync/git_service/factory.rb new file mode 100644 index 00000000..263ad842 --- /dev/null +++ b/lib/modulesync/git_service/factory.rb @@ -0,0 +1,23 @@ +module ModuleSync + module GitService + module Factory + def self.instantiate(type:, endpoint:, token:) + raise MissingCredentialsError, <<~MESSAGE if token.nil? + A token is required to use services from #{type}: + Please set environment variable: "#{type.upcase}_TOKEN" or set the token entry in module options. + MESSAGE + + case type + when :github + require 'modulesync/git_service/github' + ModuleSync::GitService::GitHub.new(token, endpoint) + when :gitlab + require 'modulesync/git_service/gitlab' + ModuleSync::GitService::GitLab.new(token, endpoint) + else + raise NotImplementedError, "Unable to manage a PR/MR for Git service: '#{type}'" + end + end + end + end +end diff --git a/lib/modulesync/source_code.rb b/lib/modulesync/source_code.rb index 248bd420..12cfeab7 100644 --- a/lib/modulesync/source_code.rb +++ b/lib/modulesync/source_code.rb @@ -1,5 +1,6 @@ require 'modulesync' require 'modulesync/git_service' +require 'modulesync/git_service/factory' require 'modulesync/repository' require 'modulesync/util' @@ -49,7 +50,7 @@ def path(*parts) end def git_service - @git_service ||= GitService.instantiate(**git_service_configuration) + @git_service ||= GitService::Factory.instantiate(**git_service_configuration) end def git_service_configuration diff --git a/spec/unit/modulesync/git_service/factory_spec.rb b/spec/unit/modulesync/git_service/factory_spec.rb new file mode 100644 index 00000000..9e6234cc --- /dev/null +++ b/spec/unit/modulesync/git_service/factory_spec.rb @@ -0,0 +1,16 @@ +require 'modulesync' +require 'modulesync/git_service/factory' + +describe ModuleSync::GitService::Factory do + context 'when instantiate a GitHub service without credentials' do + it 'raises an error' do + expect { ModuleSync::GitService::Factory.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) + end + end + + context 'when instantiate a GitLab service without credentials' do + it 'raises an error' do + expect { ModuleSync::GitService::Factory.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) + end + end +end diff --git a/spec/unit/modulesync/git_service_spec.rb b/spec/unit/modulesync/git_service_spec.rb index d8dbd619..e8016c23 100644 --- a/spec/unit/modulesync/git_service_spec.rb +++ b/spec/unit/modulesync/git_service_spec.rb @@ -9,18 +9,6 @@ ModuleSync.instance_variable_set '@options', options end - context 'when instantiate a GitHub service without credentials' do - it 'raises an error' do - expect { ModuleSync::GitService.instantiate(type: :github, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) - end - end - - context 'when instantiate a GitLab service without credentials' do - it 'raises an error' do - expect { ModuleSync::GitService.instantiate(type: :gitlab, endpoint: nil, token: nil) }.to raise_error(ModuleSync::GitService::MissingCredentialsError) - end - end - context 'when guessing the git service configuration' do before do allow(ENV).to receive(:[])