OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Move triggering of work package comment web hooks to own resource
Wieland Lindenthal60833841d95d
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 73fa5122ddd..cfe176e34bb 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -3249,6 +3249,7 @@ en:label_work_package_attachments: "Work package attachments"label_work_package_category_new: "New category"label_work_package_category_plural: "Work package categories"+ label_work_package_comments: "Work package comments"label_work_package_hierarchy: "Work package hierarchy"label_work_package_new: "New work package"label_work_package_edit: "Edit work package %{name}"diff --git a/modules/webhooks/app/workers/represented_webhook_job.rb b/modules/webhooks/app/workers/represented_webhook_job.rbindex 19486e354e3..a89fcf0feb8 100644--- a/modules/webhooks/app/workers/represented_webhook_job.rb+++ b/modules/webhooks/app/workers/represented_webhook_job.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -48,7 +50,7 @@ class RepresentedWebhookJob < WebhookJobenddef accepted_in_project?- webhook.enabled_for_project?(resource.project_id)+ webhook.enabled_for_project?(project_id)enddef request_signature(request_body)@@ -64,19 +66,23 @@ class RepresentedWebhookJob < WebhookJob}end- def payload_key- raise NotImplementedError- end-def represented_payloadpayload_representer_class.create(resource, current_user: User.current, embed_links: true)end+ def payload_key+ raise NotImplementedError+ end+def payload_representer_classraise NotImplementedErrorend+ def project_id # rubocop:disable Rails/Delegate+ resource.project_id+ end+def request_body# to_json needs to be called within the system user block in order to# have all the custom field visibility permissions set up correctly.diff --git a/modules/webhooks/app/workers/work_package_comment_webhook_job.rb b/modules/webhooks/app/workers/work_package_comment_webhook_job.rbnew file mode 100644index 00000000000..4e216c74126--- /dev/null+++ b/modules/webhooks/app/workers/work_package_comment_webhook_job.rb@@ -0,0 +1,45 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class WorkPackageCommentWebhookJob < RepresentedWebhookJob+ def payload_key+ :activity+ end++ def payload_representer_class+ ::API::V3::Activities::ActivityRepresenter+ end++ def project_id+ # For comment webhooks, the resource is a Journal+ # We need to get the project_id through the journable (WorkPackage)+ resource.journable.project_id+ end+enddiff --git a/modules/webhooks/config/locales/en.yml b/modules/webhooks/config/locales/en.ymlindex 0031b174c4e..ea70a0dd34e 100644--- a/modules/webhooks/config/locales/en.yml+++ b/modules/webhooks/config/locales/en.yml@@ -32,6 +32,8 @@ en:events:created: "Created"updated: "Updated"+ comment: "Comment"+ internal_comment: "Internal comment"explanation:text: >Upon the occurrence of an event like the creation of a work package or an update on a project, OpenProject will send a POST request to the configured web endpoints.diff --git a/modules/webhooks/lib/open_project/webhooks/event_resources.rb b/modules/webhooks/lib/open_project/webhooks/event_resources.rbindex 4c413f4d469..1fd8f285d69 100644--- a/modules/webhooks/lib/open_project/webhooks/event_resources.rb+++ b/modules/webhooks/lib/open_project/webhooks/event_resources.rb@@ -31,7 +31,7 @@ module OpenProject::Webhooksenddef resources- %i(project work_package time_entry attachment)+ %i(project work_package work_package_comment time_entry attachment)endendenddiff --git a/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rbindex 41e380c1007..802ce5f44a5 100644--- a/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb+++ b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package.rb@@ -1,3 +1,33 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#+++require_relative "base"module OpenProject::Webhooks::EventResourcesdiff --git a/modules/webhooks/lib/open_project/webhooks/event_resources/work_package_comment.rb b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package_comment.rbnew file mode 100644index 00000000000..7776ee6a6e9--- /dev/null+++ b/modules/webhooks/lib/open_project/webhooks/event_resources/work_package_comment.rb@@ -0,0 +1,64 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require_relative "base"++module OpenProject::Webhooks::EventResources+ class WorkPackageComment < Base+ class << self+ def notification_names+ [+ OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY+ ]+ end++ def available_actions+ %i(comment internal_comment)+ end++ def resource_name+ I18n.t :label_work_package_comments+ end++ protected++ def handle_notification(payload, _)+ journal = payload[:journal]+ return if journal.notes.blank?++ action = journal.internal? ? "internal_comment" : "comment"+ event_name = prefixed_event_name(action)+ active_webhooks.with_event_name(event_name).pluck(:id).each do |id|+ WorkPackageCommentWebhookJob.perform_later(id, journal, event_name)+ end+ end+ end+ end+enddiff --git a/modules/webhooks/spec/factories/webhook_factory.rb b/modules/webhooks/spec/factories/webhook_factory.rbindex d7f885951b1..8e9e4dbb132 100644--- a/modules/webhooks/spec/factories/webhook_factory.rb+++ b/modules/webhooks/spec/factories/webhook_factory.rb@@ -28,11 +28,19 @@FactoryBot.define dofactory :webhook, class: "Webhooks::Webhook" do- name { "Example Webhook" }+ transient do+ event_names { [] }+ end++ sequence(:name) { |i| "Example Webhook ##{i}" }url { "http://example.net/webhook_receiver/42" }description { "This is an example webhook" }secret { "42" }enabled { true }all_projects { true }++ callback(:after_create) do |webhook, evaluator|+ evaluator.event_names.each { |name| webhook.events.create!(name:) }+ endendenddiff --git a/modules/webhooks/spec/lib/open_project/webhooks/event_resources/work_package_comment_spec.rb b/modules/webhooks/spec/lib/open_project/webhooks/event_resources/work_package_comment_spec.rbnew file mode 100644index 00000000000..21e36c717a0--- /dev/null+++ b/modules/webhooks/spec/lib/open_project/webhooks/event_resources/work_package_comment_spec.rb@@ -0,0 +1,114 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe OpenProject::Webhooks::EventResources::WorkPackageComment do+ subject(:notification_sent) do+ OpenProject::Notifications.send(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY, journal:, send_mail: false)+ end++ let(:journal) { create(:work_package_journal, notes:, internal:) }+ let(:notes) { "I am a nice comment" }+ let(:internal) { false }++ let!(:comment_webhook) { create(:webhook, event_names: ["work_package_comment:comment"]) }+ let!(:internal_comment_webhook) { create(:webhook, event_names: ["work_package_comment:internal_comment"]) }++ context "when the journal is non-internal" do+ it "invokes comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).to have_been_enqueued.with(+ comment_webhook.id,+ journal,+ "work_package_comment:comment"+ )+ end++ it "does not invoke internal_comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).not_to have_been_enqueued.with(+ internal_comment_webhook.id,+ anything,+ anything+ )+ end+ end++ context "when the journal is internal" do+ let(:internal) { true }++ it "invokes internal_comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).to have_been_enqueued.with(+ internal_comment_webhook.id,+ journal,+ "work_package_comment:internal_comment"+ )+ end++ it "does not invoke comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).not_to have_been_enqueued.with(+ comment_webhook.id,+ anything,+ anything+ )+ end+ end++ context "when the journal has no comment" do+ let(:notes) { "" }++ it "does not invoke comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).not_to have_been_enqueued.with(+ comment_webhook.id,+ anything,+ anything+ )+ end++ it "does not invoke internal_comment web hooks" do+ notification_sent++ expect(WorkPackageCommentWebhookJob).not_to have_been_enqueued.with(+ internal_comment_webhook.id,+ anything,+ anything+ )+ end+ end+enddiff --git a/modules/webhooks/spec/workers/work_package_comment_webhook_job_spec.rb b/modules/webhooks/spec/workers/work_package_comment_webhook_job_spec.rbnew file mode 100644index 00000000000..b25b68de274--- /dev/null+++ b/modules/webhooks/spec/workers/work_package_comment_webhook_job_spec.rb@@ -0,0 +1,110 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe WorkPackageCommentWebhookJob, :webmock, type: :model do+ let(:user) { create(:admin) }+ let(:request_url) { "http://example.net/test/42" }+ let(:journal) { work_package.journals.last }+ let(:notes) { "Hello, I am a comment" }+ let(:work_package) { create(:work_package) }+ let(:webhook) { create(:webhook, all_projects: true, url: request_url, secret: nil) }++ let(:stubbed_url) { request_url }++ let(:request_headers) do+ { content_type: "application/json", accept: "application/json" }+ end++ let(:response_code) { 200 }+ let(:response_body) { "hook called" }+ let(:response_headers) do+ { content_type: "text/plain", x_spec: "foobar" }+ end++ let(:stub) do+ stub_request(:post, stubbed_url.sub("http://", "")).with(+ body: hash_including(+ "action" => event_name,+ "activity" => hash_including(+ "_type" => "Activity::Comment",+ "comment" => hash_including("raw" => notes)+ )+ ),+ headers: request_headers+ ).to_return(+ status: response_code,+ body: response_body,+ headers: response_headers+ )+ end++ let(:event_name) { "work_package_comment:comment" }++ subject(:job) { described_class.perform_now webhook.id, journal, event_name }++ before do+ journal.update!(notes:)+ allow(Webhooks::Webhook).to receive(:find).with(webhook.id).and_return(webhook)+ stub+ end++ it "requests the webhook" do+ subject+ expect(stub).to have_been_requested+ end++ it "creates a log for the call" do+ subject++ log = Webhooks::Log.last+ expect(log.webhook).to eq webhook+ expect(log.url).to eq webhook.url+ expect(log.event_name).to eq event_name+ expect(log.request_headers).to eq request_headers+ expect(log.response_code).to eq response_code+ expect(log.response_body).to eq response_body+ expect(log.response_headers).to eq response_headers+ end++ context "when the project does not match" do+ before do+ allow(webhook)+ .to receive(:enabled_for_project?).with(work_package.project_id)+ .and_return(false)+ end++ it "does not request the webhook" do+ subject+ expect(stub).not_to have_been_requested+ end+ end+end