tinybucket2 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +86 -0
  3. data/Gemfile +32 -0
  4. data/LICENSE +21 -0
  5. data/README.md +387 -0
  6. data/Rakefile +47 -0
  7. data/lib/faraday_middleware/follow_oauth_redirects.rb +71 -0
  8. data/lib/tinybucket.rb +76 -0
  9. data/lib/tinybucket/api.rb +26 -0
  10. data/lib/tinybucket/api/base_api.rb +44 -0
  11. data/lib/tinybucket/api/branch_restrictions_api.rb +51 -0
  12. data/lib/tinybucket/api/branches_api.rb +48 -0
  13. data/lib/tinybucket/api/build_status_api.rb +69 -0
  14. data/lib/tinybucket/api/comments_api.rb +81 -0
  15. data/lib/tinybucket/api/commits_api.rb +97 -0
  16. data/lib/tinybucket/api/diff_api.rb +40 -0
  17. data/lib/tinybucket/api/helper.rb +27 -0
  18. data/lib/tinybucket/api/helper/api_helper.rb +39 -0
  19. data/lib/tinybucket/api/helper/branch_restrictions_helper.rb +29 -0
  20. data/lib/tinybucket/api/helper/branches_helper.rb +29 -0
  21. data/lib/tinybucket/api/helper/build_status_helper.rb +46 -0
  22. data/lib/tinybucket/api/helper/comments_helper.rb +51 -0
  23. data/lib/tinybucket/api/helper/commits_helper.rb +42 -0
  24. data/lib/tinybucket/api/helper/diff_helper.rb +31 -0
  25. data/lib/tinybucket/api/helper/issues_helper.rb +29 -0
  26. data/lib/tinybucket/api/helper/projects_helper.rb +28 -0
  27. data/lib/tinybucket/api/helper/pull_requests_helper.rb +58 -0
  28. data/lib/tinybucket/api/helper/repo_helper.rb +31 -0
  29. data/lib/tinybucket/api/helper/repos_helper.rb +23 -0
  30. data/lib/tinybucket/api/helper/team_helper.rb +45 -0
  31. data/lib/tinybucket/api/helper/user_helper.rb +33 -0
  32. data/lib/tinybucket/api/issues_api.rb +48 -0
  33. data/lib/tinybucket/api/projects_api.rb +26 -0
  34. data/lib/tinybucket/api/pull_requests_api.rb +117 -0
  35. data/lib/tinybucket/api/repo_api.rb +56 -0
  36. data/lib/tinybucket/api/repos_api.rb +28 -0
  37. data/lib/tinybucket/api/team_api.rb +91 -0
  38. data/lib/tinybucket/api/user_api.rb +66 -0
  39. data/lib/tinybucket/api_factory.rb +21 -0
  40. data/lib/tinybucket/client.rb +107 -0
  41. data/lib/tinybucket/config.rb +10 -0
  42. data/lib/tinybucket/connection.rb +84 -0
  43. data/lib/tinybucket/constants.rb +7 -0
  44. data/lib/tinybucket/enumerator.rb +47 -0
  45. data/lib/tinybucket/error.rb +12 -0
  46. data/lib/tinybucket/error/base_error.rb +14 -0
  47. data/lib/tinybucket/error/conflict.rb +8 -0
  48. data/lib/tinybucket/error/not_found.rb +8 -0
  49. data/lib/tinybucket/error/service_error.rb +26 -0
  50. data/lib/tinybucket/iterator.rb +79 -0
  51. data/lib/tinybucket/model.rb +25 -0
  52. data/lib/tinybucket/model/base.rb +45 -0
  53. data/lib/tinybucket/model/branch.rb +48 -0
  54. data/lib/tinybucket/model/branch_restriction.rb +46 -0
  55. data/lib/tinybucket/model/build_status.rb +57 -0
  56. data/lib/tinybucket/model/comment.rb +67 -0
  57. data/lib/tinybucket/model/commit.rb +114 -0
  58. data/lib/tinybucket/model/concerns.rb +19 -0
  59. data/lib/tinybucket/model/concerns/acceptable_attributes.rb +34 -0
  60. data/lib/tinybucket/model/concerns/api_callable.rb +21 -0
  61. data/lib/tinybucket/model/concerns/enumerable.rb +20 -0
  62. data/lib/tinybucket/model/concerns/reloadable.rb +41 -0
  63. data/lib/tinybucket/model/concerns/repository_keys.rb +45 -0
  64. data/lib/tinybucket/model/error_response.rb +24 -0
  65. data/lib/tinybucket/model/issue.rb +48 -0
  66. data/lib/tinybucket/model/page.rb +45 -0
  67. data/lib/tinybucket/model/profile.rb +70 -0
  68. data/lib/tinybucket/model/project.rb +44 -0
  69. data/lib/tinybucket/model/pull_request.rb +160 -0
  70. data/lib/tinybucket/model/repository.rb +219 -0
  71. data/lib/tinybucket/model/team.rb +96 -0
  72. data/lib/tinybucket/null_logger.rb +37 -0
  73. data/lib/tinybucket/parser.rb +15 -0
  74. data/lib/tinybucket/parser/collection_parser.rb +17 -0
  75. data/lib/tinybucket/parser/object_parser.rb +17 -0
  76. data/lib/tinybucket/request.rb +59 -0
  77. data/lib/tinybucket/resource.rb +75 -0
  78. data/lib/tinybucket/resource/base.rb +35 -0
  79. data/lib/tinybucket/resource/branch_restrictions.rb +47 -0
  80. data/lib/tinybucket/resource/branches.rb +35 -0
  81. data/lib/tinybucket/resource/commit/base.rb +14 -0
  82. data/lib/tinybucket/resource/commit/build_statuses.rb +50 -0
  83. data/lib/tinybucket/resource/commit/comments.rb +34 -0
  84. data/lib/tinybucket/resource/commits.rb +46 -0
  85. data/lib/tinybucket/resource/forks.rb +24 -0
  86. data/lib/tinybucket/resource/issues.rb +35 -0
  87. data/lib/tinybucket/resource/projects.rb +49 -0
  88. data/lib/tinybucket/resource/pull_request/base.rb +20 -0
  89. data/lib/tinybucket/resource/pull_request/comments.rb +32 -0
  90. data/lib/tinybucket/resource/pull_request/commits.rb +19 -0
  91. data/lib/tinybucket/resource/pull_requests.rb +50 -0
  92. data/lib/tinybucket/resource/repos.rb +40 -0
  93. data/lib/tinybucket/resource/team/base.rb +24 -0
  94. data/lib/tinybucket/resource/team/followers.rb +15 -0
  95. data/lib/tinybucket/resource/team/following.rb +15 -0
  96. data/lib/tinybucket/resource/team/members.rb +15 -0
  97. data/lib/tinybucket/resource/team/repos.rb +15 -0
  98. data/lib/tinybucket/resource/teams.rb +22 -0
  99. data/lib/tinybucket/resource/user/base.rb +26 -0
  100. data/lib/tinybucket/resource/user/followers.rb +15 -0
  101. data/lib/tinybucket/resource/user/following.rb +15 -0
  102. data/lib/tinybucket/resource/user/repos.rb +15 -0
  103. data/lib/tinybucket/resource/watchers.rb +24 -0
  104. data/lib/tinybucket/response.rb +9 -0
  105. data/lib/tinybucket/response/handler.rb +23 -0
  106. data/lib/tinybucket/version.rb +5 -0
  107. data/tinybucket.gemspec +30 -0
  108. metadata +248 -0
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Constants
5
+ MISSING_REPOSITORY_KEY = 'This method call require repository keys.'.freeze
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ class Enumerator < ::Enumerator
5
+ # Constructor
6
+ #
7
+ # This method create a enumerator to enumerate each items of iterator.
8
+ #
9
+ # @note This method return Lazy Enumerator if run on ruby 2.0.0 later.
10
+ #
11
+ # @param iterator [Tinybucket::Iterator] iterator instance.
12
+ # @param block [Proc] a proc object to handle each item.
13
+ def initialize(iterator, block)
14
+ @iterator = iterator
15
+ @block = block
16
+
17
+ super() do |y|
18
+ loop do
19
+ v = @iterator.next
20
+ m = @block ? @block.call(v) : v
21
+ y.yield(m)
22
+ end
23
+ end
24
+
25
+ lazy if lazy_enumerable?
26
+ end
27
+
28
+ # Get collection size.
29
+ #
30
+ # @see Tinybucket::Iterator#size
31
+ #
32
+ # @return [Fixnum, NillClass] collection size.
33
+ def size
34
+ @iterator.size
35
+ end
36
+
37
+ private
38
+
39
+ def lazy_enumerable?
40
+ ruby_major_version >= 2
41
+ end
42
+
43
+ def ruby_major_version
44
+ RUBY_VERSION.split('.')[0].to_i
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Error
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :BaseError
8
+ autoload :ServiceError
9
+ autoload :Conflict
10
+ autoload :NotFound
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Error
5
+ class BaseError < StandardError
6
+ attr_reader :response_message, :response_headers
7
+
8
+ def initialize(message)
9
+ super message
10
+ @response_message = message
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Error
5
+ class Conflict < ServiceError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Error
5
+ class NotFound < ServiceError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Error
5
+ class ServiceError < BaseError
6
+ attr_accessor :http_headers
7
+ attr_reader :http_method, :request_url, :status_code, :response_body
8
+
9
+ def initialize(env)
10
+ super generate_message(env)
11
+ @http_headers = env[:response_headers]
12
+ end
13
+
14
+ private
15
+
16
+ def generate_message(env)
17
+ @http_method = env[:method].to_s.upcase
18
+ @request_url = env[:url].to_s
19
+ @status_code = env[:status]
20
+ @response_body = env[:body]
21
+
22
+ "#{@http_method} #{@request_url} #{@status_code} #{@response_body}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ # Iterator
5
+ #
6
+ # This iterator iterate items by sending request ot Bitbucket Cloud REST API.
7
+ class Iterator
8
+ # Constructor
9
+ #
10
+ # @param api_client [Tinybucket::Api::Base] an instance of Api Client class.
11
+ # @param method [Symbol] method name to invoke api_client method.
12
+ # @param args [Array<Object>] arguments to pass method call of
13
+ # api_client as arguments.
14
+ def initialize(api_client, method, *args)
15
+ @client = api_client
16
+ @method = method
17
+ @args = args
18
+
19
+ @attrs = {}
20
+ @values = []
21
+ @pos = nil
22
+ end
23
+
24
+ def next
25
+ next_value
26
+ end
27
+
28
+ # Get collection size.
29
+ #
30
+ # @note This method return {https://developer.atlassian.com/bitbucket/api/2/reference/meta/pagination
31
+ # size attribute of object collection wrapper}.
32
+ # So this method may return nil.
33
+ #
34
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/meta/pagination
35
+ # Paging through object collections
36
+ #
37
+ # @return [Fixnum, NillClas] collection size.
38
+ def size
39
+ load_next if next?
40
+ @attrs[:size]
41
+ end
42
+
43
+ private
44
+
45
+ def next_value
46
+ load_next if next?
47
+
48
+ @values.fetch(@pos).tap { @pos += 1 }
49
+ rescue IndexError
50
+ raise StopIteration
51
+ end
52
+
53
+ def next?
54
+ @pos.nil? || (@values.size == @pos && @attrs[:next])
55
+ end
56
+
57
+ def load_next
58
+ @pos = 0 if @pos.nil?
59
+
60
+ page = @client.send(@method, *next_params)
61
+
62
+ @attrs = page.attrs
63
+ @values.concat(page.items)
64
+ end
65
+
66
+ def next_params
67
+ params =
68
+ if @attrs.empty?
69
+ {}
70
+ else
71
+ query = URI.parse(@attrs[:next]).query
72
+ Hash[*query.split(/&|=/).map { |v| CGI.unescape(v) }]
73
+ end
74
+
75
+ @args[-1].merge!(params)
76
+ @args
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ extend ActiveSupport::Autoload
6
+
7
+ [
8
+ :Base,
9
+ :Branch,
10
+ :BranchRestriction,
11
+ :BuildStatus,
12
+ :Comment,
13
+ :Commit,
14
+ :ErrorResponse,
15
+ :Page,
16
+ :Profile,
17
+ :Project,
18
+ :PullRequest,
19
+ :Repository,
20
+ :Team
21
+ ].each do |klass_name|
22
+ autoload klass_name
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ class Base
6
+ include ::ActiveModel::Serializers::JSON
7
+ include Concerns::AcceptableAttributes
8
+ include Concerns::Enumerable
9
+ include Concerns::Reloadable
10
+ include Concerns::ApiCallable
11
+
12
+ def self.concern_included?(concern_name)
13
+ mod_name = "Tinybucket::Model::Concerns::#{concern_name}".constantize
14
+ ancestors.include?(mod_name)
15
+ end
16
+
17
+ def initialize(json)
18
+ self.attributes = json
19
+ @_loaded = !json.empty?
20
+ end
21
+
22
+ def attributes=(hash)
23
+ hash.each_pair do |key, value|
24
+ if acceptable_attribute?(key)
25
+ send("#{key}=".intern, value)
26
+ else
27
+ logger.warn("Ignored '#{key}' attribute (value: #{value}). [#{self.class}]")
28
+ end
29
+ end
30
+ end
31
+
32
+ def attributes
33
+ acceptable_attributes.map do |key|
34
+ { key => send(key.intern) }
35
+ end.reduce(&:merge)
36
+ end
37
+
38
+ protected
39
+
40
+ def logger
41
+ Tinybucket.logger
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ # Branch
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/branches
8
+ # Branch Resource
9
+ #
10
+ # @!attribute [rw] links
11
+ # @return [Hash]
12
+ # @!attribute [rw] type
13
+ # @return [String]
14
+ # @!attribute [rw] name
15
+ # @return [String]
16
+ # @!attribute [rw] repository
17
+ # @return [Hash]
18
+ # @!attribute [rw] target
19
+ # @return [Hash]
20
+ class Branch < Base
21
+ include Tinybucket::Model::Concerns::RepositoryKeys
22
+
23
+ acceptable_attributes :links, :type, :name, :repository, :target
24
+
25
+ # Returns the commits available for the specific branch
26
+ #
27
+ # @param options [Hash]
28
+ # @return [Tinybucket::Resource::Commits]
29
+ def commits(options = {})
30
+ commits_resource.branch(name, options)
31
+ end
32
+
33
+ private
34
+
35
+ def commits_resource(options = {})
36
+ Tinybucket::Resource::Commits.new(self, options)
37
+ end
38
+
39
+ def branches_api
40
+ create_api('Branches', repo_keys)
41
+ end
42
+
43
+ def load_model
44
+ branches_api.find(name, {})
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ # BranchRestriction
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/branch-restrictions
8
+ # branch-restrictions Resource
9
+ #
10
+ # @!attribute [rw] groups
11
+ # @return [Array]
12
+ # @!attribute [rw] id
13
+ # @return [Fixnum]
14
+ # @!attribute [rw] kind
15
+ # @return [String]
16
+ # @!attribute [rw] links
17
+ # @return [Hash]
18
+ # @!attribute [rw] pattern
19
+ # @return [String]
20
+ # @!attribute [rw] users
21
+ # @return [Array]
22
+ # @!attribute [rw] uuid
23
+ # @return [NillClass]
24
+ class BranchRestriction < Base
25
+ include Tinybucket::Model::Concerns::RepositoryKeys
26
+
27
+ acceptable_attributes :groups, :id, :kind, :links, :pattern, :users, :uuid
28
+
29
+ # Update this branch restriction
30
+ #
31
+ # @todo to be implemented
32
+ # @raise [NotImplementedError] to be implemented.
33
+ def update(_params)
34
+ raise NotImplementedError
35
+ end
36
+
37
+ # Delete this branch restriction
38
+ #
39
+ # @todo to be implemented
40
+ # @raise [NotImplementedError] to be implemented.
41
+ def destroy
42
+ raise NotImplementedError
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ # Build Status
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit/%7Bnode%7D/statuses/build
8
+ # statuses/build Resource
9
+ #
10
+ # @!attribute [rw] state
11
+ # @return [String]
12
+ # @!attribute [rw] type
13
+ # @return [String]
14
+ # @!attribute [rw] key
15
+ # @return [String]
16
+ # @!attribute [rw] name
17
+ # @return [String]
18
+ # @!attribute [rw] url
19
+ # @return [String]
20
+ # @!attribute [rw] description
21
+ # @return [String]
22
+ # @!attribute [rw] links
23
+ # @return [Hash]
24
+ class BuildStatus < Base
25
+ include Tinybucket::Model::Concerns::RepositoryKeys
26
+ include Tinybucket::Model::Concerns::Reloadable
27
+
28
+ acceptable_attributes \
29
+ :state, :type, :key, :name, :url, :description, :links, \
30
+ :created_on, :updated_on
31
+
32
+ attr_accessor :revision
33
+
34
+ # Update build status
35
+ #
36
+ # @param options [Hash]
37
+ # @option options [String] :state
38
+ # @return [Tinybucket::Model::BuildStatus]
39
+ def update(options)
40
+ build_status_api.put(revision, key, options).tap do |m|
41
+ m.repo_keys = repo_keys
42
+ m.revision = revision
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def build_status_api
49
+ create_api('BuildStatus', repo_keys)
50
+ end
51
+
52
+ def load_model
53
+ build_status_api.find(revision, key)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Model
5
+ # Comment
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commit/%7Bsha%7D/comments
8
+ # Comment Resource
9
+ #
10
+ # @!attribute [rw] links
11
+ # @return [Hash]
12
+ # @!attribute [rw] id
13
+ # @return [Fixnum]
14
+ # @!attribute [rw] parent
15
+ # @return [Hash]
16
+ # @!attribute [rw] filename
17
+ # @return [String]
18
+ # @!attribute [rw] content
19
+ # @return [Hash]
20
+ # @!attribute [rw] user
21
+ # @return [Hash]
22
+ # @!attribute [rw] inline
23
+ # @return [Hash]
24
+ # @!attribute [rw] created_on
25
+ # @return [String]
26
+ # @!attribute [rw] updated_on
27
+ # @return [String]
28
+ # @!attribute [rw] uuid
29
+ # @return [NillClass]
30
+ class Comment < Base
31
+ include Tinybucket::Model::Concerns::RepositoryKeys
32
+
33
+ acceptable_attributes \
34
+ :links, :id, :parent, :filename, :content, :user, :inline, \
35
+ :created_on, :updated_on, :uuid
36
+
37
+ # @!attribute [rw] commented_to
38
+ # @return [Tinybucket::Model::PullRequest, Tinybucket::Model::Commit]
39
+ attr_accessor :commented_to
40
+
41
+ private
42
+
43
+ def commit_api
44
+ create_api('Comments', repo_keys)
45
+ end
46
+
47
+ def pull_request_api
48
+ create_api('PullRequests', repo_keys)
49
+ end
50
+
51
+ def load_model
52
+ api =
53
+ case commented_to
54
+ when Tinybucket::Model::Commit
55
+ commit_api
56
+ when Tinybucket::Model::PullRequest
57
+ pull_request_api
58
+ else
59
+ raise ArgumentError, 'commented_to was invalid'
60
+ end
61
+
62
+ api.commented_to = commented_ato
63
+ api.find(id, {})
64
+ end
65
+ end
66
+ end
67
+ end