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, #repo

Instance Method Summary collapse

Methods inherited from Repository

#bare_clone, #mirror

Constructor Details

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

Initializer

Parameters:

  • name (String)

    name of the repository

  • owner (String)

    owner of the repository

  • credentials (Hash) (defaults to: {})
  • options (Hash) (defaults to: {})

    options for creation

Options Hash (credentials):

  • :oauth2_token (Symbol)

    used in oAuth2 authentication

  • :username (Symbol)

    used in basic authentication

  • :password (Symbol)

    used in basic authentication

See Also:



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

def initialize(name, owner, credentials = {}, options = {})
  super(name, owner)
  self.credentials = credentials
  self.options = options
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



9
10
11
# File 'lib/git_duplicator/services/github.rb', line 9

def credentials
  @credentials
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/git_duplicator/services/github.rb', line 9

def options
  @options
end

Instance Method Details

#createObject

Create the repository



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

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

#deleteObject

Delete the repositroy



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

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



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

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