diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-06-07 13:06:43 +0200 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-06-07 13:06:43 +0200 |
commit | 8c5712a4222eb9c941384f3e11f38189898a2bcb (patch) | |
tree | 9d24605d47ff1dc27c34b0d80a9752e618e871ad /lib | |
parent | acef028d85f18f3c6fcb405ce91f540fcf0f3d69 (diff) | |
parent | 1fb1e64fdd3c608f265a59607c748ba4a999d928 (diff) | |
download | gitlab-ce-8c5712a4222eb9c941384f3e11f38189898a2bcb.tar.gz |
Merge branch 'gh-disable-webhooks'
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/github_import/hook_formatter.rb | 23 | ||||
-rw-r--r-- | lib/gitlab/github_import/importer.rb | 26 |
2 files changed, 45 insertions, 4 deletions
diff --git a/lib/gitlab/github_import/hook_formatter.rb b/lib/gitlab/github_import/hook_formatter.rb new file mode 100644 index 00000000000..db1fabaa18a --- /dev/null +++ b/lib/gitlab/github_import/hook_formatter.rb @@ -0,0 +1,23 @@ +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 diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index a2ee56bee89..442b4c389fe 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -109,6 +109,9 @@ module Gitlab end def import_pull_requests + hooks = client.hooks(repo).map { |raw| HookFormatter.new(raw) }.select(&:valid?) + disable_webhooks(hooks) + pull_requests = paginate { client.pull_requests(repo, state: :all, sort: :created, direction: :asc, per_page: 100) } pull_requests = pull_requests.map { |raw| PullRequestFormatter.new(project, raw) }.select(&:valid?) @@ -116,7 +119,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_refs(branches_removed) + restore_branches(branches_removed) pull_requests.each do |pull_request| merge_request = pull_request.create! @@ -129,10 +132,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_refs(branches) + def restore_branches(branches) branches.each do |name, sha| sleep rate_limit_sleep_time if rate_limit_exceed? client.create_ref(repo, "refs/heads/#{name}", sha) @@ -141,7 +159,7 @@ module Gitlab project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*') end - def delete_refs(branches) + def clean_up_restored_branches(branches) branches.each do |name, _| sleep rate_limit_sleep_time if rate_limit_exceed? client.delete_ref(repo, "heads/#{name}") |