Commit b3c90dd5 authored by Valery Sizov's avatar Valery Sizov

GitHub importer refactoring

parent 85c2cb2a
...@@ -4,16 +4,16 @@ class Import::GithubController < Import::BaseController ...@@ -4,16 +4,16 @@ class Import::GithubController < Import::BaseController
rescue_from Octokit::Unauthorized, with: :github_unauthorized rescue_from Octokit::Unauthorized, with: :github_unauthorized
def callback 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.github_access_token = token
current_user.save current_user.save
redirect_to status_import_github_url redirect_to status_import_github_url
end end
def status def status
@repos = octo_client.repos @repos = client.repos
octo_client.orgs.each do |org| client.orgs.each do |org|
@repos += octo_client.repos(org.login) @repos += client.repos(org.login)
end end
@already_added_projects = current_user.created_projects.where(import_type: "github") @already_added_projects = current_user.created_projects.where(import_type: "github")
...@@ -29,7 +29,7 @@ class Import::GithubController < Import::BaseController ...@@ -29,7 +29,7 @@ class Import::GithubController < Import::BaseController
def create def create
@repo_id = params[:repo_id].to_i @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 @target_namespace = params[:new_namespace].presence || repo.owner.login
@project_name = repo.name @project_name = repo.name
...@@ -41,12 +41,7 @@ class Import::GithubController < Import::BaseController ...@@ -41,12 +41,7 @@ class Import::GithubController < Import::BaseController
private private
def client def client
@client ||= Gitlab::GithubImport::Client.new.client @client ||= Gitlab::GithubImport::Client.new(current_user.github_access_token)
end
def octo_client
Octokit.auto_paginate = true
@octo_client ||= Octokit::Client.new(access_token: current_user.github_access_token)
end end
def github_auth def github_auth
...@@ -56,10 +51,7 @@ class Import::GithubController < Import::BaseController ...@@ -56,10 +51,7 @@ class Import::GithubController < Import::BaseController
end end
def go_to_github_for_permissions def go_to_github_for_permissions
redirect_to client.auth_code.authorize_url({ redirect_to client.authorize_url(callback_import_github_url)
redirect_uri: callback_import_github_url,
scope: "repo, user, user:email"
})
end end
def github_unauthorized def github_unauthorized
......
module Gitlab module Gitlab
module GithubImport module GithubImport
class Client class Client
attr_reader :client attr_reader :client, :api
def initialize def initialize(access_token)
@client = ::OAuth2::Client.new( @client = ::OAuth2::Client.new(
config.app_id, config.app_id,
config.app_secret, config.app_secret,
github_options 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 end
private private
......
module Gitlab module Gitlab
module GithubImport module GithubImport
class Importer class Importer
attr_reader :project attr_reader :project, :client
def initialize(project) def initialize(project)
@project = project @project = project
@client = Client.new(project.creator.github_access_token)
@formatter = Gitlab::ImportFormatter.new @formatter = Gitlab::ImportFormatter.new
end end
def execute def execute
client = octo_client(project.creator.github_access_token)
#Issues && Comments #Issues && Comments
client.list_issues(project.import_source, state: :all).each do |issue| client.list_issues(project.import_source, state: :all).each do |issue|
if issue.pull_request.nil? if issue.pull_request.nil?
...@@ -37,11 +36,6 @@ module Gitlab ...@@ -37,11 +36,6 @@ module Gitlab
private 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) def gl_user_id(project, github_id)
user = User.joins(:identities). user = User.joins(:identities).
find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s) find_by("identities.extern_uid = ? AND identities.provider = 'github'", github_id.to_s)
......
...@@ -10,7 +10,7 @@ describe Import::GithubController do ...@@ -10,7 +10,7 @@ describe Import::GithubController do
describe "GET callback" do describe "GET callback" do
it "updates access token" do it "updates access token" do
token = "asdasd12345" 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") Gitlab.config.omniauth.providers << OpenStruct.new(app_id: "asd123", app_secret: "asd123", name: "github")
get :callback get :callback
...@@ -27,8 +27,8 @@ describe Import::GithubController do ...@@ -27,8 +27,8 @@ describe Import::GithubController do
it "assigns variables" do it "assigns variables" do
@project = create(:project, import_type: 'github', creator_id: user.id) @project = create(:project, import_type: 'github', creator_id: user.id)
controller.stub_chain(:octo_client, :repos).and_return([@repo]) controller.stub_chain(:client, :repos).and_return([@repo])
controller.stub_chain(:octo_client, :orgs).and_return([]) controller.stub_chain(:client, :orgs).and_return([])
get :status get :status
...@@ -38,8 +38,8 @@ describe Import::GithubController do ...@@ -38,8 +38,8 @@ describe Import::GithubController do
it "does not show already added project" do it "does not show already added project" do
@project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim') @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(:client, :repos).and_return([@repo])
controller.stub_chain(:octo_client, :orgs).and_return([]) controller.stub_chain(:client, :orgs).and_return([])
get :status get :status
...@@ -57,7 +57,7 @@ describe Import::GithubController do ...@@ -57,7 +57,7 @@ describe Import::GithubController do
namespace = create(:namespace, name: "john", owner: user) namespace = create(:namespace, name: "john", owner: user)
Gitlab::GithubImport::ProjectCreator.should_receive(:new).with(@repo, namespace, user). Gitlab::GithubImport::ProjectCreator.should_receive(:new).with(@repo, namespace, user).
and_return(double(execute: true)) 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 post :create, format: :js
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment