bitbucket_rest_api2 0.2.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 (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,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ # Contains methods and attributes that act on the response returned from the
7
+ # request
8
+ class Response < Faraday::Response::Middleware
9
+ CONTENT_TYPE = 'Content-Type'.freeze
10
+
11
+ class << self
12
+ attr_accessor :parser
13
+ end
14
+
15
+ def self.define_parser(&block)
16
+ @parser = block
17
+ end
18
+
19
+ def response_type(env)
20
+ env[:response_headers][CONTENT_TYPE].to_s
21
+ end
22
+
23
+ def parse_response?(env)
24
+ env[:body].respond_to? :to_str
25
+ end
26
+
27
+ end # Response
28
+ end # BitBucket
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ class Response::Helpers < Response
7
+
8
+ def on_complete(env)
9
+ env[:body].class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
10
+ include BitBucket::Result
11
+
12
+ def env
13
+ @env
14
+ end
15
+
16
+ RUBY_EVAL
17
+ env[:body].instance_eval { @env = env }
18
+ end
19
+
20
+ end # Response::Helpers
21
+ end # BitBucket
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ class Response::Jsonize < Response
7
+ dependency 'multi_json'
8
+
9
+ define_parser do |body|
10
+ if MultiJson.respond_to?(:load)
11
+ MultiJson.load body
12
+ else
13
+ MultiJson.decode body
14
+ end
15
+ end
16
+
17
+ def parse(body)
18
+ case body
19
+ when ''
20
+ nil
21
+ when 'true'
22
+ true
23
+ when 'false'
24
+ false
25
+ else
26
+ self.class.parser.call body
27
+ end
28
+ end
29
+ end # Response::Jsonize
30
+ end # BitBucket
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ class Response::Mashify < Response
7
+ dependency 'hashie/mash'
8
+
9
+ define_parser do |body|
10
+ ::Hashie::Mash.new body
11
+ end
12
+
13
+ def parse(body)
14
+ case body
15
+ when Hash
16
+ self.class.parser.call body
17
+ when Array
18
+ body.map { |item| item.is_a?(Hash) ? self.class.parser.call(item) : item }
19
+ else
20
+ body
21
+ end
22
+ end
23
+ end # Response::Mashify
24
+ end # BitBucket
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+ require 'bitbucket_rest_api/error'
5
+
6
+ module BitBucket
7
+ class Response::RaiseError < Faraday::Response::Middleware
8
+
9
+ def on_complete(env)
10
+ case env[:status].to_i
11
+ when 400
12
+ raise BitBucket::Error::BadRequest.new(env)
13
+ when 401
14
+ raise BitBucket::Error::Unauthorized.new(env)
15
+ when 403
16
+ raise BitBucket::Error::Forbidden.new(env)
17
+ when 404
18
+ raise BitBucket::Error::NotFound.new(env)
19
+ when 422
20
+ raise BitBucket::Error::UnprocessableEntity.new(env)
21
+ when 500
22
+ raise BitBucket::Error::InternalServerError.new(env)
23
+ when 503
24
+ raise BitBucket::Error::ServiceUnavailable.new(env)
25
+ when 400...600
26
+ raise BitBucket::Error::ServiceError.new(env)
27
+ end
28
+ end
29
+
30
+ end # Response::RaiseError
31
+ end # BitBucket
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'faraday'
4
+
5
+ module BitBucket
6
+ class Response::Xmlize < Response
7
+ dependency 'nokogiri'
8
+
9
+ define_parser do |body|
10
+ ::Nokogiri::XML body
11
+ end
12
+
13
+ def parse(body)
14
+ case body
15
+ when ''
16
+ nil
17
+ when 'true'
18
+ true
19
+ when 'false'
20
+ false
21
+ else
22
+ self.class.parser.call body
23
+ end
24
+ end
25
+ end # Response::Xmlize
26
+ end # BitBucket
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ module Result
5
+ include BitBucket::Constants
6
+
7
+ # TODO Add result counts method to check total items looking at result links
8
+
9
+ def ratelimit_limit
10
+ loaded? ? @env[:response_headers][RATELIMIT_LIMIT] : nil
11
+ end
12
+
13
+ def ratelimit_remaining
14
+ loaded? ? @env[:response_headers][RATELIMIT_REMAINING] : nil
15
+ end
16
+
17
+ def cache_control
18
+ loaded? ? @env[:response_headers][CACHE_CONTROL] : nil
19
+ end
20
+
21
+ def content_type
22
+ loaded? ? @env[:response_headers][CONTENT_TYPE] : nil
23
+ end
24
+
25
+ def content_length
26
+ loaded? ? @env[:response_headers][CONTENT_LENGTH] : nil
27
+ end
28
+
29
+ def etag
30
+ loaded? ? @env[:response_headers][ETAG] : nil
31
+ end
32
+
33
+ def date
34
+ loaded? ? @env[:response_headers][DATE] : nil
35
+ end
36
+
37
+ def location
38
+ loaded? ? @env[:response_headers][LOCATION] : nil
39
+ end
40
+
41
+ def server
42
+ loaded? ? @env[:response_headers][SERVER] : nil
43
+ end
44
+
45
+ def status
46
+ loaded? ? @env[:status] : nil
47
+ end
48
+
49
+ def success?
50
+ (200..299).include? status
51
+ end
52
+
53
+ # Returns raw body
54
+ def body
55
+ loaded? ? @env[:body] : nil
56
+ end
57
+
58
+ def loaded?
59
+ !!@env
60
+ end
61
+
62
+ # Return page links
63
+ def links
64
+ @@links = BitBucket::PageLinks.new(@env[:response_headers])
65
+ end
66
+
67
+ # Iterator like each for response pages. If there are no pages to
68
+ # iterate over this method will return nothing.
69
+ def each_page
70
+ yield self.body
71
+ while page_iterator.has_next?
72
+ yield next_page
73
+ end
74
+ end
75
+
76
+ # Retrives the result of the first page. Returns <tt>nil</tt> if there is
77
+ # no first page - either because you are already on the first page
78
+ # or there are no pages at all in the result.
79
+ def first_page
80
+ first_request = page_iterator.first
81
+ self.instance_eval { @env = first_request.env } if first_request
82
+ self.body
83
+ end
84
+
85
+ # Retrives the result of the next page. Returns <tt>nil</tt> if there is
86
+ # no next page or no pages at all.
87
+ def next_page
88
+ next_request = page_iterator.next
89
+ self.instance_eval { @env = next_request.env } if next_request
90
+ self.body
91
+ end
92
+
93
+ # Retrives the result of the previous page. Returns <tt>nil</tt> if there is
94
+ # no previous page or no pages at all.
95
+ def prev_page
96
+ prev_request = page_iterator.prev
97
+ self.instance_eval { @env = prev_request.env } if prev_request
98
+ self.body
99
+ end
100
+ alias :previous_page :prev_page
101
+
102
+ # Retrives the result of the last page. Returns <tt>nil</tt> if there is
103
+ # no last page - either because you are already on the last page,
104
+ # there is only one page or there are no pages at all in the result.
105
+ def last_page
106
+ last_request = page_iterator.last
107
+ self.instance_eval { @env = last_request.env } if last_request
108
+ self.body
109
+ end
110
+
111
+ # Retrives a specific result for a page given page number.
112
+ # The <tt>page_number</tt> parameter is not validate, hitting a page
113
+ # that does not exist will return BitBucket API error. Consequently, if
114
+ # there is only one page, this method returns nil
115
+ def page(page_number)
116
+ request = page_iterator.get_page(page_number)
117
+ self.instance_eval { @env = request.env } if request
118
+ self.body
119
+ end
120
+
121
+ # Returns <tt>true</tt> if there is another page in the result set,
122
+ # otherwise <tt>false</tt>
123
+ def has_next_page?
124
+ page_iterator.has_next?
125
+ end
126
+
127
+ # Repopulates objects for new values
128
+ def reset
129
+ nil
130
+ end
131
+
132
+ private
133
+
134
+ # Internally used page iterator
135
+ def page_iterator # :nodoc:
136
+ @@page_iterator = BitBucket::PageIterator.new(@env)
137
+ end
138
+
139
+ end # Result
140
+ end # BitBucket
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class User < API
5
+
6
+
7
+ DEFAULT_USER_OPTIONS = {
8
+ "first_name" => "",
9
+ "last_name" => "",
10
+ "avatar" => ""
11
+ # TODO: can this filed be modified?
12
+ # "resource_uri" => ""
13
+ }.freeze
14
+
15
+ # Creates new User API
16
+ def initialize(options = { })
17
+ super(options)
18
+ end
19
+
20
+ # Gets the basic information associated with an account and
21
+ # a list of all of the repositories owned by the user.
22
+ # See https://confluence.atlassian.com/display/BITBUCKET/user+Endpoint#userEndpoint-GETauserprofile
23
+ #
24
+ # = Examples
25
+ # bitbucket = BitBucket.new
26
+ # bitbucket.user_api.profile
27
+ #
28
+ def profile
29
+ get_request("/2.0/user")
30
+ end
31
+
32
+
33
+ # Update a user
34
+
35
+ # = Parameters
36
+ # * <tt>:first_name</tt> Optional string
37
+ # * <tt>:last_name</tt> Optional string
38
+ # * <tt>:avatar</tt> Optional string
39
+ # * <tt>:resource_uri</tt> Optional string
40
+ #
41
+ # = Examples
42
+ #
43
+ # bitbucket = BitBucket.new
44
+ # bitbucket.user_api.update :first_name => 'first-name', :last_name => 'last-name'
45
+ #
46
+
47
+ def update( params={ })
48
+ normalize! params
49
+ filter! DEFAULT_USER_OPTIONS, params
50
+
51
+ put_request("/2.0/user", DEFAULT_USER_OPTIONS.merge(params))
52
+
53
+ end
54
+
55
+
56
+ # GET a list of user privileges
57
+ def privileges
58
+ get_request("/2.0/user/privileges")
59
+ end
60
+
61
+
62
+
63
+ # GET a list of repositories an account follows
64
+ # Gets the details of the repositories that the individual or team account follows.
65
+ # This call returns the full data about the repositories including
66
+ # if the repository is a fork of another repository.
67
+ # An account always "follows" its own repositories.
68
+ def follows
69
+ get_request("/2.0/user/follows")
70
+ end
71
+
72
+
73
+ # GET a list of repositories visible to an account
74
+ # Gets the details of the repositories that the user owns
75
+ # or has at least read access to.
76
+ # Use this if you're looking for a full list of all of the repositories associated with a user.
77
+ def repositories
78
+ get_request("/2.0/user/repositories")
79
+ end
80
+
81
+ alias :repos :repositories
82
+
83
+
84
+
85
+ # GET a list of repositories the account is following
86
+ # Gets a list of the repositories the account follows.
87
+ # This is the same list that appears on the Following tab on your account dashboard.
88
+ def overview
89
+ get_request("/2.0/user/repositories/overview")
90
+ end
91
+
92
+
93
+
94
+ # GET the list of repositories on the dashboard
95
+ # Gets the repositories list from the account's dashboard.
96
+ def dashboard
97
+ get_request("/2.0/user/repositories/dashboard")
98
+ end
99
+
100
+ end # User
101
+ end # BitBucket
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Users < API
5
+ extend AutoloadHelper
6
+
7
+ # Load all the modules after initializing Repos to avoid superclass mismatch
8
+ autoload_all 'bitbucket_rest_api/users',
9
+ :Account => 'account'
10
+
11
+
12
+ # Creates new Users API
13
+ def initialize(options = { })
14
+ super(options)
15
+ end
16
+
17
+ # Access to Users::Account API
18
+ def account
19
+ @account ||= ApiFactory.new 'Users::Account'
20
+ end
21
+
22
+
23
+ end # Users
24
+ end # BitBucket
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Users::Account < API
5
+
6
+ # API about users/account , please refer to
7
+ # https://confluence.atlassian.com/display/BITBUCKET/account+Resource
8
+ #
9
+
10
+
11
+ # GET the account profile
12
+ #
13
+ def profile(accountname)
14
+ response = get_request("/2.0/users/#{accountname}")
15
+ end
16
+
17
+ # GET the account plan
18
+ def plan(accountname)
19
+ response = get_request("/2.0/users/#{accountname}/plan")
20
+ end
21
+
22
+ # GET the emails
23
+ def emails(accountname)
24
+ response = get_request("/2.0/users/#{accountname}/emails")
25
+ end
26
+
27
+ # GET the followers
28
+ def followers(accountname)
29
+ response = get_request("/2.0/users/#{accountname}/followers")
30
+ end
31
+
32
+ # GET the events
33
+ def events(accountname)
34
+ response = get_request("/2.0/users/#{accountname}/events")
35
+ end
36
+
37
+ #GET the keys
38
+ def keys(accountname)
39
+ response = get_request("/2.0/users/#{accountname}/ssh-keys")
40
+ end
41
+
42
+ #POST a new key
43
+ # params should be in format {key: "", label:""}
44
+ def new_key(accountname, params)
45
+ response = post_request("/2.0/users/#{accountname}/ssh-keys/", params)
46
+ end
47
+
48
+ #DELETE a key
49
+ def delete_key(accountname, key_id)
50
+ response = delete_request("/2.0/users/#{accountname}/ssh-keys/#{key_id}")
51
+ end
52
+ end # Users::Account
53
+ end # BitBucket