From 128693d323d1ed6f10b1351fb516a3c15919826d Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Thu, 9 Jun 2016 12:01:38 +0300 Subject: [PATCH 1/4] Add Content-Type: "application/json" on webhooks POST requests As described on the Bitbucket documentation this is the way to go when there are nested objects. https://confluence.atlassian.com/display/bitbucket/version+2#Version2-Supportedcontenttypes --- lib/bitbucket_rest_api/repos.rb | 4 ++++ lib/bitbucket_rest_api/repos/webhooks.rb | 9 ++++++--- spec/bitbucket_rest_api/repos/webhooks_spec.rb | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/bitbucket_rest_api/repos.rb b/lib/bitbucket_rest_api/repos.rb index b4d83fb..4bd31cc 100644 --- a/lib/bitbucket_rest_api/repos.rb +++ b/lib/bitbucket_rest_api/repos.rb @@ -88,6 +88,10 @@ def default_reviewers @default_reviewers ||= ApiFactory.new 'Repos::DefaultReviewers' end + def webhooks + @webhooks ||= ApiFactory.new 'Repos::Webhooks' + end + # List branches # # = Examples diff --git a/lib/bitbucket_rest_api/repos/webhooks.rb b/lib/bitbucket_rest_api/repos/webhooks.rb index 6c71ec9..e8c7b2f 100644 --- a/lib/bitbucket_rest_api/repos/webhooks.rb +++ b/lib/bitbucket_rest_api/repos/webhooks.rb @@ -34,7 +34,9 @@ def create(user_name, repo_name, params = {}) 'events' ) - post_request("/2.0/repositories/#{user_name}/#{repo_name}/hooks", params) + + options = { headers: { "Content-Type" => "application/json" } } + post_request("/2.0/repositories/#{user_name}/#{repo_name}/hooks", params, options) end def list(user_name, repo_name) @@ -67,10 +69,11 @@ def edit(user_name, repo_name, hook_uuid, params = {}) 'events' ) + + options = { headers: { "Content-Type" => "application/json" } } put_request( "/2.0/repositories/#{user_name}/#{repo_name}/hooks/#{hook_uuid}", - params - ) + params, options) end def delete(user_name, repo_name, hook_uuid) diff --git a/spec/bitbucket_rest_api/repos/webhooks_spec.rb b/spec/bitbucket_rest_api/repos/webhooks_spec.rb index 6ff995a..2eaac5a 100644 --- a/spec/bitbucket_rest_api/repos/webhooks_spec.rb +++ b/spec/bitbucket_rest_api/repos/webhooks_spec.rb @@ -112,7 +112,7 @@ :post, '/2.0/repositories/mock_username/mock_repo/hooks', post_put_params, - {} + { headers: { "Content-Type" => "application/json" } } ) subject.create( @@ -218,7 +218,7 @@ :put, '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid', post_put_params, - {} + { headers: { "Content-Type" => "application/json" } } ) subject.edit( From c29961e4adf9f9a35092dd11523c86648926c870 Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Thu, 9 Jun 2016 16:24:28 +0300 Subject: [PATCH 2/4] Add Content-Type: "application/json" header to keys endpoint too --- lib/bitbucket_rest_api/repos/keys.rb | 3 ++- spec/bitbucket_rest_api/repos/keys_spec.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/bitbucket_rest_api/repos/keys.rb b/lib/bitbucket_rest_api/repos/keys.rb index 4d94b2a..c95de0d 100644 --- a/lib/bitbucket_rest_api/repos/keys.rb +++ b/lib/bitbucket_rest_api/repos/keys.rb @@ -42,7 +42,8 @@ def create(user_name, repo_name, params={}) filter! VALID_KEY_PARAM_NAMES, params assert_required_keys(VALID_KEY_PARAM_NAMES, params) - post_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/", params) + options = { headers: { "Content-Type" => "application/json" } } + post_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/", params, options) end # Edit a key diff --git a/spec/bitbucket_rest_api/repos/keys_spec.rb b/spec/bitbucket_rest_api/repos/keys_spec.rb index cc555f4..c2043b1 100644 --- a/spec/bitbucket_rest_api/repos/keys_spec.rb +++ b/spec/bitbucket_rest_api/repos/keys_spec.rb @@ -31,7 +31,7 @@ :post, '/1.0/repositories/mock_username/mock_repo/deploy-keys/', { 'key' => 'mock_ssh_key', 'label' => 'mock_label' }, - {} + { headers: {"Content-Type"=>"application/json"} } ) end From 40e185b0879f1fa52d5b10ea78838993a3d47211 Mon Sep 17 00:00:00 2001 From: Ben Sales Date: Thu, 7 Jul 2016 16:14:21 +0200 Subject: [PATCH 3/4] Use the 2.0 version of the Bitbucket API for repository lists - this gives us the extra parameter of role, allowing us to filter the repository list by whether a user is an owner, contributor etc --- lib/bitbucket_rest_api/repos.rb | 17 ++++++++--------- spec/bitbucket_rest_api/repos_spec.rb | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/bitbucket_rest_api/repos.rb b/lib/bitbucket_rest_api/repos.rb index b4d83fb..668a1f5 100644 --- a/lib/bitbucket_rest_api/repos.rb +++ b/lib/bitbucket_rest_api/repos.rb @@ -219,20 +219,19 @@ def delete(user_name, repo_name) # # = Examples # bitbucket = BitBucket.new - # bitbucket.repos.list :user => 'user-name' + # bitbucket.repos.list :user => 'user-name', :role => 'owner' # bitbucket.repos.list :user => 'user-name', { |repo| ... } def list(*args) params = args.extract_options! normalize! params _merge_user_into_params!(params) unless params.has_key?('user') - filter! %w[ user type ], params - - response = #if (user_name = params.delete("user")) - # get_request("/1.0/users/#{user_name}", params) - #else - # For authenticated user - get_request("/1.0/user/repositories", params) - #end + params.merge!('pagelen' => 100) unless params.has_key?('pagelen') + + filter! %w[ user role pagelen ], params + + response = get_request("/2.0/repositories", params) + + response = response[:values] return response unless block_given? response.each { |el| yield el } end diff --git a/spec/bitbucket_rest_api/repos_spec.rb b/spec/bitbucket_rest_api/repos_spec.rb index 822e02d..e48b66a 100644 --- a/spec/bitbucket_rest_api/repos_spec.rb +++ b/spec/bitbucket_rest_api/repos_spec.rb @@ -100,10 +100,10 @@ before do expect(repo).to receive(:request).with( :get, - '/1.0/user/repositories', - {}, + '/2.0/repositories', + {"pagelen" => 100}, {} - ).and_return(['repo1', 'repo2' ,'repo3']) + ).and_return(values: ['repo1', 'repo2' ,'repo3']) end # FIXME: this method belongs in the User class! From 3aebebc4f8c20379d6db14ee2c9f3f64c1819cb4 Mon Sep 17 00:00:00 2001 From: Ben Sales Date: Thu, 21 Jul 2016 17:38:11 +0200 Subject: [PATCH 4/4] Don't downcase the repo name, as we might be passing a URI encoded UUID instead. --- lib/bitbucket_rest_api/repos/keys.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/bitbucket_rest_api/repos/keys.rb b/lib/bitbucket_rest_api/repos/keys.rb index c95de0d..91ef747 100644 --- a/lib/bitbucket_rest_api/repos/keys.rb +++ b/lib/bitbucket_rest_api/repos/keys.rb @@ -17,7 +17,7 @@ def list(user_name, repo_name, params={}) _validate_user_repo_params(user, repo) unless user? && repo? normalize! params - response = get_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/", params) + response = get_request("/1.0/repositories/#{user}/#{repo}/deploy-keys/", params) return response unless block_given? response.each { |el| yield el } end @@ -43,7 +43,7 @@ def create(user_name, repo_name, params={}) assert_required_keys(VALID_KEY_PARAM_NAMES, params) options = { headers: { "Content-Type" => "application/json" } } - post_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/", params, options) + post_request("/1.0/repositories/#{user}/#{repo}/deploy-keys/", params, options) end # Edit a key @@ -66,7 +66,7 @@ def edit(user_name, repo_name, key_id, params={}) normalize! params filter! VALID_KEY_PARAM_NAMES, params - put_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/#{key_id}", params) + put_request("/1.0/repositories/#{user}/#{repo}/deploy-keys/#{key_id}", params) end # Delete key @@ -81,7 +81,7 @@ def delete(user_name, repo_name, key_id, params={}) _validate_presence_of key_id normalize! params - delete_request("/1.0/repositories/#{user}/#{repo.downcase}/deploy-keys/#{key_id}", params) + delete_request("/1.0/repositories/#{user}/#{repo}/deploy-keys/#{key_id}", params) end end # Repos::Keys