From afe2c15e6bf1005e5bd1d213d3548a1a17a11137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D=C3=A1vila?= Date: Mon, 12 Mar 2018 16:01:43 -0500 Subject: Fix provider server URL used when listing repos to import Also use Gitlab::Auth::OAuth::Provider.config_for to access OmniAuth config --- app/helpers/import_helper.rb | 13 +++++---- ...4-fix-generated-url-for-external-repository.yml | 5 ++++ lib/gitlab/auth/saml/config.rb | 2 +- lib/gitlab/github_import/client.rb | 5 +--- lib/gitlab/gitlab_import/client.rb | 2 +- lib/gitlab/legacy_github_import/client.rb | 2 +- lib/google_api/auth.rb | 2 +- spec/helpers/import_helper_spec.rb | 33 ++++++++++++++++++---- 8 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 changelogs/unreleased/34604-fix-generated-url-for-external-repository.yml diff --git a/app/helpers/import_helper.rb b/app/helpers/import_helper.rb index 9149d79ecb8..4664b1728c4 100644 --- a/app/helpers/import_helper.rb +++ b/app/helpers/import_helper.rb @@ -1,4 +1,6 @@ module ImportHelper + include ::Gitlab::Utils::StrongMemoize + def has_ci_cd_only_params? false end @@ -75,17 +77,18 @@ module ImportHelper private def github_project_url(full_path) - "#{github_root_url}/#{full_path}" + URI.join(github_root_url, full_path).to_s end def github_root_url - return @github_url if defined?(@github_url) + strong_memoize(:github_url) do + provider = Gitlab::Auth::OAuth::Provider.config_for('github') - provider = Gitlab.config.omniauth.providers.find { |p| p.name == 'github' } - @github_url = provider.fetch('url', 'https://github.com') if provider + provider&.dig('url').presence || 'https://github.com' + end end def gitea_project_url(full_path) - "#{@gitea_host_url.sub(%r{/+\z}, '')}/#{full_path}" + URI.join(@gitea_host_url, full_path).to_s end end diff --git a/changelogs/unreleased/34604-fix-generated-url-for-external-repository.yml b/changelogs/unreleased/34604-fix-generated-url-for-external-repository.yml new file mode 100644 index 00000000000..c4b5f59b724 --- /dev/null +++ b/changelogs/unreleased/34604-fix-generated-url-for-external-repository.yml @@ -0,0 +1,5 @@ +--- +title: Fix generated URL when listing repoitories for import +merge_request: 17692 +author: +type: fixed diff --git a/lib/gitlab/auth/saml/config.rb b/lib/gitlab/auth/saml/config.rb index e654e7fe438..2760b1a3247 100644 --- a/lib/gitlab/auth/saml/config.rb +++ b/lib/gitlab/auth/saml/config.rb @@ -4,7 +4,7 @@ module Gitlab class Config class << self def options - Gitlab.config.omniauth.providers.find { |provider| provider.name == 'saml' } + Gitlab::Auth::OAuth::Provider.config_for('saml') end def groups diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb index 4f160e4a447..a61beafae0d 100644 --- a/lib/gitlab/github_import/client.rb +++ b/lib/gitlab/github_import/client.rb @@ -197,10 +197,7 @@ module Gitlab end def github_omniauth_provider - @github_omniauth_provider ||= - Gitlab.config.omniauth.providers - .find { |provider| provider.name == 'github' } - .to_h + @github_omniauth_provider ||= Gitlab::Auth::OAuth::Provider.config_for('github').to_h end def rate_limit_counter diff --git a/lib/gitlab/gitlab_import/client.rb b/lib/gitlab/gitlab_import/client.rb index 075b3982608..5482504e72e 100644 --- a/lib/gitlab/gitlab_import/client.rb +++ b/lib/gitlab/gitlab_import/client.rb @@ -72,7 +72,7 @@ module Gitlab end def config - Gitlab.config.omniauth.providers.find {|provider| provider.name == "gitlab"} + Gitlab::Auth::OAuth::Provider.config_for('gitlab') end def gitlab_options diff --git a/lib/gitlab/legacy_github_import/client.rb b/lib/gitlab/legacy_github_import/client.rb index 53c910d44bd..d8ed0ebca9d 100644 --- a/lib/gitlab/legacy_github_import/client.rb +++ b/lib/gitlab/legacy_github_import/client.rb @@ -83,7 +83,7 @@ module Gitlab end def config - Gitlab.config.omniauth.providers.find { |provider| provider.name == "github" } + Gitlab::Auth::OAuth::Provider.config_for('github') end def github_options diff --git a/lib/google_api/auth.rb b/lib/google_api/auth.rb index 99a82c849e0..1aeaa387a49 100644 --- a/lib/google_api/auth.rb +++ b/lib/google_api/auth.rb @@ -32,7 +32,7 @@ module GoogleApi private def config - Gitlab.config.omniauth.providers.find { |provider| provider.name == "google_oauth2" } + Gitlab::Auth::OAuth::Provider.config_for('google_oauth2') end def client diff --git a/spec/helpers/import_helper_spec.rb b/spec/helpers/import_helper_spec.rb index 9afff47f4e9..57d843c1be2 100644 --- a/spec/helpers/import_helper_spec.rb +++ b/spec/helpers/import_helper_spec.rb @@ -27,25 +27,48 @@ describe ImportHelper do describe '#provider_project_link' do context 'when provider is "github"' do + let(:github_server_url) { nil } + + before do + setting = Settingslogic.new('name' => 'github') + setting['url'] = github_server_url if github_server_url + + allow(Gitlab.config.omniauth).to receive(:providers).and_return([setting]) + end + context 'when provider does not specify a custom URL' do it 'uses default GitHub URL' do - allow(Gitlab.config.omniauth).to receive(:providers) - .and_return([Settingslogic.new('name' => 'github')]) - expect(helper.provider_project_link('github', 'octocat/Hello-World')) .to include('href="https://github.com/octocat/Hello-World"') end end context 'when provider specify a custom URL' do + let(:github_server_url) { 'https://github.company.com' } + it 'uses custom URL' do - allow(Gitlab.config.omniauth).to receive(:providers) - .and_return([Settingslogic.new('name' => 'github', 'url' => 'https://github.company.com')]) + expect(helper.provider_project_link('github', 'octocat/Hello-World')) + .to include('href="https://github.company.com/octocat/Hello-World"') + end + end + + context "when custom URL contains a '/' char at the end" do + let(:github_server_url) { 'https://github.company.com/' } + it "doesn't render double slash" do expect(helper.provider_project_link('github', 'octocat/Hello-World')) .to include('href="https://github.company.com/octocat/Hello-World"') end end + + context 'when provider is missing' do + it 'uses the default URL' do + allow(Gitlab.config.omniauth).to receive(:providers).and_return([]) + + expect(helper.provider_project_link('github', 'octocat/Hello-World')) + .to include('href="https://github.com/octocat/Hello-World"') + end + end end context 'when provider is "gitea"' do -- cgit v1.2.3