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
Boxiang Sun
gitlab-ce
Commits
e50606cd
Commit
e50606cd
authored
Apr 19, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor rake task to to import GitHub repositores
parent
09a6d328
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
92 deletions
+28
-92
lib/github/import.rb
lib/github/import.rb
+1
-0
lib/github/repositories.rb
lib/github/repositories.rb
+5
-3
lib/tasks/import.rake
lib/tasks/import.rake
+22
-89
No files found.
lib/github/import.rb
View file @
e50606cd
module
Github
class
Import
include
Gitlab
::
ShellAdapter
class
MergeRequest
<
::
MergeRequest
self
.
table_name
=
'merge_requests'
...
...
lib/github/repositories.rb
View file @
e50606cd
module
Github
class
Repositories
def
initialize
(
username
)
@username
=
username
attr_reader
:options
def
initialize
(
options
)
@options
=
options
end
def
fetch
Collection
.
new
.
fetch
(
repos_url
)
Collection
.
new
(
options
)
.
fetch
(
repos_url
)
end
private
...
...
lib/tasks/import.rake
View file @
e50606cd
require
'benchmark'
require
'rainbow/ext/string'
require_relative
'../gitlab/shell_adapter'
require_relative
'../gitlab/github_import/importer'
class
NewImporter
<
::
Gitlab
::
GithubImport
::
Importer
def
execute
# Same as ::Gitlab::GithubImport::Importer#execute, but showing some progress.
puts
'Importing repository...'
.
color
(
:aqua
)
import_repository
unless
project
.
repository_exists?
puts
'Importing labels...'
.
color
(
:aqua
)
import_labels
puts
'Importing milestones...'
.
color
(
:aqua
)
import_milestones
puts
'Importing pull requests...'
.
color
(
:aqua
)
import_pull_requests
puts
'Importing issues...'
.
color
(
:aqua
)
import_issues
puts
'Importing issue comments...'
.
color
(
:aqua
)
import_comments
(
:issues
)
puts
'Importing pull request comments...'
.
color
(
:aqua
)
import_comments
(
:pull_requests
)
puts
'Importing wiki...'
.
color
(
:aqua
)
import_wiki
# Gitea doesn't have a Release API yet
# See https://github.com/go-gitea/gitea/issues/330
unless
project
.
gitea_import?
import_releases
end
handle_errors
project
.
repository
.
after_import
project
.
import_finish
true
end
def
import_repository
begin
raise
'Blocked import URL.'
if
Gitlab
::
UrlBlocker
.
blocked_url?
(
project
.
import_url
)
project
.
create_repository
project
.
repository
.
add_remote
(
project
.
import_type
,
project
.
import_url
)
project
.
repository
.
set_remote_as_mirror
(
project
.
import_type
)
project
.
repository
.
fetch_remote
(
project
.
import_type
,
forced:
true
)
rescue
=>
e
# Expire cache to prevent scenarios such as:
# 1. First import failed, but the repo was imported successfully, so +exists?+ returns true
# 2. Retried import, repo is broken or not imported but +exists?+ still returns true
project
.
repository
.
expire_content_cache
if
project
.
repository_exists?
raise
"Error importing repository
#{
project
.
import_url
}
into
#{
project
.
path_with_namespace
}
-
#{
e
.
message
}
"
end
end
end
class
GithubImport
def
self
.
run!
(
*
args
)
...
...
@@ -69,14 +7,14 @@ class GithubImport
end
def
initialize
(
token
,
gitlab_username
,
project_path
,
extras
)
@
token
=
token
@
options
=
{
url:
'https://api.github.com'
,
token:
token
}
@project_path
=
project_path
@current_user
=
User
.
find_by_username
(
gitlab_username
)
@github_repo
=
extras
.
empty?
?
nil
:
extras
.
first
end
def
run!
@repo
=
GithubRepos
.
new
(
@
token
,
@current_user
,
@github_repo
).
choose_one!
@repo
=
GithubRepos
.
new
(
@
options
,
@current_user
,
@github_repo
).
choose_one!
raise
'No repo found!'
unless
@repo
...
...
@@ -90,25 +28,24 @@ class GithubImport
private
def
show_warning!
puts
"This will import GH
#{
@repo
.
full_name
.
bright
}
into GL
#{
@project_path
.
bright
}
as
#{
@current_user
.
name
}
"
puts
"This will import GH
#{
@repo
[
'full_name'
]
.
bright
}
into GL
#{
@project_path
.
bright
}
as
#{
@current_user
.
name
}
"
puts
"Permission checks are ignored. Press any key to continue."
.
color
(
:red
)
STDIN
.
getch
puts
'Starting the import
...
'
.
color
(
:green
)
puts
'Starting the import
(this could take a while)
'
.
color
(
:green
)
end
def
import!
import_url
=
@project
.
import_url
.
gsub
(
/\:\/\/(.*@)?/
,
"://
#{
@token
}
@"
)
@project
.
update
(
import_url:
import_url
)
@project
.
import_start
timings
=
Benchmark
.
measure
do
NewImporter
.
new
(
@project
).
execute
Github
::
Import
.
new
(
@project
,
@options
).
execute
end
puts
"Import finished. Timings:
#{
timings
}
"
.
color
(
:green
)
@project
.
import_finish
end
def
new_project
...
...
@@ -116,17 +53,17 @@ class GithubImport
namespace_path
,
_sep
,
name
=
@project_path
.
rpartition
(
'/'
)
namespace
=
find_or_create_namespace
(
namespace_path
)
Project
.
create!
(
import_url:
"https://
#{
@token
}
@github.com/
#{
@repo
.
full_name
}
.git"
,
Project
s
::
CreateService
.
new
(
@current_user
,
name:
name
,
path:
name
,
description:
@repo
.
description
,
namespace
:
namespace
,
description:
@repo
[
'description'
]
,
namespace
_id:
namespace
.
id
,
visibility_level:
visibility_level
,
import_type:
'github'
,
import_source:
@repo
.
full_name
,
creator:
@current_user
)
import_source:
@repo
[
'full_name'
]
,
skip_wiki:
@repo
[
'has_wiki'
]
)
.
execute
end
end
...
...
@@ -159,13 +96,13 @@ class GithubImport
end
def
visibility_level
@repo
.
private
?
Gitlab
::
VisibilityLevel
::
PRIVATE
:
current_application_settings
.
default_project_visibility
@repo
[
'private'
]
?
Gitlab
::
VisibilityLevel
::
PRIVATE
:
current_application_settings
.
default_project_visibility
end
end
class
GithubRepos
def
initialize
(
token
,
current_user
,
github_repo
)
@
token
=
token
def
initialize
(
options
,
current_user
,
github_repo
)
@
options
=
options
@current_user
=
current_user
@github_repo
=
github_repo
end
...
...
@@ -174,17 +111,17 @@ class GithubRepos
return
found_github_repo
if
@github_repo
repos
.
each
do
|
repo
|
print
"ID:
#{
repo
[
:id
].
to_s
.
bright
}
"
.
color
(
:green
)
p
uts
"- Name:
#{
repo
[
:full_name
]
}
"
.
color
(
:green
)
print
"ID:
#{
repo
[
'id'
].
to_s
.
bright
}
"
.
color
(
:green
)
p
rint
"
\t
Name:
#{
repo
[
'full_name'
]
}
\n
"
.
color
(
:green
)
end
print
'ID? '
.
bright
repos
.
find
{
|
repo
|
repo
[
:id
]
==
repo_id
}
repos
.
find
{
|
repo
|
repo
[
'id'
]
==
repo_id
}
end
def
found_github_repo
repos
.
find
{
|
repo
|
repo
[
:full_name
]
==
@github_repo
}
repos
.
find
{
|
repo
|
repo
[
'full_name'
]
==
@github_repo
}
end
def
repo_id
...
...
@@ -192,11 +129,7 @@ class GithubRepos
end
def
repos
@repos
||=
client
.
repos
end
def
client
@client
||=
Gitlab
::
GithubImport
::
Client
.
new
(
@token
,
{})
Github
::
Repositories
.
new
(
@options
).
fetch
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