bitbucket_rest_api2 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +7 -0
  3. data/README.md +169 -0
  4. data/lib/bitbucket_rest_api.rb +90 -0
  5. data/lib/bitbucket_rest_api/api.rb +106 -0
  6. data/lib/bitbucket_rest_api/api/actions.rb +35 -0
  7. data/lib/bitbucket_rest_api/api_factory.rb +30 -0
  8. data/lib/bitbucket_rest_api/authorization.rb +34 -0
  9. data/lib/bitbucket_rest_api/client.rb +56 -0
  10. data/lib/bitbucket_rest_api/configuration.rb +106 -0
  11. data/lib/bitbucket_rest_api/connection.rb +98 -0
  12. data/lib/bitbucket_rest_api/constants.rb +58 -0
  13. data/lib/bitbucket_rest_api/core_ext/array.rb +7 -0
  14. data/lib/bitbucket_rest_api/core_ext/hash.rb +46 -0
  15. data/lib/bitbucket_rest_api/deprecation.rb +39 -0
  16. data/lib/bitbucket_rest_api/error.rb +38 -0
  17. data/lib/bitbucket_rest_api/error/bad_events.rb +9 -0
  18. data/lib/bitbucket_rest_api/error/bad_request.rb +12 -0
  19. data/lib/bitbucket_rest_api/error/blank_value.rb +9 -0
  20. data/lib/bitbucket_rest_api/error/client_error.rb +20 -0
  21. data/lib/bitbucket_rest_api/error/forbidden.rb +12 -0
  22. data/lib/bitbucket_rest_api/error/internal_server_error.rb +12 -0
  23. data/lib/bitbucket_rest_api/error/invalid_options.rb +18 -0
  24. data/lib/bitbucket_rest_api/error/no_events.rb +9 -0
  25. data/lib/bitbucket_rest_api/error/not_found.rb +12 -0
  26. data/lib/bitbucket_rest_api/error/required_params.rb +18 -0
  27. data/lib/bitbucket_rest_api/error/service_error.rb +19 -0
  28. data/lib/bitbucket_rest_api/error/service_unavailable.rb +12 -0
  29. data/lib/bitbucket_rest_api/error/unauthorized.rb +12 -0
  30. data/lib/bitbucket_rest_api/error/unknown_value.rb +18 -0
  31. data/lib/bitbucket_rest_api/error/unprocessable_entity.rb +12 -0
  32. data/lib/bitbucket_rest_api/error/validations.rb +18 -0
  33. data/lib/bitbucket_rest_api/invitations.rb +15 -0
  34. data/lib/bitbucket_rest_api/issues.rb +230 -0
  35. data/lib/bitbucket_rest_api/issues/comments.rb +118 -0
  36. data/lib/bitbucket_rest_api/issues/components.rb +106 -0
  37. data/lib/bitbucket_rest_api/issues/milestones.rb +107 -0
  38. data/lib/bitbucket_rest_api/normalizer.rb +27 -0
  39. data/lib/bitbucket_rest_api/parameter_filter.rb +32 -0
  40. data/lib/bitbucket_rest_api/repos.rb +264 -0
  41. data/lib/bitbucket_rest_api/repos/changesets.rb +54 -0
  42. data/lib/bitbucket_rest_api/repos/commits.rb +40 -0
  43. data/lib/bitbucket_rest_api/repos/default_reviewers.rb +59 -0
  44. data/lib/bitbucket_rest_api/repos/download.rb +21 -0
  45. data/lib/bitbucket_rest_api/repos/following.rb +39 -0
  46. data/lib/bitbucket_rest_api/repos/forks.rb +69 -0
  47. data/lib/bitbucket_rest_api/repos/keys.rb +87 -0
  48. data/lib/bitbucket_rest_api/repos/pull_request.rb +160 -0
  49. data/lib/bitbucket_rest_api/repos/services.rb +103 -0
  50. data/lib/bitbucket_rest_api/repos/sources.rb +39 -0
  51. data/lib/bitbucket_rest_api/repos/webhooks.rb +96 -0
  52. data/lib/bitbucket_rest_api/request.rb +76 -0
  53. data/lib/bitbucket_rest_api/request/basic_auth.rb +31 -0
  54. data/lib/bitbucket_rest_api/request/jsonize.rb +46 -0
  55. data/lib/bitbucket_rest_api/request/oauth.rb +53 -0
  56. data/lib/bitbucket_rest_api/response.rb +28 -0
  57. data/lib/bitbucket_rest_api/response/helpers.rb +21 -0
  58. data/lib/bitbucket_rest_api/response/jsonize.rb +30 -0
  59. data/lib/bitbucket_rest_api/response/mashify.rb +24 -0
  60. data/lib/bitbucket_rest_api/response/raise_error.rb +31 -0
  61. data/lib/bitbucket_rest_api/response/xmlize.rb +26 -0
  62. data/lib/bitbucket_rest_api/result.rb +140 -0
  63. data/lib/bitbucket_rest_api/user.rb +101 -0
  64. data/lib/bitbucket_rest_api/users.rb +24 -0
  65. data/lib/bitbucket_rest_api/users/account.rb +53 -0
  66. data/lib/bitbucket_rest_api/utils/url.rb +56 -0
  67. data/lib/bitbucket_rest_api/validations.rb +25 -0
  68. data/lib/bitbucket_rest_api/validations/format.rb +24 -0
  69. data/lib/bitbucket_rest_api/validations/presence.rb +25 -0
  70. data/lib/bitbucket_rest_api/validations/required.rb +44 -0
  71. data/lib/bitbucket_rest_api/validations/token.rb +43 -0
  72. data/lib/bitbucket_rest_api/version.rb +11 -0
  73. data/spec/bitbucket_rest_api/api/actions_spec.rb +17 -0
  74. data/spec/bitbucket_rest_api/api_factory_spec.rb +30 -0
  75. data/spec/bitbucket_rest_api/api_spec.rb +86 -0
  76. data/spec/bitbucket_rest_api/authorization_spec.rb +72 -0
  77. data/spec/bitbucket_rest_api/client_spec.rb +15 -0
  78. data/spec/bitbucket_rest_api/core_ext/array_spec.rb +12 -0
  79. data/spec/bitbucket_rest_api/core_ext/hash_spec.rb +49 -0
  80. data/spec/bitbucket_rest_api/deprecation_spec.rb +30 -0
  81. data/spec/bitbucket_rest_api/error/bad_events_spec.rb +10 -0
  82. data/spec/bitbucket_rest_api/error/blank_value_spec.rb +13 -0
  83. data/spec/bitbucket_rest_api/error/no_events_spec.rb +10 -0
  84. data/spec/bitbucket_rest_api/invitations_spec.rb +21 -0
  85. data/spec/bitbucket_rest_api/issues/comments_spec.rb +89 -0
  86. data/spec/bitbucket_rest_api/issues/components_spec.rb +88 -0
  87. data/spec/bitbucket_rest_api/issues/milestones_spec.rb +88 -0
  88. data/spec/bitbucket_rest_api/issues_spec.rb +90 -0
  89. data/spec/bitbucket_rest_api/normalizer_spec.rb +30 -0
  90. data/spec/bitbucket_rest_api/parameter_filter_spec.rb +41 -0
  91. data/spec/bitbucket_rest_api/repos/changesets_spec.rb +43 -0
  92. data/spec/bitbucket_rest_api/repos/commits_spec.rb +20 -0
  93. data/spec/bitbucket_rest_api/repos/default_reviewers_spec.rb +64 -0
  94. data/spec/bitbucket_rest_api/repos/download_spec.rb +9 -0
  95. data/spec/bitbucket_rest_api/repos/following_spec.rb +52 -0
  96. data/spec/bitbucket_rest_api/repos/forks_spec.rb +45 -0
  97. data/spec/bitbucket_rest_api/repos/keys_spec.rb +72 -0
  98. data/spec/bitbucket_rest_api/repos/pull_request_spec.rb +288 -0
  99. data/spec/bitbucket_rest_api/repos/sources_spec.rb +77 -0
  100. data/spec/bitbucket_rest_api/repos/webhooks_spec.rb +245 -0
  101. data/spec/bitbucket_rest_api/repos_spec.rb +157 -0
  102. data/spec/bitbucket_rest_api/request/jsonize_spec.rb +18 -0
  103. data/spec/bitbucket_rest_api/request/oauth_spec.rb +27 -0
  104. data/spec/bitbucket_rest_api/request_spec.rb +81 -0
  105. data/spec/bitbucket_rest_api/response/jsonize_spec.rb +12 -0
  106. data/spec/bitbucket_rest_api/response/mashify_spec.rb +32 -0
  107. data/spec/bitbucket_rest_api/response/raise_error_spec.rb +41 -0
  108. data/spec/bitbucket_rest_api/user_spec.rb +77 -0
  109. data/spec/bitbucket_rest_api/utils/url_spec.rb +33 -0
  110. data/spec/bitbucket_rest_api/validations/format_spec.rb +29 -0
  111. data/spec/bitbucket_rest_api/validations/presence_spec.rb +12 -0
  112. data/spec/bitbucket_rest_api/validations/required_spec.rb +43 -0
  113. data/spec/bitbucket_rest_api/validations/token_spec.rb +16 -0
  114. data/spec/bitbucket_rest_api_spec.rb +17 -0
  115. data/spec/spec_helper.rb +24 -0
  116. metadata +373 -0
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ module Authorization
5
+
6
+ # Check whether authentication credentials are present
7
+ def authenticated?
8
+ basic_authed? || oauth_token?
9
+ end
10
+
11
+ # Check whether basic authentication credentials are present
12
+ def basic_authed?
13
+ basic_auth? || (login? && password?)
14
+ end
15
+
16
+ # Select authentication parameters
17
+ def authentication
18
+ if basic_auth?
19
+ { :basic_auth => basic_auth }
20
+ elsif login? && password?
21
+ { :login => login, :password => password }
22
+ else
23
+ { }
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def _verify_client # :nodoc:
30
+ raise ArgumentError, 'Need to provide client_id and client_secret' unless client_id? && client_secret?
31
+ end
32
+
33
+ end # Authorization
34
+ end # BitBucket
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Client < API
5
+
6
+ # This is a read-only API to the BitBucket events.
7
+ # These events power the various activity streams on the site.
8
+ def events(options = {})
9
+ raise "Unimplemented"
10
+ #@events ||= ApiFactory.new 'Events', options
11
+ end
12
+
13
+ def issues(options = {})
14
+ @issues ||= ApiFactory.new 'Issues', options
15
+ end
16
+
17
+ # An API for users to manage their own tokens.
18
+ def oauth(options = {})
19
+ @oauth ||= ApiFactory.new 'Request::OAuth', options
20
+ end
21
+ alias :authorizations :oauth
22
+
23
+ def teams(options = {})
24
+ raise "Unimplemented"
25
+ #@teams ||= ApiFactory.new 'teams', options
26
+ end
27
+
28
+ def pull_requests(options = {})
29
+ @pull_requests ||= ApiFactory.new 'Repos::PullRequest', options
30
+ end
31
+
32
+ def repos(options = {})
33
+ @repos ||= ApiFactory.new 'Repos', options
34
+ end
35
+ alias :repositories :repos
36
+
37
+ def search(options = {})
38
+ raise "Unimplemented"
39
+ #@search ||= ApiFactory.new 'Search', options
40
+ end
41
+
42
+ # Many of the resources on the users API provide a shortcut for getting
43
+ # information about the currently authenticated user.
44
+ def users(options = {})
45
+ @users ||= ApiFactory.new 'Users', options
46
+ end
47
+
48
+ def user_api(options = {})
49
+ @user_api ||= ApiFactory.new 'User', options
50
+ end
51
+
52
+ def invitations(options = {})
53
+ @invitations ||= ApiFactory.new "Invitations", options
54
+ end
55
+ end # Client
56
+ end # BitBucket
@@ -0,0 +1,106 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ module Configuration
5
+
6
+ VALID_OPTIONS_KEYS = [
7
+ :adapter,
8
+ :client_id,
9
+ :client_secret,
10
+ :new_access_token,
11
+ :oauth_token,
12
+ :oauth_secret,
13
+ :endpoint,
14
+ :mime_type,
15
+ :user_agent,
16
+ :connection_options,
17
+ :repo,
18
+ :user,
19
+ :login,
20
+ :password,
21
+ :basic_auth
22
+ ].freeze
23
+
24
+ # Other adapters are :typhoeus, :patron, :em_synchrony, :excon, :test
25
+ DEFAULT_ADAPTER = :net_http
26
+
27
+ # By default, don't set an application key
28
+ DEFAULT_CLIENT_ID = nil
29
+
30
+ # By default, don't set an application secret
31
+ DEFAULT_CLIENT_SECRET = nil
32
+
33
+ # By default, don't set an access token
34
+ DEFAULT_ACCESS_TOKEN = nil
35
+
36
+ # By default, don't set a user oauth access token
37
+ DEFAULT_OAUTH_TOKEN = nil
38
+
39
+ # By default, don't set a user oauth access token secret
40
+ DEFAULT_OAUTH_SECRET = nil
41
+
42
+ # By default, don't set a user login name
43
+ DEFAULT_LOGIN = nil
44
+
45
+ # By default, don't set a user password
46
+ DEFAULT_PASSWORD = nil
47
+
48
+ # By default, don't set a user basic authentication
49
+ DEFAULT_BASIC_AUTH = nil
50
+
51
+ # The endpoint used to connect to BitBucket if none is set, in the event that BitBucket is ever available on location
52
+ DEFAULT_ENDPOINT = 'https://api.bitbucket.org'.freeze
53
+
54
+ # The value sent in the http header for 'User-Agent' if none is set
55
+ DEFAULT_USER_AGENT = "BitBucket Ruby Gem #{BitBucket::VERSION::STRING}".freeze
56
+
57
+ # By default the <tt>Accept</tt> header will make a request for <tt>JSON</tt>
58
+ DEFAULT_MIME_TYPE = :json
59
+
60
+ # By default uses the Faraday connection options if none is set
61
+ DEFAULT_CONNECTION_OPTIONS = { }
62
+
63
+ # By default, don't set user name
64
+ DEFAULT_USER = nil
65
+
66
+ # By default, don't set repository name
67
+ DEFAULT_REPO = nil
68
+
69
+ attr_accessor *VALID_OPTIONS_KEYS
70
+
71
+ # Convenience method to allow for global setting of configuration options
72
+ def configure
73
+ yield self
74
+ end
75
+
76
+ def self.extended(base)
77
+ base.set_defaults
78
+ end
79
+
80
+ def options
81
+ options = { }
82
+ VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
83
+ options
84
+ end
85
+
86
+ def set_defaults
87
+ self.adapter = DEFAULT_ADAPTER
88
+ self.client_id = DEFAULT_CLIENT_ID
89
+ self.client_secret = DEFAULT_CLIENT_SECRET
90
+ self.new_access_token = DEFAULT_ACCESS_TOKEN
91
+ self.oauth_token = DEFAULT_OAUTH_TOKEN
92
+ self.oauth_secret = DEFAULT_OAUTH_SECRET
93
+ self.endpoint = DEFAULT_ENDPOINT
94
+ self.user_agent = DEFAULT_USER_AGENT
95
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
96
+ self.mime_type = DEFAULT_MIME_TYPE
97
+ self.user = DEFAULT_USER
98
+ self.repo = DEFAULT_REPO
99
+ self.login = DEFAULT_LOGIN
100
+ self.password = DEFAULT_PASSWORD
101
+ self.basic_auth = DEFAULT_BASIC_AUTH
102
+ self
103
+ end
104
+
105
+ end # Configuration
106
+ end # BitBucket
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'bitbucket_rest_api/response'
6
+ require 'bitbucket_rest_api/response/mashify'
7
+ require 'bitbucket_rest_api/response/jsonize'
8
+ require 'bitbucket_rest_api/response/helpers'
9
+ require 'bitbucket_rest_api/response/raise_error'
10
+ require 'bitbucket_rest_api/request/oauth'
11
+ require 'bitbucket_rest_api/request/basic_auth'
12
+ require 'bitbucket_rest_api/request/jsonize'
13
+
14
+ module BitBucket
15
+ module Connection
16
+ extend self
17
+ include BitBucket::Constants
18
+
19
+ ALLOWED_OPTIONS = [
20
+ :headers,
21
+ :url,
22
+ :params,
23
+ :request,
24
+ :ssl
25
+ ].freeze
26
+
27
+ def default_options(options={})
28
+ {
29
+ :headers => {
30
+ USER_AGENT => user_agent
31
+ },
32
+ :ssl => { :verify => false },
33
+ :url => options.fetch(:endpoint) { BitBucket.endpoint }
34
+ }.merge(options)
35
+ end
36
+
37
+ # Default middleware stack that uses default adapter as specified at
38
+ # configuration stage.
39
+ #
40
+ def default_middleware(options={})
41
+ Proc.new do |builder|
42
+ #builder.use BitBucket::Request::Jsonize
43
+ builder.use Faraday::Request::Multipart
44
+ builder.use Faraday::Request::UrlEncoded
45
+ builder.use FaradayMiddleware::OAuth, {:consumer_key => client_id, :consumer_secret => client_secret, :token => oauth_token, :token_secret => oauth_secret} if client_id? and client_secret?
46
+ builder.use BitBucket::Request::BasicAuth, authentication if basic_authed?
47
+ builder.use FaradayMiddleware::EncodeJson
48
+
49
+ builder.use Faraday::Response::Logger if ENV['DEBUG']
50
+ builder.use BitBucket::Response::Helpers
51
+ unless options[:raw]
52
+ builder.use BitBucket::Response::Mashify
53
+ builder.use BitBucket::Response::Jsonize
54
+ end
55
+ builder.use BitBucket::Response::RaiseError
56
+ builder.adapter adapter
57
+ end
58
+ end
59
+
60
+ @connection = nil
61
+
62
+ @stack = nil
63
+
64
+ def clear_cache
65
+ @connection = nil
66
+ end
67
+
68
+ def caching?
69
+ !@connection.nil?
70
+ end
71
+
72
+ # Exposes middleware builder to facilitate custom stacks and easy
73
+ # addition of new extensions such as cache adapter.
74
+ #
75
+ def stack(options={}, &block)
76
+ @stack ||= begin
77
+ if block_given?
78
+ Faraday::RackBuilder.new(&block)
79
+ else
80
+ Faraday::RackBuilder.new(&default_middleware(options))
81
+ end
82
+ end
83
+ end
84
+
85
+ # Returns a Fraday::Connection object
86
+ #
87
+ def connection(options = {})
88
+ conn_options = default_options(options)
89
+ clear_cache unless options.empty?
90
+ puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
91
+
92
+ @connection ||= Faraday.new(conn_options.merge(:builder => stack(options))) do |faraday|
93
+ faraday.response :logger if ENV['DEBUG']
94
+ end
95
+ end
96
+
97
+ end # Connection
98
+ end # BitBucket
@@ -0,0 +1,58 @@
1
+ module BitBucket
2
+ module Constants
3
+ extend self
4
+
5
+ # Response headers
6
+ RATELIMIT_REMAINING = 'X-RateLimit-Remaining'.freeze
7
+
8
+ RATELIMIT_LIMIT = 'X-RateLimit-Limit'.freeze
9
+
10
+ CONTENT_TYPE = 'Content-Type'.freeze
11
+
12
+ CONTENT_LENGTH = 'content-length'.freeze
13
+
14
+ CACHE_CONTROL = 'cache-control'.freeze
15
+
16
+ ETAG = 'ETag'.freeze
17
+
18
+ SERVER = 'Server'.freeze
19
+
20
+ DATE = 'Date'.freeze
21
+
22
+ LOCATION = 'Location'.freeze
23
+
24
+ USER_AGENT = 'User-Agent'.freeze
25
+
26
+ ACCEPT = 'Accept'.freeze
27
+
28
+ ACCEPT_CHARSET = 'Accept-Charset'.freeze
29
+
30
+ # Link headers
31
+ HEADER_LINK = "Link".freeze
32
+
33
+ HEADER_NEXT = "X-Next".freeze
34
+
35
+ HEADER_LAST = "X-Last".freeze
36
+
37
+ META_REL = "rel".freeze
38
+
39
+ META_LAST = "last".freeze
40
+
41
+ META_NEXT = "next".freeze
42
+
43
+ META_FIRST = "first".freeze
44
+
45
+ META_PREV = "prev".freeze
46
+
47
+ PARAM_PAGE = "page".freeze
48
+
49
+ PARAM_PER_PAGE = "per_page".freeze
50
+
51
+ PARAM_START_PAGE = "start_page".freeze
52
+
53
+ # URI parsing
54
+ QUERY_STR_SEP = '?'.freeze
55
+
56
+
57
+ end # Constants
58
+ end # BitBucket
@@ -0,0 +1,7 @@
1
+ class Array # :nodoc:
2
+
3
+ def extract_options!
4
+ last.is_a?(::Hash) ? pop : {}
5
+ end
6
+
7
+ end # Array
@@ -0,0 +1,46 @@
1
+ class Hash # :nodoc:
2
+
3
+ def symbolize_keys # :nodoc:
4
+ inject({}) do |hash, (key, value)|
5
+ hash[(key.to_sym rescue key) || key] = value
6
+ hash
7
+ end
8
+ end unless method_defined?(:symbolize_keys)
9
+
10
+ def symbolize_keys! # :nodoc:
11
+ hash = symbolize_keys
12
+ hash.each do |key, val|
13
+ hash[key] = case val
14
+ when Hash
15
+ val.symbolize_keys!
16
+ when Array
17
+ val.map do |item|
18
+ item.is_a?(Hash) ? item.symbolize_keys! : item
19
+ end
20
+ else
21
+ val
22
+ end
23
+ end
24
+ return hash
25
+ end unless method_defined?(:symbolize_keys!)
26
+
27
+ def serialize # :nodoc:
28
+ self.map { |key, val| [key, val].join("=") }.join("&")
29
+ end unless method_defined?(:serialize)
30
+
31
+ def all_keys # :nodoc:
32
+ keys = self.keys
33
+ keys.each do |key|
34
+ if self[key].is_a?(Hash)
35
+ keys << self[key].all_keys.compact.flatten
36
+ next
37
+ end
38
+ end
39
+ keys.flatten
40
+ end unless method_defined?(:all_keys)
41
+
42
+ def has_deep_key?(key)
43
+ self.all_keys.include? key
44
+ end unless method_defined?(:has_deep_key?)
45
+
46
+ end # Hash
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+
5
+ DEPRECATION_PREFIX = "[BitBucketAPI] Deprecation warning:"
6
+
7
+ class << self
8
+
9
+ attr_writer :deprecation_tracker
10
+
11
+ def deprecation_tracker
12
+ @deprecation_tracker ||= []
13
+ end
14
+
15
+ # Displays deprecation message to the user.
16
+ # Each message is printed once.
17
+ def deprecate(method, alternate_method=nil)
18
+ return if deprecation_tracker.include? method
19
+ deprecation_tracker << method
20
+
21
+ message = <<-NOTICE
22
+ #{DEPRECATION_PREFIX}
23
+
24
+ * #{method} is deprecated.
25
+ NOTICE
26
+ if alternate_method
27
+ message << <<-ADDITIONAL
28
+ * please use #{alternate_method} instead.
29
+ ADDITIONAL
30
+ end
31
+ warn_deprecation(message)
32
+ end
33
+
34
+ def warn_deprecation(message)
35
+ send :warn, message
36
+ end
37
+ end
38
+
39
+ end # BitBucket