Class: GitDuplicator::Services::GithubRepository

Inherits:
GitDuplicator::ServiceRepository show all
Defined in:
lib/git_duplicator/services/github.rb

Constant Summary collapse

BASE_URI =
'https://api.github.com'

Instance Attribute Summary collapse

Attributes inherited from GitDuplicator::ServiceRepository

#owner

Attributes inherited from Repository

#name

Instance Method Summary collapse

Methods inherited from Repository

#bare_clone, #mirror, #mirror_clone, #set_mirrored_remote, #update_mirrored

Constructor Details

#initialize(name, owner, options = {}) ⇒ GithubRepository

Initializer

Parameters:

  • options (Hash) (defaults to: {})
    • :credentials (Hash) credentials for remote service
      • :oauth2_token (Symbol) used in oAuth2 authentication
      • :username (Symbol) used in basic authentication
      • :password (Symbol) used in basic authentication
    • :remote_options (Hash) creation options for remote service @see @see https://developer.github.com/v3/repos/#create
    • :working_directory (String) assing a working directory


18
19
20
21
22
23
# File 'lib/git_duplicator/services/github.rb', line 18

def initialize(name, owner, options = {})
  self.credentials = options.fetch(:credentials) { {} }
  self.remote_options = options.fetch(:remote_options) { {} }
  self.working_directory = options.fetch(:working_directory) { nil }
  super(name, owner, working_directory)
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



7
8
9
# File 'lib/git_duplicator/services/github.rb', line 7

def credentials
  @credentials
end

#remote_optionsObject

Returns the value of attribute remote_options.



7
8
9
# File 'lib/git_duplicator/services/github.rb', line 7

def remote_options
  @remote_options
end

#working_directoryObject

Returns the value of attribute working_directory.



7
8
9
# File 'lib/git_duplicator/services/github.rb', line 7

def working_directory
  @working_directory
end

Instance Method Details

#createObject

Create the repository



32
33
34
35
36
37
38
# File 'lib/git_duplicator/services/github.rb', line 32

def create
  request_url = BASE_URI + '/user/repos'
  response = HTTP.with(headers(:post, request_url))
  .post(request_url, json: remote_options.merge(name: name))
  code, body = response.code.to_i, response.body
  fail(RepositoryCreationError, body) unless 201 == code
end

#deleteObject

Delete the repositroy



42
43
44
45
46
47
# File 'lib/git_duplicator/services/github.rb', line 42

def delete
  request_url = BASE_URI + "/repos/#{owner}/#{name}"
  response = HTTP.with(headers(:delete, request_url)).delete(request_url)
  code, body = response.code.to_i, response.body
  fail(RepositoryDeletionError, body) unless [204, 404].include?(code)
end

#urlObject

URL of the repositroy



26
27
28
# File 'lib/git_duplicator/services/github.rb', line 26

def url
  "[email protected]:#{owner}/#{name}.git"
end