Class: GitPusher::Service::BitBucket

Inherits:
Base
  • Object
show all
Defined in:
lib/gitpusher/service/bitbucket.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BitBucket

Returns a new instance of BitBucket.



8
9
10
11
12
13
14
15
16
# File 'lib/gitpusher/service/bitbucket.rb', line 8

def initialize(config)
  super(config)
  @user     = Pit.get(
    'bitbucket', :require => { 'user' => 'Your user name of BitBucket' }
  )['user']
  @password = Pit.get(
    'bitbucket', :require => { 'password' => 'Your user password of BitBucket' }
  )['password']
end

Instance Method Details

#create_repo(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitpusher/service/bitbucket.rb', line 37

def create_repo(name)
  puts "Creating repository #{name} on the mirror ..."
  https = Net::HTTP.new('api.bitbucket.org', 443)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.start{|http|
    request = Net::HTTP::Post.new('/1.0/repositories/')
    request.basic_auth @user, @password
    request.set_form_data({
      :name       => name,
      :scm        => 'git',
      :is_private => 'True',
    })
    response = http.request(request)
  }

  GitPusher::Repo.new(ssh_url(name))
end

#repo(name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitpusher/service/bitbucket.rb', line 18

def repo(name)
  url = sprintf 'https://api.bitbucket.org/1.0/repositories/%s/%s', @user, name
  opt = {"Authorization" => "Basic " + Base64.encode64("#{@user}:#{@password}")}
  opt[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
  begin
    json = open(url, opt) {|io|
      JSON.parse(io.read)
    }
  rescue OpenURI::HTTPError => e
    if e.message === '404 NOT FOUND'
      return nil
    else
      raise e
    end
  end

  GitPusher::Repo.new(ssh_url(name))
end