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

proxy.rb « clients « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27030f5382a3077bbdbe1bda421a2f7efd25b83d (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
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Clients
      class Proxy
        attr_reader :client

        delegate :each_object, :user, :octokit, to: :client

        REPOS_COUNT_CACHE_KEY = 'github-importer/provider-repo-count/%{type}/%{user_id}'

        def initialize(access_token, client_options)
          @client = pick_client(access_token, client_options)
        end

        def repos(search_text, options)
          return { repos: filtered(client.repos, search_text) } if use_legacy?

          fetch_repos_via_graphql(search_text, options)
        end

        def count_repos_by(relation_type, user_id)
          return if use_legacy?

          key = format(REPOS_COUNT_CACHE_KEY, type: relation_type, user_id: user_id)

          ::Gitlab::Cache::Import::Caching.read_integer(key, timeout: 5.minutes) ||
            fetch_and_cache_repos_count_via_graphql(relation_type, key)
        end

        private

        def fetch_repos_via_graphql(search_text, options)
          response = client.search_repos_by_name_graphql(search_text, options)
          {
            repos: response.dig(:data, :search, :nodes),
            page_info: response.dig(:data, :search, :pageInfo),
            count: response.dig(:data, :search, :repositoryCount)
          }
        end

        def pick_client(access_token, client_options)
          return Gitlab::GithubImport::Client.new(access_token) unless use_legacy?

          Gitlab::LegacyGithubImport::Client.new(access_token, **client_options)
        end

        def filtered(collection, search_text)
          return collection if search_text.blank?

          collection.select { |item| item[:name].to_s.downcase.include?(search_text) }
        end

        def use_legacy?
          Feature.disabled?(:remove_legacy_github_client)
        end

        def fetch_and_cache_repos_count_via_graphql(relation_type, key)
          response = client.count_repos_by_relation_type_graphql(relation_type: relation_type)
          count = response.dig(:data, :search, :repositoryCount)

          ::Gitlab::Cache::Import::Caching.write(key, count, timeout: 5.minutes)
        end
      end
    end
  end
end