Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
98909dd1
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,体验更适合开发者的 AI 搜索 >>
提交
98909dd1
编写于
12月 23, 2015
作者:
D
Douglas Barbosa Alexandre
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generate separate comments when importing GitHub Issues into GitLab
上级
dc72a8b3
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
266 addition
and
76 deletion
+266
-76
lib/gitlab/github_import/base_formatter.rb
lib/gitlab/github_import/base_formatter.rb
+21
-0
lib/gitlab/github_import/comment_formatter.rb
lib/gitlab/github_import/comment_formatter.rb
+2
-16
lib/gitlab/github_import/importer.rb
lib/gitlab/github_import/importer.rb
+19
-40
lib/gitlab/github_import/issue_formatter.rb
lib/gitlab/github_import/issue_formatter.rb
+66
-0
lib/gitlab/github_import/pull_request_formatter.rb
lib/gitlab/github_import/pull_request_formatter.rb
+6
-16
spec/lib/gitlab/github_import/comment_formatter_spec.rb
spec/lib/gitlab/github_import/comment_formatter_spec.rb
+1
-1
spec/lib/gitlab/github_import/issue_formatter_spec.rb
spec/lib/gitlab/github_import/issue_formatter_spec.rb
+139
-0
spec/lib/gitlab/github_import/pull_request_formatter_spec.rb
spec/lib/gitlab/github_import/pull_request_formatter_spec.rb
+12
-3
未找到文件。
lib/gitlab/github_import/base_formatter.rb
0 → 100644
浏览文件 @
98909dd1
module
Gitlab
module
GithubImport
class
BaseFormatter
attr_reader
:formatter
,
:project
,
:raw_data
def
initialize
(
project
,
raw_data
)
@project
=
project
@raw_data
=
raw_data
@formatter
=
Gitlab
::
ImportFormatter
.
new
end
private
def
gl_user_id
(
github_id
)
User
.
joins
(
:identities
).
find_by
(
"identities.extern_uid = ? AND identities.provider = 'github'"
,
github_id
.
to_s
).
try
(
:id
)
end
end
end
end
lib/gitlab/github_import/comment.rb
→
lib/gitlab/github_import/comment
_formatter
.rb
浏览文件 @
98909dd1
module
Gitlab
module
GithubImport
class
Comment
attr_reader
:project
,
:raw_data
def
initialize
(
project
,
raw_data
)
@project
=
project
@raw_data
=
raw_data
@formatter
=
Gitlab
::
ImportFormatter
.
new
end
class
CommentFormatter
<
BaseFormatter
def
attributes
{
project:
project
,
...
...
@@ -46,13 +38,7 @@ module Gitlab
end
def
note
@formatter
.
author_line
(
author
)
+
body
end
def
gl_user_id
(
github_id
)
User
.
joins
(
:identities
).
find_by
(
"identities.extern_uid = ? AND identities.provider = 'github'"
,
github_id
.
to_s
).
try
(
:id
)
formatter
.
author_line
(
author
)
+
body
end
end
end
...
...
lib/gitlab/github_import/importer.rb
浏览文件 @
98909dd1
...
...
@@ -21,29 +21,17 @@ module Gitlab
private
def
import_issues
# Issues && Comments
client
.
list_issues
(
project
.
import_source
,
state: :all
,
sort: :created
,
direction: :asc
).
each
do
|
issue
|
if
issue
.
pull_request
.
nil?
direction: :asc
).
each
do
|
raw_data
|
gh_issue
=
IssueFormatter
.
new
(
project
,
raw_data
)
body
=
@formatter
.
author_line
(
issue
.
user
.
login
)
body
+=
issue
.
body
||
""
if
gh_issue
.
valid?
issue
=
Issue
.
create!
(
gh_issue
.
attributes
)
if
issue
.
comments
>
0
body
+=
@formatter
.
comments_header
client
.
issue_comments
(
project
.
import_source
,
issue
.
number
).
each
do
|
c
|
body
+=
@formatter
.
comment
(
c
.
user
.
login
,
c
.
created_at
,
c
.
body
)
end
if
gh_issue
.
has_comments?
import_comments
(
gh_issue
.
number
,
issue
)
end
project
.
issues
.
create!
(
description:
body
,
title:
issue
.
title
,
state:
issue
.
state
==
'closed'
?
'closed'
:
'opened'
,
author_id:
gl_author_id
(
project
,
issue
.
user
.
id
)
)
end
end
end
...
...
@@ -52,39 +40,30 @@ module Gitlab
client
.
pull_requests
(
project
.
import_source
,
state: :all
,
sort: :created
,
direction: :asc
).
each
do
|
raw_data
|
pull_request
=
PullRequest
.
new
(
project
,
raw_data
)
pull_request
=
PullRequest
Formatter
.
new
(
project
,
raw_data
)
if
pull_request
.
valid?
merge_request
=
MergeRequest
.
create!
(
pull_request
.
attributes
)
import_comments
_on_pull_request
(
merge_request
,
raw_data
)
import_comments_on_
pull_request_diff
(
merge_request
,
raw_data
)
import_comments
(
pull_request
.
number
,
merge_request
)
import_comments_on_
diff
(
pull_request
.
number
,
merge_request
)
end
end
end
def
import_comments_on_pull_request
(
merge_request
,
pull_request
)
client
.
issue_comments
(
project
.
import_source
,
pull_request
.
number
).
each
do
|
raw_data
|
comment
=
Comment
.
new
(
project
,
raw_data
)
merge_request
.
notes
.
create!
(
comment
.
attributes
)
end
end
def
import_comments_on_pull_request_diff
(
merge_request
,
pull_request
)
client
.
pull_request_comments
(
project
.
import_source
,
pull_request
.
number
).
each
do
|
raw_data
|
comment
=
Comment
.
new
(
project
,
raw_data
)
merge_request
.
notes
.
create!
(
comment
.
attributes
)
end
def
import_comments
(
issue_number
,
noteable
)
comments
=
client
.
issue_comments
(
project
.
import_source
,
issue_number
)
create_comments
(
comments
,
noteable
)
end
def
gl_author_id
(
project
,
github_id
)
gl_user_id
(
github_id
)
||
project
.
creator_id
def
import_comments_on_diff
(
pull_request_number
,
merge_request
)
comments
=
client
.
pull_request_comments
(
project
.
import_source
,
pull_request_number
)
create_comments
(
comments
,
merge_request
)
end
def
gl_user_id
(
github_id
)
if
github_id
User
.
joins
(
:identities
).
find_by
(
"identities.extern_uid = ? AND identities.provider = 'github'"
,
github_id
.
to_s
).
try
(
:id
)
def
create_comments
(
comments
,
noteable
)
comments
.
each
do
|
raw_data
|
comment
=
CommentFormatter
.
new
(
project
,
raw_data
)
noteable
.
notes
.
create!
(
comment
.
attributes
)
end
end
end
...
...
lib/gitlab/github_import/issue_formatter.rb
0 → 100644
浏览文件 @
98909dd1
module
Gitlab
module
GithubImport
class
IssueFormatter
<
BaseFormatter
def
attributes
{
project:
project
,
title:
raw_data
.
title
,
description:
description
,
state:
state
,
author_id:
author_id
,
assignee_id:
assignee_id
,
created_at:
raw_data
.
created_at
,
updated_at:
updated_at
}
end
def
has_comments?
raw_data
.
comments
>
0
end
def
number
raw_data
.
number
end
def
valid?
raw_data
.
pull_request
.
nil?
end
private
def
assigned?
raw_data
.
assignee
.
present?
end
def
assignee_id
if
assigned?
gl_user_id
(
raw_data
.
assignee
.
id
)
end
end
def
author
raw_data
.
user
.
login
end
def
author_id
gl_user_id
(
raw_data
.
user
.
id
)
||
project
.
creator_id
end
def
body
raw_data
.
body
||
""
end
def
description
@formatter
.
author_line
(
author
)
+
body
end
def
state
raw_data
.
state
==
'closed'
?
'closed'
:
'opened'
end
def
updated_at
state
==
'closed'
?
raw_data
.
closed_at
:
raw_data
.
updated_at
end
end
end
end
lib/gitlab/github_import/pull_request.rb
→
lib/gitlab/github_import/pull_request
_formatter
.rb
浏览文件 @
98909dd1
module
Gitlab
module
GithubImport
class
PullRequest
attr_reader
:project
,
:raw_data
def
initialize
(
project
,
raw_data
)
@project
=
project
@raw_data
=
raw_data
@formatter
=
Gitlab
::
ImportFormatter
.
new
end
class
PullRequestFormatter
<
BaseFormatter
def
attributes
{
title:
raw_data
.
title
,
...
...
@@ -25,6 +17,10 @@ module Gitlab
}
end
def
number
raw_data
.
number
end
def
valid?
source_branch
.
present?
&&
target_branch
.
present?
end
...
...
@@ -54,7 +50,7 @@ module Gitlab
end
def
description
@
formatter
.
author_line
(
author
)
+
body
formatter
.
author_line
(
author
)
+
body
end
def
source_project
...
...
@@ -92,12 +88,6 @@ module Gitlab
raw_data
.
updated_at
end
end
def
gl_user_id
(
github_id
)
User
.
joins
(
:identities
).
find_by
(
"identities.extern_uid = ? AND identities.provider = 'github'"
,
github_id
.
to_s
).
try
(
:id
)
end
end
end
end
spec/lib/gitlab/github_import/comment_spec.rb
→
spec/lib/gitlab/github_import/comment_
formatter_
spec.rb
浏览文件 @
98909dd1
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
Comment
,
lib:
true
do
describe
Gitlab
::
GithubImport
::
Comment
Formatter
,
lib:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:octocat
)
{
OpenStruct
.
new
(
id:
123456
,
login:
'octocat'
)
}
let
(
:created_at
)
{
DateTime
.
strptime
(
'2013-04-10T20:09:31Z'
)
}
...
...
spec/lib/gitlab/github_import/issue_formatter_spec.rb
0 → 100644
浏览文件 @
98909dd1
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
IssueFormatter
,
lib:
true
do
let!
(
:project
)
{
create
(
:project
,
namespace:
create
(
:namespace
,
path:
'octocat'
))
}
let
(
:octocat
)
{
OpenStruct
.
new
(
id:
123456
,
login:
'octocat'
)
}
let
(
:created_at
)
{
DateTime
.
strptime
(
'2011-01-26T19:01:12Z'
)
}
let
(
:updated_at
)
{
DateTime
.
strptime
(
'2011-01-27T19:01:12Z'
)
}
let
(
:base_data
)
do
{
number:
1347
,
state:
'open'
,
title:
'Found a bug'
,
body:
"I'm having a problem with this."
,
assignee:
nil
,
user:
octocat
,
comments:
0
,
pull_request:
nil
,
created_at:
created_at
,
updated_at:
updated_at
,
closed_at:
nil
}
end
subject
(
:issue
)
{
described_class
.
new
(
project
,
raw_data
)}
describe
'#attributes'
do
context
'when issue is open'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
state:
'open'
))
}
it
'returns formatted attributes'
do
expected
=
{
project:
project
,
title:
'Found a bug'
,
description:
"*Created by: octocat*
\n\n
I'm having a problem with this."
,
state:
'opened'
,
author_id:
project
.
creator_id
,
assignee_id:
nil
,
created_at:
created_at
,
updated_at:
updated_at
}
expect
(
issue
.
attributes
).
to
eq
(
expected
)
end
end
context
'when issue is closed'
do
let
(
:closed_at
)
{
DateTime
.
strptime
(
'2011-01-28T19:01:12Z'
)
}
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
state:
'closed'
,
closed_at:
closed_at
))
}
it
'returns formatted attributes'
do
expected
=
{
project:
project
,
title:
'Found a bug'
,
description:
"*Created by: octocat*
\n\n
I'm having a problem with this."
,
state:
'closed'
,
author_id:
project
.
creator_id
,
assignee_id:
nil
,
created_at:
created_at
,
updated_at:
closed_at
}
expect
(
issue
.
attributes
).
to
eq
(
expected
)
end
end
context
'when it is assigned to someone'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
assignee:
octocat
))
}
it
'returns nil as assignee_id when is not a GitLab user'
do
expect
(
issue
.
attributes
.
fetch
(
:assignee_id
)).
to
be_nil
end
it
'returns GitLab user id as assignee_id when is a GitLab user'
do
gl_user
=
create
(
:omniauth_user
,
extern_uid:
octocat
.
id
,
provider:
'github'
)
expect
(
issue
.
attributes
.
fetch
(
:assignee_id
)).
to
eq
gl_user
.
id
end
end
context
'when author is a GitLab user'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
user:
octocat
))
}
it
'returns project#creator_id as author_id when is not a GitLab user'
do
expect
(
issue
.
attributes
.
fetch
(
:author_id
)).
to
eq
project
.
creator_id
end
it
'returns GitLab user id as author_id when is a GitLab user'
do
gl_user
=
create
(
:omniauth_user
,
extern_uid:
octocat
.
id
,
provider:
'github'
)
expect
(
issue
.
attributes
.
fetch
(
:author_id
)).
to
eq
gl_user
.
id
end
end
end
describe
'#has_comments?'
do
context
'when number of comments is greater than zero'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
comments:
1
))
}
it
'returns true'
do
expect
(
issue
.
has_comments?
).
to
eq
true
end
end
context
'when number of comments is equal to zero'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
comments:
0
))
}
it
'returns false'
do
expect
(
issue
.
has_comments?
).
to
eq
false
end
end
end
describe
'#number'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
number:
1347
))
}
it
'returns pull request number'
do
expect
(
issue
.
number
).
to
eq
1347
end
end
describe
'#valid?'
do
context
'when mention a pull request'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
pull_request:
OpenStruct
.
new
))
}
it
'returns false'
do
expect
(
issue
.
valid?
).
to
eq
false
end
end
context
'when does not mention a pull request'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
pull_request:
nil
))
}
it
'returns true'
do
expect
(
issue
.
valid?
).
to
eq
true
end
end
end
end
spec/lib/gitlab/github_import/pull_request_spec.rb
→
spec/lib/gitlab/github_import/pull_request_
formatter_
spec.rb
浏览文件 @
98909dd1
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
PullRequest
,
lib:
true
do
describe
Gitlab
::
GithubImport
::
PullRequest
Formatter
,
lib:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:source_branch
)
{
OpenStruct
.
new
(
ref:
'feature'
)
}
let
(
:target_branch
)
{
OpenStruct
.
new
(
ref:
'master'
)
}
...
...
@@ -9,6 +9,7 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
let
(
:updated_at
)
{
DateTime
.
strptime
(
'2011-01-27T19:01:12Z'
)
}
let
(
:base_data
)
do
{
number:
1347
,
state:
'open'
,
title:
'New feature'
,
body:
'Please pull these awesome changes'
,
...
...
@@ -97,11 +98,11 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
context
'when it is assigned to someone'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
assignee:
octocat
))
}
it
'returns nil as assigne
d
_id when is not a GitLab user'
do
it
'returns nil as assigne
e
_id when is not a GitLab user'
do
expect
(
pull_request
.
attributes
.
fetch
(
:assignee_id
)).
to
be_nil
end
it
'returns GitLab user id as assigne
d
_id when is a GitLab user'
do
it
'returns GitLab user id as assigne
e
_id when is a GitLab user'
do
gl_user
=
create
(
:omniauth_user
,
extern_uid:
octocat
.
id
,
provider:
'github'
)
expect
(
pull_request
.
attributes
.
fetch
(
:assignee_id
)).
to
eq
gl_user
.
id
...
...
@@ -123,6 +124,14 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
end
end
describe
'#number'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
number:
1347
))
}
it
'returns pull request number'
do
expect
(
pull_request
.
number
).
to
eq
1347
end
end
describe
'#valid?'
do
let
(
:invalid_branch
)
{
OpenStruct
.
new
(
ref:
'invalid-branch'
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录