tinybucket2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # Repo Api client
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories
8
+ # repository Resource
9
+ #
10
+ # @!attribute [rw] repo_owner
11
+ # @return [String] repository owner name.
12
+ # @!attribute [rw] repo_slug
13
+ # @return [String] {https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D repository slug}.
14
+ class RepoApi < BaseApi
15
+ include Tinybucket::Api::Helper::RepoHelper
16
+
17
+ attr_accessor :repo_owner, :repo_slug
18
+
19
+ # Send 'GET a repository' request
20
+ #
21
+ # @param options [Hash]
22
+ # @return [Tinybucket::Model::Repository]
23
+ def find(options = {})
24
+ get_path(
25
+ path_to_find,
26
+ options,
27
+ get_parser(:object, Tinybucket::Model::Repository)
28
+ )
29
+ end
30
+
31
+ # Send 'GET a list of watchers' request
32
+ #
33
+ # @param options [Hash]
34
+ # @return [Tinybucket::Model::Page]
35
+ def watchers(options = {})
36
+ get_path(
37
+ path_to_watchers,
38
+ options,
39
+ get_parser(:collection, Tinybucket::Model::Profile)
40
+ )
41
+ end
42
+
43
+ # Send 'GET a list of forks' request
44
+ #
45
+ # @param options [Hash]
46
+ # @return [Tinybucket::Model::Page]
47
+ def forks(options = {})
48
+ get_path(
49
+ path_to_forks,
50
+ options,
51
+ get_parser(:collection, Tinybucket::Model::Repository)
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # Repos Api client
6
+ class ReposApi < BaseApi
7
+ include Tinybucket::Api::Helper::ReposHelper
8
+
9
+ # Send 'GET a list of repositories for an account' request
10
+ #
11
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories
12
+ # GET a list of repositories for an account
13
+ #
14
+ # @param options [Hash]
15
+ # @return [Tinybucket::Model::Page]
16
+ def list(options = {})
17
+ opts = options.clone
18
+ opts.delete(:owner)
19
+
20
+ get_path(
21
+ path_to_list(options),
22
+ opts,
23
+ get_parser(:collection, Tinybucket::Model::Repository)
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # Team Api client
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/teams
8
+ # teams Endpoint
9
+ class TeamApi < BaseApi
10
+ include Tinybucket::Api::Helper::TeamHelper
11
+
12
+ # Send 'GET teams' request
13
+ #
14
+ # @param role_name [String] role name
15
+ # @param options [Hash]
16
+ # @return [Tinybucket::Model::Page]
17
+ def list(role_name, options = {})
18
+ get_path(
19
+ path_to_list,
20
+ { role: role_name }.merge(options),
21
+ get_parser(:collection, Tinybucket::Model::Team)
22
+ )
23
+ end
24
+
25
+ # Send 'GET the team profile' request
26
+ #
27
+ # @param name [String] The team's name
28
+ # @param options [Hash]
29
+ # @return [Tinybucket::Model::Team]
30
+ def find(name, options = {})
31
+ get_path(
32
+ path_to_find(name),
33
+ options,
34
+ get_parser(:object, Tinybucket::Model::Team)
35
+ )
36
+ end
37
+
38
+ # Send 'GET the team members' request
39
+ #
40
+ # @param name [String] The team's name
41
+ # @param options [Hash]
42
+ # @return [Tinybucket::Model::Page]
43
+ def members(name, options = {})
44
+ get_path(
45
+ path_to_members(name),
46
+ options,
47
+ get_parser(:collection, Tinybucket::Model::Team)
48
+ )
49
+ end
50
+
51
+ # Send 'GET the list of followers' request
52
+ #
53
+ # @param name [String] The team's name
54
+ # @param options [Hash]
55
+ # @return [Tinybucket::Model::Page]
56
+ def followers(name, options = {})
57
+ get_path(
58
+ path_to_followers(name),
59
+ options,
60
+ get_parser(:collection, Tinybucket::Model::Team)
61
+ )
62
+ end
63
+
64
+ # Send 'GET a lisf of accounts the team is following' request
65
+ #
66
+ # @param name [String] The team's name
67
+ # @param options [Hash]
68
+ # @return [Tinybucket::Model::Page]
69
+ def following(name, options = {})
70
+ get_path(
71
+ path_to_following(name),
72
+ options,
73
+ get_parser(:collection, Tinybucket::Model::Team)
74
+ )
75
+ end
76
+
77
+ # Send 'GET the team's repositories' request
78
+ #
79
+ # @param name [String] The team's name
80
+ # @param options [Hash]
81
+ # @return [Tinybucket::Model::Page]
82
+ def repos(name, options = {})
83
+ get_path(
84
+ path_to_repos(name),
85
+ options,
86
+ get_parser(:collection, Tinybucket::Model::Repository)
87
+ )
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # User Api client
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/users/%7Busername%7D
8
+ # users Endpoint
9
+ #
10
+ # @!attribute [rw] username
11
+ # @return [String]
12
+ class UserApi < BaseApi
13
+ include Tinybucket::Api::Helper::UserHelper
14
+
15
+ attr_accessor :username
16
+
17
+ # Send 'GET the user profile' request
18
+ #
19
+ # @param options [Hash]
20
+ # @return [Tinybucket::Model::Profile]
21
+ def profile(options = {})
22
+ get_path(
23
+ path_to_find,
24
+ options,
25
+ get_parser(:object, Tinybucket::Model::Profile)
26
+ )
27
+ end
28
+
29
+ # Send 'GET the list of followers' request
30
+ #
31
+ # @param options [Hash]
32
+ # @return [Tinybucket::Model::Page]
33
+ def followers(options = {})
34
+ get_path(
35
+ path_to_followers,
36
+ options,
37
+ get_parser(:collection, Tinybucket::Model::Profile)
38
+ )
39
+ end
40
+
41
+ # Send 'GET a list of accounts the user is following' request
42
+ #
43
+ # @param options [Hash]
44
+ # @return [Tinybucket::Model::Page]
45
+ def following(options = {})
46
+ get_path(
47
+ path_to_following,
48
+ options,
49
+ get_parser(:collection, Tinybucket::Model::Profile)
50
+ )
51
+ end
52
+
53
+ # Send 'GET the user's repositories' request
54
+ #
55
+ # @param options [Hash]
56
+ # @return [Tinybucket::Model::Page]
57
+ def repos(options = {})
58
+ get_path(
59
+ path_to_repos,
60
+ options,
61
+ get_parser(:collection, Tinybucket::Model::Repository)
62
+ )
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ class ApiFactory
5
+ class << self
6
+ def create_instance(klass_name)
7
+ klass =
8
+ begin
9
+ name = "#{klass_name}Api".intern
10
+ Tinybucket::Api.const_get name
11
+ rescue => e
12
+ # TODO: log exception
13
+ Tinybucket.logger.error e
14
+ raise ArgumentError, 'must provide klass to be instantiated'
15
+ end
16
+
17
+ klass.new
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ class Client
5
+ include ::Tinybucket::Model::Concerns::Enumerable
6
+
7
+ # Get Repositories
8
+ #
9
+ # when you want to get repositories of the owner, call with owner, options.
10
+ # ex) repos('owner', options) or repos('owner')
11
+ #
12
+ # when you want to get public repositories of the owner, call with options.
13
+ # ex) repos(options) or repos
14
+ #
15
+ # @overload repos(owner, options)
16
+ # get the repositories of the owner.
17
+ # @param owner [String, Symbol] string or symbol to describe the owner.
18
+ # @option options [Hash] a hash with options
19
+ # @overload repos(options)
20
+ # get public repositories.
21
+ # @option options [Hash] a hash with options
22
+ # @return [Tinybucket::Resource::Repos] repository resource
23
+ def repos(*args)
24
+ case args.size
25
+ when 0
26
+ public_repos
27
+ when 1
28
+ case args.first
29
+ when Hash then public_repos(args.first)
30
+ when String, Symbol then owners_repos(args.first)
31
+ else raise ArgumentError
32
+ end
33
+ when 2
34
+ case args.first
35
+ when String, Symbol then owners_repos(args[0], args[1])
36
+ else raise ArgumentError
37
+ end
38
+ else
39
+ raise ArgumentError
40
+ end
41
+ end
42
+
43
+ # Get the repository
44
+ #
45
+ # @param owner [String] repository owner name.
46
+ # @param repo_slug [String] {https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D repository slug}.
47
+ # @return [Tinybucket::Model::Repository]
48
+ def repo(owner, repo_slug)
49
+ Tinybucket::Model::Repository.new({}).tap do |m|
50
+ m.repo_owner = owner
51
+ m.repo_slug = repo_slug
52
+ end
53
+ end
54
+
55
+ # Get teams
56
+ #
57
+ # @param role_name [String] role name (one of "admin", "contributor", or "member")
58
+ # @param options
59
+ # @return [Tinybucket::Resource::Teams]
60
+ def teams(role_name, options = {})
61
+ Tinybucket::Resource::Teams.new(role_name, options)
62
+ end
63
+
64
+ # Get the team
65
+ #
66
+ # @param teamname [String] the team name.
67
+ # @return [Tinybucket::Model::Team]
68
+ def team(teamname)
69
+ Tinybucket::Model::Team.new({}).tap do |m|
70
+ m.username = teamname
71
+ end
72
+ end
73
+
74
+ # Get the user profile
75
+ #
76
+ # @param username [String] the user name.
77
+ # @return [Tinybucket::Model::Profile]
78
+ def user(username)
79
+ Tinybucket::Model::Profile.new({}).tap do |m|
80
+ m.username = username
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ # Get public repositories
87
+ #
88
+ # @param options [Hash]
89
+ # @return [Tinybucket::Resource::Repos]
90
+ def public_repos(options = {})
91
+ raise ArgumentError unless options.is_a?(Hash)
92
+
93
+ Tinybucket::Resource::Repos.new(nil, options)
94
+ end
95
+
96
+ # Get Owner's repositories
97
+ #
98
+ # @param owner [String]
99
+ # @param options [Hash]
100
+ # @return [Tinybucket::Resource::Repos]
101
+ def owners_repos(owner, options = {})
102
+ raise ArgumentError unless options.is_a?(Hash)
103
+
104
+ Tinybucket::Resource::Repos.new(owner, options)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ class Config
5
+ include ActiveSupport::Configurable
6
+ config_accessor :logger, :oauth_token, :oauth_secret, \
7
+ :cache_store_options, :access_token, \
8
+ :client_id, :client_secret, :user_agent
9
+ end
10
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Connection
5
+ DEFAULT_USER_AGENT = 'Tinybucket Ruby Bitbucket REST client'.freeze
6
+
7
+ def clear_cache
8
+ @connection = nil
9
+ end
10
+
11
+ def caching?
12
+ !@connection.nil?
13
+ end
14
+
15
+ def connection(parser = nil, options = {})
16
+ conn_options = default_options(options)
17
+ clear_cache
18
+
19
+ # TODO: cache connection for each (options, parser) pairs.
20
+ Faraday.new(
21
+ conn_options.merge(builder: stack(parser, options))
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def default_options(_options)
28
+ {
29
+ headers: {
30
+ USER_AGENT: Tinybucket.config.user_agent || DEFAULT_USER_AGENT
31
+ },
32
+ ssl: { verify: false },
33
+ url: 'https://api.bitbucket.org/2.0'.freeze
34
+ }
35
+ end
36
+
37
+ def default_middleware(_options)
38
+ proc do |conn|
39
+ configure_response_cache(conn)
40
+
41
+ conn.request :multipart
42
+ conn.request :url_encoded
43
+
44
+ configure_auth(conn)
45
+
46
+ conn.response :json, content_type: /\bjson$/
47
+ conn.use Tinybucket::Response::Handler
48
+ conn.use :instrumentation
49
+
50
+ conn.adapter Faraday.default_adapter
51
+ end
52
+ end
53
+
54
+ def configure_auth(conn)
55
+ if Tinybucket.config.access_token
56
+ conn.request :oauth2, Tinybucket.config.access_token
57
+ else
58
+ oauth_secrets = {
59
+ consumer_key: Tinybucket.config.oauth_token,
60
+ consumer_secret: Tinybucket.config.oauth_secret
61
+ }
62
+
63
+ conn.request :oauth, oauth_secrets
64
+ conn.response :follow_oauth_redirects, oauth_secrets
65
+ end
66
+ end
67
+
68
+ def configure_response_cache(conn)
69
+ return unless Tinybucket.config.cache_store_options
70
+
71
+ conn.use :http_cache, Tinybucket.config.cache_store_options
72
+ end
73
+
74
+ def stack(parser, options = {}, &block)
75
+ Faraday::RackBuilder.new(&block) and return if block_given?
76
+
77
+ # TODO: cache stack for each (options, parser) pairs
78
+ Faraday::RackBuilder.new do |conn|
79
+ conn.use parser.type, parser_options: parser.options if parser.present?
80
+ default_middleware(options).call(conn)
81
+ end
82
+ end
83
+ end
84
+ end