File: client.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (157 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true

module Bitbucket
  class Client
    attr_reader :connection

    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

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

    # 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.
    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

    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

    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

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

    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

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

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

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

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

      get_collection(path, :repo)
    end

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

    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

    private

    def fetch_data(method, *args)
      case method
      when :pull_requests then pull_requests(*args)
      when :issues then issues(*args)
      else
        raise ArgumentError, "Unknown data method #{method}"
      end
    end

    def get_collection(path, type, page_number: nil, limit: nil)
      paginator = Paginator.new(connection, path, type, page_number: page_number, limit: limit)
      Collection.new(paginator)
    end

    def pull_request_values
      PULL_REQUEST_VALUES.join(',')
    end
  end
end