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
f673923d
Commit
f673923d
authored
Jul 18, 2018
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix and clean up code for deleting branches
parent
9d59616b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
20 deletions
+27
-20
lib/gitlab/bitbucket_server_import/importer.rb
lib/gitlab/bitbucket_server_import/importer.rb
+27
-20
No files found.
lib/gitlab/bitbucket_server_import/importer.rb
View file @
f673923d
...
@@ -7,6 +7,8 @@ module Gitlab
...
@@ -7,6 +7,8 @@ module Gitlab
REMOTE_NAME
=
'bitbucket_server'
.
freeze
REMOTE_NAME
=
'bitbucket_server'
.
freeze
BATCH_SIZE
=
100
BATCH_SIZE
=
100
TempBranch
=
Struct
.
new
(
:name
,
:sha
)
def
self
.
imports_repository?
def
self
.
imports_repository?
true
true
end
end
...
@@ -73,39 +75,44 @@ module Gitlab
...
@@ -73,39 +75,44 @@ module Gitlab
"gitlab/import/pull-request/
#{
pull_request
.
iid
}
/
#{
suffix
}
"
"gitlab/import/pull-request/
#{
pull_request
.
iid
}
/
#{
suffix
}
"
end
end
# This method restores required SHAs that GitLab needs to create diffs
# into branch names as the following:
#
# gitlab/import/pull-request/N/{to,from}
def
restore_branches
(
pull_requests
)
def
restore_branches
(
pull_requests
)
shas_to_restore
=
[]
shas_to_restore
=
[]
pull_requests
.
each
do
|
pull_request
|
pull_requests
.
each
do
|
pull_request
|
shas_to_restore
<<
{
shas_to_restore
<<
TempBranch
.
new
(
temp_branch_name
(
pull_request
,
:from
),
temp_branch_name
(
pull_request
,
:from
)
=>
pull_request
.
source_branch_sha
,
pull_request
.
source_branch_sha
)
temp_branch_name
(
pull_request
,
:to
)
=>
pull_request
.
target_branch_sha
shas_to_restore
<<
TempBranch
.
new
(
temp_branch_name
(
pull_request
,
:to
),
}
pull_request
.
target_branch_sha
)
end
end
# Create the branches on the Bitbucket Server first
created_branches
=
restore_branch_shas
(
shas_to_restore
)
created_branches
=
restore_branch_shas
(
shas_to_restore
)
@temp_branches
+=
created_branches
@temp_branches
+=
created_branches
# Now sync the repository so we get the new branches
import_repository
unless
created_branches
.
empty?
import_repository
unless
created_branches
.
empty?
end
end
def
restore_branch_shas
(
shas_to_restore
)
def
restore_branch_shas
(
shas_to_restore
)
branches_created
=
[]
shas_to_restore
.
each_with_object
([])
do
|
temp_branch
,
branches_created
|
branch_name
=
temp_branch
.
name
sha
=
temp_branch
.
sha
shas_to_restore
.
each_with_index
do
|
shas
,
index
|
shas
.
each
do
|
branch_name
,
sha
|
next
if
sha_exists?
(
sha
)
next
if
sha_exists?
(
sha
)
begin
begin
client
.
create_branch
(
project_key
,
repository_slug
,
branch_name
,
sha
)
client
.
create_branch
(
project_key
,
repository_slug
,
branch_name
,
sha
)
branches_created
<<
branch_name
branches_created
<<
temp_branch
rescue
BitbucketServer
::
Connection
::
ConnectionError
=>
e
rescue
BitbucketServer
::
Connection
::
ConnectionError
=>
e
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
e
}
"
)
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
e
}
"
)
end
end
end
end
end
end
branches_created
end
def
import_repository
def
import_repository
project
.
ensure_repository
project
.
ensure_repository
project
.
repository
.
fetch_as_mirror
(
project
.
import_url
,
refmap:
self
.
class
.
refmap
,
remote_name:
REMOTE_NAME
)
project
.
repository
.
fetch_as_mirror
(
project
.
import_url
,
refmap:
self
.
class
.
refmap
,
remote_name:
REMOTE_NAME
)
...
@@ -147,12 +154,12 @@ module Gitlab
...
@@ -147,12 +154,12 @@ module Gitlab
end
end
def
delete_temp_branches
def
delete_temp_branches
@temp_branches
.
each
do
|
branch
_name
|
@temp_branches
.
each
do
|
branch
|
begin
begin
client
.
delete_branch
(
project_key
,
repository_slug
,
branch
_name
)
client
.
delete_branch
(
project_key
,
repository_slug
,
branch
.
name
,
branch
.
sha
)
project
.
repository
.
delete_branch
(
branch
_
name
)
project
.
repository
.
delete_branch
(
branch
.
name
)
rescue
BitbucketServer
::
Connection
::
ConnectionError
=>
e
rescue
BitbucketServer
::
Connection
::
ConnectionError
=>
e
@errors
<<
{
type: :delete_temp_branches
,
branch_name:
branch
_
name
,
errors:
e
.
message
}
@errors
<<
{
type: :delete_temp_branches
,
branch_name:
branch
.
name
,
errors:
e
.
message
}
end
end
end
end
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