Class: Maguro::Bitbucket

Inherits:
Object
  • Object
show all
Defined in:
lib/maguro/bitbucket.rb

Constant Summary collapse

BITBUCKET =
"bitbucket.org"
API_BASE =
"https://api.bitbucket.org/2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, app_name, organization) ⇒ Bitbucket

Returns a new instance of Bitbucket.



17
18
19
20
21
22
23
# File 'lib/maguro/bitbucket.rb', line 17

def initialize(builder, app_name, organization)
  @builder = builder
  @app_name = app_name
  @organization = organization
  self.username = nil
  self.password = nil
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



12
13
14
# File 'lib/maguro/bitbucket.rb', line 12

def app_name
  @app_name
end

#builderObject (readonly)

Returns the value of attribute builder.



12
13
14
# File 'lib/maguro/bitbucket.rb', line 12

def builder
  @builder
end

#organizationObject (readonly)

Returns the value of attribute organization.



12
13
14
# File 'lib/maguro/bitbucket.rb', line 12

def organization
  @organization
end

Instance Method Details

#create_repoObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/maguro/bitbucket.rb', line 39

def create_repo
  options = {
    scm:          'git',
    name:         app_name,
    is_private:   true,
    fork_policy:  'no_public_forks',
    language:     'ruby'
  }

  path = "#{API_BASE}/repositories/#{organization}/#{app_name}"
  response = bitbucket_api(path, Net::HTTP::Post, options)

  if response.code == "200"
    # Success
    puts "Successfully created repository for #{app_name}"
  else
    raise "Could not create Git repository for project #{app_name}"
  end
end

#delete_repoObject



59
60
61
62
63
64
65
# File 'lib/maguro/bitbucket.rb', line 59

def delete_repo
  path = "#{API_BASE}/repositories/#{organization}/#{app_name}"
  success_code = 204
  response = bitbucket_api(path, Net::HTTP::Delete, {}, success_code)
  raise "Could not delete repository." if response.code != success_code.to_s
  puts "Successfully deleted repository: '#{app_name}'"
end

#get_repoObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/maguro/bitbucket.rb', line 67

def get_repo
  path = "#{API_BASE}/repositories/#{organization}/#{app_name}"
  response = bitbucket_api(path, Net::HTTP::Get)

  # Do not raise on error, so that this method can be used
  # to query the existence of a repository
  return nil if response.code != "200"

  JSON.parse(response.body)
end

#git_urlObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/maguro/bitbucket.rb', line 25

def git_url
  if @git_url.nil?
    path = "#{API_BASE}/repositories/#{organization}/#{app_name}"
    response = bitbucket_api(path, Net::HTTP::Get)
    
    if response.code == "200"
      @git_url = JSON.parse(response.body)["links"]["clone"].find{|i| i["name"] == "https"}["href"]
    else
      raise "Could not retrieve Git url for project #{app_name}"
    end
  end
  @git_url
end