Commit 5cd6d498 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC Committed by James Lopez

Add standalone_vulnerabilities_enabled filter to API

This filter will be used to return only the projects which are using
the standalone vulnerabilities.

Related to https://gitlab.com/gitlab-org/gitlab/-/issues/213610
parent 839037bf
...@@ -29,3 +29,5 @@ module Resolvers ...@@ -29,3 +29,5 @@ module Resolvers
end end
end end
end end
Resolvers::NamespaceProjectsResolver.prepend_if_ee('::EE::Resolvers::NamespaceProjectsResolver')
...@@ -4156,6 +4156,11 @@ type Group { ...@@ -4156,6 +4156,11 @@ type Group {
""" """
first: Int first: Int
"""
Returns only the projects which have vulnerabilities
"""
hasVulnerabilities: Boolean = false
""" """
Include also subgroup projects Include also subgroup projects
""" """
...@@ -6321,6 +6326,11 @@ type Namespace { ...@@ -6321,6 +6326,11 @@ type Namespace {
""" """
first: Int first: Int
"""
Returns only the projects which have vulnerabilities
"""
hasVulnerabilities: Boolean = false
""" """
Include also subgroup projects Include also subgroup projects
""" """
......
...@@ -11538,6 +11538,16 @@ ...@@ -11538,6 +11538,16 @@
}, },
"defaultValue": "false" "defaultValue": "false"
}, },
{
"name": "hasVulnerabilities",
"description": "Returns only the projects which have vulnerabilities",
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"defaultValue": "false"
},
{ {
"name": "after", "name": "after",
"description": "Returns the elements in the list that come after the specified cursor.", "description": "Returns the elements in the list that come after the specified cursor.",
...@@ -18773,6 +18783,16 @@ ...@@ -18773,6 +18783,16 @@
}, },
"defaultValue": "false" "defaultValue": "false"
}, },
{
"name": "hasVulnerabilities",
"description": "Returns only the projects which have vulnerabilities",
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"defaultValue": "false"
},
{ {
"name": "after", "name": "after",
"description": "Returns the elements in the list that come after the specified cursor.", "description": "Returns the elements in the list that come after the specified cursor.",
......
# frozen_string_literal: true
module EE
module Resolvers
module NamespaceProjectsResolver
extend ActiveSupport::Concern
prepended do
argument :has_vulnerabilities, GraphQL::BOOLEAN_TYPE,
required: false,
default_value: false,
description: 'Returns only the projects which have vulnerabilities'
end
def resolve(include_subgroups:, has_vulnerabilities: false)
projects = super(include_subgroups: include_subgroups)
has_vulnerabilities ? projects.has_vulnerabilities : projects
end
end
end
end
...@@ -30,7 +30,7 @@ module EE ...@@ -30,7 +30,7 @@ module EE
::Types::VulnerabilityType.connection_type, ::Types::VulnerabilityType.connection_type,
null: true, null: true,
description: 'Vulnerabilities reported on the projects in the group and its subgroups', description: 'Vulnerabilities reported on the projects in the group and its subgroups',
resolver: Resolvers::VulnerabilitiesResolver resolver: ::Resolvers::VulnerabilitiesResolver
end end
end end
end end
......
...@@ -16,7 +16,7 @@ module EE ...@@ -16,7 +16,7 @@ module EE
::Types::VulnerabilityType.connection_type, ::Types::VulnerabilityType.connection_type,
null: true, null: true,
description: 'Vulnerabilities reported on the project', description: 'Vulnerabilities reported on the project',
resolver: Resolvers::VulnerabilitiesResolver resolver: ::Resolvers::VulnerabilitiesResolver
field :vulnerability_severities_count, ::Types::VulnerabilitySeveritiesCountType, null: true, field :vulnerability_severities_count, ::Types::VulnerabilitySeveritiesCountType, null: true,
description: 'Counts for each severity of vulnerability of the project', description: 'Counts for each severity of vulnerability of the project',
......
...@@ -13,7 +13,7 @@ module EE ...@@ -13,7 +13,7 @@ module EE
::Types::VulnerabilityType.connection_type, ::Types::VulnerabilityType.connection_type,
null: true, null: true,
description: "Vulnerabilities reported on projects on the current user's instance security dashboard", description: "Vulnerabilities reported on projects on the current user's instance security dashboard",
resolver: Resolvers::VulnerabilitiesResolver resolver: ::Resolvers::VulnerabilitiesResolver
field :design_management, ::Types::DesignManagementType, field :design_management, ::Types::DesignManagementType,
null: false, null: false,
...@@ -21,12 +21,12 @@ module EE ...@@ -21,12 +21,12 @@ module EE
field :geo_node, ::Types::Geo::GeoNodeType, field :geo_node, ::Types::Geo::GeoNodeType,
null: true, null: true,
resolver: Resolvers::Geo::GeoNodeResolver, resolver: ::Resolvers::Geo::GeoNodeResolver,
description: 'Find a Geo node' description: 'Find a Geo node'
field :instance_security_dashboard, ::Types::InstanceSecurityDashboardType, field :instance_security_dashboard, ::Types::InstanceSecurityDashboardType,
null: true, null: true,
resolver: Resolvers::InstanceSecurityDashboardResolver, resolver: ::Resolvers::InstanceSecurityDashboardResolver,
description: 'Fields related to Instance Security Dashboard' description: 'Fields related to Instance Security Dashboard'
def design_management def design_management
......
...@@ -151,6 +151,7 @@ module EE ...@@ -151,6 +151,7 @@ module EE
scope :with_designs, -> { where(id: DesignManagement::Design.select(:project_id)) } scope :with_designs, -> { where(id: DesignManagement::Design.select(:project_id)) }
scope :with_deleting_user, -> { includes(:deleting_user) } scope :with_deleting_user, -> { includes(:deleting_user) }
scope :with_compliance_framework_settings, -> { preload(:compliance_framework_setting) } scope :with_compliance_framework_settings, -> { preload(:compliance_framework_setting) }
scope :has_vulnerabilities, -> { joins(:vulnerabilities).group(:id) }
delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset, delegate :shared_runners_minutes, :shared_runners_seconds, :shared_runners_seconds_last_reset,
to: :statistics, allow_nil: true to: :statistics, allow_nil: true
......
---
title: Add standaloneVulnerabilitiesEnabled filter to group projects resolver for
GraphQL
merge_request: 31081
author:
type: added
# frozen_string_literal: true
require 'spec_helper'
describe Resolvers::NamespaceProjectsResolver do
include GraphqlHelpers
let(:current_user) { create(:user) }
context "with a group" do
let(:group) { create(:group) }
let(:project_1) { create(:project, namespace: group) }
let(:project_2) { create(:project, namespace: group) }
before do
project_1.add_developer(current_user)
project_2.add_developer(current_user)
create(:vulnerability, project: project_1)
end
describe '#resolve' do
subject(:projects) { resolve_projects(has_vulnerabilities) }
context 'when the `has_vulnerabilities` parameter is not truthy' do
let(:has_vulnerabilities) { false }
it { is_expected.to contain_exactly(project_1, project_2) }
end
context 'when the `has_vulnerabilities` parameter is truthy' do
let(:has_vulnerabilities) { true }
it { is_expected.to contain_exactly(project_1) }
end
end
end
def resolve_projects(has_vulnerabilities)
args = {
include_subgroups: false,
has_vulnerabilities: has_vulnerabilities
}
resolve(described_class, obj: group, args: args, ctx: { current_user: current_user })
end
end
...@@ -219,6 +219,19 @@ describe Project do ...@@ -219,6 +219,19 @@ describe Project do
end end
end end
end end
describe '.has_vulnerabilities' do
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
before do
create(:vulnerability, project: project_1)
end
subject { described_class.has_vulnerabilities }
it { is_expected.to contain_exactly(project_1) }
end
end end
describe 'validations' do describe 'validations' do
......
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