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
use default end points for most of the relations api
ulferts481fe5b2918c
diff --git a/app/contracts/relations/delete_contract.rb b/app/contracts/relations/delete_contract.rbnew file mode 100644index 00000000000..08b641c6ead--- /dev/null+++ b/app/contracts/relations/delete_contract.rb@@ -0,0 +1,37 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)+#+# 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-2017 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 doc/COPYRIGHT.rdoc for more details.+#++++module Relations+ class DeleteContract < ::DeleteContract+ delete_permission -> {+ user.allowed_to? :manage_work_package_relations, model.from.project+ }+ end+enddiff --git a/app/services/relations/base_service.rb b/app/services/relations/base_service.rbindex 8906e8feb67..63c3f84cdbd 100644--- a/app/services/relations/base_service.rb+++ b/app/services/relations/base_service.rb@@ -40,33 +40,33 @@ class Relations::BaseServiceprivate- def update_relation(relation, attributes)- relation.attributes = relation.attributes.merge attributes+ def update_relation(model, attributes)+ model.attributes = model.attributes.merge attributes- success, errors = validate_and_save(relation, user)- success, errors = retry_with_inverse_for_relates(relation, errors) unless success+ success, errors = validate_and_save(model, user)+ success, errors = retry_with_inverse_for_relates(model, errors) unless success- result = ServiceResult.new success: success, errors: errors, result: relation+ result = ServiceResult.new success: success, errors: errors, result: model- if success && relation.follows?- reschedule_result = reschedule(relation)+ if success && model.follows?+ reschedule_result = reschedule(model)result.merge!(reschedule_result)endresultend- def set_defaults(relation)- if Relation::TYPE_FOLLOWS == relation.relation_type- relation.delay ||= 0+ def set_defaults(model)+ if Relation::TYPE_FOLLOWS == model.relation_type+ model.delay ||= 0else- relation.delay = nil+ model.delay = nilendend- def reschedule(relation)+ def reschedule(model)schedule_result = WorkPackages::SetScheduleService- .new(user: user, work_package: relation.to)+ .new(user: user, work_package: model.to).call# The to-work_package will not be altered by the schedule service so@@ -80,12 +80,12 @@ class Relations::BaseServiceschedule_resultend- def retry_with_inverse_for_relates(relation, errors)+ def retry_with_inverse_for_relates(model, errors)if errors.symbols_for(:base).include?(:"typed_dag.circular_dependency") &&- relation.canonical_type == Relation::TYPE_RELATES- relation.from, relation.to = relation.to, relation.from+ model.canonical_type == Relation::TYPE_RELATES+ model.from, model.to = model.to, model.from- validate_and_save(relation, user)+ validate_and_save(model, user)else[false, errors]enddiff --git a/app/services/relations/delete_service.rb b/app/services/relations/delete_service.rbnew file mode 100644index 00000000000..fc0ede01891--- /dev/null+++ b/app/services/relations/delete_service.rb@@ -0,0 +1,29 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)+#+# 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-2017 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 doc/COPYRIGHT.rdoc for more details.+#++++class Relations::DeleteService < ::BaseServices::Delete; enddiff --git a/app/services/relations/update_service.rb b/app/services/relations/update_service.rbindex eb498f243e0..46c410e4fe6 100644--- a/app/services/relations/update_service.rb+++ b/app/services/relations/update_service.rb@@ -29,17 +29,17 @@#++class Relations::UpdateService < Relations::BaseService- attr_accessor :relation+ attr_accessor :model- def initialize(user:, relation:)+ def initialize(user:, model:)super(user: user)- self.relation = relation+ self.model = modelself.contract_class = Relations::UpdateContractend- def call(attributes: {}, send_notifications: true)- in_context(send_notifications) do- update_relation relation, attributes+ def call(attributes)+ in_context(attributes[:send_notifications]) do+ update_relation model, attributesendendenddiff --git a/lib/api/v3/relations/relations_api.rb b/lib/api/v3/relations/relations_api.rbindex ee1ac75a3b2..83c66522a3b 100644--- a/lib/api/v3/relations/relations_api.rb+++ b/lib/api/v3/relations/relations_api.rb@@ -36,8 +36,6 @@ module APImodule V3module Relationsclass RelationsAPI < ::API::OpenProjectAPI- helpers ::API::V3::Relations::RelationsHelper-resources :relations doget doscope = Relation@@ -55,32 +53,8 @@ module APIendget &::API::V3::Utilities::Endpoints::Show.new(model: Relation).mount-- patch do- rep = parse_representer.new Relation.new, current_user: current_user- relation = rep.from_json request.body.read- attributes = filter_attributes relation- service = ::Relations::UpdateService.new relation: @relation,- user: current_user- call = service.call attributes: attributes,- send_notifications: (params[:notify] != 'false')-- if call.success?- representer.new call.result, current_user: current_user, embed_links: true- else- fail ::API::Errors::ErrorBase.create_and_merge_errors(call.errors)- end- end-- delete do- project_id = project_id_for_relation params[:id]- project = Project.find project_id-- authorize :manage_work_package_relations, context: project-- Relation.destroy params[:id]- status 204- end+ patch &::API::V3::Utilities::Endpoints::Update.new(model: Relation).mount+ delete &::API::V3::Utilities::Endpoints::Delete.new(model: Relation).mountendendenddiff --git a/lib/api/v3/relations/relations_helper.rb b/lib/api/v3/relations/relations_helper.rbindex 551bab20790..135d39d0f6b 100644--- a/lib/api/v3/relations/relations_helper.rb+++ b/lib/api/v3/relations/relations_helper.rb@@ -30,14 +30,6 @@ module APImodule V3module Relationsmodule RelationsHelper- def filter_attributes(relation)- relation- .changes- .map { |k, v| [k, v.last] }- .to_h- .with_indifferent_access- end-def representer::API::V3::Relations::RelationRepresenterend@@ -45,17 +37,6 @@ module APIdef parse_representer::API::V3::Relations::RelationPayloadRepresenterend-- def project_id_for_relation(id)- relations = Relation.table_name- work_packages = WorkPackage.table_name-- Relation- .joins(:from)- .where("#{relations}.id" => id)- .pluck("#{work_packages}.project_id")- .first- endendendenddiff --git a/spec/requests/api/v3/relations/relations_api_spec.rb b/spec/requests/api/v3/relations/relations_api_spec.rbindex e134e61abec..74ce3996c18 100644--- a/spec/requests/api/v3/relations/relations_api_spec.rb+++ b/spec/requests/api/v3/relations/relations_api_spec.rb@@ -369,15 +369,45 @@ describe 'API v3 Relation resource', type: :request, content_type: :json doenddescribe "deleting a relation" do- before do- relation+ let(:path) do+ api_v3_paths.relation(relation.id)+ end++ let(:permissions) { %i[view_work_packages manage_work_package_relations] }+ let(:role) { FactoryBot.create(:role, permissions: permissions) }- delete api_v3_paths.relation(relation.id)+ let(:current_user) do+ FactoryBot.create(:user).tap do |user|+ FactoryBot.create(:member,+ project: to.project,+ user: user,+ roles: [role])+ FactoryBot.create(:member,+ project: from.project,+ user: user,+ roles: [role])+ end+ end++ before do+ delete pathendit "should return 204 and destroy the relation" doexpect(last_response.status).to eq 204- expect(Relation.exists?(relation.id)).to eq false+ expect(Relation.exists?(relation.id)).to be_falsey+ end++ context 'lacking the permission' do+ let(:permissions) { %i[view_work_packages] }++ it 'returns 403' do+ expect(last_response.status).to eq 403+ end++ it 'leaves the relation' do+ expect(Relation.exists?(relation.id)).to be_truthy+ endendenddiff --git a/spec/services/relations/update_service_spec.rb b/spec/services/relations/update_service_spec.rbindex 23a1a1e832d..7bc1ec4f9b2 100644--- a/spec/services/relations/update_service_spec.rb+++ b/spec/services/relations/update_service_spec.rb@@ -50,7 +50,7 @@ describe Relations::UpdateService dostart_date: work_package2_start_date)endlet(:instance) do- described_class.new(user: user, relation: relation)+ described_class.new(user: user, model: relation)endlet(:relation) dorelation = FactoryBot.build_stubbed(:relation)