tinybucket2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +86 -0
  3. data/Gemfile +32 -0
  4. data/LICENSE +21 -0
  5. data/README.md +387 -0
  6. data/Rakefile +47 -0
  7. data/lib/faraday_middleware/follow_oauth_redirects.rb +71 -0
  8. data/lib/tinybucket.rb +76 -0
  9. data/lib/tinybucket/api.rb +26 -0
  10. data/lib/tinybucket/api/base_api.rb +44 -0
  11. data/lib/tinybucket/api/branch_restrictions_api.rb +51 -0
  12. data/lib/tinybucket/api/branches_api.rb +48 -0
  13. data/lib/tinybucket/api/build_status_api.rb +69 -0
  14. data/lib/tinybucket/api/comments_api.rb +81 -0
  15. data/lib/tinybucket/api/commits_api.rb +97 -0
  16. data/lib/tinybucket/api/diff_api.rb +40 -0
  17. data/lib/tinybucket/api/helper.rb +27 -0
  18. data/lib/tinybucket/api/helper/api_helper.rb +39 -0
  19. data/lib/tinybucket/api/helper/branch_restrictions_helper.rb +29 -0
  20. data/lib/tinybucket/api/helper/branches_helper.rb +29 -0
  21. data/lib/tinybucket/api/helper/build_status_helper.rb +46 -0
  22. data/lib/tinybucket/api/helper/comments_helper.rb +51 -0
  23. data/lib/tinybucket/api/helper/commits_helper.rb +42 -0
  24. data/lib/tinybucket/api/helper/diff_helper.rb +31 -0
  25. data/lib/tinybucket/api/helper/issues_helper.rb +29 -0
  26. data/lib/tinybucket/api/helper/projects_helper.rb +28 -0
  27. data/lib/tinybucket/api/helper/pull_requests_helper.rb +58 -0
  28. data/lib/tinybucket/api/helper/repo_helper.rb +31 -0
  29. data/lib/tinybucket/api/helper/repos_helper.rb +23 -0
  30. data/lib/tinybucket/api/helper/team_helper.rb +45 -0
  31. data/lib/tinybucket/api/helper/user_helper.rb +33 -0
  32. data/lib/tinybucket/api/issues_api.rb +48 -0
  33. data/lib/tinybucket/api/projects_api.rb +26 -0
  34. data/lib/tinybucket/api/pull_requests_api.rb +117 -0
  35. data/lib/tinybucket/api/repo_api.rb +56 -0
  36. data/lib/tinybucket/api/repos_api.rb +28 -0
  37. data/lib/tinybucket/api/team_api.rb +91 -0
  38. data/lib/tinybucket/api/user_api.rb +66 -0
  39. data/lib/tinybucket/api_factory.rb +21 -0
  40. data/lib/tinybucket/client.rb +107 -0
  41. data/lib/tinybucket/config.rb +10 -0
  42. data/lib/tinybucket/connection.rb +84 -0
  43. data/lib/tinybucket/constants.rb +7 -0
  44. data/lib/tinybucket/enumerator.rb +47 -0
  45. data/lib/tinybucket/error.rb +12 -0
  46. data/lib/tinybucket/error/base_error.rb +14 -0
  47. data/lib/tinybucket/error/conflict.rb +8 -0
  48. data/lib/tinybucket/error/not_found.rb +8 -0
  49. data/lib/tinybucket/error/service_error.rb +26 -0
  50. data/lib/tinybucket/iterator.rb +79 -0
  51. data/lib/tinybucket/model.rb +25 -0
  52. data/lib/tinybucket/model/base.rb +45 -0
  53. data/lib/tinybucket/model/branch.rb +48 -0
  54. data/lib/tinybucket/model/branch_restriction.rb +46 -0
  55. data/lib/tinybucket/model/build_status.rb +57 -0
  56. data/lib/tinybucket/model/comment.rb +67 -0
  57. data/lib/tinybucket/model/commit.rb +114 -0
  58. data/lib/tinybucket/model/concerns.rb +19 -0
  59. data/lib/tinybucket/model/concerns/acceptable_attributes.rb +34 -0
  60. data/lib/tinybucket/model/concerns/api_callable.rb +21 -0
  61. data/lib/tinybucket/model/concerns/enumerable.rb +20 -0
  62. data/lib/tinybucket/model/concerns/reloadable.rb +41 -0
  63. data/lib/tinybucket/model/concerns/repository_keys.rb +45 -0
  64. data/lib/tinybucket/model/error_response.rb +24 -0
  65. data/lib/tinybucket/model/issue.rb +48 -0
  66. data/lib/tinybucket/model/page.rb +45 -0
  67. data/lib/tinybucket/model/profile.rb +70 -0
  68. data/lib/tinybucket/model/project.rb +44 -0
  69. data/lib/tinybucket/model/pull_request.rb +160 -0
  70. data/lib/tinybucket/model/repository.rb +219 -0
  71. data/lib/tinybucket/model/team.rb +96 -0
  72. data/lib/tinybucket/null_logger.rb +37 -0
  73. data/lib/tinybucket/parser.rb +15 -0
  74. data/lib/tinybucket/parser/collection_parser.rb +17 -0
  75. data/lib/tinybucket/parser/object_parser.rb +17 -0
  76. data/lib/tinybucket/request.rb +59 -0
  77. data/lib/tinybucket/resource.rb +75 -0
  78. data/lib/tinybucket/resource/base.rb +35 -0
  79. data/lib/tinybucket/resource/branch_restrictions.rb +47 -0
  80. data/lib/tinybucket/resource/branches.rb +35 -0
  81. data/lib/tinybucket/resource/commit/base.rb +14 -0
  82. data/lib/tinybucket/resource/commit/build_statuses.rb +50 -0
  83. data/lib/tinybucket/resource/commit/comments.rb +34 -0
  84. data/lib/tinybucket/resource/commits.rb +46 -0
  85. data/lib/tinybucket/resource/forks.rb +24 -0
  86. data/lib/tinybucket/resource/issues.rb +35 -0
  87. data/lib/tinybucket/resource/projects.rb +49 -0
  88. data/lib/tinybucket/resource/pull_request/base.rb +20 -0
  89. data/lib/tinybucket/resource/pull_request/comments.rb +32 -0
  90. data/lib/tinybucket/resource/pull_request/commits.rb +19 -0
  91. data/lib/tinybucket/resource/pull_requests.rb +50 -0
  92. data/lib/tinybucket/resource/repos.rb +40 -0
  93. data/lib/tinybucket/resource/team/base.rb +24 -0
  94. data/lib/tinybucket/resource/team/followers.rb +15 -0
  95. data/lib/tinybucket/resource/team/following.rb +15 -0
  96. data/lib/tinybucket/resource/team/members.rb +15 -0
  97. data/lib/tinybucket/resource/team/repos.rb +15 -0
  98. data/lib/tinybucket/resource/teams.rb +22 -0
  99. data/lib/tinybucket/resource/user/base.rb +26 -0
  100. data/lib/tinybucket/resource/user/followers.rb +15 -0
  101. data/lib/tinybucket/resource/user/following.rb +15 -0
  102. data/lib/tinybucket/resource/user/repos.rb +15 -0
  103. data/lib/tinybucket/resource/watchers.rb +24 -0
  104. data/lib/tinybucket/response.rb +9 -0
  105. data/lib/tinybucket/response/handler.rb +23 -0
  106. data/lib/tinybucket/version.rb +5 -0
  107. data/tinybucket.gemspec +30 -0
  108. metadata +248 -0
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module IssuesHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_list
12
+ build_path(base_path)
13
+ end
14
+
15
+ def path_to_find(issue)
16
+ build_path(base_path,
17
+ [issue, 'issue'])
18
+ end
19
+
20
+ def base_path
21
+ build_path('/repositories',
22
+ [repo_owner, 'repo_owner'],
23
+ [repo_slug, 'repo_slug'],
24
+ 'refs', 'issues')
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module ProjectsHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_list(owner)
12
+ build_path(base_path(owner), '/')
13
+ end
14
+
15
+ def path_to_find(owner, project_key)
16
+ build_path(base_path(owner),
17
+ [project_key, 'project_key'])
18
+ end
19
+
20
+ def base_path(owner)
21
+ build_path('/teams',
22
+ [owner, 'owner'],
23
+ 'projects')
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module PullRequestsHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_list
12
+ base_path
13
+ end
14
+
15
+ def path_to_find(pr_id)
16
+ build_path(base_path,
17
+ [pr_id, 'pullrequest_id'])
18
+ end
19
+
20
+ def path_to_commits(pr_id)
21
+ build_path(base_path,
22
+ [pr_id, 'pullrequest_id'],
23
+ 'commits')
24
+ end
25
+
26
+ def path_to_approve(pr_id)
27
+ build_path(base_path,
28
+ [pr_id, 'pullrequest_id'],
29
+ 'approve')
30
+ end
31
+
32
+ def path_to_diff(pr_id)
33
+ build_path(path_to_find(pr_id),
34
+ 'diff')
35
+ end
36
+
37
+ def path_to_decline(pr_id)
38
+ build_path(base_path,
39
+ [pr_id, 'pullrequest_id'],
40
+ 'decline')
41
+ end
42
+
43
+ def path_to_merge(pr_id)
44
+ build_path(base_path,
45
+ [pr_id, 'pullrequest_id'],
46
+ 'merge')
47
+ end
48
+
49
+ def base_path
50
+ build_path('/repositories',
51
+ [repo_owner, 'repo_owner'],
52
+ [repo_slug, 'repo_slug'],
53
+ 'pullrequests')
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module RepoHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_find
12
+ base_path
13
+ end
14
+
15
+ def path_to_watchers
16
+ build_path(base_path, '/watchers')
17
+ end
18
+
19
+ def path_to_forks
20
+ build_path(base_path, '/forks')
21
+ end
22
+
23
+ def base_path
24
+ build_path('/repositories',
25
+ [repo_owner, 'repo_owner'],
26
+ [repo_slug, 'repo_slug'])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module ReposHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_list(options)
12
+ owner = options[:owner]
13
+ return base_path if owner.blank?
14
+ build_path(base_path, [owner, 'owner'])
15
+ end
16
+
17
+ def base_path
18
+ '/repositories'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module TeamHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_list
12
+ base_path
13
+ end
14
+
15
+ def path_to_find(name)
16
+ team_path(name)
17
+ end
18
+
19
+ def path_to_members(name)
20
+ build_path(team_path(name), 'members')
21
+ end
22
+
23
+ def path_to_followers(name)
24
+ build_path(team_path(name), 'followers')
25
+ end
26
+
27
+ def path_to_following(name)
28
+ build_path(team_path(name), 'following')
29
+ end
30
+
31
+ def path_to_repos(name)
32
+ build_path(team_path(name), 'repositories')
33
+ end
34
+
35
+ def base_path
36
+ build_path('/teams')
37
+ end
38
+
39
+ def team_path(name)
40
+ build_path(base_path, [name, 'teamname'])
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ module Helper
6
+ module UserHelper
7
+ include ::Tinybucket::Api::Helper::ApiHelper
8
+
9
+ private
10
+
11
+ def path_to_find
12
+ base_path
13
+ end
14
+
15
+ def path_to_followers
16
+ build_path(base_path, 'followers')
17
+ end
18
+
19
+ def path_to_following
20
+ build_path(base_path, 'following')
21
+ end
22
+
23
+ def path_to_repos
24
+ build_path('/repositories', [username, 'username'])
25
+ end
26
+
27
+ def base_path
28
+ build_path('/users', [username, 'username'])
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # Issues Api client
6
+ #
7
+ # @!attribute [rw] repo_owner
8
+ # @return [String] repository owner name.
9
+ # @!attribute [rw] repo_slug
10
+ # @return [String] {https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D repo_slug}.
11
+ class IssuesApi < BaseApi
12
+ include Tinybucket::Api::Helper::IssuesHelper
13
+
14
+ attr_accessor :repo_owner, :repo_slug
15
+
16
+ # Send 'GET a Issues list for a repository' request
17
+ #
18
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/Issues#get
19
+ # GET a Issues list for a repository
20
+ #
21
+ # @param options [Hash]
22
+ # @return [Tinybucket::Model::Page]
23
+ def list(options = {})
24
+ get_path(
25
+ path_to_list,
26
+ options,
27
+ get_parser(:collection, Tinybucket::Model::Issue)
28
+ )
29
+ end
30
+
31
+ # Send 'GET an individual Issue' request
32
+ #
33
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/Issues/%7Bname%7D#get
34
+ # GET an individual Issue
35
+ #
36
+ # @param name [String] The Issue name
37
+ # @param options [Hash]
38
+ # @return [Tinybucket::Model::Issue]
39
+ def find(name, options = {})
40
+ get_path(
41
+ path_to_find(name),
42
+ options,
43
+ get_parser(:object, Tinybucket::Model::Issue)
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ class ProjectsApi < BaseApi
6
+ include Tinybucket::Api::Helper::ProjectsHelper
7
+ attr_accessor :owner
8
+
9
+ def list(options = {})
10
+ get_path(
11
+ path_to_list(owner),
12
+ options,
13
+ get_parser(:collection, Tinybucket::Model::Project)
14
+ )
15
+ end
16
+
17
+ def find(project_key, options = {})
18
+ get_path(
19
+ path_to_find(owner, project_key),
20
+ options,
21
+ get_parser(:object, Tinybucket::Model::Project)
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinybucket
4
+ module Api
5
+ # PullRequests Api client
6
+ #
7
+ # @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests
8
+ # pullrequests Resource
9
+ #
10
+ # @!attribute [rw] repo_owner
11
+ # @return [String] repository owner name.
12
+ # @!attribute [rw] repo_slug
13
+ # @return [String] {https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D repository slug}.
14
+ class PullRequestsApi < BaseApi
15
+ include Tinybucket::Api::Helper::PullRequestsHelper
16
+
17
+ attr_accessor :repo_owner, :repo_slug
18
+
19
+ # Send 'GET a list of open pull requests' request
20
+ #
21
+ # @param options [Hash]
22
+ # @return [Tinybucket::Model::Page]
23
+ def list(options = {})
24
+ get_path(
25
+ path_to_list,
26
+ options,
27
+ get_parser(:collection, Tinybucket::Model::PullRequest)
28
+ )
29
+ end
30
+
31
+ # Send 'GET a specific pull request' request
32
+ #
33
+ # @param pr_id [String] The pull request identifier
34
+ # @param options [Hash]
35
+ # @return [Tinybucket::Model::PullRequest]
36
+ def find(pr_id, options = {})
37
+ get_path(
38
+ path_to_find(pr_id),
39
+ options,
40
+ get_parser(:object, Tinybucket::Model::PullRequest)
41
+ )
42
+ end
43
+
44
+ # Send 'GET the commits for a pull request' request
45
+ #
46
+ # @param pr_id [String] The pull request identifier
47
+ # @param options [Hash]
48
+ # @return [Tinybucket::Model::PullRequest]
49
+ def commits(pr_id, options = {})
50
+ get_path(
51
+ path_to_commits(pr_id),
52
+ options,
53
+ get_parser(:collection, Tinybucket::Model::Commit)
54
+ )
55
+ end
56
+
57
+ # Send 'POST a pull request approval' request
58
+ #
59
+ # @note This method return true if this pull request already approved.
60
+ #
61
+ # @param pr_id [String] The pull request identifier
62
+ # @param options [Hash]
63
+ # @return [true, false]
64
+ def approve(pr_id, options = {})
65
+ result = post_path(path_to_approve(pr_id), options)
66
+ (result['approved'] == true)
67
+ rescue Tinybucket::Error::Conflict => e
68
+ logger.debug 'Already approved: ' + e.inspect
69
+ true
70
+ end
71
+
72
+ # Send 'DELETE a pull request approval' request
73
+ #
74
+ # @note This method return true if this pull request is not approved yet.
75
+ #
76
+ # @param pr_id [String] The pull request identifier
77
+ # @param options [Hash]
78
+ # @return [true]
79
+ def unapprove(pr_id, options = {})
80
+ delete_path(path_to_approve(pr_id), options)
81
+ true
82
+ rescue Tinybucket::Error::NotFound => e
83
+ logger.debug 'Already unapproved: ' + e.inspect
84
+ true
85
+ end
86
+
87
+ # Send 'Decline or reject a pull request' request
88
+ #
89
+ # @param pr_id [String] The pull request identifier
90
+ # @param options [Hash]
91
+ # @return [true, false]
92
+ def decline(pr_id, options = {})
93
+ result = post_path(path_to_decline(pr_id), options)
94
+ (result['state'] == 'DECLINED')
95
+ end
96
+
97
+ # Send 'Accept and merge a pull request' request
98
+ #
99
+ # @param pr_id [String] The pull request identifier
100
+ # @param options [Hash]
101
+ # @return [true, false]
102
+ def merge(pr_id, options = {})
103
+ result = post_path(path_to_merge(pr_id), options)
104
+ (result['state'] == 'MERGED')
105
+ end
106
+
107
+ # Send 'GET the diff for a pull request' request
108
+ #
109
+ # @param pr_id [String] The pull request identifier
110
+ # @param options [Hash]
111
+ # @return [String] diff as raw text.
112
+ def diff(pr_id, options = {})
113
+ get_path(path_to_diff(pr_id), options)
114
+ end
115
+ end
116
+ end
117
+ end