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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
3aa89795
Commit
3aa89795
authored
Apr 19, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Github import
parent
a7cb336e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
25 deletions
+38
-25
lib/github/import.rb
lib/github/import.rb
+38
-25
No files found.
lib/github/import.rb
View file @
3aa89795
...
@@ -27,12 +27,13 @@ module Github
...
@@ -27,12 +27,13 @@ module Github
self
.
reset_callbacks
:validate
self
.
reset_callbacks
:validate
end
end
attr_reader
:project
,
:repository
,
:
options
,
:cached_label_id
s
,
attr_reader
:project
,
:repository
,
:
repo
,
:options
,
:error
s
,
:cached_
gitlab_users
,
:cached_user_ids
,
:error
s
:cached_
label_ids
,
:cached_gitlab_users
,
:cached_user_id
s
def
initialize
(
project
,
options
)
def
initialize
(
project
,
options
)
@project
=
project
@project
=
project
@repository
=
project
.
repository
@repository
=
project
.
repository
@repo
=
project
.
import_source
@options
=
options
@options
=
options
@cached_label_ids
=
{}
@cached_label_ids
=
{}
@cached_user_ids
=
{}
@cached_user_ids
=
{}
...
@@ -40,19 +41,32 @@ module Github
...
@@ -40,19 +41,32 @@ module Github
@errors
=
[]
@errors
=
[]
end
end
def
execute
(
owner
,
repo
)
def
execute
# Fetch repository
fetch_repository
fetch_labels
fetch_milestones
fetch_pull_requests
fetch_issues
expire_repository_cache
errors
end
private
def
fetch_repository
begin
begin
project
.
create_repository
project
.
create_repository
project
.
repository
.
add_remote
(
'github'
,
"https://{token}@github.com/
#{
owner
}
/
#{
repo
}
.git"
)
project
.
repository
.
add_remote
(
'github'
,
"https://{token}@github.com/
#{
repo
}
.git"
)
project
.
repository
.
set_remote_as_mirror
(
'github'
)
project
.
repository
.
set_remote_as_mirror
(
'github'
)
project
.
repository
.
fetch_remote
(
'github'
,
forced:
true
)
project
.
repository
.
fetch_remote
(
'github'
,
forced:
true
)
rescue
Gitlab
::
Shell
::
Error
=>
e
rescue
Gitlab
::
Shell
::
Error
=>
e
error
(
:project
,
"https://github.com/
#{
owner
}
/
#{
repo
}
.git"
,
e
.
message
)
error
(
:project
,
"https://github.com/
#{
repo
}
.git"
,
e
.
message
)
end
end
end
# Fetch
labels
def
fetch_
labels
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/labels"
url
=
"/repos/
#{
repo
}
/labels"
while
url
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
)
response
=
Github
::
Client
.
new
(
options
).
get
(
url
)
...
@@ -72,13 +86,13 @@ module Github
...
@@ -72,13 +86,13 @@ module Github
end
end
# Cache labels
# Cache labels
# TODO: we should take group labels in account
project
.
labels
.
select
(
:id
,
:title
).
find_each
do
|
label
|
project
.
labels
.
select
(
:id
,
:title
).
find_each
do
|
label
|
@cached_label_ids
[
label
.
title
]
=
label
.
id
@cached_label_ids
[
label
.
title
]
=
label
.
id
end
end
end
# Fetch
milestones
def
fetch_
milestones
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/milestones"
url
=
"/repos/
#{
repo
}
/milestones"
while
url
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
)
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
)
...
@@ -104,9 +118,10 @@ module Github
...
@@ -104,9 +118,10 @@ module Github
url
=
response
.
rels
[
:next
]
url
=
response
.
rels
[
:next
]
end
end
end
# Fetch pull
requests
def
fetch_pull_
requests
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/pulls"
url
=
"/repos/
#{
repo
}
/pulls"
while
url
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
...
@@ -141,14 +156,13 @@ module Github
...
@@ -141,14 +156,13 @@ module Github
merge_request
.
merge_request_diffs
.
create
merge_request
.
merge_request_diffs
.
create
# Fetch review comments
# Fetch review comments
review_comments_url
=
"/repos/
#{
owner
}
/
#{
repo
}
/pulls/
#{
pull_request
.
iid
}
/comments"
review_comments_url
=
"/repos/
#{
repo
}
/pulls/
#{
pull_request
.
iid
}
/comments"
fetch_comments
(
merge_request
,
:review_comment
,
review_comments_url
)
fetch_comments
(
merge_request
,
:review_comment
,
review_comments_url
)
# Fetch comments
# Fetch comments
comments_url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues/
#{
pull_request
.
iid
}
/comments"
comments_url
=
"/repos/
#{
repo
}
/issues/
#{
pull_request
.
iid
}
/comments"
fetch_comments
(
merge_request
,
:comment
,
comments_url
)
fetch_comments
(
merge_request
,
:comment
,
comments_url
)
rescue
=>
e
rescue
=>
e
error
(
:pull_request
,
pull_request
.
url
,
e
.
message
)
ensure
ensure
clean_up_restored_branches
(
pull_request
)
clean_up_restored_branches
(
pull_request
)
end
end
...
@@ -156,9 +170,10 @@ module Github
...
@@ -156,9 +170,10 @@ module Github
url
=
response
.
rels
[
:next
]
url
=
response
.
rels
[
:next
]
end
end
end
# Fetch
issues
def
fetch_
issues
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues"
url
=
"/repos/
#{
repo
}
/issues"
while
url
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
...
@@ -196,7 +211,7 @@ module Github
...
@@ -196,7 +211,7 @@ module Github
if
representation
.
has_comments?
if
representation
.
has_comments?
# Fetch comments
# Fetch comments
comments_url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues/
#{
issue
.
iid
}
/comments"
comments_url
=
"/repos/
#{
repo
}
/issues/
#{
issue
.
iid
}
/comments"
fetch_comments
(
issue
,
:comment
,
comments_url
)
fetch_comments
(
issue
,
:comment
,
comments_url
)
end
end
end
end
...
@@ -207,14 +222,8 @@ module Github
...
@@ -207,14 +222,8 @@ module Github
url
=
response
.
rels
[
:next
]
url
=
response
.
rels
[
:next
]
end
end
repository
.
expire_content_cache
errors
end
end
private
def
fetch_comments
(
noteable
,
type
,
url
)
def
fetch_comments
(
noteable
,
type
,
url
)
while
url
while
url
comments
=
Github
::
Client
.
new
(
options
).
get
(
url
)
comments
=
Github
::
Client
.
new
(
options
).
get
(
url
)
...
@@ -310,6 +319,10 @@ module Github
...
@@ -310,6 +319,10 @@ module Github
"*Created by:
#{
author
.
username
}
*
\n\n
#{
body
}
"
"*Created by:
#{
author
.
username
}
*
\n\n
#{
body
}
"
end
end
def
expire_repository_cache
repository
.
expire_content_cache
end
def
error
(
type
,
url
,
message
)
def
error
(
type
,
url
,
message
)
errors
<<
{
type:
type
,
url:
Gitlab
::
UrlSanitizer
.
sanitize
(
url
),
error:
message
}
errors
<<
{
type:
type
,
url:
Gitlab
::
UrlSanitizer
.
sanitize
(
url
),
error:
message
}
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