tinybucket 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -5
  3. data/README.md +100 -24
  4. data/lib/tinybucket.rb +1 -0
  5. data/lib/tinybucket/api.rb +1 -0
  6. data/lib/tinybucket/api/build_status_api.rb +55 -0
  7. data/lib/tinybucket/api/helper.rb +1 -0
  8. data/lib/tinybucket/api/helper/build_status_helper.rb +35 -0
  9. data/lib/tinybucket/client.rb +28 -45
  10. data/lib/tinybucket/enumerator.rb +4 -3
  11. data/lib/tinybucket/model.rb +2 -1
  12. data/lib/tinybucket/model/base.rb +1 -17
  13. data/lib/tinybucket/model/build_status.rb +55 -0
  14. data/lib/tinybucket/model/commit.rb +32 -15
  15. data/lib/tinybucket/model/concerns.rb +1 -0
  16. data/lib/tinybucket/model/concerns/api_callable.rb +19 -0
  17. data/lib/tinybucket/model/profile.rb +9 -26
  18. data/lib/tinybucket/model/pull_request.rb +18 -28
  19. data/lib/tinybucket/model/repository.rb +29 -76
  20. data/lib/tinybucket/model/team.rb +9 -34
  21. data/lib/tinybucket/parser.rb +1 -0
  22. data/lib/tinybucket/parser/build_status_parser.rb +9 -0
  23. data/lib/tinybucket/resource.rb +70 -0
  24. data/lib/tinybucket/resource/base.rb +29 -0
  25. data/lib/tinybucket/resource/branch_restrictions.rb +45 -0
  26. data/lib/tinybucket/resource/commit/base.rb +12 -0
  27. data/lib/tinybucket/resource/commit/build_statuses.rb +47 -0
  28. data/lib/tinybucket/resource/commit/comments.rb +32 -0
  29. data/lib/tinybucket/resource/commits.rb +33 -0
  30. data/lib/tinybucket/resource/forks.rb +22 -0
  31. data/lib/tinybucket/resource/pull_request/base.rb +18 -0
  32. data/lib/tinybucket/resource/pull_request/comments.rb +30 -0
  33. data/lib/tinybucket/resource/pull_request/commits.rb +17 -0
  34. data/lib/tinybucket/resource/pull_requests.rb +48 -0
  35. data/lib/tinybucket/resource/repos.rb +38 -0
  36. data/lib/tinybucket/resource/team/base.rb +22 -0
  37. data/lib/tinybucket/resource/team/followers.rb +13 -0
  38. data/lib/tinybucket/resource/team/following.rb +13 -0
  39. data/lib/tinybucket/resource/team/members.rb +13 -0
  40. data/lib/tinybucket/resource/team/repos.rb +13 -0
  41. data/lib/tinybucket/resource/user/base.rb +24 -0
  42. data/lib/tinybucket/resource/user/followers.rb +13 -0
  43. data/lib/tinybucket/resource/user/following.rb +13 -0
  44. data/lib/tinybucket/resource/user/repos.rb +13 -0
  45. data/lib/tinybucket/resource/watchers.rb +22 -0
  46. data/lib/tinybucket/version.rb +1 -1
  47. data/spec/fixtures/build_status.json +16 -0
  48. data/spec/fixtures/repositories/test_owner/get.json +1 -1
  49. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/statuses/build/post.json +16 -0
  50. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/statuses/build/test_status/get.json +16 -0
  51. data/spec/fixtures/repositories/test_owner/test_repo/commit/1/statuses/build/test_status/put.json +16 -0
  52. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/comments/get.json +1 -1
  53. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/1/commits/get.json +1 -1
  54. data/spec/fixtures/repositories/test_owner/test_repo/pullrequests/get.json +1 -1
  55. data/spec/fixtures/repositories/test_owner/test_repo/watchers/get.json +1 -1
  56. data/spec/lib/tinybucket/api/build_status_api_spec.rb +65 -0
  57. data/spec/lib/tinybucket/client_spec.rb +8 -4
  58. data/spec/lib/tinybucket/model/build_status_spec.rb +66 -0
  59. data/spec/lib/tinybucket/model/commit_spec.rb +19 -1
  60. data/spec/lib/tinybucket/model/profile_spec.rb +3 -3
  61. data/spec/lib/tinybucket/model/pull_request_spec.rb +3 -15
  62. data/spec/lib/tinybucket/model/repository_spec.rb +14 -20
  63. data/spec/lib/tinybucket/model/team_spec.rb +16 -4
  64. data/spec/lib/tinybucket/resource/branch_restrictions_spec.rb +60 -0
  65. data/spec/lib/tinybucket/resource/commit/build_statuses_spec.rb +50 -0
  66. data/spec/lib/tinybucket/resource/commit/comments_spec.rb +49 -0
  67. data/spec/lib/tinybucket/resource/commits_spec.rb +43 -0
  68. data/spec/lib/tinybucket/resource/forks_spec.rb +36 -0
  69. data/spec/lib/tinybucket/resource/pull_request/comments_spec.rb +41 -0
  70. data/spec/lib/tinybucket/resource/pull_request/commits_spec.rb +41 -0
  71. data/spec/lib/tinybucket/resource/pull_requests_spec.rb +59 -0
  72. data/spec/lib/tinybucket/resource/repos_spec.rb +76 -0
  73. data/spec/lib/tinybucket/resource/team/followers_spec.rb +27 -0
  74. data/spec/lib/tinybucket/resource/team/following_spec.rb +27 -0
  75. data/spec/lib/tinybucket/resource/team/members_spec.rb +27 -0
  76. data/spec/lib/tinybucket/resource/team/repos_spec.rb +27 -0
  77. data/spec/lib/tinybucket/resource/user/followers_spec.rb +27 -0
  78. data/spec/lib/tinybucket/resource/user/following_spec.rb +27 -0
  79. data/spec/lib/tinybucket/resource/user/repos_spec.rb +27 -0
  80. data/spec/lib/tinybucket/resource/watchers_spec.rb +38 -0
  81. data/spec/support/api_response_macros.rb +45 -1
  82. metadata +77 -3
@@ -10,12 +10,13 @@ module Tinybucket
10
10
  # @param block [Proc] a proc object to handle each item.
11
11
  def initialize(iterator, block)
12
12
  @iterator = iterator
13
+ @block = block
13
14
 
14
15
  super() do |y|
15
16
  loop do
16
- m = y.yield(@iterator.next)
17
- m = block.call(m) if block
18
- m
17
+ v = @iterator.next
18
+ m = @block ? @block.call(v) : v
19
+ y.yield(m)
19
20
  end
20
21
  end
21
22
 
@@ -5,8 +5,9 @@ module Tinybucket
5
5
  [
6
6
  :Base,
7
7
  :BranchRestriction,
8
- :Commit,
8
+ :BuildStatus,
9
9
  :Comment,
10
+ :Commit,
10
11
  :ErrorResponse,
11
12
  :Page,
12
13
  :Profile,
@@ -5,6 +5,7 @@ module Tinybucket
5
5
  include Concerns::AcceptableAttributes
6
6
  include Concerns::Enumerable
7
7
  include Concerns::Reloadable
8
+ include Concerns::ApiCallable
8
9
 
9
10
  def self.concern_included?(concern_name)
10
11
  mod_name = "Tinybucket::Model::Concerns::#{concern_name}".constantize
@@ -36,23 +37,6 @@ module Tinybucket
36
37
 
37
38
  protected
38
39
 
39
- def create_api(api_key, keys)
40
- key = ('@' + api_key.underscore).intern
41
- api = instance_variable_get(key)
42
- return api if api.present?
43
-
44
- api = create_instance(api_key)
45
- api.repo_owner = keys[:repo_owner]
46
- api.repo_slug = keys[:repo_slug]
47
- instance_variable_set(key, api)
48
-
49
- api
50
- end
51
-
52
- def create_instance(klass_name)
53
- ApiFactory.create_instance(klass_name)
54
- end
55
-
56
40
  def logger
57
41
  Tinybucket.logger
58
42
  end
@@ -0,0 +1,55 @@
1
+ module Tinybucket
2
+ module Model
3
+ # Build Status
4
+ #
5
+ # @see https://confluence.atlassian.com/bitbucket/statuses-build-resource-779295267.html
6
+ # statuses/build Resource
7
+ #
8
+ # @!attribute [rw] state
9
+ # @return [String]
10
+ # @!attribute [rw] type
11
+ # @return [String]
12
+ # @!attribute [rw] key
13
+ # @return [String]
14
+ # @!attribute [rw] name
15
+ # @return [String]
16
+ # @!attribute [rw] url
17
+ # @return [String]
18
+ # @!attribute [rw] description
19
+ # @return [String]
20
+ # @!attribute [rw] links
21
+ # @return [Hash]
22
+ class BuildStatus < Base
23
+ include Tinybucket::Model::Concerns::RepositoryKeys
24
+ include Tinybucket::Model::Concerns::Reloadable
25
+
26
+ acceptable_attributes \
27
+ :state, :type, :key, :name, :url, :description, :links, \
28
+ :created_on, :updated_on
29
+
30
+ attr_accessor :revision
31
+
32
+ # Update build status
33
+ #
34
+ # @param options [Hash]
35
+ # @option options [String] :state
36
+ # @return [Tinybucket::Model::BuildStatus]
37
+ def update(options)
38
+ build_status_api.put(revision, key, options).tap do |m|
39
+ m.repo_keys = repo_keys
40
+ m.revision = revision
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def build_status_api
47
+ create_api('BuildStatus', repo_keys)
48
+ end
49
+
50
+ def load_model
51
+ build_status_api.find(revision, key)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -36,14 +36,9 @@ module Tinybucket
36
36
  # Get comments which associate with this commit.
37
37
  #
38
38
  # @param options [Hash]
39
- # @return [Tinybucket::Enumerator] enumerator to enumerate comments
40
- # as {Tinybucket::Model::Comment} instance.
39
+ # @return [Tinybucket::Resource::Commit::Comments]
41
40
  def comments(options = {})
42
- enumerator(
43
- comments_api,
44
- :list,
45
- options
46
- ) { |m| block_given? ? yield(m) : m }
41
+ comments_resource(options)
47
42
  end
48
43
 
49
44
  # Get the specific commit comment which associate with this commit.
@@ -52,13 +47,14 @@ module Tinybucket
52
47
  # @param options [Hash]
53
48
  # @return [Tinybucket::Model::Comment]
54
49
  def comment(comment_id, options = {})
55
- comments_api.find(comment_id, options)
50
+ comments_resource.find(comment_id, options)
56
51
  end
57
52
 
58
53
  # Give approval on this commit.
59
54
  #
60
55
  # @param options [Hash]
61
- # @return [true, false]
56
+ # @return [true]
57
+ # @return [false]
62
58
  def approve(options = {})
63
59
  commit_api.approve(hash, options)
64
60
  end
@@ -66,19 +62,40 @@ module Tinybucket
66
62
  # Revoke approval on this commit.
67
63
  #
68
64
  # @param options [Hash]
69
- # @return [true, false]
65
+ # @return [true]
66
+ # @return [false]
70
67
  def unapprove(options = {})
71
68
  commit_api.unapprove(hash, options)
72
69
  end
73
70
 
71
+ # Get build status resource
72
+ #
73
+ # @param options [Hash]
74
+ # @return [Tinybucket::Resource::Commit::BuildStatuses]
75
+ def build_statuses(options = {})
76
+ build_statuses_resource(options)
77
+ end
78
+
79
+ # Get the specific build status which associate with key.
80
+ #
81
+ # @param key [String]
82
+ # @param options [Hash]
83
+ # @return [Tinybucket::Model::BuildStatus]
84
+ # @return [nil] when build_status does not found.
85
+ def build_status(key, options = {})
86
+ build_statuses_resource.find(key, options)
87
+ rescue Tinybucket::Error::NotFound
88
+ nil
89
+ end
90
+
74
91
  private
75
92
 
76
- def comments_api
77
- raise ArgumentError, MISSING_REPOSITORY_KEY unless repo_keys?
93
+ def comments_resource(options = {})
94
+ Tinybucket::Resource::Commit::Comments.new(self, options)
95
+ end
78
96
 
79
- api = create_api('Comments', repo_keys)
80
- api.commented_to = self
81
- api
97
+ def build_statuses_resource(options = {})
98
+ Tinybucket::Resource::Commit::BuildStatuses.new(self, options)
82
99
  end
83
100
 
84
101
  def commit_api
@@ -5,6 +5,7 @@ module Tinybucket
5
5
 
6
6
  [
7
7
  :AcceptableAttributes,
8
+ :ApiCallable,
8
9
  :Enumerable,
9
10
  :RepositoryKeys,
10
11
  :Reloadable
@@ -0,0 +1,19 @@
1
+ module Tinybucket
2
+ module Model
3
+ module Concerns
4
+ module ApiCallable
5
+ protected
6
+
7
+ def create_api(name, keys = {})
8
+ api = ApiFactory.create_instance(name)
9
+ return api if keys.empty?
10
+
11
+ api.tap do |m|
12
+ m.repo_owner = keys[:repo_owner]
13
+ m.repo_slug = keys[:repo_slug]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -31,50 +31,33 @@ module Tinybucket
31
31
  # Get this user's followers
32
32
  #
33
33
  # @param options [Hash]
34
- # @return [Tinybucket::Enumerator] enumerator to enumerate followers
35
- # as {Tinybucket::Model::Profile} instance.
34
+ # @return [Tinybucket::Resource::User::Followers]
36
35
  def followers(options = {})
37
- enumerator(
38
- user_api,
39
- :followers,
40
- options
41
- ) { |m| block_given? ? yield(m) : m }
36
+ Tinybucket::Resource::User::Followers.new(username, options)
42
37
  end
43
38
 
44
39
  # Get users which this user is following
45
40
  #
46
41
  # @param options [Hash]
47
- # @return [Tinybucket::Enumerator] an enumerator to enumerate followings
48
- # as {Tinybucket::Model::Profile} instance.
42
+ # @return [Tinybucket::Resource::User::Following]
49
43
  def following(options = {})
50
- enumerator(
51
- user_api,
52
- :following,
53
- options
54
- ) { |m| block_given? ? yield(m) : m }
44
+ Tinybucket::Resource::User::Following.new(username, options)
55
45
  end
56
46
 
57
47
  # Get this user's repositories
58
48
  #
59
49
  # @param options [Hash]
60
- # @return [Tinybucket::Enumerator] an enumerator to enumerate repositories
61
- # as {Tinybucket::Model::Repository} instance.
50
+ # @return [Tinybucket::Resource::User::Repos]
62
51
  def repos(options = {})
63
- enumerator(
64
- user_api,
65
- :repos,
66
- options
67
- ) { |m| block_given? ? yield(m) : m }
52
+ Tinybucket::Resource::User::Repos.new(username, options)
68
53
  end
69
54
 
70
55
  private
71
56
 
72
57
  def user_api
73
- return @user if @user
74
-
75
- @user = create_instance('User')
76
- @user.username = username
77
- @user
58
+ create_api('User').tap do |api|
59
+ api.username = username
60
+ end
78
61
  end
79
62
 
80
63
  def load_model
@@ -55,14 +55,6 @@ module Tinybucket
55
55
  :created_on, :author, :updated_on, :merge_commit, :closed_by,
56
56
  :reviewers, :participants, :uuid, :type
57
57
 
58
- # Create a new pull request.
59
- #
60
- # @todo to be implemented.
61
- # @raise [NotImplementedError] to be implemented.
62
- def create(_params)
63
- raise NotImplementedError
64
- end
65
-
66
58
  # Update this pull request.
67
59
  #
68
60
  # @todo to be implemented.
@@ -98,36 +90,26 @@ module Tinybucket
98
90
  # Get commits associated with this pull request.
99
91
  #
100
92
  # @param options [Hash]
101
- # @return [Tinybucket::Enumerator] enumerator to enumerate commits
102
- # as {Tinybucket::Model::Commit} instance.
93
+ # @return [Tinybucket::Resource::PullRequest::Commits]
103
94
  def commits(options = {})
104
- enumerator(
105
- pull_request_api,
106
- :commits,
107
- id,
108
- options
109
- ) { |m| block_given? ? yield(m) : m }
95
+ commits_resource(options)
110
96
  end
111
97
 
112
98
  # Get comments on this pull request.
113
99
  #
114
100
  # @param options [Hash]
115
- # @return [Tinybucket::Enumerator] enumerator to enumerate comments
116
- # as {Tinybucket::Model::Comment} instance.
101
+ # @return [Tinybucket::Resource::PullRequest::Comments]
117
102
  def comments(options = {})
118
- enumerator(
119
- comment_api,
120
- :list,
121
- options
122
- ) { |m| block_given? ? yield(m) : m }
103
+ comments_resource(options)
123
104
  end
124
105
 
125
106
  # Get the specific comment on this pull request.
126
107
  #
108
+ # @param comment_id [String]
127
109
  # @param options [Hash]
128
110
  # @return [Tinybucket::Model::Comment]
129
111
  def comment(comment_id, options = {})
130
- comment_api.find(comment_id, options)
112
+ comments_resource.find(comment_id, options)
131
113
  end
132
114
 
133
115
  # Get the diff for this pull request.
@@ -146,12 +128,20 @@ module Tinybucket
146
128
  pull_request_api.merge(id, options)
147
129
  end
148
130
 
131
+ # Get activities on this pull requests.
132
+ #
133
+ def activities(_options = {})
134
+ raise NotImplementedError
135
+ end
136
+
149
137
  private
150
138
 
151
- def comment_api
152
- api = create_api('Comments', repo_keys)
153
- api.commented_to = self
154
- api
139
+ def commits_resource(options = {})
140
+ Tinybucket::Resource::PullRequest::Commits.new(self, options)
141
+ end
142
+
143
+ def comments_resource(options = {})
144
+ Tinybucket::Resource::PullRequest::Comments.new(self, options)
155
145
  end
156
146
 
157
147
  def pull_request_api
@@ -58,14 +58,6 @@ module Tinybucket
58
58
  end
59
59
  end
60
60
 
61
- # Create a new repository.
62
- #
63
- # @todo to be implemented.
64
- # @raise [NotImplementedError] to be implemented.
65
- def create(_params)
66
- raise NotImplementedError
67
- end
68
-
69
61
  # Remove this repository
70
62
  #
71
63
  # @todo to be implemented.
@@ -80,14 +72,7 @@ module Tinybucket
80
72
  # @return [Tinybucket::Enumerator] an enumerator to enumerate
81
73
  # pull requests as {Tinybucket::Model::PullRequest} instance.
82
74
  def pull_requests(options = {})
83
- enumerator(
84
- pull_requests_api,
85
- :list,
86
- options
87
- ) do |m|
88
- inject_repo_keys(m)
89
- block_given? ? yield(m) : m
90
- end
75
+ pull_requests_resource(options)
91
76
  end
92
77
 
93
78
  # Get the specific pull request on this repository.
@@ -95,61 +80,32 @@ module Tinybucket
95
80
  # @param pullrequest_id [String]
96
81
  # @param options [Hash]
97
82
  # @return [Tinybucket::Model::PullRequest]
98
- def pull_request(pullrequest_id = nil, options = {})
99
- m = if pullrequest_id.present?
100
- pull_requests_api.find(pullrequest_id, options)
101
- else
102
- Tinybucket::Model::PullRequest.new({})
103
- end
104
- inject_repo_keys(m)
83
+ def pull_request(pullrequest_id, options = {})
84
+ pull_requests_resource.find(pullrequest_id, options)
105
85
  end
106
86
 
107
87
  # Get watchers on this repository.
108
88
  #
109
89
  # @param options [Hash]
110
- # @return [Tinybucket::Enumerator] an enumerator to enumerate watchers
111
- # as {Tinybucket::Model::Profile} instance.
90
+ # @return [Tinybucket::Resource::Watchers]
112
91
  def watchers(options = {})
113
- enumerator(
114
- repo_api,
115
- :watchers,
116
- options
117
- ) do |m|
118
- inject_repo_keys(m)
119
- block_given? ? yield(m) : m
120
- end
92
+ watchers_resource(options)
121
93
  end
122
94
 
123
95
  # Get repository forks.
124
96
  #
125
97
  # @param options [Hash]
126
- # @return [Tinybucket::Enumerator] an enumerator to enumerate forks
127
- # as {Tinybucket::Model::Repository} instance.
98
+ # @return [Tinybucket::Resource::Forks]
128
99
  def forks(options = {})
129
- enumerator(
130
- repo_api,
131
- :forks,
132
- options
133
- ) do |m|
134
- inject_repo_keys(m)
135
- block_given? ? yield(m) : m
136
- end
100
+ forks_resource(options)
137
101
  end
138
102
 
139
103
  # Get commits on this repository.
140
104
  #
141
105
  # @param options [Hash]
142
- # @return [Tinybucket::Enumerator] an enumerator to enumerate commits
143
- # as {Tinybucket::Model::Commit} instance.
106
+ # @return [Tinybucket::Resource::Commits]
144
107
  def commits(options = {})
145
- enumerator(
146
- commits_api,
147
- :list,
148
- options
149
- ) do |m|
150
- inject_repo_keys(m)
151
- block_given? ? yield(m) : m
152
- end
108
+ commits_resource(options)
153
109
  end
154
110
 
155
111
  # Get the specific commit on this repository.
@@ -158,25 +114,15 @@ module Tinybucket
158
114
  # @param options [Hash]
159
115
  # @return [Tinybucket::Model::Commit]
160
116
  def commit(revision, options = {})
161
- m = commits_api.find(revision, options)
162
- inject_repo_keys(m)
117
+ commits_resource.find(revision, options)
163
118
  end
164
119
 
165
120
  # Get the branch restriction information associated with this repository.
166
121
  #
167
122
  # @param options [Hash]
168
- # @return [Tinybucket::Enumerator] an enumerator to enumerate
169
- # branch restriction information as
170
- # {Tinybucket::Model::BranchRestriction} instance.
123
+ # @return [Tinybucket::Resource::BranchRestrictions]
171
124
  def branch_restrictions(options = {})
172
- enumerator(
173
- restrictions_api,
174
- :list,
175
- options
176
- ) do |m|
177
- inject_repo_keys(m)
178
- block_given? ? yield(m) : m
179
- end
125
+ branch_restrictions_resource(options)
180
126
  end
181
127
 
182
128
  # Get the specific branch restriction information on this repository.
@@ -185,8 +131,7 @@ module Tinybucket
185
131
  # @param options [Hash]
186
132
  # @return [Tinybucket::Model::BranchRestriction]
187
133
  def branch_restriction(restriction_id, options = {})
188
- m = restrictions_api.find(restriction_id, options)
189
- inject_repo_keys(m)
134
+ branch_restrictions_resource.find(restriction_id, options)
190
135
  end
191
136
 
192
137
  # Get the diff for this repository.
@@ -210,20 +155,28 @@ module Tinybucket
210
155
 
211
156
  private
212
157
 
213
- def pull_requests_api
214
- create_api('PullRequests', repo_keys)
158
+ def branch_restrictions_resource(options = {})
159
+ Tinybucket::Resource::BranchRestrictions.new(self, options)
215
160
  end
216
161
 
217
- def repo_api
218
- create_api('Repo', repo_keys)
162
+ def commits_resource(options = {})
163
+ Tinybucket::Resource::Commits.new(self, options)
164
+ end
165
+
166
+ def pull_requests_resource(options = {})
167
+ Tinybucket::Resource::PullRequests.new(self, options)
168
+ end
169
+
170
+ def watchers_resource(options = {})
171
+ Tinybucket::Resource::Watchers.new(self, options)
219
172
  end
220
173
 
221
- def commits_api
222
- create_api('Commits', repo_keys)
174
+ def forks_resource(options = {})
175
+ Tinybucket::Resource::Forks.new(self, options)
223
176
  end
224
177
 
225
- def restrictions_api
226
- create_api('BranchRestrictions', repo_keys)
178
+ def repo_api
179
+ create_api('Repo', repo_keys)
227
180
  end
228
181
 
229
182
  def diff_api