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
Jérome Perrin
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
self
.
reset_callbacks
:validate
end
attr_reader
:project
,
:repository
,
:
options
,
:cached_label_id
s
,
:cached_
gitlab_users
,
:cached_user_ids
,
:error
s
attr_reader
:project
,
:repository
,
:
repo
,
:options
,
:error
s
,
:cached_
label_ids
,
:cached_gitlab_users
,
:cached_user_id
s
def
initialize
(
project
,
options
)
@project
=
project
@repository
=
project
.
repository
@repo
=
project
.
import_source
@options
=
options
@cached_label_ids
=
{}
@cached_user_ids
=
{}
...
...
@@ -40,19 +41,32 @@ module Github
@errors
=
[]
end
def
execute
(
owner
,
repo
)
# Fetch repository
def
execute
fetch_repository
fetch_labels
fetch_milestones
fetch_pull_requests
fetch_issues
expire_repository_cache
errors
end
private
def
fetch_repository
begin
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
.
fetch_remote
(
'github'
,
forced:
true
)
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
# Fetch
labels
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/labels"
def
fetch_
labels
url
=
"/repos/
#{
repo
}
/labels"
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
)
...
...
@@ -72,13 +86,13 @@ module Github
end
# Cache labels
# TODO: we should take group labels in account
project
.
labels
.
select
(
:id
,
:title
).
find_each
do
|
label
|
@cached_label_ids
[
label
.
title
]
=
label
.
id
end
end
# Fetch
milestones
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/milestones"
def
fetch_
milestones
url
=
"/repos/
#{
repo
}
/milestones"
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
)
...
...
@@ -104,9 +118,10 @@ module Github
url
=
response
.
rels
[
:next
]
end
end
# Fetch pull
requests
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/pulls"
def
fetch_pull_
requests
url
=
"/repos/
#{
repo
}
/pulls"
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
...
...
@@ -141,14 +156,13 @@ module Github
merge_request
.
merge_request_diffs
.
create
# 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
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
)
rescue
=>
e
error
(
:pull_request
,
pull_request
.
url
,
e
.
message
)
ensure
clean_up_restored_branches
(
pull_request
)
end
...
...
@@ -156,9 +170,10 @@ module Github
url
=
response
.
rels
[
:next
]
end
end
# Fetch
issues
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues"
def
fetch_
issues
url
=
"/repos/
#{
repo
}
/issues"
while
url
response
=
Github
::
Client
.
new
(
options
).
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
...
...
@@ -196,7 +211,7 @@ module Github
if
representation
.
has_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
)
end
end
...
...
@@ -207,14 +222,8 @@ module Github
url
=
response
.
rels
[
:next
]
end
repository
.
expire_content_cache
errors
end
private
def
fetch_comments
(
noteable
,
type
,
url
)
while
url
comments
=
Github
::
Client
.
new
(
options
).
get
(
url
)
...
...
@@ -310,6 +319,10 @@ module Github
"*Created by:
#{
author
.
username
}
*
\n\n
#{
body
}
"
end
def
expire_repository_cache
repository
.
expire_content_cache
end
def
error
(
type
,
url
,
message
)
errors
<<
{
type:
type
,
url:
Gitlab
::
UrlSanitizer
.
sanitize
(
url
),
error:
message
}
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