Class: Bitbucket::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbucket/client.rb

Constant Summary collapse

PULL_REQUEST_VALUES =
%w[
  pagelen
  size
  page
  next
  previous
  values.comment_count
  values.task_count
  values.type
  values.id
  values.title
  values.description
  values.state
  values.merge_commit
  values.close_source_branch
  values.closed_by
  values.author
  values.reason
  values.created_on
  values.updated_on
  values.destination
  values.source
  values.links
  values.summary
  values.reviewers
  next
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



35
36
37
# File 'lib/bitbucket/client.rb', line 35

def initialize(options = {})
  @connection = Connection.new(options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/bitbucket/client.rb', line 5

def connection
  @connection
end

Instance Method Details

#each_page(method, representation_type, *args) ⇒ Object

Fetches data from the Bitbucket API and yields a Page object for every page of data, without loading all of them into memory.

method - The method name used for getting the data. representation_type - The representation type name used to wrap the result args - Arguments to pass to the method.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bitbucket/client.rb', line 45

def each_page(method, representation_type, *args)
  options =
    if args.last.is_a?(Hash)
      args.last
    else
      {}
    end

  loop do
    parsed_response = fetch_data(method, *args)
    object = Page.new(parsed_response, representation_type)

    yield object

    break unless object.next?

    options[:next_url] = object.next

    if args.last.is_a?(Hash)
      args[-1] = options
    else
      args.push(options)
    end
  end
end

#issue_comments(repo, issue_id) ⇒ Object



87
88
89
90
# File 'lib/bitbucket/client.rb', line 87

def issue_comments(repo, issue_id)
  path = "/repositories/#{repo}/issues/#{issue_id}/comments?sort=created_on"
  get_collection(path, :comment)
end

#issues(repo, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/bitbucket/client.rb', line 76

def issues(repo, options = {})
  path = "/repositories/#{repo}/issues?sort=created_on"

  if options[:raw]
    path = options[:next_url] if options[:next_url]
    connection.get(path)
  else
    get_collection(path, :issue)
  end
end

#last_issue(repo) ⇒ Object



71
72
73
74
# File 'lib/bitbucket/client.rb', line 71

def last_issue(repo)
  parsed_response = connection.get("/repositories/#{repo}/issues?pagelen=1&sort=-created_on&state=ALL")
  Bitbucket::Representation::Issue.new(parsed_response['values'].first)
end

#pull_request_comments(repo, pull_request) ⇒ Object



103
104
105
106
# File 'lib/bitbucket/client.rb', line 103

def pull_request_comments(repo, pull_request)
  path = "/repositories/#{repo}/pullrequests/#{pull_request}/comments?sort=created_on"
  get_collection(path, :pull_request_comment)
end

#pull_request_diff(repo, pull_request) ⇒ Object



108
109
110
111
# File 'lib/bitbucket/client.rb', line 108

def pull_request_diff(repo, pull_request)
  path = "/repositories/#{repo}/pullrequests/#{pull_request}/diff"
  connection.get(path)
end

#pull_requests(repo, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/bitbucket/client.rb', line 92

def pull_requests(repo, options = {})
  path = "/repositories/#{repo}/pullrequests?state=ALL&sort=created_on&fields=#{pull_request_values}"

  if options[:raw]
    path = options[:next_url] if options[:next_url]
    connection.get(path)
  else
    get_collection(path, :pull_request)
  end
end

#repo(name) ⇒ Object



113
114
115
116
# File 'lib/bitbucket/client.rb', line 113

def repo(name)
  parsed_response = connection.get("/repositories/#{name}")
  Representation::Repo.new(parsed_response)
end

#repos(filter: nil) ⇒ Object



118
119
120
121
122
123
# File 'lib/bitbucket/client.rb', line 118

def repos(filter: nil)
  path = "/repositories?role=member&sort=created_on"
  path += "&q=name~\"#{filter}\"" if filter

  get_collection(path, :repo)
end

#userObject



125
126
127
128
129
130
# File 'lib/bitbucket/client.rb', line 125

def user
  @user ||= begin
    parsed_response = connection.get('/user')
    Representation::User.new(parsed_response)
  end
end

#users(workspace_key, page_number: nil, limit: nil) ⇒ Object



132
133
134
135
# File 'lib/bitbucket/client.rb', line 132

def users(workspace_key, page_number: nil, limit: nil)
  path = "/workspaces/#{workspace_key}/members"
  get_collection(path, :user, page_number: page_number, limit: limit)
end