Commit 7e5d436c authored by Patrick Bair's avatar Patrick Bair

Merge branch 'pedropombeiro/349897-graphql-add-groups-to-runner-type' into 'master'

GraphQL: Add groups property to CiRunner type

See merge request gitlab-org/gitlab!78454
parents a19fba1b 1101f569
...@@ -63,6 +63,8 @@ module Types ...@@ -63,6 +63,8 @@ module Types
description: 'Executor last advertised by the runner.', description: 'Executor last advertised by the runner.',
method: :executor_name, method: :executor_name,
feature_flag: :graphql_ci_runner_executor 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.'
def job_count def job_count
# We limit to 1 above the JOB_COUNT_LIMIT to indicate that more items exist after JOB_COUNT_LIMIT # We limit to 1 above the JOB_COUNT_LIMIT to indicate that more items exist after JOB_COUNT_LIMIT
...@@ -92,6 +94,24 @@ module Types ...@@ -92,6 +94,24 @@ module Types
end end
end end
end end
def groups
BatchLoader::GraphQL.for(runner.id).batch(key: :runner_groups) do |runner_ids, loader, args|
runner_and_namespace_ids =
::Ci::RunnerNamespace
.where(runner_id: runner_ids)
.pluck(:runner_id, :namespace_id)
group_ids_by_runner_id = runner_and_namespace_ids.group_by(&:first).transform_values { |v| v.pluck(1) }
group_ids = runner_and_namespace_ids.pluck(1).uniq
groups = Group.where(id: group_ids).index_by(&:id)
runner_ids.each do |runner_id|
loader.call(runner_id, group_ids_by_runner_id[runner_id]&.map { |group_id| groups[group_id] })
end
end
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
private private
......
...@@ -9022,6 +9022,7 @@ Represents the total number of issues and their weights for a particular day. ...@@ -9022,6 +9022,7 @@ Represents the total number of issues and their weights for a particular day.
| <a id="cirunnerdescription"></a>`description` | [`String`](#string) | Description of the runner. | | <a id="cirunnerdescription"></a>`description` | [`String`](#string) | Description of the runner. |
| <a id="cirunnereditadminurl"></a>`editAdminUrl` | [`String`](#string) | Admin form URL of the runner. Only available for administrators. | | <a id="cirunnereditadminurl"></a>`editAdminUrl` | [`String`](#string) | Admin form URL of the runner. Only available for administrators. |
| <a id="cirunnerexecutorname"></a>`executorName` | [`String`](#string) | Executor last advertised by the runner. Available only when feature flag `graphql_ci_runner_executor` is enabled. This flag is disabled by default, because the feature is experimental and is subject to change without notice. | | <a id="cirunnerexecutorname"></a>`executorName` | [`String`](#string) | Executor last advertised by the runner. Available only when feature flag `graphql_ci_runner_executor` is enabled. This flag is disabled by default, because the feature is experimental and is subject to change without notice. |
| <a id="cirunnergroups"></a>`groups` | [`GroupConnection`](#groupconnection) | Groups the runner is associated with. For group runners only. (see [Connections](#connections)) |
| <a id="cirunnerid"></a>`id` | [`CiRunnerID!`](#cirunnerid) | ID of the runner. | | <a id="cirunnerid"></a>`id` | [`CiRunnerID!`](#cirunnerid) | ID of the runner. |
| <a id="cirunneripaddress"></a>`ipAddress` | [`String`](#string) | IP address of the runner. | | <a id="cirunneripaddress"></a>`ipAddress` | [`String`](#string) | IP address of the runner. |
| <a id="cirunnerjobcount"></a>`jobCount` | [`Int`](#int) | Number of jobs processed by the runner (limited to 1000, plus one to indicate that more items exist). | | <a id="cirunnerjobcount"></a>`jobCount` | [`Int`](#int) | Number of jobs processed by the runner (limited to 1000, plus one to indicate that more items exist). |
...@@ -12,6 +12,7 @@ RSpec.describe GitlabSchema.types['CiRunner'] do ...@@ -12,6 +12,7 @@ RSpec.describe GitlabSchema.types['CiRunner'] do
id description created_at contacted_at maximum_timeout access_level active status 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 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 project_count job_count admin_url edit_admin_url user_permissions executor_name
groups
] ]
expect(described_class).to include_graphql_fields(*expected_fields) expect(described_class).to include_graphql_fields(*expected_fields)
......
...@@ -223,6 +223,29 @@ RSpec.describe 'Query.runner(id)' do ...@@ -223,6 +223,29 @@ RSpec.describe 'Query.runner(id)' do
end end
end end
describe 'for group runner request' do
let(:query) do
%(
query {
runner(id: "gid://gitlab/Ci::Runner/#{active_group_runner.id}") {
groups {
nodes {
id
}
}
}
}
)
end
it 'retrieves groups field with expected value' do
post_graphql(query, current_user: user)
runner_data = graphql_data_at(:runner, :groups)
expect(runner_data).to eq 'nodes' => [{ 'id' => group.to_global_id.to_s }]
end
end
describe 'for runner with status' do describe 'for runner with status' do
let_it_be(:stale_runner) { create(:ci_runner, description: 'Stale runner 1', created_at: 3.months.ago) } let_it_be(:stale_runner) { create(:ci_runner, description: 'Stale runner 1', created_at: 3.months.ago) }
let_it_be(:never_contacted_instance_runner) { create(:ci_runner, description: 'Missing runner 1', created_at: 1.month.ago, contacted_at: nil) } let_it_be(:never_contacted_instance_runner) { create(:ci_runner, description: 'Missing runner 1', created_at: 1.month.ago, contacted_at: nil) }
...@@ -326,9 +349,15 @@ RSpec.describe 'Query.runner(id)' do ...@@ -326,9 +349,15 @@ RSpec.describe 'Query.runner(id)' do
describe 'by regular user' do describe 'by regular user' do
let(:user) { create(:user) } let(:user) { create(:user) }
context 'on instance runner' do
it_behaves_like 'retrieval by unauthorized user', :active_instance_runner it_behaves_like 'retrieval by unauthorized user', :active_instance_runner
end end
context 'on group runner' do
it_behaves_like 'retrieval by unauthorized user', :active_group_runner
end
end
describe 'by non-admin user' do describe 'by non-admin user' do
let(:user) { create(:user) } let(:user) { create(:user) }
......
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