diff --git a/app/actions/providers/create_github_provider.rb b/app/actions/providers/create_github_provider.rb index db5023db..fdecb90c 100644 --- a/app/actions/providers/create_github_provider.rb +++ b/app/actions/providers/create_github_provider.rb @@ -6,11 +6,10 @@ class Providers::CreateGithubProvider promises :provider executed do |context| - client_options = { access_token: context.provider.access_token } - if context.provider.enterprise? - client_options[:api_endpoint] = "#{context.provider.api_base_url}/api/v3/" - end - client = Octokit::Client.new(client_options) + client = Git::Github::Client.build_client( + access_token: context.provider.access_token, + api_base_url: context.provider.api_base_url + ) username = client.user[:login] context.provider.auth = { info: { diff --git a/app/controllers/integrations/github/repositories_controller.rb b/app/controllers/integrations/github/repositories_controller.rb index 020da3dd..5121926e 100644 --- a/app/controllers/integrations/github/repositories_controller.rb +++ b/app/controllers/integrations/github/repositories_controller.rb @@ -1,6 +1,10 @@ class Integrations::Github::RepositoriesController < ApplicationController def index - client = Octokit::Client.new(access_token: current_account.github_provider.access_token) + provider = current_account.github_provider + client = Git::Github::Client.build_client( + access_token: provider.access_token, + api_base_url: provider.api_base_url + ) if params[:q].present? client.auto_paginate = true @repositories = client.repos(current_account.github_username) diff --git a/app/services/git/github/client.rb b/app/services/git/github/client.rb index 60e5892e..fc59a833 100644 --- a/app/services/git/github/client.rb +++ b/app/services/git/github/client.rb @@ -12,6 +12,14 @@ class Git::Github::Client < Git::Client ) end + def self.build_client(access_token:, api_base_url: nil) + client_options = { access_token: } + if api_base_url && api_base_url != "https://api.github.com" + client_options[:api_endpoint] = "#{api_base_url}/api/v3/" + end + Octokit::Client.new(client_options) + end + def commits(branch) client.commits(repository_url, branch).map do |commit| Git::Common::Commit.new( @@ -29,11 +37,7 @@ class Git::Github::Client < Git::Client end def initialize(access_token:, repository_url:, api_base_url: nil) - client_options = { access_token: } - if api_base_url && api_base_url != "https://api.github.com" - client_options[:api_endpoint] = "#{api_base_url}/api/v3/" - end - @client = Octokit::Client.new(client_options) + @client = self.class.build_client(access_token:, api_base_url:) @repository_url = repository_url end diff --git a/spec/actions/providers/create_spec.rb b/spec/actions/providers/create_spec.rb index 8142f126..f08e1ba4 100644 --- a/spec/actions/providers/create_spec.rb +++ b/spec/actions/providers/create_spec.rb @@ -22,7 +22,7 @@ RSpec.describe Providers::Create do let(:provider) { build(:provider, :github) } context 'when the access token is valid' do before do - allow(Octokit::Client).to receive(:new).and_return(double(user: { login: 'test_user' }, scopes: [ 'repo', 'write:packages' ])) + allow(Git::Github::Client).to receive(:build_client).and_return(double(user: { login: 'test_user' }, scopes: [ 'repo', 'write:packages' ])) end it 'sets the provider auth info' do @@ -37,7 +37,7 @@ RSpec.describe Providers::Create do context 'when the access token is invalid' do before do - allow(Octokit::Client).to receive(:new).and_raise(Octokit::Unauthorized) + allow(Git::Github::Client).to receive(:build_client).and_raise(Octokit::Unauthorized) end it 'adds an error to the provider' do @@ -48,7 +48,7 @@ RSpec.describe Providers::Create do context 'when the scopes are invalid' do before do - allow(Octokit::Client).to receive(:new).and_return(double(user: { login: 'test_user' }, scopes: [ 'repo' ])) + allow(Git::Github::Client).to receive(:build_client).and_return(double(user: { login: 'test_user' }, scopes: [ 'repo' ])) end it 'fails the context with an invalid scopes message' do