Skip to content

Commit

Permalink
GitService: Move factory part into dedicated module
Browse files Browse the repository at this point in the history
  • Loading branch information
neomilium committed Oct 6, 2021
1 parent bb02241 commit ba59519
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
18 changes: 0 additions & 18 deletions lib/modulesync/git_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions lib/modulesync/git_service/factory.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/modulesync/source_code.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'modulesync'
require 'modulesync/git_service'
require 'modulesync/git_service/factory'
require 'modulesync/repository'
require 'modulesync/util'

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/modulesync/git_service/factory_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions spec/unit/modulesync/git_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(:[])
Expand Down

0 comments on commit ba59519

Please sign in to comment.