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
8ab13e14
Commit
8ab13e14
authored
Jul 05, 2018
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Batch re-creating branches and fetching the remote
parent
cc4a39cc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
24 deletions
+51
-24
lib/gitlab/bitbucket_server_import/importer.rb
lib/gitlab/bitbucket_server_import/importer.rb
+51
-24
No files found.
lib/gitlab/bitbucket_server_import/importer.rb
View file @
8ab13e14
...
@@ -5,6 +5,7 @@ module Gitlab
...
@@ -5,6 +5,7 @@ module Gitlab
attr_reader
:project
,
:project_key
,
:repository_slug
,
:client
,
:errors
,
:users
attr_reader
:project
,
:project_key
,
:repository_slug
,
:client
,
:errors
,
:users
REMOTE_NAME
=
'bitbucket_server'
.
freeze
REMOTE_NAME
=
'bitbucket_server'
.
freeze
BATCH_SIZE
=
100
def
self
.
imports_repository?
def
self
.
imports_repository?
true
true
...
@@ -64,31 +65,42 @@ module Gitlab
...
@@ -64,31 +65,42 @@ module Gitlab
project
.
repository
.
commit
(
sha
)
project
.
repository
.
commit
(
sha
)
end
end
def
track_temp_branch
(
pull_request
,
index
)
def
temp_branch_name
(
pull_request
,
suffix
)
temp_branch_name
=
"gitlab/import/pull-request/
#{
pull_request
.
iid
}
-
#{
index
}
"
"gitlab/import/pull-request/
#{
pull_request
.
iid
}
/
#{
suffix
}
"
end
def
restore_branches
(
pull_requests
)
shas_to_restore
=
[]
pull_requests
.
each
do
|
pull_request
|
shas_to_restore
<<
{
temp_branch_name
(
pull_request
,
:from
)
=>
pull_request
.
source_branch_sha
,
temp_branch_name
(
pull_request
,
:to
)
=>
pull_request
.
target_branch_sha
}
end
@temp_branches
<<
temp_branch_name
created_branches
=
restore_branch_shas
(
shas_to_restore
)
temp_branch_name
@temp_branches
<<
created_branches
import_repository
unless
created_branches
.
empty?
end
end
def
restore_branches
(
pull_request
)
def
restore_branch_shas
(
shas_to_restore
)
shas_to_restore
=
[
pull_request
.
source_branch_sha
,
pull_request
.
target_branch_sha
]
branches_created
=
[]
resync
=
false
shas_to_restore
.
each_with_index
do
|
sha
,
index
|
shas_to_restore
.
each_with_index
do
|
shas
,
index
|
next
if
sha_exists?
(
sha
)
shas
.
each
do
|
branch_name
,
sha
|
next
if
sha_exists?
(
sha
)
branch_name
=
track_temp_branch
(
pull_request
,
index
)
response
=
client
.
create_branch
(
project_key
,
repository_slug
,
branch_name
,
sha
)
response
=
client
.
create_branch
(
project_key
,
repository_slug
,
branch_name
,
sha
)
if
response
.
success?
if
response
.
success?
resync
=
true
branches_created
<<
branch_name
else
else
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
response
.
code
}
"
)
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
response
.
code
}
"
)
end
end
end
end
end
import_repository
if
resync
branches_created
end
end
def
import_repository
def
import_repository
...
@@ -103,20 +115,35 @@ module Gitlab
...
@@ -103,20 +115,35 @@ module Gitlab
raise
e
.
message
raise
e
.
message
end
end
# Bitbucket Server keeps tracks of references for open pull requests in
# refs/heads/pull-requests, but closed and merged requests get moved
# into hidden internal refs under stash-refs/pull-requests. Unless the
# SHAs involved are at the tip of a branch or tag, there is no way to
# retrieve the server for those commits.
#
# To avoid losing history, we use the Bitbucket API to re-create the branch
# on the remote server. Then we have to issue a `git fetch` to download these
# branches.
def
import_pull_requests
def
import_pull_requests
pull_requests
=
client
.
pull_requests
(
project_key
,
repository_slug
)
pull_requests
=
client
.
pull_requests
(
project_key
,
repository_slug
).
to_a
pull_requests
.
each
do
|
pull_request
|
begin
# Creating branches on the server and fetching the newly-created branches
import_bitbucket_pull_request
(
pull_request
)
# may take a number of network round-trips. Do this in batches so that we can
rescue
StandardError
=>
e
# avoid doing a git fetch for every new branch.
errors
<<
{
type: :pull_request
,
iid:
pull_request
.
iid
,
errors:
e
.
message
,
trace:
e
.
backtrace
.
join
(
"
\n
"
),
raw_response:
pull_request
.
raw
}
pull_requests
.
each_slice
(
BATCH_SIZE
)
do
|
batch
|
restore_branches
(
batch
)
batch
.
each
do
|
pull_request
|
begin
import_bitbucket_pull_request
(
pull_request
)
rescue
StandardError
=>
e
errors
<<
{
type: :pull_request
,
iid:
pull_request
.
iid
,
errors:
e
.
message
,
trace:
e
.
backtrace
.
join
(
"
\n
"
),
raw_response:
pull_request
.
raw
}
end
end
end
end
end
end
end
def
import_bitbucket_pull_request
(
pull_request
)
def
import_bitbucket_pull_request
(
pull_request
)
restore_branches
(
pull_request
)
description
=
''
description
=
''
description
+=
@formatter
.
author_line
(
pull_request
.
author
)
unless
find_user_id
(
pull_request
.
author_email
)
description
+=
@formatter
.
author_line
(
pull_request
.
author
)
unless
find_user_id
(
pull_request
.
author_email
)
description
+=
pull_request
.
description
description
+=
pull_request
.
description
...
...
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