tinybucket 0.1.7 → 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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +2 -1
  4. data/Gemfile +4 -4
  5. data/README.md +48 -20
  6. data/Rakefile +7 -0
  7. data/lib/tinybucket.rb +7 -4
  8. data/lib/tinybucket/api/base_api.rb +2 -19
  9. data/lib/tinybucket/api/branch_restrictions_api.rb +35 -11
  10. data/lib/tinybucket/api/comments_api.rb +38 -4
  11. data/lib/tinybucket/api/commits_api.rb +60 -10
  12. data/lib/tinybucket/api/diff_api.rb +22 -0
  13. data/lib/tinybucket/api/helper/api_helper.rb +0 -11
  14. data/lib/tinybucket/api/helper/commits_helper.rb +7 -0
  15. data/lib/tinybucket/api/pull_requests_api.rb +79 -22
  16. data/lib/tinybucket/api/repo_api.rb +37 -17
  17. data/lib/tinybucket/api/repos_api.rb +13 -6
  18. data/lib/tinybucket/api/team_api.rb +54 -29
  19. data/lib/tinybucket/api/user_api.rb +43 -23
  20. data/lib/tinybucket/api_factory.rb +2 -4
  21. data/lib/tinybucket/client.rb +39 -20
  22. data/lib/tinybucket/config.rb +1 -1
  23. data/lib/tinybucket/connection.rb +3 -3
  24. data/lib/tinybucket/enumerator.rb +44 -0
  25. data/lib/tinybucket/error.rb +2 -0
  26. data/lib/tinybucket/error/conflict.rb +6 -0
  27. data/lib/tinybucket/error/not_found.rb +6 -0
  28. data/lib/tinybucket/error/service_error.rb +8 -4
  29. data/lib/tinybucket/iterator.rb +77 -0
  30. data/lib/tinybucket/model/base.rb +6 -6
  31. data/lib/tinybucket/model/branch_restriction.rb +27 -0
  32. data/lib/tinybucket/model/comment.rb +32 -6
  33. data/lib/tinybucket/model/commit.rb +63 -9
  34. data/lib/tinybucket/model/concerns.rb +1 -0
  35. data/lib/tinybucket/model/concerns/enumerable.rb +18 -0
  36. data/lib/tinybucket/model/error_response.rb +16 -1
  37. data/lib/tinybucket/model/page.rb +25 -49
  38. data/lib/tinybucket/model/profile.rb +56 -8
  39. data/lib/tinybucket/model/pull_request.rb +114 -18
  40. data/lib/tinybucket/model/repository.rb +177 -36
  41. data/lib/tinybucket/model/team.rb +70 -10
  42. data/lib/tinybucket/request.rb +2 -0
  43. data/lib/tinybucket/response.rb +1 -1
  44. data/lib/tinybucket/response/handler.rb +21 -0
  45. data/lib/tinybucket/version.rb +1 -1
  46. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/approve/post.json +16 -0
  47. data/spec/lib/tinybucket/api/branch_restrictions_api_spec.rb +1 -2
  48. data/spec/lib/tinybucket/api/comments_api_spec.rb +1 -2
  49. data/spec/lib/tinybucket/api/commits_api_spec.rb +92 -3
  50. data/spec/lib/tinybucket/api/pull_requests_api_spec.rb +34 -4
  51. data/spec/lib/tinybucket/api/repo_api_spec.rb +1 -2
  52. data/spec/lib/tinybucket/api/repos_api_spec.rb +1 -2
  53. data/spec/lib/tinybucket/api/team_api_spec.rb +1 -6
  54. data/spec/lib/tinybucket/api/user_api_spec.rb +15 -2
  55. data/spec/lib/tinybucket/api_factory_spec.rb +2 -7
  56. data/spec/lib/tinybucket/client_spec.rb +10 -25
  57. data/spec/lib/tinybucket/error/service_error_spec.rb +23 -0
  58. data/spec/lib/tinybucket/model/commit_spec.rb +27 -4
  59. data/spec/lib/tinybucket/model/page_spec.rb +0 -30
  60. data/spec/lib/tinybucket/model/profile_spec.rb +3 -3
  61. data/spec/lib/tinybucket/model/pull_request_spec.rb +11 -5
  62. data/spec/lib/tinybucket/model/repository_spec.rb +5 -5
  63. data/spec/lib/tinybucket/model/team_spec.rb +4 -4
  64. data/spec/lib/tinybucket_spec.rb +10 -27
  65. data/spec/spec_helper.rb +3 -2
  66. data/spec/support/api_response_macros.rb +2 -2
  67. metadata +12 -3
  68. data/lib/tinybucket/response/error_handler.rb +0 -14
@@ -27,9 +27,8 @@ RSpec.describe Tinybucket::Api::CommentsApi do
27
27
  end
28
28
 
29
29
  let(:commented_to) { nil }
30
- let(:api_config) { {} }
31
30
  let(:api) do
32
- api = Tinybucket::Api::CommentsApi.new(api_config)
31
+ api = Tinybucket::Api::CommentsApi.new
33
32
  api.repo_owner = owner
34
33
  api.repo_slug = slug
35
34
  api.commented_to = commented_to if commented_to.present?
@@ -8,9 +8,8 @@ RSpec.describe Tinybucket::Api::CommitsApi do
8
8
  let(:slug) { 'test_repo' }
9
9
  let(:options) { {} }
10
10
 
11
- let(:api_config) { {} }
12
11
  let(:api) do
13
- api = Tinybucket::Api::CommitsApi.new(api_config)
12
+ api = Tinybucket::Api::CommitsApi.new
14
13
  api.repo_owner = owner
15
14
  api.repo_slug = slug
16
15
  api
@@ -18,7 +17,15 @@ RSpec.describe Tinybucket::Api::CommitsApi do
18
17
 
19
18
  it { expect(api).to be_a_kind_of(Tinybucket::Api::BaseApi) }
20
19
 
21
- before { stub_apiresponse(:get, request_path) if request_path }
20
+ let(:request_method) { :get }
21
+ let(:stub_options) { nil }
22
+
23
+ before do
24
+ if request_path
25
+ opts = stub_options.present? ? stub_options : {}
26
+ stub_apiresponse(request_method, request_path, opts)
27
+ end
28
+ end
22
29
 
23
30
  describe 'list' do
24
31
  subject { api.list(options) }
@@ -60,4 +67,86 @@ RSpec.describe Tinybucket::Api::CommitsApi do
60
67
  it { expect(subject).to be_instance_of(Tinybucket::Model::Commit) }
61
68
  end
62
69
  end
70
+
71
+ describe 'approve' do
72
+ let(:revision) { '1' }
73
+ let(:request_method) { :post }
74
+
75
+ subject { api.approve(revision, options) }
76
+
77
+ context 'without owner' do
78
+ let(:owner) { nil }
79
+ it { expect { subject }.to raise_error(ArgumentError) }
80
+ end
81
+
82
+ context 'without slug' do
83
+ let(:slug) { nil }
84
+ it { expect { subject }.to raise_error(ArgumentError) }
85
+ end
86
+
87
+ context 'when pull request is not yet approved' do
88
+ let(:request_path) do
89
+ "/repositories/#{owner}/#{slug}/commit/#{revision}/approve"
90
+ end
91
+
92
+ it { expect(subject).to be_truthy }
93
+ end
94
+
95
+ context 'when pull request is already approved.' do
96
+ let(:request_path) do
97
+ "/repositories/#{owner}/#{slug}/commit/#{revision}/approve"
98
+ end
99
+
100
+ let(:stub_options) do
101
+ {
102
+ status_code: 409,
103
+ message: JSON.generate(
104
+ error: { message: 'You already approved this pull request.' }
105
+ )
106
+ }
107
+ end
108
+ it { expect(subject).to be_truthy }
109
+ end
110
+ end
111
+
112
+ describe 'unapprove' do
113
+ let(:revision) { '1' }
114
+ let(:request_method) { :delete }
115
+ subject { api.unapprove(revision) }
116
+
117
+ context 'without owner' do
118
+ let(:owner) { nil }
119
+ it { expect { subject }.to raise_error(ArgumentError) }
120
+ end
121
+
122
+ context 'without slug' do
123
+ let(:slug) { nil }
124
+ it { expect { subject }.to raise_error(ArgumentError) }
125
+ end
126
+
127
+ context 'when commit is not yet approved' do
128
+ let(:request_path) do
129
+ "/repositories/#{owner}/#{slug}/commit/#{revision}/approve"
130
+ end
131
+
132
+ let(:stub_options) do
133
+ {
134
+ status_code: 404,
135
+ message: JSON.generate(
136
+ error: { message: 'You haven\'t approve this pull request.' }
137
+ )
138
+ }
139
+ end
140
+ it { expect(subject).to be_truthy }
141
+ end
142
+
143
+ context 'when commit is already approved' do
144
+ let(:request_path) do
145
+ "/repositories/#{owner}/#{slug}/commit/#{revision}/approve"
146
+ end
147
+ let(:stub_options) { { status_code: 204, message: '' } }
148
+
149
+ it { expect(subject).to be_truthy }
150
+ end
151
+ end
63
152
  end
@@ -3,9 +3,8 @@ require 'spec_helper'
3
3
  RSpec.describe Tinybucket::Api::PullRequestsApi do
4
4
  include ApiResponseMacros
5
5
 
6
- let(:api_config) { {} }
7
6
  let(:api) do
8
- api = Tinybucket::Api::PullRequestsApi.new(api_config)
7
+ api = Tinybucket::Api::PullRequestsApi.new
9
8
  api.repo_owner = owner
10
9
  api.repo_slug = slug
11
10
  api
@@ -163,7 +162,20 @@ RSpec.describe Tinybucket::Api::PullRequestsApi do
163
162
  let(:request_path) do
164
163
  "/repositories/#{owner}/#{slug}/pullrequests/1/approve"
165
164
  end
166
- it { expect(subject).to be_truthy }
165
+ context 'when pull request is not yet approved.' do
166
+ it { expect(subject).to be_truthy }
167
+ end
168
+ context 'when pull request is already approved.' do
169
+ let(:stub_options) do
170
+ {
171
+ status_code: 409,
172
+ message: JSON.generate(
173
+ error: { message: 'You already approved this pull request.' }
174
+ )
175
+ }
176
+ end
177
+ it { expect(subject).to be_truthy }
178
+ end
167
179
  end
168
180
  end
169
181
 
@@ -192,7 +204,25 @@ RSpec.describe Tinybucket::Api::PullRequestsApi do
192
204
  let(:request_path) do
193
205
  "/repositories/#{owner}/#{slug}/pullrequests/1/approve"
194
206
  end
195
- it { expect(subject).to be_truthy }
207
+
208
+ context 'when pull request is approved' do
209
+ # In this case, bitbucket api respond '204 No content'.
210
+ let(:stub_options) { { status_code: 204, message: '' } }
211
+ it { expect(subject).to be_truthy }
212
+ end
213
+
214
+ context 'when pull request is not approved yet.' do
215
+ # In this case, bitbucket api respond '404 NotFound'.
216
+ let(:stub_options) do
217
+ {
218
+ status_code: 404,
219
+ message: JSON.generate(
220
+ error: { message: 'You haven\'t approve this pull request.' }
221
+ )
222
+ }
223
+ end
224
+ it { expect(subject).to be_truthy }
225
+ end
196
226
  end
197
227
  end
198
228
 
@@ -7,9 +7,8 @@ RSpec.describe Tinybucket::Api::RepoApi do
7
7
  let(:repo_slug) { 'test_repo' }
8
8
  let(:request_path) { nil }
9
9
 
10
- let(:api_config) { {} }
11
10
  let(:api) do
12
- api = Tinybucket::Api::RepoApi.new(api_config)
11
+ api = Tinybucket::Api::RepoApi.new
13
12
  api.repo_owner = repo_owner
14
13
  api.repo_slug = repo_slug
15
14
  api
@@ -3,8 +3,7 @@ require 'spec_helper.rb'
3
3
  RSpec.describe Tinybucket::Api::ReposApi do
4
4
  include ApiResponseMacros
5
5
 
6
- let(:api_config) { {} }
7
- let(:api) { Tinybucket::Api::ReposApi.new(api_config) }
6
+ let(:api) { Tinybucket::Api::ReposApi.new }
8
7
 
9
8
  it { expect(api).to be_a_kind_of(Tinybucket::Api::BaseApi) }
10
9
 
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
  RSpec.describe Tinybucket::Api::TeamApi do
4
4
  include ApiResponseMacros
5
5
 
6
- let(:api_config) { {} }
7
- let(:api) do
8
- Tinybucket::Api::TeamApi.new(api_config)
9
- end
10
-
6
+ let(:api) { Tinybucket::Api::TeamApi.new }
11
7
  let(:teamname) { 'test_team' }
12
8
  let(:request_path) { nil }
13
9
  before { stub_apiresponse(:get, request_path) if request_path }
@@ -83,5 +79,4 @@ RSpec.describe Tinybucket::Api::TeamApi do
83
79
  it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
84
80
  end
85
81
  end
86
-
87
82
  end
@@ -3,9 +3,8 @@ require 'spec_helper'
3
3
  RSpec.describe Tinybucket::Api::UserApi do
4
4
  include ApiResponseMacros
5
5
 
6
- let(:api_config) { {} }
7
6
  let(:api) do
8
- api = Tinybucket::Api::UserApi.new(api_config)
7
+ api = Tinybucket::Api::UserApi.new
9
8
  api.username = user
10
9
  api
11
10
  end
@@ -57,4 +56,18 @@ RSpec.describe Tinybucket::Api::UserApi do
57
56
  it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
58
57
  end
59
58
  end
59
+
60
+ describe 'repos' do
61
+ subject { api.repos }
62
+
63
+ context 'when without username' do
64
+ let(:user) { nil }
65
+ it { expect { subject }.to raise_error(ArgumentError) }
66
+ end
67
+
68
+ context 'when with username' do
69
+ let(:request_path) { "/repositories/#{user}" }
70
+ it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
71
+ end
72
+ end
60
73
  end
@@ -3,15 +3,10 @@ require 'spec_helper'
3
3
  RSpec.describe Tinybucket::ApiFactory do
4
4
 
5
5
  describe 'create_instance' do
6
- let(:klass_name) { 'PullRequests' }
7
- let(:config) { {} }
8
- let(:options) { {} }
9
-
10
- subject do
11
- Tinybucket::ApiFactory.create_instance(klass_name, config, options)
12
- end
6
+ subject { Tinybucket::ApiFactory.create_instance(klass_name) }
13
7
 
14
8
  context 'with valid klass_name' do
9
+ let(:klass_name) { 'PullRequests' }
15
10
  it { expect(subject).to be_a_kind_of(Tinybucket::Api::BaseApi) }
16
11
  end
17
12
 
@@ -3,26 +3,11 @@ require 'spec_helper.rb'
3
3
  RSpec.describe Tinybucket::Client do
4
4
  include ApiResponseMacros
5
5
 
6
- let(:client) { Tinybucket::Client.new({}) }
6
+ let(:client) { Tinybucket::Client.new }
7
7
 
8
8
  describe 'new' do
9
- let(:options) { {} }
10
-
11
- context 'without block' do
12
- subject { Tinybucket::Client.new(options) }
13
- it { expect(subject.config).to eq(options) }
14
- end
15
-
16
- context 'with block' do
17
- subject do
18
- Tinybucket::Client.new do |config|
19
- options.each_pair do |key, value|
20
- config.send("#{key}=", value)
21
- end
22
- end
23
- end
24
- it { expect(subject.config).to eq(options) }
25
- end
9
+ subject { Tinybucket::Client.new }
10
+ it { expect(subject).to be_an_instance_of(Tinybucket::Client) }
26
11
  end
27
12
 
28
13
  describe 'repos' do
@@ -31,12 +16,12 @@ RSpec.describe Tinybucket::Client do
31
16
 
32
17
  context 'without options' do
33
18
  subject { client.repos }
34
- it { expect(subject).to be_instance_of(Tinybucket::Model::Page) }
19
+ it { expect(subject).to be_instance_of(Tinybucket::Enumerator) }
35
20
  end
36
21
  context 'with options' do
37
22
  subject { client.repos(options) }
38
23
  let(:options) { {} }
39
- it { expect(subject).to be_instance_of(Tinybucket::Model::Page) }
24
+ it { expect(subject).to be_instance_of(Tinybucket::Enumerator) }
40
25
  end
41
26
  end
42
27
 
@@ -47,30 +32,30 @@ RSpec.describe Tinybucket::Client do
47
32
  context 'without options' do
48
33
  let(:request_path) { "/repositories/#{owner}" }
49
34
  subject { client.repos(owner) }
50
- it { expect(subject).to be_instance_of(Tinybucket::Model::Page) }
35
+ it { expect(subject).to be_instance_of(Tinybucket::Enumerator) }
51
36
  end
52
37
  context 'with options' do
53
38
  let(:request_path) { "/repositories/#{owner}" }
54
39
  subject { client.repos(owner, options) }
55
40
  let(:options) { {} }
56
- it { expect(subject).to be_instance_of(Tinybucket::Model::Page) }
41
+ it { expect(subject).to be_instance_of(Tinybucket::Enumerator) }
57
42
  end
58
43
  end
59
44
 
60
45
  context 'when invalid argument passed' do
61
46
  context 'with a integer' do
62
47
  subject { client.repos(20) }
63
- it { expect { subject }.to raise_error }
48
+ it { expect { subject }.to raise_error(ArgumentError) }
64
49
  end
65
50
 
66
51
  context 'with a string and string' do
67
52
  subject { client.repos('test_owner', 'test_repository') }
68
- it { expect { subject }.to raise_error }
53
+ it { expect { subject }.to raise_error(ArgumentError) }
69
54
  end
70
55
 
71
56
  context 'with three arguments' do
72
57
  subject { client.repos('a', 'b', 'c') }
73
- it { expect { subject }.to raise_error }
58
+ it { expect { subject }.to raise_error(ArgumentError) }
74
59
  end
75
60
  end
76
61
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Tinybucket::Error::ServiceError do
4
+ context 'initialize' do
5
+ let(:env) do
6
+ {
7
+ response_headers: [],
8
+ method: 'POST',
9
+ url: 'https://api.example.org/path/to',
10
+ status: 500,
11
+ body: 'Internal Server Error'
12
+ }
13
+ end
14
+
15
+ subject { Tinybucket::Error::ServiceError.new(env) }
16
+
17
+ it { expect(subject).to be_an_instance_of(Tinybucket::Error::ServiceError) }
18
+ it { expect(subject.http_method).to eq(env[:method]) }
19
+ it { expect(subject.request_url).to eq(env[:url]) }
20
+ it { expect(subject.status_code).to eq(env[:status]) }
21
+ it { expect(subject.response_body).to eq(env[:body]) }
22
+ end
23
+ end
@@ -20,7 +20,15 @@ RSpec.describe Tinybucket::Model::Commit do
20
20
  m
21
21
  end
22
22
 
23
- before { stub_apiresponse(:get, request_path) if request_path }
23
+ let(:request_method) { :get }
24
+ let(:stub_options) { nil }
25
+
26
+ before do
27
+ if request_path
28
+ opts = stub_options.present? ? stub_options : {}
29
+ stub_apiresponse(request_method, request_path, opts)
30
+ end
31
+ end
24
32
 
25
33
  it_behaves_like 'model has acceptable_attributes',
26
34
  Tinybucket::Model::Commit,
@@ -43,7 +51,7 @@ RSpec.describe Tinybucket::Model::Commit do
43
51
  "/repositories/#{owner}/#{slug}/commit/1/comments"
44
52
  end
45
53
  subject { model.comments }
46
- it { expect(subject).to be_an_instance_of(Tinybucket::Model::Page) }
54
+ it { expect(subject).to be_an_instance_of(Tinybucket::Enumerator) }
47
55
  end
48
56
 
49
57
  describe '#comment' do
@@ -58,10 +66,25 @@ RSpec.describe Tinybucket::Model::Commit do
58
66
  end
59
67
 
60
68
  describe '#approve' do
61
- pending 'TODO implement method'
69
+ let(:request_method) { :post }
70
+ let(:request_path) do
71
+ "/repositories/#{owner}/#{slug}/commit/1/approve"
72
+ end
73
+
74
+ subject { model.approve }
75
+
76
+ it { expect(subject).to be_truthy }
62
77
  end
63
78
 
64
79
  describe '#unapprove' do
65
- pending 'TODO implement method'
80
+ let(:request_method) { :delete }
81
+ let(:request_path) do
82
+ "/repositories/#{owner}/#{slug}/commit/1/approve"
83
+ end
84
+ let(:stub_options) { { status_code: 204, message: '' } }
85
+
86
+ subject { model.unapprove }
87
+
88
+ it { expect(subject).to be_truthy }
66
89
  end
67
90
  end
@@ -25,34 +25,4 @@ RSpec.describe Tinybucket::Model::Page do
25
25
  expect(subject.items.size).to eq(json['values'].size)
26
26
  end
27
27
  end
28
-
29
- describe 'size' do
30
- subject { model.size }
31
-
32
- let(:base_json) do
33
- text = File.read('spec/fixtures/repositories/get.json')
34
- JSON.parse(text)
35
- end
36
-
37
- context 'when json contains size = nil' do
38
- let(:json) do
39
- base_json['size'] = nil
40
- base_json
41
- end
42
- it { expect(subject).to be_nil }
43
- end
44
-
45
- context 'when json contains size' do
46
- let(:size) { 1000 }
47
- let(:json) do
48
- base_json['size'] = size
49
- base_json
50
- end
51
- it { expect(subject).to eq(size) }
52
- end
53
- end
54
-
55
- describe 'each' do
56
- pending 'TODO add specs'
57
- end
58
28
  end