lib/geet/git/repository.rb in geet-0.1.9 vs lib/geet/git/repository.rb in geet-0.1.10
- old
+ new
@@ -17,62 +17,62 @@
}x
ORIGIN_NAME = 'origin'
UPSTREAM_NAME = 'upstream'
- def initialize(api_token, upstream: false, location: nil)
- @api_token = api_token
+ def initialize(upstream: false, location: nil)
@upstream = upstream
@location = location
+ @api_token = extract_env_api_token
end
# REMOTE FUNCTIONALITIES (REPOSITORY)
def collaborators
- provider_module::Collaborator.list(api_interface)
+ attempt_provider_call(:Collaborator, :list, api_interface)
end
def labels
- provider_module::Label.list(api_interface)
+ attempt_provider_call(:Label, :list, api_interface)
end
def create_gist(filename, content, description: nil, publik: false)
- provider_module::Gist.create(filename, content, api_interface, description: description, publik: publik)
+ attempt_provider_call(:Gist, :create, filename, content, api_interface, description: description, publik: publik)
end
def create_issue(title, description)
- provider_module::Issue.create(title, description, api_interface)
+ attempt_provider_call(:Issue, :create, title, description, api_interface)
end
def abstract_issues(milestone: nil)
- provider_module::AbstractIssue.list(api_interface, milestone: milestone)
+ attempt_provider_call(:AbstractIssue, :list, api_interface, milestone: milestone)
end
def issues
- provider_module::Issue.list(api_interface)
+ attempt_provider_call(:Issue, :list, api_interface)
end
def milestone(number)
- provider_module::Milestone.find(number, api_interface)
+ attempt_provider_call(:Milestone, :find, number, api_interface)
end
def milestones
- provider_module::Milestone.list(api_interface)
+ attempt_provider_call(:Milestone, :list, api_interface)
end
def create_pr(title, description, head)
- provider_module::PR.create(title, description, head, api_interface)
+ attempt_provider_call(:PR, :create, title, description, head, api_interface)
end
def prs(head: nil)
- provider_module::PR.list(api_interface, head: head)
+ attempt_provider_call(:PR, :list, api_interface, head: head)
end
# REMOTE FUNCTIONALITIES (ACCOUNT)
def authenticated_user
- provider_module::Account.new(api_interface).authenticated_user
+ attempt_provider_call(:Account, :new, api_interface).authenticated_user
end
# OTHER/CONVENIENCE FUNCTIONALITIES
def current_branch
@@ -103,10 +103,16 @@
remote_url
end
# PROVIDER
+ def extract_env_api_token
+ env_variable_name = "#{provider_domain[/(.*)\.\w+/, 1].upcase}_API_TOKEN"
+
+ ENV[env_variable_name] || raise("#{env_variable_name} not set!")
+ end
+
def provider_domain
# We assume that it's not possible to have origin and upstream on different providers.
#
remote_url = remote(ORIGIN_NAME)
@@ -115,16 +121,30 @@
raise "Can't identify domain in the provider domain string: #{provider_domain}" if domain !~ /(.*)\.\w+/
domain
end
- def provider_module
+ # Attempt to find the provider class and send the specified method, returning a friendly
+ # error (functionality X [Y] is missing) when a class/method is missing.
+ def attempt_provider_call(class_name, meth, *args)
module_name = provider_domain[/(.*)\.\w+/, 1].capitalize
require_provider_modules
- Kernel.const_get("Geet::#{module_name}")
+ full_class_name = "Geet::#{module_name}::#{class_name}"
+
+ if Kernel.const_defined?(full_class_name)
+ klass = Kernel.const_get(full_class_name)
+
+ if ! klass.respond_to?(meth)
+ raise "The functionality invoked (#{class_name} #{meth}) is not currently supported!"
+ end
+
+ klass.send(meth, *args)
+ else
+ raise "The functionality (#{class_name}) invoked is not currently supported!"
+ end
end
def require_provider_modules
provider_dirname = provider_domain[/(.*)\.\w+/, 1]
files_pattern = "#{__dir__}/../#{provider_dirname}/*.rb"
@@ -133,10 +153,10 @@
end
# OTHER HELPERS
def api_interface
- provider_module::ApiInterface.new(@api_token, path(upstream: @upstream), @upstream)
+ attempt_provider_call(:ApiInterface, :new, @api_token, path(upstream: @upstream), @upstream)
end
# Example: `donaldduck/geet`
#
def path(upstream: false)