From fedd3d4ece7b90a24c980a84de66005936b23428 Mon Sep 17 00:00:00 2001 From: Ethan Urie Date: Fri, 31 Jul 2020 14:06:40 -0400 Subject: [PATCH 1/4] Add issue system hook --- app/models/hooks/system_hook.rb | 4 +++- app/views/admin/hooks/_form.html.haml | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/hooks/system_hook.rb b/app/models/hooks/system_hook.rb index c8a0cc059122ca..db2fb5b9f932d7 100644 --- a/app/models/hooks/system_hook.rb +++ b/app/models/hooks/system_hook.rb @@ -7,12 +7,14 @@ class SystemHook < WebHook :repository_update_hooks, :push_hooks, :tag_push_hooks, - :merge_request_hooks + :merge_request_hooks, + :issue_hooks ] default_value_for :push_events, false default_value_for :repository_update_events, true default_value_for :merge_requests_events, false + default_value_for :issues_events, false validates :url, system_hook_url: true diff --git a/app/views/admin/hooks/_form.html.haml b/app/views/admin/hooks/_form.html.haml index 17bb054b869ec1..7a20860aadfc4c 100644 --- a/app/views/admin/hooks/_form.html.haml +++ b/app/views/admin/hooks/_form.html.haml @@ -44,6 +44,13 @@ %strong Merge request events %p.light This URL will be triggered when a merge request is created/updated/merged + %li + = form.check_box :issues_events, class: 'float-left' + .prepend-left-20 + = form.label :issues_events, class: 'list-label' do + %strong Issue events + %p.light + This URL will be triggered when an issue is created .form-group = form.label :enable_ssl_verification, 'SSL verification', class: 'label-bold checkbox' .form-check -- GitLab From 05806181c09b10fb61bd0d0c60faf38fa5f2c22b Mon Sep 17 00:00:00 2001 From: Ethan Urie Date: Tue, 6 Oct 2020 17:32:38 -0400 Subject: [PATCH 2/4] Specs are passing --- app/services/issues/create_service.rb | 4 +++- spec/factories/system_hooks.rb | 14 ++++++++++++++ spec/models/hooks/system_hook_spec.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/services/issues/create_service.rb b/app/services/issues/create_service.rb index fb7683f940d171..3eac54d9862d57 100644 --- a/app/services/issues/create_service.rb +++ b/app/services/issues/create_service.rb @@ -11,7 +11,9 @@ def execute(skip_system_notes: false) filter_spam_check_params filter_resolve_discussion_params - create(@issue, skip_system_notes: skip_system_notes) + issue = create(@issue, skip_system_notes: skip_system_notes) + execute_hooks(issue, 'create') + issue end def before_create(issue) diff --git a/spec/factories/system_hooks.rb b/spec/factories/system_hooks.rb index a2d163a060fa5c..11329ab09dae8f 100644 --- a/spec/factories/system_hooks.rb +++ b/spec/factories/system_hooks.rb @@ -3,5 +3,19 @@ FactoryBot.define do factory :system_hook do url { generate(:url) } + + trait :all_events_enabled do + push_events { true } + merge_requests_events { true } + tag_push_events { true } + issues_events { true } + confidential_issues_events { true } + note_events { true } + confidential_note_events { true } + job_events { true } + pipeline_events { true } + wiki_page_events { true } + deployment_events { true } + end end end diff --git a/spec/models/hooks/system_hook_spec.rb b/spec/models/hooks/system_hook_spec.rb index e56d08c1847769..4810eb6b9c9940 100644 --- a/spec/models/hooks/system_hook_spec.rb +++ b/spec/models/hooks/system_hook_spec.rb @@ -138,6 +138,19 @@ headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' } ).once end + + it "issue_create hook" do + system_hook.issues_events = true + system_hook.save! + + # create(:issue, project: project) + Issues::CreateService.new(project, user).execute + + expect(WebMock).to have_requested(:post, system_hook.url).with( + body: /"object_kind":"issue"/, + headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' } + ) + end end describe '.repository_update_hooks' do -- GitLab From 68b79650bc0118278812cf200f579b88489876d4 Mon Sep 17 00:00:00 2001 From: Ethan Urie Date: Wed, 7 Oct 2020 16:52:57 -0400 Subject: [PATCH 3/4] Move `execute_hooks` to `after_create` --- app/services/issues/create_service.rb | 5 ++-- spec/factories/system_hooks.rb | 14 ----------- spec/models/hooks/system_hook_spec.rb | 34 ++++++++++++++++++++------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/app/services/issues/create_service.rb b/app/services/issues/create_service.rb index 3eac54d9862d57..be335a69d677d5 100644 --- a/app/services/issues/create_service.rb +++ b/app/services/issues/create_service.rb @@ -11,9 +11,7 @@ def execute(skip_system_notes: false) filter_spam_check_params filter_resolve_discussion_params - issue = create(@issue, skip_system_notes: skip_system_notes) - execute_hooks(issue, 'create') - issue + create(@issue, skip_system_notes: skip_system_notes) end def before_create(issue) @@ -34,6 +32,7 @@ def after_create(issue) resolve_discussions_with_issue(issue) delete_milestone_total_issue_counter_cache(issue.milestone) track_incident_action(current_user, issue, :incident_created) + execute_hooks(issue, 'create') super end diff --git a/spec/factories/system_hooks.rb b/spec/factories/system_hooks.rb index 11329ab09dae8f..a2d163a060fa5c 100644 --- a/spec/factories/system_hooks.rb +++ b/spec/factories/system_hooks.rb @@ -3,19 +3,5 @@ FactoryBot.define do factory :system_hook do url { generate(:url) } - - trait :all_events_enabled do - push_events { true } - merge_requests_events { true } - tag_push_events { true } - issues_events { true } - confidential_issues_events { true } - note_events { true } - confidential_note_events { true } - job_events { true } - pipeline_events { true } - wiki_page_events { true } - deployment_events { true } - end end end diff --git a/spec/models/hooks/system_hook_spec.rb b/spec/models/hooks/system_hook_spec.rb index 4810eb6b9c9940..3bd3d4f84939cb 100644 --- a/spec/models/hooks/system_hook_spec.rb +++ b/spec/models/hooks/system_hook_spec.rb @@ -139,17 +139,33 @@ ).once end - it "issue_create hook" do - system_hook.issues_events = true - system_hook.save! + describe "issue_create hook" do + it "fires when enabled" do + issue = build(:issue, project: project) - # create(:issue, project: project) - Issues::CreateService.new(project, user).execute + allow(Issue).to receive(:new).and_return(issue) + system_hook.issues_events = true + system_hook.save! - expect(WebMock).to have_requested(:post, system_hook.url).with( - body: /"object_kind":"issue"/, - headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' } - ) + Issues::CreateService.new(project, user).execute + + expect(WebMock).to have_requested(:post, system_hook.url).with( + body: /"object_kind":"issue".*"action":"create"/, + headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' } + ) + end + + it "doesn't fire when disabled" do + issue = build(:issue, project: project) + + allow(Issue).to receive(:new).and_return(issue) + + Issues::CreateService.new(project, user).execute + + expect(WebMock).not_to have_requested(:post, system_hook.url).with( + body: /"object_kind":"issue".*"action":"create"/ + ) + end end end -- GitLab From 2ec89acae02ab0326174ab263321ff2737dd5f0b Mon Sep 17 00:00:00 2001 From: Ethan Urie Date: Tue, 20 Oct 2020 17:33:10 -0400 Subject: [PATCH 4/4] Add the changelog file --- changelogs/unreleased/37248-add-global-issue-hooks.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/37248-add-global-issue-hooks.yml diff --git a/changelogs/unreleased/37248-add-global-issue-hooks.yml b/changelogs/unreleased/37248-add-global-issue-hooks.yml new file mode 100644 index 00000000000000..5f738c2e5f37ef --- /dev/null +++ b/changelogs/unreleased/37248-add-global-issue-hooks.yml @@ -0,0 +1,5 @@ +--- +title: Add system hook for Issue creation. +merge_request: 45719 +author: +type: added -- GitLab