<commit_before># coding: utf-8

module BitbucketApiExtension; end

class BitbucketApiExtension::Api

  BITBUCKET_URI = 'https://bitbucket.org'
  BITBUCKET_API_URI = 'https://api.bitbucket.org/1.0/repositories'

  attr_reader :account, :project

  def initialize(project, account=nil)
    @project = project
    @account = account
  end

  # プルリクエスト一覧を取得する
  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

  # プルリクエストの詳細情報を取得する
  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)
      p "---"
      commands = html.xpath('//pre[@class="merge-commands"]/code').text.split("\n")
    end
    detail
  end

  # 指定したプルリクエストのマージコマンドを取得する
  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

  # 指定したプルリクエストに関連するコメントを取得する
  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(<commit_msg>add pull request detail<commit_after># coding: utf-8

module BitbucketApiExtension; end

class BitbucketApiExtension::Api

  BITBUCKET_URI = 'https://bitbucket.org'
  BITBUCKET_API_URI = 'https://api.bitbucket.org/1.0/repositories'

  attr_reader :account, :project

  def initialize(project, account=nil)
    @project = project
    @account = account
  end

  # プルリクエスト一覧を取得する
  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

  # プルリクエストの詳細情報を取得する
  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

  # 指定したプルリクエストのマージコマンドを取得する
  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

  # 指定したプルリクエストに関連するコメントを取得する
  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(
{'fraction_non_alphanumeric': 0.12950916617386163, 'fraction_numerical': 0.0011827321111768185, 'mean_word_length': 2.736377025036819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3848915', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1696, 'n_words': 251}