Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0
2.4.1
1 change: 1 addition & 0 deletions lib/bitbucket_rest_api/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(message)
bad_events
no_events
blank_value
refresh_token
].each do |error|
require "bitbucket_rest_api/error/#{error}"
end
12 changes: 12 additions & 0 deletions lib/bitbucket_rest_api/error/refresh_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# encoding: utf-8

module BitBucket #:nodoc
# Raised when BitBucket receives a RefreshToken error
module Error
class RefreshToken < BitBucketError
def initialize(env)
super(env)
end
end
end # Error
end # BitBucket
2 changes: 1 addition & 1 deletion lib/bitbucket_rest_api/repos/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get(user_name_or_project_key, repo_name, hook_uuid)
_validate_user_repo_params(user, repo) unless user? && repo?

get_request(
"/2.0/repositories/#{user_name}/#{repo_name}/hooks/#{hook_uuid}"
"/2.0/repositories/#{user_name_or_project_key}/#{repo_name}/hooks/#{hook_uuid}"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes existing failing test.

)
end

Expand Down
49 changes: 33 additions & 16 deletions lib/bitbucket_rest_api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def delete_request(path, params={}, options={})
request(:delete, path, params, options)
end

def retry_token_refresh_errors
count = 0
begin
yield
rescue BitBucket::Error::RefreshToken
count += 1
if count <= 3
sleep 0.3 * count
retry
end
raise
end
end

def request(method, path, params, options={})
if !METHODS.include?(method)
raise ArgumentError, "unkown http method: #{method}"
Expand All @@ -36,25 +50,28 @@ def request(method, path, params, options={})

puts "EXECUTED: #{method} - #{path} with #{params} and #{options}" if ENV['DEBUG']

conn = connection(options)
path_prefix = (path.include?('/ssh') && BitBucket.options[:bitbucket_server]) ? '/rest/keys' : conn.path_prefix
path = (path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'

response = conn.send(method) do |request|
request['Authorization'] = "Bearer #{new_access_token}" unless new_access_token.nil?
case method.to_sym
when *(METHODS - METHODS_WITH_BODIES)
request.body = params.delete('data') if params.has_key?('data')
request.url(path, params)
when *METHODS_WITH_BODIES
request.path = path
unless params.empty?
# data = extract_data_from_params(params)
# request.body = MultiJson.dump(data)
request.body = MultiJson.dump(params)
response = retry_token_refresh_errors do
conn = connection(options)
path_prefix = (path.include?('/ssh') && BitBucket.options[:bitbucket_server]) ? '/rest/keys' : conn.path_prefix
path = (path_prefix + path).gsub(/\/\//,'/') if conn.path_prefix != '/'

response = conn.send(method) do |request|
request['Authorization'] = "Bearer #{new_access_token}" unless new_access_token.nil?
case method.to_sym
when *(METHODS - METHODS_WITH_BODIES)
request.body = params.delete('data') if params.has_key?('data')
request.url(path, params)
when *METHODS_WITH_BODIES
request.path = path
unless params.empty?
# data = extract_data_from_params(params)
# request.body = MultiJson.dump(data)
request.body = MultiJson.dump(params)
end
end
end
end

response.body
end

Expand Down
5 changes: 5 additions & 0 deletions lib/bitbucket_rest_api/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'faraday'
require 'bitbucket_rest_api/error'

REFRESH_TOKEN = %r{Access token expired. Use your refresh token to obtain a new access token.}

module BitBucket
class Response::RaiseError < Faraday::Response::Middleware

Expand All @@ -11,6 +13,9 @@ def on_complete(env)
when 400
raise BitBucket::Error::BadRequest.new(env)
when 401
if env[:body] =~ REFRESH_TOKEN

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there's no way to determine if its a 'retriable' 401 without using regex to parse the message?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I can think of and unfortunately, I have not been able to reproduce this locally so I am sort of flying blind.

raise BitBucket::Error::RefreshToken.new(env)
end
raise BitBucket::Error::Unauthorized.new(env)
when 403
raise BitBucket::Error::Forbidden.new(env)
Expand Down