Class: BitbucketApiExtension::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbucket-api-extension/api.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, account = nil) ⇒ Api

Returns a new instance of Api.



12
13
14
15
# File 'lib/bitbucket-api-extension/api.rb', line 12

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

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



10
11
12
# File 'lib/bitbucket-api-extension/api.rb', line 10

def 
  @account
end

#projectObject (readonly)

Returns the value of attribute project.



10
11
12
# File 'lib/bitbucket-api-extension/api.rb', line 10

def project
  @project
end

Instance Method Details

#merge_commands(pull_request) ⇒ Object

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



61
62
63
64
65
66
67
68
# 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) ⇒ Object

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



88
89
90
91
# 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) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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_requestsObject

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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