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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# frozen_string_literal: true
module Gitlab
module GithubImport
module Clients
module SearchRepos
def search_repos_by_name_graphql(name, options = {})
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))
end
private
def graphql_request(query)
with_retry do
octokit.post(
'/graphql',
{ query: query }.to_json
).to_h
end
end
def graphql_search_repos_body(name, options)
query = search_repos_query(name, options)
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
},
repositoryCount
}
}
TEXT
end
def search_repos_query(string, options = {})
base = "#{string} in:name is:public,private fork:true"
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)
if options[:organization_login].present?
"#{search_string} org:#{options[:organization_login]}"
else
organizations_subquery
end
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
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
def organizations_subquery
each_object(:organizations)
.map { |org| "org:#{org[:login]}" }
.join(' ')
end
def count_by_relation_type_query(options)
query = search_repos_query(nil, options)
query = "query: \"#{query}\""
<<-TEXT
{
search(type: REPOSITORY, #{query}) {
repositoryCount
}
}
TEXT
end
end
end
end
end
|