Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
b3c90dd5
Commit
b3c90dd5
authored
Feb 05, 2015
by
Valery Sizov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GitHub importer refactoring
parent
85c2cb2a
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
31 deletions
+45
-31
app/controllers/import/github_controller.rb
app/controllers/import/github_controller.rb
+7
-15
lib/gitlab/github_import/client.rb
lib/gitlab/github_import/client.rb
+30
-2
lib/gitlab/github_import/importer.rb
lib/gitlab/github_import/importer.rb
+2
-8
spec/controllers/import/github_controller_spec.rb
spec/controllers/import/github_controller_spec.rb
+6
-6
No files found.
app/controllers/import/github_controller.rb
View file @
b3c90dd5
...
@@ -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
...
...
lib/gitlab/github_import/client.rb
View file @
b3c90dd5
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
...
...
lib/gitlab/github_import/importer.rb
View file @
b3c90dd5
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
)
...
...
spec/controllers/import/github_controller_spec.rb
View file @
b3c90dd5
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment