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
4a3b895d
Commit
4a3b895d
authored
Apr 12, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Github import to avoid memory leak
parent
104144f3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
129 additions
and
58 deletions
+129
-58
lib/github/import.rb
lib/github/import.rb
+129
-58
No files found.
lib/github/import.rb
View file @
4a3b895d
...
@@ -9,6 +9,24 @@ module Github
...
@@ -9,6 +9,24 @@ module Github
self
.
reset_callbacks
:validate
self
.
reset_callbacks
:validate
end
end
class
Issue
<
::
Issue
self
.
table_name
=
'issues'
self
.
reset_callbacks
:save
self
.
reset_callbacks
:commit
self
.
reset_callbacks
:update
self
.
reset_callbacks
:validate
end
class
Note
<
::
Note
self
.
table_name
=
'notes'
self
.
reset_callbacks
:save
self
.
reset_callbacks
:commit
self
.
reset_callbacks
:update
self
.
reset_callbacks
:validate
end
attr_reader
:project
,
:repository
,
:cached_user_ids
,
:errors
attr_reader
:project
,
:repository
,
:cached_user_ids
,
:errors
def
initialize
(
project
)
def
initialize
(
project
)
...
@@ -22,7 +40,7 @@ module Github
...
@@ -22,7 +40,7 @@ module Github
# Fetch repository
# 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://
881a01d03026458e51285a4c7038c9fe4daa5561
@github.com/
#{
owner
}
/
#{
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
)
project
.
repository
.
remove_remote
(
'github'
)
project
.
repository
.
remove_remote
(
'github'
)
...
@@ -31,74 +49,127 @@ module Github
...
@@ -31,74 +49,127 @@ module Github
end
end
# Fetch labels
# Fetch labels
labels
=
Github
::
Labels
.
new
(
owner
,
repo
).
fetch
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/labels"
labels
.
each
do
|
raw
|
loop
do
begin
response
=
Github
::
Client
.
new
.
get
(
url
)
label
=
Github
::
Representation
::
Label
.
new
(
raw
)
project
.
labels
.
create!
(
title:
label
.
title
,
color:
label
.
color
)
response
.
body
.
each
do
|
raw
|
rescue
=>
e
begin
error
(
:label
,
label
.
url
,
e
.
message
)
label
=
Github
::
Representation
::
Label
.
new
(
raw
)
next
if
project
.
labels
.
where
(
title:
label
.
title
).
exists?
project
.
labels
.
create!
(
title:
label
.
title
,
color:
label
.
color
)
rescue
=>
e
error
(
:label
,
label
.
url
,
e
.
message
)
end
end
end
break
unless
url
=
response
.
rels
[
:next
]
end
end
# Fetch milestones
# Fetch milestones
milestones
=
Github
::
Milestones
.
new
(
owner
,
repo
).
fetch
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/milestones"
milestones
.
each
do
|
raw
|
loop
do
begin
response
=
Github
::
Client
.
new
.
get
(
url
,
state: :all
)
milestone
=
Github
::
Representation
::
Milestone
.
new
(
raw
)
response
.
body
.
each
do
|
raw
|
project
.
milestones
.
create!
(
begin
iid:
milestone
.
iid
,
milestone
=
Github
::
Representation
::
Milestone
.
new
(
raw
)
title:
milestone
.
title
,
next
if
project
.
milestones
.
where
(
iid:
milestone
.
iid
).
exists?
description:
milestone
.
description
,
due_date:
milestone
.
due_date
,
project
.
milestones
.
create!
(
state:
milestone
.
state
,
iid:
milestone
.
iid
,
created_at:
milestone
.
created_at
,
title:
milestone
.
title
,
updated_at:
milestone
.
updated_at
description:
milestone
.
description
,
)
due_date:
milestone
.
due_date
,
rescue
=>
e
state:
milestone
.
state
,
error
(
:milestone
,
milestone
.
url
,
e
.
message
)
created_at:
milestone
.
created_at
,
updated_at:
milestone
.
updated_at
)
rescue
=>
e
error
(
:milestone
,
milestone
.
url
,
e
.
message
)
end
end
end
break
unless
url
=
response
.
rels
[
:next
]
end
end
# Fetch pull requests
# Fetch pull requests
pull_requests
=
Github
::
PullRequests
.
new
(
owner
,
repo
).
fetch
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/pulls"
pull_requests
.
each
do
|
raw
|
loop
do
pull_request
=
Github
::
Representation
::
PullRequest
.
new
(
project
,
raw
)
response
=
Github
::
Client
.
new
.
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
next
unless
pull_request
.
valid?
response
.
body
.
each
do
|
raw
|
begin
pull_request
=
Github
::
Representation
::
PullRequest
.
new
(
project
,
raw
)
restore_source_branch
(
pull_request
)
unless
pull_request
.
source_branch_exists?
merge_request
=
MergeRequest
.
find_or_initialize_by
(
iid:
pull_request
.
iid
,
source_project_id:
project
.
id
)
restore_target_branch
(
pull_request
)
unless
pull_request
.
target_branch_exists?
next
unless
merge_request
.
new_record?
&&
pull_request
.
valid?
merge_request
=
MergeRequest
.
find_or_initialize_by
(
iid:
pull_request
.
iid
,
source_project_id:
project
.
id
)
do
|
record
|
begin
record
.
iid
=
pull_request
.
iid
restore_source_branch
(
pull_request
)
unless
pull_request
.
source_branch_exists?
record
.
title
=
pull_request
.
title
restore_target_branch
(
pull_request
)
unless
pull_request
.
target_branch_exists?
record
.
description
=
pull_request
.
description
record
.
source_project
=
pull_request
.
source_project
merge_request
.
iid
=
pull_request
.
iid
record
.
source_branch
=
pull_request
.
source_branch_name
merge_request
.
title
=
pull_request
.
title
record
.
source_branch_sha
=
pull_request
.
source_branch_sha
merge_request
.
description
=
pull_request
.
description
record
.
target_project
=
pull_request
.
target_project
merge_request
.
source_project
=
pull_request
.
source_project
record
.
target_branch
=
pull_request
.
target_branch_name
merge_request
.
source_branch
=
pull_request
.
source_branch_name
record
.
target_branch_sha
=
pull_request
.
target_branch_sha
merge_request
.
source_branch_sha
=
pull_request
.
source_branch_sha
record
.
state
=
pull_request
.
state
merge_request
.
target_project
=
pull_request
.
target_project
record
.
milestone_id
=
milestone_id
(
pull_request
.
milestone
)
merge_request
.
target_branch
=
pull_request
.
target_branch_name
record
.
author_id
=
user_id
(
pull_request
.
author
,
project
.
creator_id
)
merge_request
.
target_branch_sha
=
pull_request
.
target_branch_sha
record
.
assignee_id
=
user_id
(
pull_request
.
assignee
)
merge_request
.
state
=
pull_request
.
state
record
.
created_at
=
pull_request
.
created_at
merge_request
.
milestone_id
=
milestone_id
(
pull_request
.
milestone
)
record
.
updated_at
=
pull_request
.
updated_at
merge_request
.
author_id
=
user_id
(
pull_request
.
author
,
project
.
creator_id
)
merge_request
.
assignee_id
=
user_id
(
pull_request
.
assignee
)
merge_request
.
created_at
=
pull_request
.
created_at
merge_request
.
updated_at
=
pull_request
.
updated_at
merge_request
.
save
(
validate:
false
)
merge_request
.
merge_request_diffs
.
create
rescue
=>
e
error
(
:pull_request
,
pull_request
.
url
,
e
.
message
)
ensure
clean_up_restored_branches
(
pull_request
)
end
end
end
break
unless
url
=
response
.
rels
[
:next
]
end
merge_request
.
save
(
validate:
false
)
# Fetch issues
merge_request
.
merge_request_diffs
.
create
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues"
rescue
=>
e
error
(
:pull_request
,
pull_request
.
url
,
"
#{
e
.
message
}
\n\n
#{
e
.
exception
.
backtrace
.
join
(
'\n'
)
}
"
)
loop
do
ensure
response
=
Github
::
Client
.
new
.
get
(
url
,
state: :all
,
sort: :created
,
direction: :asc
)
clean_up_restored_branches
(
pull_request
)
response
.
body
.
each
do
|
raw
|
representation
=
Github
::
Representation
::
Issue
.
new
(
raw
)
next
if
representation
.
pull_request?
next
if
Issue
.
where
(
iid:
representation
.
iid
,
project_id:
project
.
id
).
exists?
begin
issue
=
Issue
.
new
issue
.
iid
=
representation
.
iid
issue
.
project_id
=
project
.
id
issue
.
title
=
representation
.
title
issue
.
description
=
representation
.
description
issue
.
state
=
representation
.
state
issue
.
milestone_id
=
milestone_id
(
representation
.
milestone
)
issue
.
author_id
=
user_id
(
representation
.
author
,
project
.
creator_id
)
issue
.
assignee_id
=
user_id
(
representation
.
assignee
)
issue
.
created_at
=
representation
.
created_at
issue
.
updated_at
=
representation
.
updated_at
issue
.
save
(
validate:
false
)
rescue
=>
e
error
(
:issue
,
representation
.
url
,
e
.
message
)
end
end
end
break
unless
url
=
response
.
rels
[
:next
]
end
end
repository
.
expire_content_cache
repository
.
expire_content_cache
...
...
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