Commit 64355323 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Add link_type filter argument to issue_links resolver

This will allow client applications to filter the issue_links of
vulnerabilities by link_type.
parent 23c70707
......@@ -12233,6 +12233,11 @@ type Vulnerability {
Returns the last _n_ elements from the list.
"""
last: Int
"""
Filter issue links by link type
"""
linkType: [VulnerabilityIssueLinkType!]
): VulnerabilityIssueLinkConnection!
"""
......
......@@ -36076,6 +36076,24 @@
"name": "issueLinks",
"description": "List of issue links related to the vulnerability",
"args": [
{
"name": "linkType",
"description": "Filter issue links by link type",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "VulnerabilityIssueLinkType",
"ofType": null
}
}
},
"defaultValue": null
},
{
"name": "after",
"description": "Returns the elements in the list that come after the specified cursor.",
# frozen_string_literal: true
module Resolvers
module Vulnerabilities
class IssueLinksResolver < BaseResolver
type Types::Vulnerability::IssueLinkType, null: true
argument :link_type, [Types::Vulnerability::IssueLinkTypeEnum],
required: false,
description: 'Filter issue links by link type'
delegate :issue_links, to: :object, private: true
def resolve(link_type: nil, **)
issue_links.by_link_type(link_type)
end
end
end
end
......@@ -35,7 +35,8 @@ module Types
resolve: -> (obj, _args, _ctx) { ::Gitlab::Routing.url_helpers.project_security_vulnerability_path(obj.project, obj) }
field :issue_links, ::Types::Vulnerability::IssueLinkType.connection_type, null: false,
description: "List of issue links related to the vulnerability"
description: "List of issue links related to the vulnerability",
resolver: Resolvers::Vulnerabilities::IssueLinksResolver
field :location, VulnerabilityLocationType, null: true,
description: 'Location metadata for the vulnerability. Its fields depend on the type of security scan that found the vulnerability',
......
......@@ -17,5 +17,7 @@ module Vulnerabilities
message: N_('already has a "created" issue link')
},
if: :created?
scope :by_link_type, -> (link_type) { link_type ? where(link_type: link_type.downcase) : all }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Resolvers::Vulnerabilities::IssueLinksResolver do
include GraphqlHelpers
describe '#resolve' do
let_it_be(:user) { create(:user) }
let_it_be(:vulnerability) { create(:vulnerability) }
let_it_be(:related_issue) { create(:vulnerabilities_issue_link, :related, vulnerability: vulnerability) }
let_it_be(:created_issue) { create(:vulnerabilities_issue_link, :created, vulnerability: vulnerability) }
subject { resolve(described_class, obj: vulnerability, args: filters, ctx: { current_user: user }) }
context 'when there is no filter given' do
let(:filters) { {} }
it { is_expected.to match_array([related_issue, created_issue]) }
end
context 'when the link_type filter is given' do
context 'when the filter is `CREATED`' do
let(:filters) { { link_type: 'CREATED' } }
it { is_expected.to match_array([created_issue]) }
end
context 'when the filter is `RELATED`' do
let(:filters) { { link_type: 'RELATED' } }
it { is_expected.to match_array([related_issue]) }
end
end
end
end
......@@ -78,4 +78,29 @@ describe Vulnerabilities::IssueLink do
end
end
end
describe '.by_link_type' do
let_it_be(:created_issue_link) { create(:vulnerabilities_issue_link, :created) }
let_it_be(:related_issue_link) { create(:vulnerabilities_issue_link, :related) }
subject { described_class.by_link_type(link_type).to_a }
context 'when the given argument is `nil`' do
let(:link_type) { nil }
it { is_expected.to match_array([created_issue_link, related_issue_link]) }
end
context 'when the given argument is an uppercase string enum value' do
let(:link_type) { 'CREATED' }
it { is_expected.to match_array([created_issue_link]) }
end
context 'when the given argument is an uppercase symbol enum value' do
let(:link_type) { :RELATED }
it { is_expected.to match_array([related_issue_link]) }
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