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,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitBucket::Repos::Sources do
4
+ let(:subject) { BitBucket::Repos::Sources.new }
5
+
6
+ describe '#list' do
7
+ context 'when some parameters are missing' do
8
+ it 'raises an error' do
9
+ expect do
10
+ subject.list(
11
+ 'mock_username',
12
+ 'mock_repo'
13
+ )
14
+ end.to raise_error(ArgumentError)
15
+ end
16
+ end
17
+
18
+ context 'when path parameter is empty' do
19
+ before do
20
+ expect(subject).to receive(:request).with(
21
+ :get,
22
+ '/2.0/repositories/mock_username/mock_repo/src/moch_sha/',
23
+ {},
24
+ {}
25
+ )
26
+ end
27
+
28
+ it 'sends a GET request for a list of all source files' do
29
+ subject.list('mock_username', 'mock_repo', 'moch_sha', '')
30
+ end
31
+ end
32
+
33
+ context 'when path parameter is defined' do
34
+ before do
35
+ expect(subject).to receive(:request).with(
36
+ :get,
37
+ '/2.0/repositories/mock_username/mock_repo/src/moch_sha/app/controller',
38
+ {},
39
+ {}
40
+ )
41
+ end
42
+
43
+ it 'send a GET request for a list of the source files under the specified path' do
44
+ subject.list('mock_username', 'mock_repo', 'moch_sha', 'app/controller')
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#get' do
50
+ context 'when some parameters are missing' do
51
+ it 'raises an error' do
52
+ expect do
53
+ subject.get(
54
+ 'mock_username',
55
+ 'mock_repo',
56
+ 'moch_sha'
57
+ )
58
+ end.to raise_error(ArgumentError)
59
+ end
60
+ end
61
+
62
+ context 'when path parameter is defined' do
63
+ before do
64
+ expect(subject).to receive(:request).with(
65
+ :get,
66
+ '/2.0/repositories/mock_username/mock_repo/raw/moch_sha/app/assets/images/logo.jpg',
67
+ {},
68
+ {}
69
+ )
70
+ end
71
+
72
+ it "send a GET request for a source file's size and contents" do
73
+ subject.get('mock_username', 'mock_repo', 'moch_sha', 'app/assets/images/logo.jpg')
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,245 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitBucket::Repos::Webhooks, wip: true do
4
+ subject { described_class.new }
5
+ let(:post_put_params) do
6
+ {
7
+ 'description' => 'mock_description',
8
+ 'url' => 'mock_url',
9
+ 'active' => true,
10
+ 'events' => ['repo:push']
11
+ }
12
+ end
13
+
14
+ let(:missing_key_params) do
15
+ {
16
+ 'description' => 'mock_description',
17
+ 'active' => true,
18
+ 'events' => ['repo:push']
19
+ }
20
+ end
21
+
22
+ let(:blank_value_params) do
23
+ {
24
+ 'description' => '',
25
+ 'url' => 'mock_url',
26
+ 'active' => true,
27
+ 'events' => ['repo:push']
28
+ }
29
+ end
30
+
31
+ let(:bad_event_params) do
32
+ {
33
+ 'description' => 'mock_description',
34
+ 'url' => 'mock_url',
35
+ 'active' => true,
36
+ 'events' => ['bad:event']
37
+ }
38
+ end
39
+
40
+ let(:missing_events_params) do
41
+ {
42
+ 'description' => 'mock_description',
43
+ 'url' => 'mock_url',
44
+ 'active' => true,
45
+ 'events' => []
46
+ }
47
+ end
48
+
49
+ describe '#create' do
50
+ context 'when required fields are missing from params' do
51
+ context 'when a required key is missing' do
52
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
53
+ expect do
54
+ subject.create(
55
+ 'mock_username',
56
+ 'mock_repo',
57
+ missing_key_params
58
+ )
59
+ end.to raise_error(BitBucket::Error::RequiredParams)
60
+ end
61
+ end
62
+
63
+ context 'when values of required keys are blank' do
64
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
65
+ expect do
66
+ subject.create(
67
+ 'mock_username',
68
+ 'mock_repo',
69
+ blank_value_params
70
+ )
71
+ end.to raise_error(
72
+ BitBucket::Error::BlankValue,
73
+ "The value for: 'description', cannot be blank :("
74
+ )
75
+ end
76
+ end
77
+ end
78
+
79
+ it 'validates the given events' do
80
+ allow(subject).to(receive(:request))
81
+
82
+ expect do
83
+ subject.create(
84
+ 'mock_username',
85
+ 'mock_repo',
86
+ bad_event_params
87
+
88
+ )
89
+ end.to raise_error(
90
+ BitBucket::Error::BadEvents,
91
+ "The event: 'bad:event', does not exist :("
92
+ )
93
+ end
94
+
95
+ it 'checks that at least one event is given' do
96
+ allow(subject).to(receive(:request))
97
+
98
+ expect do
99
+ subject.create(
100
+ 'mock_username',
101
+ 'mock_repo',
102
+ missing_events_params
103
+ )
104
+ end.to raise_error(
105
+ BitBucket::Error::NoEvents,
106
+ "At least one event is required, none given :("
107
+ )
108
+ end
109
+
110
+ it 'makes a POST request to create a specific webhook' do
111
+ expect(subject).to receive(:request).with(
112
+ :post,
113
+ '/2.0/repositories/mock_username/mock_repo/hooks',
114
+ post_put_params,
115
+ {}
116
+ )
117
+
118
+ subject.create(
119
+ 'mock_username',
120
+ 'mock_repo',
121
+ post_put_params
122
+ )
123
+ end
124
+ end
125
+
126
+ describe '#list' do
127
+ it 'makes a GET request for all the webhooks beloning to the given repo' do
128
+ expect(subject).to receive(:request).with(
129
+ :get,
130
+ '/2.0/repositories/mock_username/mock_repo/hooks',
131
+ {},
132
+ {}
133
+ )
134
+
135
+ subject.list('mock_username', 'mock_repo')
136
+ end
137
+ end
138
+
139
+ describe '#get' do
140
+ it 'makes a GET request for a specific webook' do
141
+ expect(subject).to receive(:request).with(
142
+ :get,
143
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
144
+ {},
145
+ {}
146
+ )
147
+
148
+ subject.get('mock_username', 'mock_repo', 'mock_uuid')
149
+ end
150
+ end
151
+
152
+ describe '#edit' do
153
+ context 'when required fields are missing from params' do
154
+ context 'when a required key is missing' do
155
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
156
+ expect do
157
+ subject.edit(
158
+ 'mock_username',
159
+ 'mock_repo',
160
+ 'mock_uuid',
161
+ missing_key_params
162
+ )
163
+ end.to raise_error(BitBucket::Error::RequiredParams)
164
+ end
165
+ end
166
+
167
+ context 'when values of required keys are blank' do
168
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
169
+ expect do
170
+ subject.edit(
171
+ 'mock_username',
172
+ 'mock_repo',
173
+ 'mock_uuid',
174
+ blank_value_params
175
+ )
176
+ end.to raise_error(
177
+ BitBucket::Error::BlankValue,
178
+ "The value for: 'description', cannot be blank :("
179
+ )
180
+ end
181
+ end
182
+ end
183
+
184
+ it 'validates the existence of the given events' do
185
+ allow(subject).to(receive(:request))
186
+
187
+ expect do
188
+ subject.edit(
189
+ 'mock_username',
190
+ 'mock_repo',
191
+ 'mock_uuid',
192
+ bad_event_params
193
+ )
194
+ end.to raise_error(
195
+ BitBucket::Error::BadEvents,
196
+ "The event: 'bad:event', does not exist :("
197
+ )
198
+ end
199
+
200
+ it 'validates that at least one event is given' do
201
+ allow(subject).to(receive(:request))
202
+
203
+ expect do
204
+ subject.edit(
205
+ 'mock_username',
206
+ 'mock_repo',
207
+ 'mock_uuid',
208
+ missing_events_params
209
+ )
210
+ end.to raise_error(
211
+ BitBucket::Error::NoEvents,
212
+ "At least one event is required, none given :("
213
+ )
214
+ end
215
+
216
+ it 'makes a PUT request for the given webhook' do
217
+ expect(subject).to receive(:request).with(
218
+ :put,
219
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
220
+ post_put_params,
221
+ {}
222
+ )
223
+
224
+ subject.edit(
225
+ 'mock_username',
226
+ 'mock_repo',
227
+ 'mock_uuid',
228
+ post_put_params
229
+ )
230
+ end
231
+ end
232
+
233
+ describe '#delete' do
234
+ it 'sends a DELETE request for the given webhook' do
235
+ expect(subject).to receive(:request).with(
236
+ :delete,
237
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
238
+ {},
239
+ {}
240
+ )
241
+
242
+ subject.delete('mock_username', 'mock_repo', 'mock_uuid')
243
+ end
244
+ end
245
+ end
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+
3
+ describe BitBucket::Repos do
4
+ let(:repo) { BitBucket::Repos.new }
5
+
6
+ # class_eval is setting global variables in api.rb, this is creating
7
+ # failed expectations when these variables are not reset to nil before
8
+ # the next test is run. Therefore we must clear them manually as
9
+ # for the user attribute below...
10
+ after do
11
+ repo.clear_user
12
+ end
13
+
14
+ describe '.create' do
15
+ before do
16
+ expect(repo).to receive(:request).with(
17
+ :post,
18
+ '/2.0/repositories/',
19
+ BitBucket::Repos::DEFAULT_REPO_OPTIONS.merge({ 'owner' => 'mock_owner', 'name' => 'mock_repo' }),
20
+ {}
21
+ )
22
+ end
23
+
24
+ it 'should send a POST request to create the repo' do
25
+ repo.create({ 'owner' => 'mock_owner', 'name' => 'mock_repo' })
26
+ end
27
+ end
28
+
29
+ describe '.delete' do
30
+ before do
31
+ expect(repo).to receive(:request).with(
32
+ :delete,
33
+ '/2.0/repositories/mock_username/mock_repo',
34
+ {},
35
+ {}
36
+ )
37
+ end
38
+
39
+ it 'should send a DELETE request for the given repo' do
40
+ repo.delete('mock_username', 'mock_repo')
41
+ end
42
+ end
43
+
44
+ # TODO: fix case where block_given? returns true
45
+ describe '.branches' do
46
+ before do
47
+ expect(repo).to receive(:request).with(
48
+ :get,
49
+ '/2.0/repositories/mock_username/mock_repo/branches/',
50
+ {},
51
+ {}
52
+ ).and_return(['branch1', 'branch2', 'branch3'])
53
+ end
54
+
55
+ context 'without a block' do
56
+ it 'invokes the .request method' do
57
+ repo.branches('mock_username', 'mock_repo')
58
+ end
59
+ end
60
+
61
+ context 'with a block' do
62
+ it 'invokes the .request method' do
63
+ repo.branches('mock_username', 'mock_repo') { |branch| branch }
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '.edit' do
69
+ before do
70
+ expect(repo).to receive(:request).with(
71
+ :put,
72
+ '/2.0/repositories/mock_username/mock_repo/',
73
+ BitBucket::Repos::DEFAULT_REPO_OPTIONS.merge({ 'owner' => 'mock_owner' }),
74
+ {}
75
+ )
76
+ end
77
+
78
+ it 'should send a PUT request for the given repo' do
79
+ repo.edit('mock_username', 'mock_repo', { 'owner' => 'mock_owner' })
80
+ end
81
+ end
82
+
83
+ # TODO: make sure this gets documented in gem since it is not in official docs
84
+ describe '.get' do
85
+ before do
86
+ expect(repo).to receive(:request).with(
87
+ :get,
88
+ '/2.0/repositories/mock_username/mock_repo',
89
+ {},
90
+ {}
91
+ )
92
+ end
93
+
94
+ it 'should send a GET request for the given repo' do
95
+ repo.get('mock_username', 'mock_repo', {})
96
+ end
97
+ end
98
+
99
+ describe '.list' do
100
+ before do
101
+ expect(repo).to receive(:request).with(
102
+ :get,
103
+ '/2.0/user/repositories',
104
+ {},
105
+ {}
106
+ ).and_return(['repo1', 'repo2' ,'repo3'])
107
+ end
108
+
109
+ # FIXME: this method belongs in the User class!
110
+ context 'without a block' do
111
+ it 'should send a GET request for the authenticated users repos' do
112
+ repo.list
113
+ end
114
+ end
115
+
116
+ context 'with a block' do
117
+ it 'should send a GET request for the authenticated users repos' do
118
+ repo.list { |repo| repo }
119
+ end
120
+ end
121
+ end
122
+
123
+ describe '.tags' do
124
+ before do
125
+ expect(repo).to receive(:request).with(
126
+ :get,
127
+ '/2.0/repositories/mock_username/mock_repo/tags/',
128
+ {},
129
+ {}
130
+ ).and_return(['tag1', 'tag2' ,'tag3'])
131
+ end
132
+
133
+ context 'without a block' do
134
+ it 'should send a GET request for the tags belonging to the given repo' do
135
+ repo.tags('mock_username', 'mock_repo')
136
+ end
137
+ end
138
+
139
+ context 'with a block' do
140
+ it 'should send a GET request for the tags belonging to the given repo' do
141
+ repo.tags('mock_username', 'mock_repo') { |tag| tag }
142
+ end
143
+ end
144
+ end
145
+
146
+ describe "getter methods" do
147
+ it "returns an object of the correct class" do
148
+ expect(repo.changesets).to be_a BitBucket::Repos::Changesets
149
+ expect(repo.keys).to be_a BitBucket::Repos::Keys
150
+ expect(repo.following).to be_a BitBucket::Repos::Following
151
+ expect(repo.commits).to be_a BitBucket::Repos::Commits
152
+ expect(repo.pull_request).to be_a BitBucket::Repos::PullRequest
153
+ expect(repo.forks).to be_a BitBucket::Repos::Forks
154
+ expect(repo.download).to be_a BitBucket::Repos::Download
155
+ end
156
+ end
157
+ end