PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/bulk_imports/common/pipelines/wiki_pipeline.rb

https://gitlab.com/gitlab-r2devops/gitlab-foss
Ruby | 55 lines | 40 code | 14 blank | 1 comment | 1 complexity | f62a90f715ec0ade05e3c6103317dfed MD5 | raw file
  1. # frozen_string_literal: true
  2. module BulkImports
  3. module Common
  4. module Pipelines
  5. class WikiPipeline
  6. include Pipeline
  7. def extract(*)
  8. url = url_from_parent_path(context.entity.source_full_path) if source_wiki_exists?
  9. BulkImports::Pipeline::ExtractedData.new(data: { url: url })
  10. end
  11. def transform(_, data)
  12. data&.slice(:url)
  13. end
  14. def load(context, data)
  15. return unless data&.dig(:url)
  16. wiki = context.portable.wiki
  17. url = data[:url].sub("://", "://oauth2:#{context.configuration.access_token}@")
  18. Gitlab::UrlBlocker.validate!(url, allow_local_network: allow_local_requests?, allow_localhost: allow_local_requests?)
  19. wiki.ensure_repository
  20. wiki.repository.fetch_as_mirror(url)
  21. end
  22. private
  23. def url_from_parent_path(parent_path)
  24. wiki_path = parent_path + ".wiki.git"
  25. root = context.configuration.url
  26. Gitlab::Utils.append_path(root, wiki_path)
  27. end
  28. def allow_local_requests?
  29. Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
  30. end
  31. def source_wiki_exists?
  32. wikis = client.get(context.entity.wikis_url_path).parsed_response
  33. wikis.any?
  34. end
  35. def client
  36. BulkImports::Clients::HTTP.new(url: context.configuration.url, token: context.configuration.access_token)
  37. end
  38. end
  39. end
  40. end
  41. end