Class: BitbucketMigration::Api
- Inherits:
-
Object
- Object
- BitbucketMigration::Api
- Defined in:
- lib/bitbucket_migration/api.rb
Defined Under Namespace
Classes: Endpoint, Repository
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#team ⇒ Object
readonly
Returns the value of attribute team.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#create_repository(name, language = nil) ⇒ Object
Create new repository.
-
#delete_repository(name) ⇒ Object
Delete existing repository.
-
#get_repositories ⇒ Object
Get repository list #### TO-do rewrite to return array of Repository objects.
-
#get_repository(name) ⇒ Object
Get information for specific repository.
-
#initialize(username, password, team) ⇒ Api
constructor
A new instance of Api.
-
#repository_exists?(name) ⇒ Boolean
Check if repository with given name exists on the bitbucket side.
Constructor Details
#initialize(username, password, team) ⇒ Api
Returns a new instance of Api.
9 10 11 12 13 14 |
# File 'lib/bitbucket_migration/api.rb', line 9 def initialize(username, password, team) @username ||= username @password ||= password @team ||= team @endpoint ||= Endpoint.new(@username, @password, @team) end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/bitbucket_migration/api.rb', line 7 def endpoint @endpoint end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
7 8 9 |
# File 'lib/bitbucket_migration/api.rb', line 7 def password @password end |
#team ⇒ Object (readonly)
Returns the value of attribute team.
7 8 9 |
# File 'lib/bitbucket_migration/api.rb', line 7 def team @team end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/bitbucket_migration/api.rb', line 7 def username @username end |
Instance Method Details
#create_repository(name, language = nil) ⇒ Object
Create new repository
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/bitbucket_migration/api.rb', line 42 def create_repository(name, language=nil) raise ArgumentError.new("Unable to create repository if name is nil") if name == nil raise RuntimeError.new("Unable to create new repository over existing one") if self.repository_exists?(name) payload = { 'scm' => 'git', 'name' => name, 'is_private' => true } payload['language'] = language if language != nil # Sending POST request with payload response = RestClient.post(@endpoint.repository(name), payload.to_json, :content_type => :json, :accept => :json) if response.code == 200 || response.code == 201 return self.get_repository(name) end end |
#delete_repository(name) ⇒ Object
Delete existing repository
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bitbucket_migration/api.rb', line 56 def delete_repository(name) raise ArgumentError.new("Unable to delete repository if name is nil") if name == nil begin response = RestClient.delete(@endpoint.repository(name)) case response.code when 200 when 204 return true else return false end rescue => e e.response end end |
#get_repositories ⇒ Object
Get repository list #### TO-do rewrite to return array of Repository objects
17 18 19 |
# File 'lib/bitbucket_migration/api.rb', line 17 def get_repositories return JSON.parse(RestClient.get(@endpoint.repositories)) end |
#get_repository(name) ⇒ Object
Get information for specific repository
22 23 24 25 26 27 28 |
# File 'lib/bitbucket_migration/api.rb', line 22 def get_repository(name) raise ArgumentError.new("Unable to query with nil value for name") if name == nil response = RestClient.get(@endpoint.repository(name)) if response.code == 200 return Repository.new(JSON.parse(response)) end end |
#repository_exists?(name) ⇒ Boolean
Check if repository with given name exists on the bitbucket side
31 32 33 34 35 36 37 38 39 |
# File 'lib/bitbucket_migration/api.rb', line 31 def repository_exists?(name) raise ArgumentError.new("Unable to query with nil value for name") if name == nil begin repository = JSON.parse(RestClient.get(@endpoint.repository(name))) return repository['name'] == name ? true : false rescue RestClient::ResourceNotFound return false end end |