Skip to content
代码片段 群组 项目
未验证 提交 dbe0b13b 编辑于 作者: Stefanos Xanthopoulos's avatar Stefanos Xanthopoulos 提交者: GitLab
浏览文件

Filter custom fields by field type

Add ability to filter the `customFields` GraphQL field by `fieldType`.
Changelog: added
EE: true
上级 f802775a
No related branches found
No related tags found
无相关合并请求
......@@ -26737,6 +26737,7 @@ four standard [pagination arguments](#pagination-arguments):
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="groupcustomfieldsactive"></a>`active` | [`Boolean`](#boolean) | Filter for active fields. If `false`, excludes active fields. If `true`, returns only active fields. |
| <a id="groupcustomfieldsfieldtype"></a>`fieldType` | [`CustomFieldType`](#customfieldtype) | Filter for selected field type. |
| <a id="groupcustomfieldssearch"></a>`search` | [`String`](#string) | Search query for custom field name. |
| <a id="groupcustomfieldsworkitemtypeids"></a>`workItemTypeIds` | [`[WorkItemsTypeID!]`](#workitemstypeid) | Filter custom fields associated to the given work item types. If empty, returns custom fields not associated to any work item type. |
 
......@@ -15,7 +15,7 @@ def self.active_fields_for_work_item(work_item)
end
def initialize(
current_user, group:, active: nil, search: nil, work_item_type_ids: nil,
current_user, group:, active: nil, field_type: nil, search: nil, work_item_type_ids: nil,
skip_permissions_check: false
)
raise ArgumentError, 'group argument is missing' if group.nil?
......@@ -23,6 +23,7 @@ def initialize(
@current_user = current_user
@group = group
@active = active
@field_type = field_type
@search = search
@work_item_type_ids = work_item_type_ids
@skip_permissions_check = skip_permissions_check
......@@ -38,6 +39,7 @@ def execute
items = Issuables::CustomField.of_namespace(@group)
items = by_status(items)
items = by_search(items)
items = by_field_type(items)
items = by_work_item_type_ids(items)
items.ordered_by_status_and_name
end
......@@ -65,5 +67,11 @@ def by_work_item_type_ids(items)
items.with_work_item_types(@work_item_type_ids)
end
def by_field_type(items)
return items if @field_type.nil?
items.of_field_type(@field_type)
end
end
end
......@@ -12,6 +12,10 @@ class CustomFieldsResolver < BaseResolver
description: 'Filter for active fields. If `false`, excludes active fields. ' \
'If `true`, returns only active fields.'
argument :field_type, ::Types::Issuables::CustomFieldTypeEnum,
required: false,
description: 'Filter for selected field type.'
argument :search, GraphQL::Types::String,
required: false,
description: 'Search query for custom field name.'
......@@ -22,13 +26,14 @@ class CustomFieldsResolver < BaseResolver
'If empty, returns custom fields not associated to any work item type.',
prepare: ->(global_ids, _ctx) { global_ids.map(&:model_id) }
def resolve_with_lookahead(active: nil, search: nil, work_item_type_ids: nil)
def resolve_with_lookahead(active: nil, field_type: nil, search: nil, work_item_type_ids: nil)
work_item_type_ids = work_item_type_ids_from(work_item_type_ids) unless work_item_type_ids.nil?
custom_fields = ::Issuables::CustomFieldsFinder.new(
current_user,
group: object,
active: active,
field_type: field_type,
search: search,
work_item_type_ids: work_item_type_ids
).execute
......
......@@ -40,6 +40,7 @@ class CustomField < ApplicationRecord
scope :active, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
scope :ordered_by_status_and_name, -> { order(Arel.sql('archived_at IS NULL').desc, name: :asc) }
scope :of_field_type, ->(field_type) { where(field_type: field_type) }
class << self
def without_any_work_item_types
......
......@@ -9,5 +9,9 @@
trait :archived do
archived_at { Time.current }
end
trait :number do
field_type { :number }
end
end
end
......@@ -102,6 +102,27 @@
end
end
context "when filtering by field type" do
let_it_be(:custom_field_number) { create(:custom_field, :number, namespace: group, name: 'number field') }
context "when field type is nil" do
let(:params) { {} }
it 'returns all custom fields for the group' do
expect(custom_fields).to contain_exactly(custom_field_1, custom_field_2, custom_field_archived,
custom_field_number)
end
end
context "when field is has a value" do
let(:params) { { field_type: "number" } }
it 'returns custom fields of type number' do
expect(custom_fields).to contain_exactly(custom_field_number)
end
end
end
context 'when group is nil' do
let(:group) { nil }
......
......@@ -316,6 +316,14 @@
end
end
describe ".of_field_type" do
let_it_be(:custom_field_number) { create(:custom_field, :number, namespace: group) }
it "returns custom field of a given field type" do
expect(described_class.of_field_type("number")).to contain_exactly(custom_field_number)
end
end
describe 'work item type scopes' do
let_it_be(:issue_type) { create(:work_item_type, :issue) }
let_it_be(:task_type) { create(:work_item_type, :task) }
......
......@@ -33,10 +33,10 @@
let(:query) do
<<~QUERY
query($active: Boolean, $search: String, $workItemTypeIds: [WorkItemsTypeID!]) {
query($active: Boolean, $fieldType: CustomFieldType, $search: String, $workItemTypeIds: [WorkItemsTypeID!]) {
group(fullPath: "#{group.full_path}") {
id
customFields(active: $active, search: $search, workItemTypeIds: $workItemTypeIds) {
customFields(active: $active, fieldType: $fieldType, search: $search, workItemTypeIds: $workItemTypeIds) {
nodes {
id
name
......@@ -111,6 +111,17 @@
end
end
context "when filtering by field type" do
it 'returns fields of the given type' do
post_graphql(query, current_user: guest, variables: { fieldType: 'TEXT' })
expect(response).to have_gitlab_http_status(:ok)
expect(graphql_data_at(:group, :customFields, :nodes)).to match([
custom_field_attributes(text_field)
])
end
end
context 'when searching by name' do
it 'returns matching fields' do
post_graphql(query, current_user: guest, variables: { search: 'field' })
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册