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
98909dd1
Commit
98909dd1
authored
Dec 23, 2015
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generate separate comments when importing GitHub Issues into GitLab
parent
dc72a8b3
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
266 additions
and
76 deletions
+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
No files found.
lib/gitlab/github_import/base_formatter.rb
0 → 100644
View file @
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
View file @
98909dd1
module
Gitlab
module
Gitlab
module
GithubImport
module
GithubImport
class
Comment
class
CommentFormatter
<
BaseFormatter
attr_reader
:project
,
:raw_data
def
initialize
(
project
,
raw_data
)
@project
=
project
@raw_data
=
raw_data
@formatter
=
Gitlab
::
ImportFormatter
.
new
end
def
attributes
def
attributes
{
{
project:
project
,
project:
project
,
...
@@ -46,13 +38,7 @@ module Gitlab
...
@@ -46,13 +38,7 @@ module Gitlab
end
end
def
note
def
note
@formatter
.
author_line
(
author
)
+
body
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
)
end
end
end
end
end
end
...
...
lib/gitlab/github_import/importer.rb
View file @
98909dd1
...
@@ -21,30 +21,18 @@ module Gitlab
...
@@ -21,30 +21,18 @@ module Gitlab
private
private
def
import_issues
def
import_issues
# Issues && Comments
client
.
list_issues
(
project
.
import_source
,
state: :all
,
client
.
list_issues
(
project
.
import_source
,
state: :all
,
sort: :created
,
sort: :created
,
direction: :asc
).
each
do
|
issue
|
direction: :asc
).
each
do
|
raw_data
|
if
issue
.
pull_request
.
nil?
gh_issue
=
IssueFormatter
.
new
(
project
,
raw_data
)
body
=
@formatter
.
author_line
(
issue
.
user
.
login
)
body
+=
issue
.
body
||
""
if
issue
.
comments
>
0
if
gh_issue
.
valid?
body
+=
@formatter
.
comments_header
issue
=
Issue
.
create!
(
gh_issue
.
attributes
)
client
.
issue_comments
(
project
.
import_source
,
issue
.
number
).
each
do
|
c
|
if
gh_issue
.
has_comments?
body
+=
@formatter
.
comment
(
c
.
user
.
login
,
c
.
created_at
,
c
.
body
)
import_comments
(
gh_issue
.
number
,
issue
)
end
end
end
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
end
end
...
@@ -52,39 +40,30 @@ module Gitlab
...
@@ -52,39 +40,30 @@ module Gitlab
client
.
pull_requests
(
project
.
import_source
,
state: :all
,
client
.
pull_requests
(
project
.
import_source
,
state: :all
,
sort: :created
,
sort: :created
,
direction: :asc
).
each
do
|
raw_data
|
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?
if
pull_request
.
valid?
merge_request
=
MergeRequest
.
create!
(
pull_request
.
attributes
)
merge_request
=
MergeRequest
.
create!
(
pull_request
.
attributes
)
import_comments
_on_pull_request
(
merge_request
,
raw_data
)
import_comments
(
pull_request
.
number
,
merge_request
)
import_comments_on_
pull_request_diff
(
merge_request
,
raw_data
)
import_comments_on_
diff
(
pull_request
.
number
,
merge_request
)
end
end
end
end
end
end
def
import_comments_on_pull_request
(
merge_request
,
pull_request
)
def
import_comments
(
issue_number
,
noteable
)
client
.
issue_comments
(
project
.
import_source
,
pull_request
.
number
).
each
do
|
raw_data
|
comments
=
client
.
issue_comments
(
project
.
import_source
,
issue_number
)
comment
=
Comment
.
new
(
project
,
raw_data
)
create_comments
(
comments
,
noteable
)
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
end
end
def
gl_author_id
(
project
,
github_id
)
def
import_comments_on_diff
(
pull_request_number
,
merge_request
)
gl_user_id
(
github_id
)
||
project
.
creator_id
comments
=
client
.
pull_request_comments
(
project
.
import_source
,
pull_request_number
)
create_comments
(
comments
,
merge_request
)
end
end
def
gl_user_id
(
github_id
)
def
create_comments
(
comments
,
noteable
)
if
github_id
comments
.
each
do
|
raw_data
|
User
.
joins
(
:identities
).
comment
=
CommentFormatter
.
new
(
project
,
raw_data
)
find_by
(
"identities.extern_uid = ? AND identities.provider = 'github'"
,
github_id
.
to_s
).
noteable
.
notes
.
create!
(
comment
.
attributes
)
try
(
:id
)
end
end
end
end
end
end
...
...
lib/gitlab/github_import/issue_formatter.rb
0 → 100644
View file @
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
View file @
98909dd1
module
Gitlab
module
Gitlab
module
GithubImport
module
GithubImport
class
PullRequest
class
PullRequestFormatter
<
BaseFormatter
attr_reader
:project
,
:raw_data
def
initialize
(
project
,
raw_data
)
@project
=
project
@raw_data
=
raw_data
@formatter
=
Gitlab
::
ImportFormatter
.
new
end
def
attributes
def
attributes
{
{
title:
raw_data
.
title
,
title:
raw_data
.
title
,
...
@@ -25,6 +17,10 @@ module Gitlab
...
@@ -25,6 +17,10 @@ module Gitlab
}
}
end
end
def
number
raw_data
.
number
end
def
valid?
def
valid?
source_branch
.
present?
&&
target_branch
.
present?
source_branch
.
present?
&&
target_branch
.
present?
end
end
...
@@ -54,7 +50,7 @@ module Gitlab
...
@@ -54,7 +50,7 @@ module Gitlab
end
end
def
description
def
description
@
formatter
.
author_line
(
author
)
+
body
formatter
.
author_line
(
author
)
+
body
end
end
def
source_project
def
source_project
...
@@ -92,12 +88,6 @@ module Gitlab
...
@@ -92,12 +88,6 @@ module Gitlab
raw_data
.
updated_at
raw_data
.
updated_at
end
end
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
end
end
end
spec/lib/gitlab/github_import/comment_spec.rb
→
spec/lib/gitlab/github_import/comment_
formatter_
spec.rb
View file @
98909dd1
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
Comment
,
lib:
true
do
describe
Gitlab
::
GithubImport
::
Comment
Formatter
,
lib:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:octocat
)
{
OpenStruct
.
new
(
id:
123456
,
login:
'octocat'
)
}
let
(
:octocat
)
{
OpenStruct
.
new
(
id:
123456
,
login:
'octocat'
)
}
let
(
:created_at
)
{
DateTime
.
strptime
(
'2013-04-10T20:09:31Z'
)
}
let
(
:created_at
)
{
DateTime
.
strptime
(
'2013-04-10T20:09:31Z'
)
}
...
...
spec/lib/gitlab/github_import/issue_formatter_spec.rb
0 → 100644
View file @
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
View file @
98909dd1
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
PullRequest
,
lib:
true
do
describe
Gitlab
::
GithubImport
::
PullRequest
Formatter
,
lib:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:source_branch
)
{
OpenStruct
.
new
(
ref:
'feature'
)
}
let
(
:source_branch
)
{
OpenStruct
.
new
(
ref:
'feature'
)
}
let
(
:target_branch
)
{
OpenStruct
.
new
(
ref:
'master'
)
}
let
(
:target_branch
)
{
OpenStruct
.
new
(
ref:
'master'
)
}
...
@@ -9,6 +9,7 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
...
@@ -9,6 +9,7 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
let
(
:updated_at
)
{
DateTime
.
strptime
(
'2011-01-27T19:01:12Z'
)
}
let
(
:updated_at
)
{
DateTime
.
strptime
(
'2011-01-27T19:01:12Z'
)
}
let
(
:base_data
)
do
let
(
:base_data
)
do
{
{
number:
1347
,
state:
'open'
,
state:
'open'
,
title:
'New feature'
,
title:
'New feature'
,
body:
'Please pull these awesome changes'
,
body:
'Please pull these awesome changes'
,
...
@@ -97,11 +98,11 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
...
@@ -97,11 +98,11 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
context
'when it is assigned to someone'
do
context
'when it is assigned to someone'
do
let
(
:raw_data
)
{
OpenStruct
.
new
(
base_data
.
merge
(
assignee:
octocat
))
}
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
expect
(
pull_request
.
attributes
.
fetch
(
:assignee_id
)).
to
be_nil
end
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'
)
gl_user
=
create
(
:omniauth_user
,
extern_uid:
octocat
.
id
,
provider:
'github'
)
expect
(
pull_request
.
attributes
.
fetch
(
:assignee_id
)).
to
eq
gl_user
.
id
expect
(
pull_request
.
attributes
.
fetch
(
:assignee_id
)).
to
eq
gl_user
.
id
...
@@ -123,6 +124,14 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
...
@@ -123,6 +124,14 @@ describe Gitlab::GithubImport::PullRequest, lib: true do
end
end
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
describe
'#valid?'
do
let
(
:invalid_branch
)
{
OpenStruct
.
new
(
ref:
'invalid-branch'
)
}
let
(
:invalid_branch
)
{
OpenStruct
.
new
(
ref:
'invalid-branch'
)
}
...
...
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