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

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

119 lines
3.5 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
module Gitlab
module GithubImport
module Clients
module SearchRepos
def search_repos_by_name_graphql(name, options = {})
2023-05-27 22:25:52 +05:30
graphql_request(graphql_search_repos_body(name, options))
end
def count_repos_by_relation_type_graphql(options)
graphql_request(count_by_relation_type_query(options))
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
2023-05-27 22:25:52 +05:30
def graphql_request(query)
2023-03-04 22:38:38 +05:30
with_retry do
2023-05-27 22:25:52 +05:30
octokit.post(
'/graphql',
{ query: query }.to_json
).to_h
2023-03-04 22:38:38 +05:30
end
end
def graphql_search_repos_body(name, options)
2023-03-17 16:20:25 +05:30
query = search_repos_query(name, options)
2023-03-04 22:38:38 +05:30
query = "query: \"#{query}\""
first = options[:first].present? ? ", first: #{options[:first]}" : ''
after = options[:after].present? ? ", after: \"#{options[:after]}\"" : ''
<<-TEXT
{
search(type: REPOSITORY, #{query}#{first}#{after}) {
nodes {
__typename
... on Repository {
id: databaseId
name
full_name: nameWithOwner
owner { login }
}
}
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
2023-05-27 22:25:52 +05:30
},
repositoryCount
2023-03-04 22:38:38 +05:30
}
}
TEXT
end
2023-03-17 16:20:25 +05:30
def search_repos_query(string, options = {})
2023-07-09 08:55:56 +05:30
base = "#{string} in:name is:public,private fork:true"
2023-03-17 16:20:25 +05:30
case options[:relation_type]
when 'organization' then organization_repos_query(base, options)
when 'collaborated' then collaborated_repos_query(base)
when 'owned' then owned_repos_query(base)
# TODO: remove after https://gitlab.com/gitlab-org/gitlab/-/issues/385113 get done
else legacy_all_repos_query(base)
end
end
def organization_repos_query(search_string, options)
2023-05-27 22:25:52 +05:30
if options[:organization_login].present?
"#{search_string} org:#{options[:organization_login]}"
else
organizations_subquery
end
2023-03-17 16:20:25 +05:30
end
def collaborated_repos_query(search_string)
"#{search_string} #{collaborations_subquery}"
end
def owned_repos_query(search_string)
"#{search_string} user:#{octokit.user.to_h[:login]}"
end
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
def legacy_all_repos_query(search_string)
[
search_string,
"user:#{octokit.user.to_h[:login]}",
collaborations_subquery,
organizations_subquery
].join(' ')
end
def collaborations_subquery
each_object(:repos, nil, { affiliation: 'collaborator' })
.map { |repo| "repo:#{repo[:full_name]}" }
.join(' ')
end
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
def organizations_subquery
each_object(:organizations)
.map { |org| "org:#{org[:login]}" }
.join(' ')
2023-03-04 22:38:38 +05:30
end
2023-05-27 22:25:52 +05:30
def count_by_relation_type_query(options)
query = search_repos_query(nil, options)
query = "query: \"#{query}\""
<<-TEXT
{
search(type: REPOSITORY, #{query}) {
repositoryCount
}
}
TEXT
end
2023-03-04 22:38:38 +05:30
end
end
end
end