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

snippets_repository_pipeline.rb « pipelines « projects « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a371c33d9ea66c0198ecfeff1ab1d7f1013079a0 (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
66
67
68
69
70
71
72
73
# frozen_string_literal: true

module BulkImports
  module Projects
    module Pipelines
      class SnippetsRepositoryPipeline
        include Pipeline
        include HexdigestCacheStrategy

        extractor Common::Extractors::GraphqlExtractor, query: Graphql::GetSnippetRepositoryQuery

        def transform(_context, data)
          data.tap do |d|
            d['createdAt'] = DateTime.parse(data['createdAt'])
          end
        end

        def load(context, data)
          return unless data['httpUrlToRepo'].present?

          oauth2_url = oauth2(data['httpUrlToRepo'])
          validate_url(oauth2_url)

          matched_snippet = find_matched_snippet(data)
          # Skip snippets that we couldn't find a match. Probably because more snippets were
          # added after the migration had already started, namely after the SnippetsPipeline
          # has already run.
          return unless matched_snippet

          matched_snippet.create_repository
          matched_snippet.repository.fetch_as_mirror(oauth2_url)
          response = Snippets::RepositoryValidationService.new(nil, matched_snippet).execute

          # skips matched_snippet repository creation if repository is invalid
          return cleanup_snippet_repository(matched_snippet) if response.error?

          Snippets::UpdateStatisticsService.new(matched_snippet).execute
        end

        private

        def find_matched_snippet(data)
          Snippet.find_by_project_title_trunc_created_at(
            context.portable, data['title'], data['createdAt'])
        end

        def allow_local_requests?
          Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
        end

        def oauth2(url)
          url.sub("://", "://oauth2:#{context.configuration.access_token}@")
        end

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

        def cleanup_snippet_repository(snippet)
          snippet.repository.remove
          snippet.snippet_repository.delete
          snippet.repository.expire_exists_cache
        end
      end
    end
  end
end