summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Sizov <valery@gitlab.com>2015-02-06 05:51:02 +0000
committerValery Sizov <valery@gitlab.com>2015-02-06 05:51:02 +0000
commitbc57ff0ef023db3d07f1adfa6a309fb4a24ed203 (patch)
tree48135878c66cfdca4a5c1c996cd8e210bf32e157
parent85c2cb2a1980d494aef198c59e4f3859b2668ba3 (diff)
parentb3c90dd51418d0c41df4ccd57d9480ea44b35eec (diff)
downloadgitlab-ce-bc57ff0ef023db3d07f1adfa6a309fb4a24ed203.tar.gz
Merge branch 'refactor_import' into 'master'
GitHub importer refactoring See merge request !1477
-rw-r--r--app/controllers/import/github_controller.rb22
-rw-r--r--lib/gitlab/github_import/client.rb32
-rw-r--r--lib/gitlab/github_import/importer.rb10
-rw-r--r--spec/controllers/import/github_controller_spec.rb12
4 files changed, 45 insertions, 31 deletions
diff --git a/app/controllers/import/github_controller.rb b/app/controllers/import/github_controller.rb
index 108fc4396a6..c869c7c86f3 100644
--- a/app/controllers/import/github_controller.rb
+++ b/app/controllers/import/github_controller.rb
@@ -4,16 +4,16 @@ class Import::GithubController < Import::BaseController
rescue_from Octokit::Unauthorized, with: :github_unauthorized
def callback
- token = client.auth_code.get_token(params[:code]).token
+ token = client.get_token(params[:code])
current_user.github_access_token = token
current_user.save
redirect_to status_import_github_url
end
def status
- @repos = octo_client.repos
- octo_client.orgs.each do |org|
- @repos += octo_client.repos(org.login)
+ @repos = client.repos
+ client.orgs.each do |org|
+ @repos += client.repos(org.login)
end
@already_added_projects = current_user.created_projects.where(import_type: "github")
@@ -29,7 +29,7 @@ class Import::GithubController < Import::BaseController
def create
@repo_id = params[:repo_id].to_i
- repo = octo_client.repo(@repo_id)
+ repo = client.repo(@repo_id)
@target_namespace = params[:new_namespace].presence || repo.owner.login
@project_name = repo.name
@@ -41,12 +41,7 @@ class Import::GithubController < Import::BaseController
private
def client
- @client ||= Gitlab::GithubImport::Client.new.client
- end
-
- def octo_client
- Octokit.auto_paginate = true
- @octo_client ||= Octokit::Client.new(access_token: current_user.github_access_token)
+ @client ||= Gitlab::GithubImport::Client.new(current_user.github_access_token)
end
def github_auth
@@ -56,10 +51,7 @@ class Import::GithubController < Import::BaseController
end
def go_to_github_for_permissions
- redirect_to client.auth_code.authorize_url({
- redirect_uri: callback_import_github_url,
- scope: "repo, user, user:email"
- })
+ redirect_to client.authorize_url(callback_import_github_url)
end
def github_unauthorized
diff --git a/lib/gitlab/github_import/client.rb b/lib/gitlab/github_import/client.rb
index cf43d36c6c3..c9904fe8779 100644
--- a/lib/gitlab/github_import/client.rb
+++ b/lib/gitlab/github_import/client.rb
@@ -1,14 +1,42 @@
module Gitlab
module GithubImport
class Client
- attr_reader :client
+ attr_reader :client, :api
- def initialize
+ def initialize(access_token)
@client = ::OAuth2::Client.new(
config.app_id,
config.app_secret,
github_options
)
+
+ if access_token
+ ::Octokit.auto_paginate = true
+ @api = ::Octokit::Client.new(access_token: access_token)
+ end
+ end
+
+ def authorize_url(redirect_uri)
+ client.auth_code.authorize_url({
+ redirect_uri: redirect_uri,
+ scope: "repo, user, user:email"
+ })
+ end
+
+ def get_token(code)
+ client.auth_code.get_token(code).token
+ end
+
+ def method_missing(method, *args, &block)
+ if api.respond_to?(method)
+ api.send(method, *args, &block)
+ else
+ super(method, *args, &block)
+ end
+ end
+
+ def respond_to?(method)
+ api.respond_to?(method) || super
end
private
diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb
index 1f02ee49b6a..bc2b645b2d9 100644
--- a/lib/gitlab/github_import/importer.rb
+++ b/lib/gitlab/github_import/importer.rb
@@ -1,16 +1,15 @@
module Gitlab
module GithubImport
class Importer
- attr_reader :project
+ attr_reader :project, :client
def initialize(project)
@project = project
+ @client = Client.new(project.creator.github_access_token)
@formatter = Gitlab::ImportFormatter.new
end
def execute
- client = octo_client(project.creator.github_access_token)
-
#Issues && Comments
client.list_issues(project.import_source, state: :all).each do |issue|
if issue.pull_request.nil?
@@ -37,11 +36,6 @@ module Gitlab
private
- def octo_client(access_token)
- ::Octokit.auto_paginate = true
- ::Octokit::Client.new(access_token: access_token)
- end
-
def gl_user_id(project, github_id)
user = User.joins(:identities).
find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s)
diff --git a/spec/controllers/import/github_controller_spec.rb b/spec/controllers/import/github_controller_spec.rb
index 01063567733..f80b3884d88 100644
--- a/spec/controllers/import/github_controller_spec.rb
+++ b/spec/controllers/import/github_controller_spec.rb
@@ -10,7 +10,7 @@ describe Import::GithubController do
describe "GET callback" do
it "updates access token" do
token = "asdasd12345"
- Gitlab::GithubImport::Client.any_instance.stub_chain(:client, :auth_code, :get_token, :token).and_return(token)
+ Gitlab::GithubImport::Client.any_instance.stub(:get_token).and_return(token)
Gitlab.config.omniauth.providers << OpenStruct.new(app_id: "asd123", app_secret: "asd123", name: "github")
get :callback
@@ -27,8 +27,8 @@ describe Import::GithubController do
it "assigns variables" do
@project = create(:project, import_type: 'github', creator_id: user.id)
- controller.stub_chain(:octo_client, :repos).and_return([@repo])
- controller.stub_chain(:octo_client, :orgs).and_return([])
+ controller.stub_chain(:client, :repos).and_return([@repo])
+ controller.stub_chain(:client, :orgs).and_return([])
get :status
@@ -38,8 +38,8 @@ describe Import::GithubController do
it "does not show already added project" do
@project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim')
- controller.stub_chain(:octo_client, :repos).and_return([@repo])
- controller.stub_chain(:octo_client, :orgs).and_return([])
+ controller.stub_chain(:client, :repos).and_return([@repo])
+ controller.stub_chain(:client, :orgs).and_return([])
get :status
@@ -57,7 +57,7 @@ describe Import::GithubController do
namespace = create(:namespace, name: "john", owner: user)
Gitlab::GithubImport::ProjectCreator.should_receive(:new).with(@repo, namespace, user).
and_return(double(execute: true))
- controller.stub_chain(:octo_client, :repo).and_return(@repo)
+ controller.stub_chain(:client, :repo).and_return(@repo)
post :create, format: :js
end