mirror of
https://gitlab.com/gitlab-org/gitlab-foss.git
synced 2025-08-20 14:11:11 +00:00
Merge branch 'refactor_import' into 'master'
GitHub importer refactoring See merge request !1477
This commit is contained in:
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user