From 48cb1c509686fc40f65ad153f36ab5d22feb63ac Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 8 Nov 2017 21:20:46 +0100 Subject: [PATCH 1/2] Restore GH enterprise support in the Rake task This restores GH enterprise support in the GH import Rake task. --- lib/tasks/import.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 943cbe6d80c..aafbe52e5f8 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -101,8 +101,8 @@ end class GithubRepos def initialize(token, current_user, github_repo) - @client = Octokit::Client - .new(access_token: token, auto_paginate: true, per_page: 100) + @client = Gitlab::GithubImport::Client.new(token) + @client.octokit.auto_paginate = true @current_user = current_user @github_repo = github_repo @@ -130,7 +130,7 @@ class GithubRepos end def repos - @client.list_repositories + @client.octokit.list_repositories end end From f37fe2edc80b83513cb9d30b6c97e85c9ccca29c Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 8 Nov 2017 21:37:01 +0100 Subject: [PATCH 2/2] Support importing GH projects without rate limits GitHub Enterprise disables rate limiting for the API, resulting in HTTP 404 errors when requesting rate limiting details. This changes Gitlab::GithubImport::Client so it can deal with rate limiting being disabled. --- lib/gitlab/github_import/client.rb | 6 +++++ spec/lib/gitlab/github_import/client_spec.rb | 27 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb index c1c338487a7..5da9befa08e 100644 --- a/lib/gitlab/github_import/client.rb +++ b/lib/gitlab/github_import/client.rb @@ -129,6 +129,8 @@ module Gitlab # whether we are running in parallel mode or not. For more information see # `#rate_or_wait_for_rate_limit`. def with_rate_limit + return yield unless rate_limiting_enabled? + request_count_counter.increment raise_or_wait_for_rate_limit unless requests_remaining? @@ -170,6 +172,10 @@ module Gitlab octokit.rate_limit.resets_in + 5 end + def rate_limiting_enabled? + @rate_limiting_enabled ||= api_endpoint.include?('.github.com') + end + def api_endpoint custom_api_endpoint || default_api_endpoint end diff --git a/spec/lib/gitlab/github_import/client_spec.rb b/spec/lib/gitlab/github_import/client_spec.rb index 9cbd9bcc14e..5b2642d9473 100644 --- a/spec/lib/gitlab/github_import/client_spec.rb +++ b/spec/lib/gitlab/github_import/client_spec.rb @@ -185,6 +185,17 @@ describe Gitlab::GithubImport::Client do client.with_rate_limit { } end + + it 'ignores rate limiting when disabled' do + expect(client) + .to receive(:rate_limiting_enabled?) + .and_return(false) + + expect(client) + .not_to receive(:requests_remaining?) + + expect(client.with_rate_limit { 10 }).to eq(10) + end end describe '#requests_remaining?' do @@ -362,4 +373,20 @@ describe Gitlab::GithubImport::Client do end end end + + describe '#rate_limiting_enabled?' do + let(:client) { described_class.new('foo') } + + it 'returns true when using GitHub.com' do + expect(client.rate_limiting_enabled?).to eq(true) + end + + it 'returns false for GitHub enterprise installations' do + expect(client) + .to receive(:api_endpoint) + .and_return('https://github.kittens.com/') + + expect(client.rate_limiting_enabled?).to eq(false) + end + end end