From d28e7206820654cae17462835ac9a03b959b28d1 Mon Sep 17 00:00:00 2001 From: Mario Celi Date: Wed, 9 Feb 2022 16:47:05 -0500 Subject: [PATCH] Add issuableUpdate subscription to GraphQL New subscription is only triggered when a work item is updated for now --- app/graphql/graphql_triggers.rb | 4 ++++ app/graphql/types/issuable_type.rb | 4 +++- app/graphql/types/subscription_type.rb | 3 +++ app/services/work_items/update_service.rb | 7 +++++++ doc/api/graphql/reference/index.md | 1 + spec/graphql/graphql_triggers_spec.rb | 14 +++++++++++++ spec/graphql/types/issuable_type_spec.rb | 6 +++++- spec/graphql/types/subscription_type_spec.rb | 1 + .../work_items/update_service_spec.rb | 20 +++++++++++++++++++ 9 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/graphql/graphql_triggers.rb b/app/graphql/graphql_triggers.rb index 290cd4d71460df..ac1a4a6b9ef615 100644 --- a/app/graphql/graphql_triggers.rb +++ b/app/graphql/graphql_triggers.rb @@ -8,4 +8,8 @@ def self.issuable_assignees_updated(issuable) def self.issue_crm_contacts_updated(issue) GitlabSchema.subscriptions.trigger('issueCrmContactsUpdated', { issuable_id: issue.to_gid }, issue) end + + def self.issuable_title_updated(issuable) + GitlabSchema.subscriptions.trigger('issuableTitleUpdated', { issuable_id: issuable.to_gid }, issuable) + end end diff --git a/app/graphql/types/issuable_type.rb b/app/graphql/types/issuable_type.rb index 6ca74087f8aa9a..4a39b5ed6ec623 100644 --- a/app/graphql/types/issuable_type.rb +++ b/app/graphql/types/issuable_type.rb @@ -5,10 +5,12 @@ class IssuableType < BaseUnion graphql_name 'Issuable' description 'Represents an issuable.' - possible_types Types::IssueType, Types::MergeRequestType + possible_types Types::IssueType, Types::MergeRequestType, Types::WorkItemType def self.resolve_type(object, context) case object + when WorkItem + Types::WorkItemType when Issue Types::IssueType when MergeRequest diff --git a/app/graphql/types/subscription_type.rb b/app/graphql/types/subscription_type.rb index 3629edb5b33e66..db6a247179db3e 100644 --- a/app/graphql/types/subscription_type.rb +++ b/app/graphql/types/subscription_type.rb @@ -9,5 +9,8 @@ class SubscriptionType < ::Types::BaseObject field :issue_crm_contacts_updated, subscription: Subscriptions::IssuableUpdated, null: true, description: 'Triggered when the crm contacts of an issuable are updated.' + + field :issuable_title_updated, subscription: Subscriptions::IssuableUpdated, null: true, + description: 'Triggered when the title of an issuable is updated.' end end diff --git a/app/services/work_items/update_service.rb b/app/services/work_items/update_service.rb index d610c11aeaf00a..5c45f4d90e5398 100644 --- a/app/services/work_items/update_service.rb +++ b/app/services/work_items/update_service.rb @@ -2,5 +2,12 @@ module WorkItems class UpdateService < ::Issues::UpdateService + private + + def after_update(issuable) + super + + GraphqlTriggers.issuable_title_updated(issuable) if issuable.previous_changes.key?(:title) + end end end diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md index 298762a9d5d9e1..f56af0a3a75dda 100644 --- a/doc/api/graphql/reference/index.md +++ b/doc/api/graphql/reference/index.md @@ -18761,6 +18761,7 @@ One of: - [`Epic`](#epic) - [`Issue`](#issue) - [`MergeRequest`](#mergerequest) +- [`WorkItem`](#workitem) #### `JobNeedUnion` diff --git a/spec/graphql/graphql_triggers_spec.rb b/spec/graphql/graphql_triggers_spec.rb index 0b53c633077042..2d83edca36374b 100644 --- a/spec/graphql/graphql_triggers_spec.rb +++ b/spec/graphql/graphql_triggers_spec.rb @@ -17,4 +17,18 @@ GraphqlTriggers.issuable_assignees_updated(issue) end end + + describe '.issuable_title_updated' do + it 'triggers the issuableTitleUpdated subscription' do + work_item = create(:work_item) + + expect(GitlabSchema.subscriptions).to receive(:trigger).with( + 'issuableTitleUpdated', + { issuable_id: work_item.to_gid }, + work_item + ).and_call_original + + GraphqlTriggers.issuable_title_updated(work_item) + end + end end diff --git a/spec/graphql/types/issuable_type_spec.rb b/spec/graphql/types/issuable_type_spec.rb index 992a58f524bd06..cb18bbe2eabcaa 100644 --- a/spec/graphql/types/issuable_type_spec.rb +++ b/spec/graphql/types/issuable_type_spec.rb @@ -4,7 +4,7 @@ RSpec.describe GitlabSchema.types['Issuable'] do it 'returns possible types' do - expect(described_class.possible_types).to include(Types::IssueType, Types::MergeRequestType) + expect(described_class.possible_types).to include(Types::IssueType, Types::MergeRequestType, Types::WorkItemType) end describe '.resolve_type' do @@ -16,6 +16,10 @@ expect(described_class.resolve_type(build(:merge_request), {})).to eq(Types::MergeRequestType) end + it 'resolves work items' do + expect(described_class.resolve_type(build(:work_item), {})).to eq(Types::WorkItemType) + end + it 'raises an error for invalid types' do expect { described_class.resolve_type(build(:user), {}) }.to raise_error 'Unsupported issuable type' end diff --git a/spec/graphql/types/subscription_type_spec.rb b/spec/graphql/types/subscription_type_spec.rb index bf933945a31da1..593795de004d4a 100644 --- a/spec/graphql/types/subscription_type_spec.rb +++ b/spec/graphql/types/subscription_type_spec.rb @@ -7,6 +7,7 @@ expected_fields = %i[ issuable_assignees_updated issue_crm_contacts_updated + issuable_title_updated ] expect(described_class).to have_graphql_fields(*expected_fields).only diff --git a/spec/services/work_items/update_service_spec.rb b/spec/services/work_items/update_service_spec.rb index 70dce26d63e7f8..f71f1060e40466 100644 --- a/spec/services/work_items/update_service_spec.rb +++ b/spec/services/work_items/update_service_spec.rb @@ -18,6 +18,26 @@ stub_spam_services end + context 'when title is changed' do + let(:opts) { { title: 'changed' } } + + it 'triggers issuable_title_updated graphql subscription' do + expect(GraphqlTriggers).to receive(:issuable_title_updated).with(work_item).and_call_original + + update_work_item + end + end + + context 'when title is not changed' do + let(:opts) { { description: 'changed' } } + + it 'does not trigger issuable_title_updated graphql subscription' do + expect(GraphqlTriggers).not_to receive(:issuable_title_updated) + + update_work_item + end + end + context 'when updating state_event' do context 'when state_event is close' do let(:opts) { { state_event: 'close' } } -- GitLab