class BitbucketApiExtension::Api

Constants

BITBUCKET_API_URI
BITBUCKET_URI

Attributes

account[R]
project[R]

Public Class Methods

new(project, account=nil) click to toggle source
# File lib/bitbucket-api-extension/api.rb, line 12
def initialize(project, account=nil)
  @project = project
  @account = account
end

Public Instance Methods

merge_commands(pull_request) click to toggle source

指定したプルリクエストのマージコマンドを取得する

# File lib/bitbucket-api-extension/api.rb, line 61
def merge_commands(pull_request)
  commands = []
  open(pull_request.request_page_url, auth_option) do |f|
    html = Nokogiri::HTML(f.read)
    commands = html.xpath('//pre[@class="merge-commands"]/code').text.split("\n")
  end
  commands
end
post_comment(pull_request, text) click to toggle source

指定したプルリクエストにコメントを書き込む

# File lib/bitbucket-api-extension/api.rb, line 88
def post_comment(pull_request, text)
  url = pull_request_comment_url(@project.organization_name, @project.name, pull_request.id)
  post(url, content: text)
end
pull_request_comment(pull_request) click to toggle source

指定したプルリクエストに関連するコメントを取得する

# File lib/bitbucket-api-extension/api.rb, line 71
def pull_request_comment(pull_request)
  url = pull_request_comment_url(@project.organization_name, @project.name, pull_request.id)
  comment = []
  open(url, auth_option) do |f|
    list = JSON.parse(f.read)
    comment = list.map do |c|
      BitbucketApiExtension::Comment.new(
        pull_request_id: c["pull_request_id"],
        author_username: c["author_info"]["username"],
        author_display_name: c["author_info"]["display_name"],
        comment: c["content"])
    end
  end
  comment
end
pull_request_detail(pull_request) click to toggle source

プルリクエストの詳細情報を取得する

# File lib/bitbucket-api-extension/api.rb, line 42
def pull_request_detail(pull_request)
  detail = BitbucketApiExtension::PullRequestDetail.new(pull_request.attributes)
  open(pull_request.request_page_url, auth_option) do |f|
    html = Nokogiri::HTML(f.read)
    from_to_elements = html.css('div.clearfix div.compare-widget-container div.compare-widget')
    from = from_to_elements.first
    push = from_to_elements.last
    detail.from_user_or_team_name = from.attributes['data-user-name'].try(:value)
    detail.from_repository_name = from.attributes['data-repo-name'].try(:value)
    detail.from_branch_name = from.attributes['data-branch-name'].try(:value)
    detail.from_commit_id = from.attributes['data-commit-id'].try(:value)
    detail.to_user_or_team_name = push.attributes['data-user-name'].try(:value)
    detail.to_repository_name = push.attributes['data-repo-name'].try(:value)
    detail.to_branch_name = push.attributes['data-branch-name'].try(:value)
  end
  detail
end
pull_requests() click to toggle source

プルリクエスト一覧を取得する

# File lib/bitbucket-api-extension/api.rb, line 18
def pull_requests
  uri = pull_request_list_url(@project.organization_name, @project.name)
  list = []
  open(uri, auth_option) do |f|
    html = Nokogiri::HTML(f.read).xpath('//table[contains(@class,"pullrequest-list")]/tbody/tr')
    list = html.map do |request|
      title = request.xpath('td[@class="title"]/div/a').text
      BitbucketApiExtension::PullRequest.new(
        title: title,
        id: title.scan(/#(\d+):.*/)
                 .flatten
                 .first,
        request_page_url: request.xpath('td[@class="title"]/div/a')
                                 .map{ |link| BITBUCKET_URI + link['href'] }
                                 .first
                                 .to_s,
        author: request.xpath('td[@class="author"]/div/span')
                       .text)
    end
  end
  list
end

Private Instance Methods

auth_option() click to toggle source
# File lib/bitbucket-api-extension/api.rb, line 95
def auth_option
  if @account.nil?
    {}
  else
    {http_basic_authentication: [@account.user_id, @account.user_password]}
  end
end
post(uri, body) click to toggle source
# File lib/bitbucket-api-extension/api.rb, line 111
def post(uri, body)
  uri = URI.parse(uri)
  request = Net::HTTP::Post.new(uri.path)
  request.basic_auth(@account.user_id, @account.user_password)
  request.set_form_data(body)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.start { |h| h.request(request) }
end
pull_request_comment_url(organization_name, project_name, id) click to toggle source
# File lib/bitbucket-api-extension/api.rb, line 107
def pull_request_comment_url(organization_name, project_name, id)
  "#{BITBUCKET_API_URI}/#{organization_name}/#{project_name}/pullrequests/#{id}/comments"
end
pull_request_list_url(organization_name, project_name) click to toggle source
# File lib/bitbucket-api-extension/api.rb, line 103
def pull_request_list_url(organization_name, project_name)
  "#{BITBUCKET_URI}/#{organization_name}/#{project_name}/pull-requests"
end