Welcome to mirror list, hosted at ThFree Co, Russian Federation.

wiki_pipeline.rb « pipelines « common « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 429a28dcb4c8eddf70793094a8f81e8ef2e4d9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module BulkImports
  module Common
    module Pipelines
      class WikiPipeline
        include Pipeline

        def extract(*)
          url = url_from_parent_path(context.entity.source_full_path) if source_wiki_exists?

          BulkImports::Pipeline::ExtractedData.new(data: { url: url })
        end

        def transform(_, data)
          data&.slice(:url)
        end

        def load(context, data)
          return unless data&.dig(:url)

          wiki = context.portable.wiki
          url = data[:url].sub("://", "://oauth2:#{context.configuration.access_token}@")

          Gitlab::HTTP_V2::UrlBlocker.validate!(
            url,
            schemes: %w[http https],
            allow_local_network: allow_local_requests?,
            allow_localhost: allow_local_requests?,
            deny_all_requests_except_allowed: Gitlab::CurrentSettings.deny_all_requests_except_allowed?)

          wiki.create_wiki_repository
          wiki.repository.fetch_as_mirror(url)
        end

        private

        def url_from_parent_path(parent_path)
          wiki_path = parent_path + ".wiki.git"
          root = context.configuration.url
          Gitlab::Utils.append_path(root, wiki_path)
        end

        def allow_local_requests?
          Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
        end

        def source_wiki_exists?
          wikis = client.get(context.entity.wikis_url_path).parsed_response

          wikis.any?
        rescue BulkImports::NetworkError => e
          # 403 is returned when wiki is disabled in settings
          return if e.response&.forbidden? || e.response&.not_found?

          raise
        end

        def client
          BulkImports::Clients::HTTP.new(url: context.configuration.url, token: context.configuration.access_token)
        end
      end
    end
  end
end