debian-mirror-gitlab/lib/gitlab/github_import/clients/proxy.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
2.1 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
module Clients
class Proxy
attr_reader :client
2023-05-27 22:25:52 +05:30
delegate :each_object, :user, :octokit, to: :client
REPOS_COUNT_CACHE_KEY = 'github-importer/provider-repo-count/%{type}/%{user_id}'
2023-03-04 22:38:38 +05:30
def initialize(access_token, client_options)
@client = pick_client(access_token, client_options)
end
2023-03-17 16:20:25 +05:30
def repos(search_text, options)
2023-03-04 22:38:38 +05:30
return { repos: filtered(client.repos, search_text) } if use_legacy?
2023-05-27 22:25:52 +05:30
fetch_repos_via_graphql(search_text, options)
2023-03-04 22:38:38 +05:30
end
2023-05-27 22:25:52 +05:30
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)
2023-03-04 22:38:38 +05:30
2023-05-27 22:25:52 +05:30
::Gitlab::Cache::Import::Caching.read_integer(key, timeout: 5.minutes) ||
fetch_and_cache_repos_count_via_graphql(relation_type, key)
2023-03-04 22:38:38 +05:30
end
2023-05-27 22:25:52 +05:30
private
2023-03-17 16:20:25 +05:30
def fetch_repos_via_graphql(search_text, options)
response = client.search_repos_by_name_graphql(search_text, options)
2023-03-04 22:38:38 +05:30
{
repos: response.dig(:data, :search, :nodes),
2023-05-27 22:25:52 +05:30
page_info: response.dig(:data, :search, :pageInfo),
count: response.dig(:data, :search, :repositoryCount)
2023-03-04 22:38:38 +05:30
}
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
2023-05-27 22:25:52 +05:30
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)
2023-03-04 22:38:38 +05:30
end
end
end
end
end