gitpusher /lib/gitpusher/service/bitbucket.rb

Language Ruby Lines 64
MD5 Hash 42bc11d0afaf09f9dc192f9449026702 Estimated Cost $1,285 (why?)
Repository https://bitbucket.org/KitaitiMakoto/gitpusher.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'open-uri'
require 'net/http'

module GitPusher
  module Service
    class BitBucket < Base

      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

      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

      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

      private
      def ssh_url(name)
        sprintf "git@bitbucket.org:%s/%s.git", @user, name
      end

    end
  end
end
Back to Top