From e2791c081acca51b3b050a17e1416148998253a5 Mon Sep 17 00:00:00 2001 From: Ilias Spyropoulos Date: Tue, 5 Apr 2016 12:51:53 +0300 Subject: [PATCH 1/2] Add a Privileges class It implements all available API calls for the privileges Resource https://confluence.atlassian.com/display/bitbucket/privileges+endpoint --- lib/bitbucket_rest_api.rb | 40 ++++++++-------- lib/bitbucket_rest_api/client.rb | 4 ++ lib/bitbucket_rest_api/privileges.rb | 56 ++++++++++++++++++++++ spec/bitbucket_rest_api/client_spec.rb | 1 + spec/bitbucket_rest_api/privileges_spec.rb | 35 ++++++++++++++ 5 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 lib/bitbucket_rest_api/privileges.rb create mode 100644 spec/bitbucket_rest_api/privileges_spec.rb diff --git a/lib/bitbucket_rest_api.rb b/lib/bitbucket_rest_api.rb index 6197a10..175ff2e 100644 --- a/lib/bitbucket_rest_api.rb +++ b/lib/bitbucket_rest_api.rb @@ -58,29 +58,29 @@ def lookup_constant(const_name) extend AutoloadHelper autoload_all 'bitbucket_rest_api', - :API => 'api', - :ApiFactory => 'api_factory', - :Authorization => 'authorization', - :Authorizations => 'authorizations', - :Validations => 'validations', - :ParameterFilter => 'parameter_filter', - :Normalizer => 'normalizer', - :Client => 'client', - :CoreExt => 'core_ext', - :Request => 'request', - :Response => 'response', - :Result => 'result', - - :Repos => 'repos', - #:Error => 'error', - :Issues => 'issues', - :User => 'user', - :Users => 'users', - :Invitations => 'invitations' + :API => 'api', + :ApiFactory => 'api_factory', + :Authorization => 'authorization', + :Authorizations => 'authorizations', + :Validations => 'validations', + :ParameterFilter => 'parameter_filter', + :Normalizer => 'normalizer', + :Client => 'client', + :CoreExt => 'core_ext', + :Request => 'request', + :Response => 'response', + :Result => 'result', + + :Repos => 'repos', + #:Error => 'error', + :Issues => 'issues', + :User => 'user', + :Users => 'users', + :Invitations => 'invitations', + :Privileges => 'privileges' #:Teams => 'teams', #:PullRequests => 'pull_requests', - #:Users => 'users', #:Events => 'events', #:Search => 'search', #:PageLinks => 'page_links', diff --git a/lib/bitbucket_rest_api/client.rb b/lib/bitbucket_rest_api/client.rb index 40c01e6..aab6f96 100644 --- a/lib/bitbucket_rest_api/client.rb +++ b/lib/bitbucket_rest_api/client.rb @@ -52,5 +52,9 @@ def user_api(options = {}) def invitations(options = {}) @invitations ||= ApiFactory.new "Invitations", options end + + def privileges(options = {}) + @privileges ||= ApiFactory.new 'Privileges', options + end end # Client end # BitBucket diff --git a/lib/bitbucket_rest_api/privileges.rb b/lib/bitbucket_rest_api/privileges.rb new file mode 100644 index 0000000..49beaf5 --- /dev/null +++ b/lib/bitbucket_rest_api/privileges.rb @@ -0,0 +1,56 @@ +# encoding: utf-8 + +module BitBucket + class Privileges < API + VALID_KEY_PARAM_NAMES = %w(filter).freeze + + # Creates new Privileges API + def initialize(options = {}) + super(options) + end + + # Gets a list of the privileges granted on a repository. + # + # = Parameters + # *filter - Filters for a particular privilege. The acceptable values are:read, write and admin. + # + # = Examples + # bitbucket = BitBucket.new + # bitbucket.privileges.list 'user-name', 'repo-name', :filter => "admin" + # + def list_on_repo(user_name, repo_name, params={}) + _update_user_repo_params(user_name, repo_name) + _validate_user_repo_params(user, repo) unless user? && repo? + + normalize! params + filter! VALID_KEY_PARAM_NAMES, params + + response = get_request("/1.0/privileges/#{user}/#{repo.downcase}/", params) + return response unless block_given? + response.each { |el| yield el } + end + alias :all_on_repo :list_on_repo + + # Gets a list of all the privilege across all an account's repositories. + # + # = Parameters + # *filter - Filters for a particular privilege. The acceptable values are:read, write and admin. + # + # = Examples + # bitbucket = BitBucket.new + # bitbucket.privileges.list 'user-name', :filter => "admin" + # + def list(user_name, params={}) + _update_user_repo_params(user_name) + _validate_presence_of user_name + + normalize! params + filter! VALID_KEY_PARAM_NAMES, params + + response = get_request("/1.0/privileges/#{user}/", params) + return response unless block_given? + response.each { |el| yield el } + end + alias :all :list + end # Privileges +end # BitBucket diff --git a/spec/bitbucket_rest_api/client_spec.rb b/spec/bitbucket_rest_api/client_spec.rb index 79c32b3..3a00733 100644 --- a/spec/bitbucket_rest_api/client_spec.rb +++ b/spec/bitbucket_rest_api/client_spec.rb @@ -11,5 +11,6 @@ expect(client.invitations).to be_a BitBucket::Invitations expect(client.pull_requests).to be_a BitBucket::Repos::PullRequest expect(client.oauth).to be_a BitBucket::Request::OAuth + expect(client.privileges).to be_a BitBucket::Privileges end end diff --git a/spec/bitbucket_rest_api/privileges_spec.rb b/spec/bitbucket_rest_api/privileges_spec.rb new file mode 100644 index 0000000..7ff2523 --- /dev/null +++ b/spec/bitbucket_rest_api/privileges_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe BitBucket::Privileges do + subject { described_class.new } + + describe '#list_on_repo' do + before do + expect(subject).to receive(:request).with( + :get, + "/1.0/privileges/mock_username/mock_repo/", + { 'filter' => 'admin' }, + {} + ) + end + + it 'sends a GET request for privileges granted on the given repo filtered with filter param' do + subject.list_on_repo('mock_username', 'mock_repo', filter: "admin") + end + end + + describe '#list' do + before do + expect(subject).to receive(:request).with( + :get, + "/1.0/privileges/mock_username/", + { 'filter' => 'admin' }, + {} + ) + end + + it "sends a GET request for privileges granted on all user's repositories filtered with filter param" do + subject.list('mock_username', filter: "admin") + end + end +end From 5b59f307bfd5339c57a414056644dbcd7f408f64 Mon Sep 17 00:00:00 2001 From: Zareh Langridge Date: Fri, 18 Nov 2016 16:33:55 +0000 Subject: [PATCH 2/2] Corrected issue where teams code was no longer autoloaded --- lib/bitbucket_rest_api.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/bitbucket_rest_api.rb b/lib/bitbucket_rest_api.rb index 9399c52..80f59f7 100644 --- a/lib/bitbucket_rest_api.rb +++ b/lib/bitbucket_rest_api.rb @@ -78,7 +78,8 @@ def lookup_constant(const_name) :User => 'user', :Users => 'users', :Invitations => 'invitations', - :Privileges => 'privileges' + :Privileges => 'privileges', + :Teams => 'teams' #:Teams => 'teams', #:PullRequests => 'pull_requests',