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,118 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Issues::Comments < API
5
+
6
+ VALID_ISSUE_COMMENT_PARAM_NAME = %w[
7
+ content
8
+ ].freeze
9
+
10
+ # Creates new Issues::Comments API
11
+ def initialize(options = {})
12
+ super(options)
13
+ end
14
+
15
+ # List comments on an issue
16
+ #
17
+ # = Examples
18
+ # bitbucket = BitBucket.new
19
+ # bitbucket.issues.comments.all 'user-name', 'repo-name', 'issue-id'
20
+ # bitbucket.issues.comments.all 'user-name', 'repo-name', 'issue-id' {|com| .. }
21
+ #
22
+ def list(user_name, repo_name, issue_id, params={})
23
+ _update_user_repo_params(user_name, repo_name)
24
+ _validate_user_repo_params(user, repo) unless user? && repo?
25
+ _validate_presence_of issue_id
26
+
27
+ normalize! params
28
+ # _merge_mime_type(:issue_comment, params)
29
+
30
+ response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/#{issue_id}/comments/", params)
31
+ return response unless block_given?
32
+ response.each { |el| yield el }
33
+ end
34
+ alias :all :list
35
+
36
+ # Get a single comment
37
+ #
38
+ # = Examples
39
+ # bitbucket = BitBucket.new
40
+ # bitbucket.issues.comments.find 'user-name', 'repo-name', 'comment-id'
41
+ #
42
+ def get(user_name, repo_name, comment_id, params={})
43
+ _update_user_repo_params(user_name, repo_name)
44
+ _validate_user_repo_params(user, repo) unless user? && repo?
45
+ _validate_presence_of comment_id
46
+
47
+ normalize! params
48
+ # _merge_mime_type(:issue_comment, params)
49
+
50
+ get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/comments/#{comment_id}", params)
51
+ end
52
+ alias :find :get
53
+
54
+ # Create a comment
55
+ #
56
+ # = Inputs
57
+ # <tt>:content</tt> Required string
58
+ #
59
+ # = Examples
60
+ # bitbucket = BitBucket.new
61
+ # bitbucket.issues.comments.create 'user-name', 'repo-name', 'issue-id',
62
+ # "content" => 'a new comment'
63
+ #
64
+ def create(user_name, repo_name, issue_id, params={})
65
+ _update_user_repo_params(user_name, repo_name)
66
+ _validate_user_repo_params(user, repo) unless user? && repo?
67
+ _validate_presence_of issue_id
68
+
69
+ normalize! params
70
+ # _merge_mime_type(:issue_comment, params)
71
+ filter! VALID_ISSUE_COMMENT_PARAM_NAME, params
72
+ assert_required_keys(%w[ content ], params)
73
+
74
+ post_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/#{issue_id}/comments/", params)
75
+ end
76
+
77
+ # Edit a comment
78
+ #
79
+ # = Inputs
80
+ # <tt>:content</tt> Required string
81
+ #
82
+ # = Examples
83
+ # bitbucket = BitBucket.new
84
+ # bitbucket.issues.comments.edit 'user-name', 'repo-name', 'comment-id',
85
+ # "content" => 'a new comment'
86
+ #
87
+ def edit(user_name, repo_name, comment_id, params={})
88
+ _update_user_repo_params(user_name, repo_name)
89
+ _validate_user_repo_params(user, repo) unless user? && repo?
90
+ _validate_presence_of comment_id
91
+
92
+ normalize! params
93
+ # _merge_mime_type(:issue_comment, params)
94
+ filter! VALID_ISSUE_COMMENT_PARAM_NAME, params
95
+ assert_required_keys(%w[ content ], params)
96
+
97
+ put_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/comments/#{comment_id}", params)
98
+ end
99
+
100
+ # Delete a comment
101
+ #
102
+ # = Examples
103
+ # bitbucket = BitBucket.new
104
+ # bitbucket.issues.comments.delete 'user-name', 'repo-name', 'comment-id'
105
+ #
106
+ def delete(user_name, repo_name, comment_id, params={})
107
+ _update_user_repo_params(user_name, repo_name)
108
+ _validate_user_repo_params(user, repo) unless user? && repo?
109
+ _validate_presence_of comment_id
110
+
111
+ normalize! params
112
+ # _merge_mime_type(:issue_comment, params)
113
+
114
+ delete_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/comments/#{comment_id}", params)
115
+ end
116
+
117
+ end # Issues::Comments
118
+ end # BitBucket
@@ -0,0 +1,106 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Issues::Components < API
5
+
6
+ VALID_COMPONENT_INPUTS = %w[ name ].freeze
7
+
8
+ # Creates new Issues::Components API
9
+ def initialize(options = {})
10
+ super(options)
11
+ end
12
+ # List all components for a repository
13
+ #
14
+ # = Examples
15
+ # bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
16
+ # bitbucket.issues.components.list
17
+ # bitbucket.issues.components.list { |component| ... }
18
+ #
19
+ def list(user_name, repo_name, params={})
20
+ _update_user_repo_params(user_name, repo_name)
21
+ _validate_user_repo_params(user, repo) unless user? && repo?
22
+ normalize! params
23
+
24
+ response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/components", params)
25
+ return response unless block_given?
26
+ response.each { |el| yield el }
27
+ end
28
+ alias :all :list
29
+
30
+ # Get a single component
31
+ #
32
+ # = Examples
33
+ # bitbucket = BitBucket.new
34
+ # bitbucket.issues.components.find 'user-name', 'repo-name', 'component-id'
35
+ #
36
+ def get(user_name, repo_name, component_id, params={})
37
+ _update_user_repo_params(user_name, repo_name)
38
+ _validate_user_repo_params(user, repo) unless user? && repo?
39
+ _validate_presence_of component_id
40
+ normalize! params
41
+
42
+ get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/components/#{component_id}", params)
43
+ end
44
+ alias :find :get
45
+
46
+ # Create a component
47
+ #
48
+ # = Inputs
49
+ # <tt>:name</tt> - Required string
50
+ #
51
+ # = Examples
52
+ # bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
53
+ # bitbucket.issues.components.create :name => 'API'
54
+ #
55
+ def create(user_name, repo_name, params={})
56
+ _update_user_repo_params(user_name, repo_name)
57
+ _validate_user_repo_params(user, repo) unless user? && repo?
58
+
59
+ normalize! params
60
+ filter! VALID_COMPONENT_INPUTS, params
61
+ assert_required_keys(VALID_COMPONENT_INPUTS, params)
62
+
63
+ post_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/components", params)
64
+ end
65
+
66
+ # Update a component
67
+ #
68
+ # = Inputs
69
+ # <tt>:name</tt> - Required string
70
+ #
71
+ # = Examples
72
+ # @bitbucket = BitBucket.new
73
+ # @bitbucket.issues.components.update 'user-name', 'repo-name', 'component-id',
74
+ # :name => 'API'
75
+ #
76
+ def update(user_name, repo_name, component_id, params={})
77
+ _update_user_repo_params(user_name, repo_name)
78
+ _validate_user_repo_params(user, repo) unless user? && repo?
79
+ _validate_presence_of component_id
80
+
81
+ normalize! params
82
+ filter! VALID_COMPONENT_INPUTS, params
83
+ assert_required_keys(VALID_COMPONENT_INPUTS, params)
84
+
85
+ put_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/components/#{component_id}", params)
86
+ end
87
+ alias :edit :update
88
+
89
+ # Delete a component
90
+ #
91
+ # = Examples
92
+ # bitbucket = BitBucket.new
93
+ # bitbucket.issues.components.delete 'user-name', 'repo-name', 'component-id'
94
+ #
95
+ def delete(user_name, repo_name, component_id, params={})
96
+ _update_user_repo_params(user_name, repo_name)
97
+ _validate_user_repo_params(user, repo) unless user? && repo?
98
+
99
+ _validate_presence_of component_id
100
+ normalize! params
101
+
102
+ delete_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/components/#{component_id}", params)
103
+ end
104
+
105
+ end # Issues::Components
106
+ end # BitBucket
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Issues::Milestones < API
5
+
6
+ VALID_MILESTONE_INPUTS = %w[
7
+ name
8
+ ].freeze # :nodoc:
9
+
10
+ # Creates new Issues::Milestones API
11
+ def initialize(options = {})
12
+ super(options)
13
+ end
14
+
15
+ # List milestones for a repository
16
+ #
17
+ # = Examples
18
+ # bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
19
+ # bitbucket.issues.milestones.list
20
+ #
21
+ def list(user_name, repo_name, params={})
22
+ _update_user_repo_params(user_name, repo_name)
23
+ _validate_user_repo_params(user, repo) unless user? && repo?
24
+
25
+ normalize! params
26
+
27
+ response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/milestones", params)
28
+ return response unless block_given?
29
+ response.each { |el| yield el }
30
+ end
31
+ alias :all :list
32
+
33
+ # Get a single milestone
34
+ #
35
+ # = Examples
36
+ # bitbucket = BitBucket.new
37
+ # bitbucket.issues.milestones.get 'user-name', 'repo-name', 'milestone-id'
38
+ #
39
+ def get(user_name, repo_name, milestone_id, params={})
40
+ _update_user_repo_params(user_name, repo_name)
41
+ _validate_user_repo_params(user, repo) unless user? && repo?
42
+ _validate_presence_of milestone_id
43
+ normalize! params
44
+
45
+ get_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/milestones/#{milestone_id}", params)
46
+ end
47
+ alias :find :get
48
+
49
+ # Create a milestone
50
+ #
51
+ # = Inputs
52
+ # <tt>:name</tt> - Required string
53
+ #
54
+ # = Examples
55
+ # bitbucket = BitBucket.new :user => 'user-name', :repo => 'repo-name'
56
+ # bitbucket.issues.milestones.create :name => 'hello-world'
57
+ #
58
+ def create(user_name, repo_name, params={})
59
+ _update_user_repo_params(user_name, repo_name)
60
+ _validate_user_repo_params(user, repo) unless user? && repo?
61
+
62
+ normalize! params
63
+ filter! VALID_MILESTONE_INPUTS, params
64
+ assert_required_keys(%w[ name ], params)
65
+
66
+ post_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/milestones", params)
67
+ end
68
+
69
+ # Update a milestone
70
+ #
71
+ # = Inputs
72
+ # <tt>:name</tt> - Required string
73
+ #
74
+ # = Examples
75
+ # bitbucket = BitBucket.new
76
+ # bitbucket.issues.milestones.update 'user-name', 'repo-name', 'milestone-id',
77
+ # :name => 'hello-world'
78
+ #
79
+ def update(user_name, repo_name, milestone_id, params={})
80
+ _update_user_repo_params(user_name, repo_name)
81
+ _validate_user_repo_params(user, repo) unless user? && repo?
82
+ _validate_presence_of milestone_id
83
+
84
+ normalize! params
85
+ filter! VALID_MILESTONE_INPUTS, params
86
+ assert_required_keys(%w[ name ], params)
87
+
88
+ put_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/milestones/#{milestone_id}", params)
89
+ end
90
+
91
+ # Delete a milestone
92
+ #
93
+ # = Examples
94
+ # bitbucket = BitBucket.new
95
+ # bitbucket.issues.milestones.delete 'user-name', 'repo-name', 'milestone-id'
96
+ #
97
+ def delete(user_name, repo_name, milestone_id, params={})
98
+ _update_user_repo_params(user_name, repo_name)
99
+ _validate_user_repo_params(user, repo) unless user? && repo?
100
+ _validate_presence_of milestone_id
101
+ normalize! params
102
+
103
+ delete_request("/2.0/repositories/#{user}/#{repo.downcase}/issues/milestones/#{milestone_id}", params)
104
+ end
105
+
106
+ end # Issues::Milestones
107
+ end # BitBucket
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module BitBucket
4
+ # Deals with normalizing client supplied parameter keys.
5
+ module Normalizer
6
+
7
+ # Turns any keys from nested hashes including nested arrays into strings
8
+ #
9
+ def normalize!(params)
10
+ case params
11
+ when Hash
12
+ params.keys.each do |k|
13
+ params[k.to_s] = params.delete(k)
14
+ normalize!(params[k.to_s])
15
+ end
16
+ when Array
17
+ params.map! do |el|
18
+ normalize!(el)
19
+ end
20
+ else
21
+ params.to_s
22
+ end
23
+ return params
24
+ end
25
+
26
+ end # Normalizer
27
+ end # BitBucket
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module BitBucket
4
+ # Allows you to specify parameters keys which will be preserved
5
+ # in parameters hash and its subhashes. Any keys from the nested
6
+ # hash that do not match will be removed.
7
+ module ParameterFilter
8
+
9
+ # Removes any keys from nested hashes that don't match predefiend keys
10
+ #
11
+ def filter!(keys, params, options={:recursive => true}) # :nodoc:
12
+ case params
13
+ when Hash
14
+ params.keys.each do |k, v|
15
+ unless (keys.include?(k) or BitBucket::Validations::VALID_API_KEYS.include?(k))
16
+ params.delete(k)
17
+ else
18
+ filter!(keys, params[k]) if options[:recursive]
19
+ end
20
+ end
21
+ when Array
22
+ params.map! do |el|
23
+ filter!(keys, el) if options[:recursive]
24
+ end
25
+ else
26
+ params
27
+ end
28
+ return params
29
+ end
30
+
31
+ end # Filter
32
+ end # BitBucket
@@ -0,0 +1,264 @@
1
+ # encoding: utf-8
2
+
3
+ module BitBucket
4
+ class Repos < API
5
+ extend AutoloadHelper
6
+
7
+ # Load all the modules after initializing Repos to avoid superclass mismatch
8
+ autoload_all 'bitbucket_rest_api/repos',
9
+ :Changesets => 'changesets',
10
+ :Keys => 'keys',
11
+ :Services => 'services',
12
+ :Following => 'following',
13
+ :Sources => 'sources',
14
+ :Forks => 'forks',
15
+ :Commits => 'commits',
16
+ :Download => 'download',
17
+ :Webhooks => 'webhooks',
18
+ :PullRequest => 'pull_request',
19
+ :DefaultReviewers => 'default_reviewers'
20
+
21
+ DEFAULT_REPO_OPTIONS = {
22
+ "website" => "",
23
+ "is_private" => false,
24
+ "has_issues" => false,
25
+ "has_wiki" => false,
26
+ "scm" => "git",
27
+ "no_public_forks" => false
28
+ }.freeze
29
+
30
+ VALID_REPO_OPTIONS = %w[
31
+ owner
32
+ name
33
+ description
34
+ website
35
+ is_private
36
+ has_issues
37
+ has_wiki
38
+ no_public_forks
39
+ language
40
+ scm
41
+ ].freeze
42
+
43
+ # Creates new Repositories API
44
+ def initialize(options = { })
45
+ super(options)
46
+ end
47
+
48
+ # Access to Repos::Commits API
49
+ def changesets
50
+ @changesets ||= ApiFactory.new 'Repos::Changesets'
51
+ end
52
+
53
+ # Access to Repos::Keys API
54
+ def keys
55
+ @keys ||= ApiFactory.new 'Repos::Keys'
56
+ end
57
+
58
+ # Access to Repos::Watchin API
59
+ def following
60
+ @following ||= ApiFactory.new 'Repos::Following'
61
+ end
62
+
63
+ # Access to Repos::Commits API
64
+ def sources
65
+ @sources ||= ApiFactory.new 'Repos::Sources'
66
+ end
67
+
68
+ # Access to Repos::Services API
69
+ def services
70
+ @services ||= ApiFactory.new 'Repos::Services'
71
+ end
72
+ def forks
73
+ @forks ||= ApiFactory.new 'Repos::Forks'
74
+ end
75
+ def commits
76
+ @commits ||=ApiFactory.new 'Repos::Commits'
77
+ end
78
+ def download
79
+ @download ||=ApiFactory.new "Repos::Download"
80
+ end
81
+
82
+ # Access to Repos::PullRequests API
83
+ def pull_request
84
+ @pull_request ||= ApiFactory.new 'Repos::PullRequest'
85
+ end
86
+
87
+ def default_reviewers
88
+ @default_reviewers ||= ApiFactory.new 'Repos::DefaultReviewers'
89
+ end
90
+
91
+ # List branches
92
+ #
93
+ # = Examples
94
+ #
95
+ # bitbucket = BitBucket.new
96
+ # bibucket.repos.branches 'user-name', 'repo-name'
97
+ #
98
+ # repos = BitBucket::Repos.new
99
+ # repos.branches 'user-name', 'repo-name'
100
+ #
101
+ def branches(user_name, repo_name, params={})
102
+ _update_user_repo_params(user_name, repo_name)
103
+ _validate_user_repo_params(user, repo) unless (user? && repo?)
104
+ normalize! params
105
+
106
+ response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/branches/", params)
107
+ return response unless block_given?
108
+ response.each { |el| yield el }
109
+ end
110
+
111
+ alias :list_branches :branches
112
+
113
+ # FIXME: 'POST a new repository' is a deprecated feature of the API
114
+ # Create a new repository for the authenticated user.
115
+ #
116
+ # = Parameters
117
+ # <tt>:name</tt> - Required string
118
+ # <tt>:description</tt> - Optional string
119
+ # <tt>:website</tt> - Optional string
120
+ # <tt>:is_private</tt> - Optional boolean - <tt>true</tt> to create a private repository, <tt>false</tt> to create a public one.
121
+ # <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
122
+ # <tt>:has_wiki</tt> - Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
123
+ # <tt>:owner</tt> Optional string - The team in which this repository will be created
124
+ #
125
+ # = Examples
126
+ # bitbucket = BitBucket.new
127
+ # bitbucket.repos.create "name" => 'repo-name'
128
+ # "description": "This is your first repo",
129
+ # "website": "https://bitbucket.com",
130
+ # "is_private": false,
131
+ # "has_issues": true,
132
+ # "has_wiki": true
133
+ #
134
+ # Create a new repository in this team. The authenticated user
135
+ # must be a member of this team
136
+ #
137
+ # Examples:
138
+ # bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
139
+ # bitbucket.repos.create :name => 'repo-name', :owner => 'team-name'
140
+ #
141
+ def create(*args)
142
+ params = args.extract_options!
143
+ normalize! params
144
+ filter! VALID_REPO_OPTIONS + %w[ org ], params
145
+ assert_required_keys(%w[ name ], params)
146
+
147
+ # Requires authenticated user
148
+ post_request("/2.0/repositories/", DEFAULT_REPO_OPTIONS.merge(params))
149
+ end
150
+
151
+ # Edit a repository
152
+ #
153
+ # = Parameters
154
+ # * <tt>:name</tt> Required string
155
+ # * <tt>:description</tt> Optional string
156
+ # * <tt>:website</tt> Optional string
157
+ # * <tt>:private</tt> - Optional boolean - <tt>false</tt> to create public reps, <tt>false</tt> to create a private one
158
+ # * <tt>:has_issues</tt> Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
159
+ # * <tt>:has_wiki</tt> Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
160
+ # * <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
161
+ #
162
+ # = Examples
163
+ #
164
+ # bitbucket = BitBucket.new
165
+ # bitbucket.repos.edit 'user-name', 'repo-name',
166
+ # :name => 'hello-world',
167
+ # :description => 'This is your first repo',
168
+ # :website => "https://bitbucket.com",
169
+ # :public => true, :has_issues => true
170
+ #
171
+ def edit(user_name, repo_name, params={ })
172
+ _update_user_repo_params(user_name, repo_name)
173
+ _validate_user_repo_params(user, repo) unless user? && repo?
174
+
175
+ normalize! params
176
+ filter! VALID_REPO_OPTIONS, params
177
+
178
+ put_request("/2.0/repositories/#{user}/#{repo.downcase}/", DEFAULT_REPO_OPTIONS.merge(params))
179
+ end
180
+
181
+ # Get a repository
182
+ #
183
+ # = Examples
184
+ # bitbucket = BitBucket.new
185
+ # bitbucket.repos.get 'user-name', 'repo-name'
186
+ #
187
+ def get(user_name, repo_name, params={ })
188
+ _update_user_repo_params(user_name, repo_name)
189
+ _validate_user_repo_params(user, repo) unless user? && repo?
190
+ normalize! params
191
+
192
+ get_request("/2.0/repositories/#{user}/#{repo.downcase}", params)
193
+ end
194
+
195
+ alias :find :get
196
+
197
+ # FIXME: 'DELETE an existing repository' is a deprecated feature of the API
198
+ # Delete a repository
199
+ #
200
+ # = Examples
201
+ # @bitbucket = BitBucket.new
202
+ # @bitbucket.repos.delete 'user-name', 'repo-name'
203
+ #
204
+ def delete(user_name, repo_name)
205
+ _update_user_repo_params(user_name, repo_name)
206
+ _validate_user_repo_params(user, repo) unless user? && repo?
207
+
208
+ delete_request("/2.0/repositories/#{user}/#{repo.downcase}")
209
+ end
210
+
211
+ # List repositories for the authenticated user
212
+ #
213
+ # = Examples
214
+ # bitbucket = BitBucket.new :oauth_token => '...', :oauth_secret => '...'
215
+ # bitbucket.repos.list
216
+ # bitbucket.repos.list { |repo| ... }
217
+ #
218
+ # List public repositories for the specified user.
219
+ #
220
+ # = Examples
221
+ # bitbucket = BitBucket.new
222
+ # bitbucket.repos.list :user => 'user-name'
223
+ # bitbucket.repos.list :user => 'user-name', { |repo| ... }
224
+ def list(*args)
225
+ params = args.extract_options!
226
+ normalize! params
227
+ _merge_user_into_params!(params) unless params.has_key?('user')
228
+ filter! %w[ user type ], params
229
+
230
+ response = #if (user_name = params.delete("user"))
231
+ # get_request("/2.0/users/#{user_name}", params)
232
+ #else
233
+ # For authenticated user
234
+ get_request("/2.0/user/repositories", params)
235
+ #end
236
+ return response unless block_given?
237
+ response.each { |el| yield el }
238
+ end
239
+
240
+ alias :all :list
241
+
242
+ # List tags
243
+ #
244
+ # = Examples
245
+ # bitbucket = BitBucket.new
246
+ # bitbucket.repos.tags 'user-name', 'repo-name'
247
+ # bitbucket.repos.tags 'user-name', 'repo-name' { |tag| ... }
248
+ #
249
+ def tags(user_name, repo_name, params={ })
250
+ _update_user_repo_params(user_name, repo_name)
251
+ _validate_user_repo_params(user, repo) unless user? && repo?
252
+ normalize! params
253
+
254
+ response = get_request("/2.0/repositories/#{user}/#{repo.downcase}/tags/", params)
255
+ return response unless block_given?
256
+ response.each { |el| yield el }
257
+ end
258
+
259
+ alias :list_tags :tags
260
+ alias :repo_tags :tags
261
+ alias :repository_tags :tags
262
+
263
+ end # Repos
264
+ end # BitBucket