PageRenderTime 122ms CodeModel.GetById 34ms RepoModel.GetById 3ms app.codeStats 0ms

/lib/bitbucket-api-extension/api.rb

https://bitbucket.org/snishio/bitbucket-api-extension
Ruby | 121 lines | 100 code | 15 blank | 6 comment | 1 complexity | 4fbd3d5828aaad7f4b0af99e6355c666 MD5 | raw file
  1. # coding: utf-8
  2. module BitbucketApiExtension; end
  3. class BitbucketApiExtension::Api
  4. BITBUCKET_URI = 'https://bitbucket.org'
  5. BITBUCKET_API_URI = 'https://api.bitbucket.org/1.0/repositories'
  6. attr_reader :account, :project
  7. def initialize(project, account=nil)
  8. @project = project
  9. @account = account
  10. end
  11. # プルリクエスト一覧を取得する
  12. def pull_requests
  13. uri = pull_request_list_url(@project.organization_name, @project.name)
  14. list = []
  15. open(uri, auth_option) do |f|
  16. html = Nokogiri::HTML(f.read).xpath('//table[contains(@class,"pullrequest-list")]/tbody/tr')
  17. list = html.map do |request|
  18. title = request.xpath('td[@class="title"]/div/a').text
  19. BitbucketApiExtension::PullRequest.new(
  20. title: title,
  21. id: title.scan(/#(\d+):.*/)
  22. .flatten
  23. .first,
  24. request_page_url: request.xpath('td[@class="title"]/div/a')
  25. .map{ |link| BITBUCKET_URI + link['href'] }
  26. .first
  27. .to_s,
  28. author: request.xpath('td[@class="author"]/div/span')
  29. .text)
  30. end
  31. end
  32. list
  33. end
  34. # プルリクエストの詳細情報を取得する
  35. def pull_request_detail(pull_request)
  36. detail = BitbucketApiExtension::PullRequestDetail.new(pull_request.attributes)
  37. open(pull_request.request_page_url, auth_option) do |f|
  38. html = Nokogiri::HTML(f.read)
  39. from_to_elements = html.css('div.clearfix div.compare-widget-container div.compare-widget')
  40. from = from_to_elements.first
  41. push = from_to_elements.last
  42. detail.from_user_or_team_name = from.attributes['data-user-name'].try(:value)
  43. detail.from_repository_name = from.attributes['data-repo-name'].try(:value)
  44. detail.from_branch_name = from.attributes['data-branch-name'].try(:value)
  45. detail.from_commit_id = from.attributes['data-commit-id'].try(:value)
  46. detail.to_user_or_team_name = push.attributes['data-user-name'].try(:value)
  47. detail.to_repository_name = push.attributes['data-repo-name'].try(:value)
  48. detail.to_branch_name = push.attributes['data-branch-name'].try(:value)
  49. end
  50. detail
  51. end
  52. # 指定したプルリクエストのマージコマンドを取得する
  53. def merge_commands(pull_request)
  54. commands = []
  55. open(pull_request.request_page_url, auth_option) do |f|
  56. html = Nokogiri::HTML(f.read)
  57. commands = html.xpath('//pre[@class="merge-commands"]/code').text.split("\n")
  58. end
  59. commands
  60. end
  61. # 指定したプルリクエストに関連するコメントを取得する
  62. def pull_request_comment(pull_request)
  63. url = pull_request_comment_url(@project.organization_name, @project.name, pull_request.id)
  64. comment = []
  65. open(url, auth_option) do |f|
  66. list = JSON.parse(f.read)
  67. comment = list.map do |c|
  68. BitbucketApiExtension::Comment.new(
  69. pull_request_id: c["pull_request_id"],
  70. author_username: c["author_info"]["username"],
  71. author_display_name: c["author_info"]["display_name"],
  72. comment: c["content"])
  73. end
  74. end
  75. comment
  76. end
  77. # 指定したプルリクエストにコメントを書き込む
  78. def post_comment(pull_request, text)
  79. url = pull_request_comment_url(@project.organization_name, @project.name, pull_request.id)
  80. post(url, content: text)
  81. end
  82. private
  83. def auth_option
  84. if @account.nil?
  85. {}
  86. else
  87. {http_basic_authentication: [@account.user_id, @account.user_password]}
  88. end
  89. end
  90. def pull_request_list_url(organization_name, project_name)
  91. "#{BITBUCKET_URI}/#{organization_name}/#{project_name}/pull-requests"
  92. end
  93. def pull_request_comment_url(organization_name, project_name, id)
  94. "#{BITBUCKET_API_URI}/#{organization_name}/#{project_name}/pullrequests/#{id}/comments"
  95. end
  96. def post(uri, body)
  97. uri = URI.parse(uri)
  98. request = Net::HTTP::Post.new(uri.path)
  99. request.basic_auth(@account.user_id, @account.user_password)
  100. request.set_form_data(body)
  101. http = Net::HTTP.new(uri.host, uri.port)
  102. http.use_ssl = true
  103. http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  104. http.start { |h| h.request(request) }
  105. end
  106. end