Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
8ab13e14
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8ab13e14
编写于
7月 05, 2018
作者:
S
Stan Hu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Batch re-creating branches and fetching the remote
上级
cc4a39cc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
51 addition
and
24 deletion
+51
-24
lib/gitlab/bitbucket_server_import/importer.rb
lib/gitlab/bitbucket_server_import/importer.rb
+51
-24
未找到文件。
lib/gitlab/bitbucket_server_import/importer.rb
浏览文件 @
8ab13e14
...
...
@@ -5,6 +5,7 @@ module Gitlab
attr_reader
:project
,
:project_key
,
:repository_slug
,
:client
,
:errors
,
:users
REMOTE_NAME
=
'bitbucket_server'
.
freeze
BATCH_SIZE
=
100
def
self
.
imports_repository?
true
...
...
@@ -64,31 +65,42 @@ module Gitlab
project
.
repository
.
commit
(
sha
)
end
def
track_temp_branch
(
pull_request
,
index
)
temp_branch_name
=
"gitlab/import/pull-request/
#{
pull_request
.
iid
}
-
#{
index
}
"
def
temp_branch_name
(
pull_request
,
suffix
)
"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
temp_branch_name
created_branches
=
restore_branch_shas
(
shas_to_restore
)
@temp_branches
<<
created_branches
import_repository
unless
created_branches
.
empty?
end
def
restore_branches
(
pull_request
)
shas_to_restore
=
[
pull_request
.
source_branch_sha
,
pull_request
.
target_branch_sha
]
resync
=
false
def
restore_branch_shas
(
shas_to_restore
)
branches_created
=
[]
shas_to_restore
.
each_with_index
do
|
sha
,
index
|
next
if
sha_exists?
(
sha
)
shas_to_restore
.
each_with_index
do
|
shas
,
index
|
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?
resync
=
true
else
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
response
.
code
}
"
)
if
response
.
success?
branches_created
<<
branch_name
else
Rails
.
logger
.
warn
(
"BitbucketServerImporter: Unable to recreate branch for SHA
#{
sha
}
:
#{
response
.
code
}
"
)
end
end
end
import_repository
if
resync
branches_created
end
def
import_repository
...
...
@@ -103,20 +115,35 @@ module Gitlab
raise
e
.
message
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
pull_requests
=
client
.
pull_requests
(
project_key
,
repository_slug
)
pull_requests
.
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
}
pull_requests
=
client
.
pull_requests
(
project_key
,
repository_slug
).
to_a
# Creating branches on the server and fetching the newly-created branches
# may take a number of network round-trips. Do this in batches so that we can
# avoid doing a git fetch for every new branch.
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
def
import_bitbucket_pull_request
(
pull_request
)
restore_branches
(
pull_request
)
description
=
''
description
+=
@formatter
.
author_line
(
pull_request
.
author
)
unless
find_user_id
(
pull_request
.
author_email
)
description
+=
pull_request
.
description
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录