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
bug/65077 Inline comment attachments are not linked to the comment when submit is via API (#19304)
Kabiru Mwenja34e136169ed5
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 38c9c6cc656..319c03e7b5f 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -111,7 +111,6 @@ class WorkPackages::ActivitiesTabController < ApplicationControllercall = create_journal_service_callif call.success? && call.result- claim_journal_attachments_for(call.result)set_last_server_timestamp_to_headershandle_successful_create_call(call)else@@ -129,7 +128,6 @@ class WorkPackages::ActivitiesTabController < ApplicationControllercall = update_journal_service_callif call.success? && call.result- claim_journal_attachments_for(call.result)update_item_show_component(journal: call.result, grouped_emoji_reactions: grouped_emoji_reactions_for_journal)elsehandle_failed_create_or_update_call(call)@@ -321,12 +319,6 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerJournals::UpdateService.new(model: @journal, user: User.current).call(notes:)end- def claim_journal_attachments_for(journal)- WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService- .new(user: User.current, model: journal)- .call- end-def generate_time_based_update_streams(last_update_timestamp)journals = @work_package.journalsdiff --git a/app/services/add_work_package_note_service.rb b/app/services/add_work_package_note_service.rbindex 929c3b2aa09..34efb080103 100644--- a/app/services/add_work_package_note_service.rb+++ b/app/services/add_work_package_note_service.rb@@ -44,7 +44,7 @@ class AddWorkPackageNoteServiceself.contract_class = WorkPackages::CreateNoteContractend- def call(notes, send_notifications: nil, internal: false)+ def call(notes, send_notifications: nil, internal: false) # rubocop:disable Metrics/AbcSizein_context(work_package, send_notifications:) dowork_package.add_journal(user:, notes:, internal:)@@ -60,7 +60,18 @@ class AddWorkPackageNoteServiceendend- ServiceResult.new(success:, result: journal, errors:)+ ServiceResult.new(success:, result: journal, errors:).on_success do |r|+ attachments_claims = claim_attachments_for(r.result)+ r.add_dependent!(attachments_claims)+ endendend++ private++ def claim_attachments_for(journal)+ WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService+ .new(user: User.current, model: journal)+ .call+ endenddiff --git a/app/services/journals/update_service.rb b/app/services/journals/update_service.rbindex db3ba619ede..b6837cd920f 100644--- a/app/services/journals/update_service.rb+++ b/app/services/journals/update_service.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -31,11 +33,28 @@ module Journalsprotecteddef after_perform(call)+ if call.success? && activity_comment?(call.result)+ attachments_claims = claim_attachments_for(call.result)+ call.add_dependent!(attachments_claims)+ end+OpenProject::Notifications.send(OpenProject::Events::JOURNAL_UPDATED,journal: call.result,send_notification: Journal::NotificationConfiguration.active?)callend++ private++ def activity_comment?(journal)+ journal.notes.present?+ end++ def claim_attachments_for(journal)+ WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService+ .new(user: User.current, model: journal)+ .call+ endendenddiff --git a/spec/requests/api/v3/activities_api_spec.rb b/spec/requests/api/v3/activities_api_spec.rbindex 9c97193b06e..f68dd164ffa 100644--- a/spec/requests/api/v3/activities_api_spec.rb+++ b/spec/requests/api/v3/activities_api_spec.rb@@ -190,6 +190,25 @@ RSpec.describe API::V3::Activities::ActivitiesAPI, content_type: :json doendendend++ context "with attachments" do+ let(:attachment1) { create(:attachment, container: nil, author: current_user) }+ let(:attachment2) { create(:attachment, container: nil, author: current_user) }++ let(:comment) do+ <<~HTML+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment1.id}/content">+ Lorem ipsum dolor sit amet+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment2.id}/content">+ consectetur adipiscing elit+ HTML+ end++ it "creates attachment claims" do+ expect(last_response.body).to be_json_eql(comment.to_json).at_path("comment/raw")+ expect(activity.reload.attachments).to contain_exactly(attachment1, attachment2)+ end+ endenddescribe "#get api" dodiff --git a/spec/requests/api/v3/activities_by_work_package_resource_spec.rb b/spec/requests/api/v3/activities_by_work_package_resource_spec.rbindex a5b8650009e..81382095e41 100644--- a/spec/requests/api/v3/activities_by_work_package_resource_spec.rb+++ b/spec/requests/api/v3/activities_by_work_package_resource_spec.rb@@ -209,6 +209,28 @@ RSpec.describe API::V3::Activities::ActivitiesByWorkPackageAPI, with_ee: [:interendendend++ context "with attachments" do+ include_context "create activity"++ let(:attachment1) { create(:attachment, container: nil, author: current_user) }+ let(:attachment2) { create(:attachment, container: nil, author: current_user) }++ let(:comment) do+ <<~HTML+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment1.id}/content">+ Lorem ipsum dolor sit amet+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment2.id}/content">+ consectetur adipiscing elit+ HTML+ end++ it "creates attachment claims" do+ expect(last_response.body).to be_json_eql(comment.to_json).at_path("comment/raw")+ journal = work_package.journals.last+ expect(journal.attachments).to contain_exactly(attachment1, attachment2)+ end+ endendendenddiff --git a/spec/services/add_work_package_note_service_spec.rb b/spec/services/add_work_package_note_service_spec.rbindex 5132e82b441..b86f4a2e9a8 100644--- a/spec/services/add_work_package_note_service_spec.rb+++ b/spec/services/add_work_package_note_service_spec.rb@@ -57,6 +57,10 @@ RSpec.describe AddWorkPackageNoteService, type: :model doendlet(:valid_contract) { true }let(:contract_errors) { instance_double(ActiveModel::Errors, full_messages: ["error message"]) }+ let(:claims_service) { WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService }+ let(:mock_claim_service_instance) do+ instance_double(claims_service, call: ServiceResult.success(result: []))+ endlet(:send_notifications) { false }@@ -64,6 +68,7 @@ RSpec.describe AddWorkPackageNoteService, type: :model doallow(instance).to receive(:contract_class).and_return(mock_contract)allow(work_package).to receive(:add_journal).and_call_originalallow(work_package).to receive(:save_journals).and_return(true)+ allow(claims_service).to receive(:new).and_return(mock_claim_service_instance)endsubject { instance.call("blubs", send_notifications:) }@@ -86,6 +91,51 @@ RSpec.describe AddWorkPackageNoteService, type: :model doendend+ context "when the journal notes have attachments" do+ let(:attachment1) { build_stubbed(:attachment) }+ let(:attachment2) { build_stubbed(:attachment) }++ let(:notes) do+ <<~HTML+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment1.id}/content">+ Lorem ipsum dolor sit amet+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment2.id}/content">+ consectetur adipiscing elit+ HTML+ end++ let(:mock_claim_service_instance) do+ instance_double(claims_service, call: ServiceResult.success(result: [attachment1, attachment2]))+ end++ subject { instance.call(notes, send_notifications:) }++ context "and note creation is successful" do+ it "creates an attachment claim" do+ expect(subject).to be_success+ expect(WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService)+ .to have_received(:new).with(user: user, model: work_package.journals.last)++ dependent_results = subject.dependent_results+ expect(dependent_results.size).to eq(1)+ expect(dependent_results.first).to be_a_success+ expect(dependent_results.first.result).to contain_exactly(attachment1, attachment2)+ end+ end++ context "and note creation is unsuccessful" do+ let(:valid_contract) { false }++ it "does not create an attachment claim" do+ expect(subject).to be_a_failure+ expect(WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService)+ .not_to have_received(:new)++ expect(subject.dependent_results).to be_empty+ end+ end+ end+it "creates an advisory lock" doallow(OpenProject::Mutex).to receive(:with_advisory_lock_transaction)diff --git a/spec/services/journals/update_service_spec.rb b/spec/services/journals/update_service_spec.rbnew file mode 100644index 00000000000..c4df6a3545b--- /dev/null+++ b/spec/services/journals/update_service_spec.rb@@ -0,0 +1,104 @@+# 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"+require "services/base_services/behaves_like_update_service"++RSpec.describe Journals::UpdateService do+ it_behaves_like "BaseServices update service" do+ let(:factory) { :journal }++ describe "Inline attachments" do+ let(:model_instance) { build_stubbed(:work_package_journal, notes: "Foobar") }+ let(:claims_service) { WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService }+ let(:attachment1) { build_stubbed(:attachment) }+ let(:attachment2) { build_stubbed(:attachment) }++ let(:notes) do+ <<~HTML+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment1.id}/content">+ Lorem ipsum dolor sit amet+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment2.id}/content">+ consectetur adipiscing elit+ HTML+ end++ let(:call_attributes) { { notes: } }++ let(:mock_claim_service_instance) do+ instance_double(claims_service, call: ServiceResult.success(result: [attachment1, attachment2]))+ end++ before do+ allow(claims_service).to receive(:new).and_return(mock_claim_service_instance)+ end++ context "when the journal notes have attachments" do+ context "and note creation is successful" do+ it "creates an attachment claim" do+ expect(subject).to be_success+ expect(WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService)+ .to have_received(:new).with(user: user, model: model_instance)++ dependent_results = subject.dependent_results+ expect(dependent_results.size).to eq(1)+ expect(dependent_results.first).to be_a_success+ expect(dependent_results.first.result).to contain_exactly(attachment1, attachment2)+ end+ end++ context "and note creation is unsuccessful" do+ let(:model_save_result) { false }++ it "does not create an attachment claim" do+ expect(subject).to be_a_failure+ expect(WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService)+ .not_to have_received(:new)++ expect(subject.dependent_results).to be_empty+ end+ end+ end++ context "with empty notes" do+ let(:model_instance) { build_stubbed(:work_package_journal, notes: "") }++ it "does not create an attachment claim" do+ expect(subject).to be_a_success++ expect(WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService)+ .not_to have_received(:new)++ expect(subject.dependent_results).to be_empty+ end+ end+ end+ end+end