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
Jérome Perrin
gitlab-ce
Commits
33c8f315
Commit
33c8f315
authored
Apr 13, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply labels to issues/merge requests
parent
db322009
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
42 deletions
+74
-42
lib/github/import.rb
lib/github/import.rb
+66
-42
lib/github/representation/issue.rb
lib/github/representation/issue.rb
+8
-0
No files found.
lib/github/import.rb
View file @
33c8f315
...
...
@@ -27,11 +27,12 @@ module Github
self
.
reset_callbacks
:validate
end
attr_reader
:project
,
:repository
,
:cached_user_ids
,
:errors
attr_reader
:project
,
:repository
,
:cached_
label_ids
,
:cached_
user_ids
,
:errors
def
initialize
(
project
)
@project
=
project
@repository
=
project
.
repository
@cached_label_ids
=
{}
@cached_user_ids
=
{}
@errors
=
[]
end
...
...
@@ -40,7 +41,7 @@ module Github
# Fetch repository
begin
project
.
create_repository
project
.
repository
.
add_remote
(
'github'
,
"https://
881a01d03026458e51285a4c7038c9fe4daa5561
@github.com/
#{
owner
}
/
#{
repo
}
.git"
)
project
.
repository
.
add_remote
(
'github'
,
"https://
{token}
@github.com/
#{
owner
}
/
#{
repo
}
.git"
)
project
.
repository
.
set_remote_as_mirror
(
'github'
)
project
.
repository
.
fetch_remote
(
'github'
,
forced:
true
)
project
.
repository
.
remove_remote
(
'github'
)
...
...
@@ -57,6 +58,8 @@ module Github
response
.
body
.
each
do
|
raw
|
begin
label
=
Github
::
Representation
::
Label
.
new
(
raw
)
# TODO: we should take group labels in account
next
if
project
.
labels
.
where
(
title:
label
.
title
).
exists?
project
.
labels
.
create!
(
title:
label
.
title
,
color:
label
.
color
)
...
...
@@ -68,6 +71,12 @@ module Github
break
unless
url
=
response
.
rels
[
:next
]
end
# Cache labels
# TODO: we should take group labels in account
project
.
labels
.
select
(
:id
,
:title
).
find_each
do
|
label
|
@cached_label_ids
[
label
.
title
]
=
label
.
id
end
# Fetch milestones
url
=
"/repos/
#{
owner
}
/
#{
repo
}
/milestones"
...
...
@@ -208,50 +217,61 @@ module Github
response
.
body
.
each
do
|
raw
|
representation
=
Github
::
Representation
::
Issue
.
new
(
raw
)
next
if
representation
.
pull_request?
next
if
Issue
.
where
(
iid:
representation
.
iid
,
project_id:
project
.
id
).
exists?
begin
issue
=
Issue
.
new
issue
.
iid
=
representation
.
iid
issue
.
project_id
=
project
.
id
issue
.
title
=
representation
.
title
issue
.
description
=
representation
.
description
issue
.
state
=
representation
.
state
issue
.
milestone_id
=
milestone_id
(
representation
.
milestone
)
issue
.
author_id
=
user_id
(
representation
.
author
,
project
.
creator_id
)
issue
.
assignee_id
=
user_id
(
representation
.
assignee
)
issue
.
created_at
=
representation
.
created_at
issue
.
updated_at
=
representation
.
updated_at
issue
.
save
(
validate:
false
)
if
issue
.
has_comments?
# Fetch comments
comments_url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues/
#{
issue
.
iid
}
/comments"
loop
do
comments
=
Github
::
Client
.
new
.
get
(
comments_url
)
ActiveRecord
::
Base
.
no_touching
do
comments
.
body
.
each
do
|
raw
|
begin
comment
=
Github
::
Representation
::
Comment
.
new
(
raw
)
note
=
Note
.
new
note
.
project_id
=
project
.
id
note
.
noteable
=
issue
note
.
note
=
comment
.
note
note
.
author_id
=
user_id
(
comment
.
author
,
project
.
creator_id
)
note
.
created_at
=
comment
.
created_at
note
.
updated_at
=
comment
.
updated_at
note
.
save!
(
validate:
false
)
rescue
=>
e
error
(
:comment
,
comment
.
url
,
e
.
message
)
# Every pull request is an issue, but not every issue
# is a pull request. For this reason, "shared" actions
# for both features, like manipulating assignees, labels
# and milestones, are provided within the Issues API.
if
representation
.
pull_request?
next
unless
representation
.
has_labels?
merge_request
=
MergeRequest
.
find_by!
(
target_project_id:
project
.
id
,
iid:
representation
.
iid
)
merge_request
.
update_attribute
(
:label_ids
,
label_ids
(
representation
.
labels
))
else
next
if
Issue
.
where
(
iid:
representation
.
iid
,
project_id:
project
.
id
).
exists?
issue
=
Issue
.
new
issue
.
iid
=
representation
.
iid
issue
.
project_id
=
project
.
id
issue
.
title
=
representation
.
title
issue
.
description
=
representation
.
description
issue
.
state
=
representation
.
state
issue
.
label_ids
=
label_ids
(
representation
.
labels
)
issue
.
milestone_id
=
milestone_id
(
representation
.
milestone
)
issue
.
author_id
=
user_id
(
representation
.
author
,
project
.
creator_id
)
issue
.
assignee_id
=
user_id
(
representation
.
assignee
)
issue
.
created_at
=
representation
.
created_at
issue
.
updated_at
=
representation
.
updated_at
issue
.
save
(
validate:
false
)
if
representation
.
has_comments?
# Fetch comments
comments_url
=
"/repos/
#{
owner
}
/
#{
repo
}
/issues/
#{
issue
.
iid
}
/comments"
loop
do
comments
=
Github
::
Client
.
new
.
get
(
comments_url
)
ActiveRecord
::
Base
.
no_touching
do
comments
.
body
.
each
do
|
raw
|
begin
comment
=
Github
::
Representation
::
Comment
.
new
(
raw
)
note
=
Note
.
new
note
.
project_id
=
project
.
id
note
.
noteable
=
issue
note
.
note
=
comment
.
note
note
.
author_id
=
user_id
(
comment
.
author
,
project
.
creator_id
)
note
.
created_at
=
comment
.
created_at
note
.
updated_at
=
comment
.
updated_at
note
.
save!
(
validate:
false
)
rescue
=>
e
error
(
:comment
,
comment
.
url
,
e
.
message
)
end
end
end
end
break
unless
comments_url
=
comments
.
rels
[
:next
]
break
unless
comments_url
=
comments
.
rels
[
:next
]
end
end
end
rescue
=>
e
...
...
@@ -290,6 +310,10 @@ module Github
remove_branch
(
pull_request
.
target_branch_name
)
unless
pull_request
.
target_branch_exists?
end
def
label_ids
(
issuable
)
issuable
.
map
{
|
attrs
|
cached_label_ids
[
attrs
.
fetch
(
'name'
)]
}.
compact
end
def
milestone_id
(
milestone
)
return
unless
milestone
.
present?
...
...
lib/github/representation/issue.rb
View file @
33c8f315
...
...
@@ -13,6 +13,10 @@ module Github
raw
[
'body'
]
||
''
end
def
labels
raw
[
'labels'
]
end
def
milestone
return
unless
raw
[
'milestone'
].
present?
...
...
@@ -53,6 +57,10 @@ module Github
raw
[
'comments'
]
>
0
end
def
has_labels?
labels
.
count
>
0
end
def
pull_request?
raw
[
'pull_request'
].
present?
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