Class: Resolvers::Ci::RunnerProjectsResolver

Inherits:
BaseResolver
  • Object
show all
Includes:
Gitlab::Graphql::Authorize::AuthorizeResource, LooksAhead, ProjectSearchArguments
Defined in:
app/graphql/resolvers/ci/runner_projects_resolver.rb

Constant Summary

Constants included from Gitlab::Graphql::Authorize::AuthorizeResource

Gitlab::Graphql::Authorize::AuthorizeResource::ConfigurationError, Gitlab::Graphql::Authorize::AuthorizeResource::RESOURCE_ACCESS_ERROR

Instance Method Summary collapse

Methods included from LooksAhead

#apply_lookahead, #resolve

Methods included from Gitlab::Graphql::Authorize::AuthorizeResource

#authorize!, #authorized_find!, #authorized_resource?, #find_object, #raise_resource_not_available_error!

Methods inherited from BaseResolver

as_single, authorization, authorized?, before_connection_authorization, before_connection_authorization_block, calculate_ext_conn_complexity, calls_gitaly!, complexity, complexity_multiplier, #current_user, field_options, last, #object, #offset_pagination, requires_argument!, resolver_complexity, #select_result, single, #single?, single_definition_blocks, singular_type, when_single

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Instance Method Details

#resolve_with_lookahead(**args) ⇒ Object



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
# File 'app/graphql/resolvers/ci/runner_projects_resolver.rb', line 27

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)

    unique_project_ids = plucked_runner_and_project_ids.collect { |_runner_id, project_id| project_id }.uniq
    projects = ProjectsFinder
                 .new(current_user: current_user,
                      params: project_finder_params(args),
                      project_ids_relation: unique_project_ids)
                 .execute
    projects = apply_lookahead(projects)
    Preloaders::ProjectPolicyPreloader.new(projects, current_user).execute
    sorted_project_ids = projects.map(&:id)
    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
    project_ids_by_runner_id =
      plucked_runner_and_project_ids
        .group_by(&:first)
        .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) }

    runner_ids.each do |runner_id|
      runner_project_ids = sorted_project_ids_by_runner_id[runner_id] || []
      runner_projects = runner_project_ids.map { |id| projects_by_id[id] }

      loader.call(runner_id, runner_projects)
    end
  end
  # rubocop:enable CodeReuse/ActiveRecord
end