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
Léo-Paul Géneau
gitlab-ce
Commits
659d5d48
Commit
659d5d48
authored
Jun 03, 2016
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Disable Webhooks before proceeding with the GitHub import
parent
ac4e3e8c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
4 deletions
+110
-4
lib/gitlab/github_import/hook_formatter.rb
lib/gitlab/github_import/hook_formatter.rb
+23
-0
lib/gitlab/github_import/importer.rb
lib/gitlab/github_import/importer.rb
+22
-4
spec/lib/gitlab/github_import/hook_formatter_spec.rb
spec/lib/gitlab/github_import/hook_formatter_spec.rb
+65
-0
No files found.
lib/gitlab/github_import/hook_formatter.rb
0 → 100644
View file @
659d5d48
module
Gitlab
module
GithubImport
class
HookFormatter
EVENTS
=
%w[* create delete pull_request push]
.
freeze
attr_reader
:raw
delegate
:id
,
:name
,
:active
,
to: :raw
def
initialize
(
raw
)
@raw
=
raw
end
def
config
raw
.
config
.
attrs
end
def
valid?
(
EVENTS
&
raw
.
events
).
any?
&&
active
end
end
end
end
lib/gitlab/github_import/importer.rb
View file @
659d5d48
...
...
@@ -69,6 +69,9 @@ module Gitlab
end
def
import_pull_requests
hooks
=
client
.
hooks
(
repo
).
map
{
|
raw
|
HookFormatter
.
new
(
raw
)
}.
select
(
&
:valid?
)
disable_webhooks
(
hooks
)
pull_requests
=
client
.
pull_requests
(
repo
,
state: :all
,
sort: :created
,
direction: :asc
)
.
map
{
|
raw
|
PullRequestFormatter
.
new
(
project
,
raw
)
}
.
select
(
&
:valid?
)
...
...
@@ -77,7 +80,7 @@ module Gitlab
target_branches_removed
=
pull_requests
.
reject
(
&
:target_branch_exists?
).
map
{
|
pr
|
[
pr
.
target_branch_name
,
pr
.
target_branch_sha
]
}
branches_removed
=
source_branches_removed
|
target_branches_removed
create_ref
s
(
branches_removed
)
restore_branche
s
(
branches_removed
)
pull_requests
.
each
do
|
pull_request
|
merge_request
=
MergeRequest
.
new
(
pull_request
.
attributes
)
...
...
@@ -93,10 +96,25 @@ module Gitlab
rescue
ActiveRecord
::
RecordInvalid
=>
e
raise
Projects
::
ImportService
::
Error
,
e
.
message
ensure
delete_refs
(
branches_removed
)
clean_up_restored_branches
(
branches_removed
)
clean_up_disabled_webhooks
(
hooks
)
end
def
disable_webhooks
(
hooks
)
update_webhooks
(
hooks
,
active:
false
)
end
def
clean_up_disabled_webhooks
(
hooks
)
update_webhooks
(
hooks
,
active:
true
)
end
def
update_webhooks
(
hooks
,
options
)
hooks
.
each
do
|
hook
|
client
.
edit_hook
(
repo
,
hook
.
id
,
hook
.
name
,
hook
.
config
,
options
)
end
end
def
create_ref
s
(
branches
)
def
restore_branche
s
(
branches
)
branches
.
each
do
|
name
,
sha
|
client
.
create_ref
(
repo
,
"refs/heads/
#{
name
}
"
,
sha
)
end
...
...
@@ -104,7 +122,7 @@ module Gitlab
project
.
repository
.
fetch_ref
(
repo_url
,
'+refs/heads/*'
,
'refs/heads/*'
)
end
def
delete_ref
s
(
branches
)
def
clean_up_restored_branche
s
(
branches
)
branches
.
each
do
|
name
,
_
|
client
.
delete_ref
(
repo
,
"heads/
#{
name
}
"
)
project
.
repository
.
rm_branch
(
project
.
creator
,
name
)
...
...
spec/lib/gitlab/github_import/hook_formatter_spec.rb
0 → 100644
View file @
659d5d48
require
'spec_helper'
describe
Gitlab
::
GithubImport
::
HookFormatter
,
lib:
true
do
describe
'#id'
do
it
'returns raw id'
do
raw
=
double
(
id:
100000
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
id
).
to
eq
100000
end
end
describe
'#name'
do
it
'returns raw id'
do
raw
=
double
(
name:
'web'
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
name
).
to
eq
'web'
end
end
describe
'#config'
do
it
'returns raw config.attrs'
do
raw
=
double
(
config:
double
(
attrs:
{
url:
'http://something.com/webhook'
}))
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
config
).
to
eq
({
url:
'http://something.com/webhook'
})
end
end
describe
'#valid?'
do
it
'returns true when events contains the wildcard event'
do
raw
=
double
(
events:
[
'*'
,
'commit_comment'
],
active:
true
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
true
end
it
'returns true when events contains the create event'
do
raw
=
double
(
events:
[
'create'
,
'commit_comment'
],
active:
true
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
true
end
it
'returns true when events contains delete event'
do
raw
=
double
(
events:
[
'delete'
,
'commit_comment'
],
active:
true
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
true
end
it
'returns true when events contains pull_request event'
do
raw
=
double
(
events:
[
'pull_request'
,
'commit_comment'
],
active:
true
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
true
end
it
'returns false when events does not contains branch related events'
do
raw
=
double
(
events:
[
'member'
,
'commit_comment'
],
active:
true
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
false
end
it
'returns false when hook is not active'
do
raw
=
double
(
events:
[
'pull_request'
,
'commit_comment'
],
active:
false
)
formatter
=
described_class
.
new
(
raw
)
expect
(
formatter
.
valid?
).
to
eq
false
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