Commit 613fa619 authored by Pedro Pombeiro's avatar Pedro Pombeiro

GraphQL: Add projects property to CiRunner type

Changelog: added
parent 9ff0dafa
......@@ -65,6 +65,8 @@ module Types
feature_flag: :graphql_ci_runner_executor
field :groups, ::Types::GroupType.connection_type, null: true,
description: 'Groups the runner is associated with. For group runners only.'
field :projects, ::Types::ProjectType.connection_type, null: true,
description: 'Projects the runner is associated with. For project runners only.'
def job_count
# We limit to 1 above the JOB_COUNT_LIMIT to indicate that more items exist after JOB_COUNT_LIMIT
......@@ -112,6 +114,26 @@ module Types
end
end
end
def projects
return unless runner.project_type?
BatchLoader::GraphQL.for(runner.id).batch(key: :runner_projects) do |runner_ids, loader, args|
runner_and_project_ids =
::Ci::RunnerProject
.where(runner_id: runner_ids)
.pluck(:runner_id, :project_id)
project_ids_by_runner_id = runner_and_project_ids.group_by(&:first).transform_values { |v| v.pluck(1) }
project_ids = runner_and_project_ids.pluck(1).uniq
projects = Project.where(id: project_ids).index_by(&:id)
runner_ids.each do |runner_id|
loader.call(runner_id, project_ids_by_runner_id[runner_id]&.map { |project_id| projects[project_id] } || [])
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
private
......
......@@ -9030,6 +9030,7 @@ Represents the total number of issues and their weights for a particular day.
| <a id="cirunnermaximumtimeout"></a>`maximumTimeout` | [`Int`](#int) | Maximum timeout (in seconds) for jobs processed by the runner. |
| <a id="cirunnerprivateprojectsminutescostfactor"></a>`privateProjectsMinutesCostFactor` | [`Float`](#float) | Private projects' "minutes cost factor" associated with the runner (GitLab.com only). |
| <a id="cirunnerprojectcount"></a>`projectCount` | [`Int`](#int) | Number of projects that the runner is associated with. |
| <a id="cirunnerprojects"></a>`projects` | [`ProjectConnection`](#projectconnection) | Projects the runner is associated with. For project runners only. (see [Connections](#connections)) |
| <a id="cirunnerpublicprojectsminutescostfactor"></a>`publicProjectsMinutesCostFactor` | [`Float`](#float) | Public projects' "minutes cost factor" associated with the runner (GitLab.com only). |
| <a id="cirunnerrevision"></a>`revision` | [`String`](#string) | Revision of the runner. |
| <a id="cirunnerrununtagged"></a>`runUntagged` | [`Boolean!`](#boolean) | Indicates the runner is able to run untagged jobs. |
......@@ -12,7 +12,7 @@ RSpec.describe GitlabSchema.types['CiRunner'] do
id description created_at contacted_at maximum_timeout access_level active status
version short_sha revision locked run_untagged ip_address runner_type tag_list
project_count job_count admin_url edit_admin_url user_permissions executor_name
groups
groups projects
]
expect(described_class).to include_graphql_fields(*expected_fields)
......
......@@ -302,21 +302,36 @@ RSpec.describe 'Query.runner(id)' do
let!(:job) { create(:ci_build, runner: project_runner1) }
context 'requesting project and job counts' do
context 'requesting projects and counts for projects and jobs' do
let(:query) do
%(
query {
projectRunner1: runner(id: "#{project_runner1.to_global_id}") {
projectCount
jobCount
projects {
nodes {
id
}
}
}
projectRunner2: runner(id: "#{project_runner2.to_global_id}") {
projectCount
jobCount
projects {
nodes {
id
}
}
}
activeInstanceRunner: runner(id: "#{active_instance_runner.to_global_id}") {
projectCount
jobCount
projects {
nodes {
id
}
}
}
}
)
......@@ -335,13 +350,23 @@ RSpec.describe 'Query.runner(id)' do
expect(runner1_data).to match a_hash_including(
'jobCount' => 1,
'projectCount' => 2)
'projectCount' => 2,
'projects' => {
'nodes' => [
{ 'id' => project1.to_global_id.to_s },
{ 'id' => project2.to_global_id.to_s }
]
})
expect(runner2_data).to match a_hash_including(
'jobCount' => 0,
'projectCount' => 0)
'projectCount' => 0,
'projects' => {
'nodes' => []
})
expect(runner3_data).to match a_hash_including(
'jobCount' => 0,
'projectCount' => nil)
'projectCount' => nil,
'projects' => nil)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment