PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/gitpusher/service/bitbucket.rb

https://bitbucket.org/KitaitiMakoto/gitpusher
Ruby | 63 lines | 55 code | 8 blank | 0 comment | 1 complexity | 42bc11d0afaf09f9dc192f9449026702 MD5 | raw file
  1. require 'open-uri'
  2. require 'net/http'
  3. module GitPusher
  4. module Service
  5. class BitBucket < Base
  6. def initialize(config)
  7. super(config)
  8. @user = Pit.get(
  9. 'bitbucket', :require => { 'user' => 'Your user name of BitBucket' }
  10. )['user']
  11. @password = Pit.get(
  12. 'bitbucket', :require => { 'password' => 'Your user password of BitBucket' }
  13. )['password']
  14. end
  15. def repo(name)
  16. url = sprintf 'https://api.bitbucket.org/1.0/repositories/%s/%s', @user, name
  17. opt = {"Authorization" => "Basic " + Base64.encode64("#{@user}:#{@password}")}
  18. opt[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE
  19. begin
  20. json = open(url, opt) {|io|
  21. JSON.parse(io.read)
  22. }
  23. rescue OpenURI::HTTPError => e
  24. if e.message === '404 NOT FOUND'
  25. return nil
  26. else
  27. raise e
  28. end
  29. end
  30. GitPusher::Repo.new(ssh_url(name))
  31. end
  32. def create_repo(name)
  33. puts "Creating repository #{name} on the mirror ..."
  34. https = Net::HTTP.new('api.bitbucket.org', 443)
  35. https.use_ssl = true
  36. https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  37. https.start{|http|
  38. request = Net::HTTP::Post.new('/1.0/repositories/')
  39. request.basic_auth @user, @password
  40. request.set_form_data({
  41. :name => name,
  42. :scm => 'git',
  43. :is_private => 'True',
  44. })
  45. response = http.request(request)
  46. }
  47. GitPusher::Repo.new(ssh_url(name))
  48. end
  49. private
  50. def ssh_url(name)
  51. sprintf "git@bitbucket.org:%s/%s.git", @user, name
  52. end
  53. end
  54. end
  55. end