debian-mirror-gitlab/app/graphql/resolvers/ci/runner_projects_resolver.rb

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

83 lines
3.3 KiB
Ruby
Raw Normal View History

2022-10-11 01:57:18 +05:30
# frozen_string_literal: true
module Resolvers
module Ci
class RunnerProjectsResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
include LooksAhead
include ProjectSearchArguments
type Types::ProjectType.connection_type, null: true
authorize :read_runner
authorizes_object!
alias_method :runner, :object
argument :sort, GraphQL::Types::String,
required: false,
2023-07-09 08:55:56 +05:30
default_value: 'id_asc', # TODO: Remove in %17.0 and move :sort to ProjectSearchArguments, see https://gitlab.com/gitlab-org/gitlab/-/issues/372117
2022-10-11 01:57:18 +05:30
deprecated: {
2023-07-09 08:55:56 +05:30
reason: 'Default sort order will change in GitLab 17.0. ' \
'Specify `"id_asc"` if you require the query results to be ordered by ascending IDs',
2022-10-11 01:57:18 +05:30
milestone: '15.4'
},
2022-11-25 23:54:43 +05:30
description: "Sort order of results. Format: `<field_name>_<sort_direction>`, " \
"for example: `id_desc` or `name_asc`"
2022-10-11 01:57:18 +05:30
def resolve_with_lookahead(**args)
return unless runner.project_type?
# rubocop:disable CodeReuse/ActiveRecord
BatchLoader::GraphQL.for(runner.id).batch(key: :runner_projects) do |runner_ids, loader|
plucked_runner_and_project_ids = ::Ci::RunnerProject
.select(:runner_id, :project_id)
.where(runner_id: runner_ids)
.pluck(:runner_id, :project_id)
2023-06-20 00:43:36 +05:30
unique_project_ids = plucked_runner_and_project_ids.collect { |_runner_id, project_id| project_id }.uniq
2022-10-11 01:57:18 +05:30
projects = ProjectsFinder
.new(current_user: current_user,
params: project_finder_params(args),
2023-06-20 00:43:36 +05:30
project_ids_relation: unique_project_ids)
2022-10-11 01:57:18 +05:30
.execute
2023-03-04 22:38:38 +05:30
projects = apply_lookahead(projects)
2022-10-11 01:57:18 +05:30
Preloaders::ProjectPolicyPreloader.new(projects, current_user).execute
2023-06-20 00:43:36 +05:30
sorted_project_ids = projects.map(&:id)
2022-10-11 01:57:18 +05:30
projects_by_id = projects.index_by(&:id)
# In plucked_runner_and_project_ids, first() represents the runner ID, and second() the project ID,
# so let's group the project IDs by runner ID
2023-06-20 00:43:36 +05:30
project_ids_by_runner_id =
2022-10-11 01:57:18 +05:30
plucked_runner_and_project_ids
.group_by(&:first)
2023-06-20 00:43:36 +05:30
.transform_values { |runner_id_and_project_id| runner_id_and_project_id.map(&:second) }
# Reorder the project IDs according to the order in sorted_project_ids
sorted_project_ids_by_runner_id =
project_ids_by_runner_id.transform_values { |project_ids| sorted_project_ids.intersection(project_ids) }
2022-10-11 01:57:18 +05:30
runner_ids.each do |runner_id|
2023-06-20 00:43:36 +05:30
runner_project_ids = sorted_project_ids_by_runner_id[runner_id] || []
runner_projects = runner_project_ids.map { |id| projects_by_id[id] }
2022-10-11 01:57:18 +05:30
loader.call(runner_id, runner_projects)
end
end
# rubocop:enable CodeReuse/ActiveRecord
end
2023-03-04 22:38:38 +05:30
private
def unconditional_includes
[:project_feature]
end
def preloads
super.merge({
2023-05-27 22:25:52 +05:30
full_path: [:route, { namespace: [:route] }],
web_url: [:route, { namespace: [:route] }]
})
2023-03-04 22:38:38 +05:30
end
2022-10-11 01:57:18 +05:30
end
end
end