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

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

67 lines
2.1 KiB
Ruby
Raw Normal View History

2022-07-23 23:45:48 +05:30
# frozen_string_literal: true
module Resolvers
module Ci
class RunnerOwnerProjectResolver < BaseResolver
include LooksAhead
type Types::ProjectType, null: true
alias_method :runner, :object
2022-10-11 01:57:18 +05:30
def resolve_with_lookahead(**_args)
2022-07-23 23:45:48 +05:30
resolve_owner
end
2022-10-11 01:57:18 +05:30
private
2023-03-04 22:38:38 +05:30
def node_selection(selection = lookahead)
# There are no nodes or edges selections in RunnerOwnerProjectResolver, but rather a project directly
selection
end
def unconditional_includes
[:project_feature]
end
2022-07-23 23:45:48 +05:30
2023-03-04 22:38:38 +05:30
def preloads
{
full_path: [:route, { namespace: [:route] }],
web_url: [:route, { namespace: [:route] }]
}
2022-07-23 23:45:48 +05:30
end
def resolve_owner
return unless runner.project_type?
BatchLoader::GraphQL.for(runner.id).batch(key: :runner_owner_projects) do |runner_ids, loader|
# rubocop: disable CodeReuse/ActiveRecord
runner_and_projects_with_row_number =
::Ci::RunnerProject
.where(runner_id: runner_ids)
.select('id, runner_id, project_id, ROW_NUMBER() OVER (PARTITION BY runner_id ORDER BY id ASC)')
runner_and_owner_projects =
::Ci::RunnerProject
.select(:id, :runner_id, :project_id)
.from("(#{runner_and_projects_with_row_number.to_sql}) temp WHERE row_number = 1")
owner_project_id_by_runner_id =
runner_and_owner_projects
.group_by(&:runner_id)
.transform_values { |runner_projects| runner_projects.first.project_id }
project_ids = owner_project_id_by_runner_id.values.uniq
2023-03-04 22:38:38 +05:30
projects = apply_lookahead(Project.id_in(project_ids))
2022-10-11 01:57:18 +05:30
Preloaders::ProjectPolicyPreloader.new(projects, current_user).execute
projects_by_id = projects.index_by(&:id)
2022-07-23 23:45:48 +05:30
runner_ids.each do |runner_id|
owner_project_id = owner_project_id_by_runner_id[runner_id]
2022-10-11 01:57:18 +05:30
loader.call(runner_id, projects_by_id[owner_project_id])
2022-07-23 23:45:48 +05:30
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
end
end