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
19 changes: 17 additions & 2 deletions app/models/concerns/user/github_syncable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
require "net/http"
require "json"
require "openssl"
require "socket"

module User::GithubSyncable
extend ActiveSupport::Concern

GITHUB_GRAPHQL_ENDPOINT = "https://api.github.com/graphql"

TRANSIENT_NETWORK_ERRORS = [
Net::OpenTimeout,
Net::ReadTimeout,
Net::WriteTimeout,
EOFError,
IOError,
SocketError,
Errno::ECONNRESET,
Errno::ECONNREFUSED,
Errno::EPIPE,
OpenSSL::SSL::SSLError
].freeze

# Sync from OAuth callback data
def sync_github_data_from_oauth!(auth_data)
api_token = auth_data.credentials.token
Expand Down Expand Up @@ -155,12 +170,12 @@ def github_graphql_request(query, api_token, retries: 3)
else
return { errors: [ "HTTP #{response.code}: #{response.message}" ] }
end
rescue Net::OpenTimeout, Net::ReadTimeout => e
rescue *TRANSIENT_NETWORK_ERRORS => e
if attempt < retries - 1
sleep(2 ** (attempt + 1))
next
else
return { errors: [ "Request timed out: #{e.message}" ] }
return { errors: [ "Network error: #{e.class}: #{e.message}" ] }
end
end
end
Expand Down
51 changes: 51 additions & 0 deletions test/models/concerns/user/github_syncable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "test_helper"
require "webmock/minitest"

class User::GithubSyncableTest < ActiveSupport::TestCase
setup do
WebMock.disable_net_connect!(allow_localhost: true)
@user = users(:user_with_testimonial)
@user.update_columns(username: "octocat")
end

teardown do
WebMock.reset!
WebMock.allow_net_connect!
end

# Skip the exponential backoff so tests run fast.
setup { User.singleton_class.send(:define_method, :sleep) { |_| } }
teardown { User.singleton_class.send(:remove_method, :sleep) }

test "batch_sync_github_data! retries on EOFError and succeeds on second attempt" do
stub_request(:post, User::GithubSyncable::GITHUB_GRAPHQL_ENDPOINT)
.to_raise(EOFError.new("end of file reached"))
.then.to_return(
status: 200,
body: {
data: {
user_0: { login: "octocat", name: "The Octocat", email: nil, bio: nil, company: nil, websiteUrl: nil, twitterUsername: nil, location: nil, avatarUrl: "https://example.com/a.png" },
repos_0: { nodes: [] }
}
}.to_json
)

result = User.batch_sync_github_data!([ @user ], api_token: "token")

assert_equal 1, result[:updated], result[:errors].inspect
assert_equal 0, result[:failed]
end

test "batch_sync_github_data! returns a network error after exhausting retries" do
stub_request(:post, User::GithubSyncable::GITHUB_GRAPHQL_ENDPOINT)
.to_raise(EOFError.new("end of file reached"))

result = User.batch_sync_github_data!([ @user ], api_token: "token")

assert_equal 0, result[:updated]
assert_equal 1, result[:failed]
assert_match(/EOFError/, result[:errors].first.to_s)
end
end