Skip to content

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

wip - add topics#update end point to bcf api

ulferts5131309274b1
diff --git a/modules/bcf/app/contracts/bcf/issues/base_contract.rb b/modules/bcf/app/contracts/bcf/issues/base_contract.rb
index 76a0e696de7..3be0a0889ee 100644
--- a/modules/bcf/app/contracts/bcf/issues/base_contract.rb
+++ b/modules/bcf/app/contracts/bcf/issues/base_contract.rb
@@ -30,8 +30,6 @@
module Bcf::Issues
class BaseContract < ::ModelContract
- attribute :uuid
- attribute :work_package
attribute :index
def validate
@@ -39,5 +37,13 @@ module Bcf::Issues
super
end
+
+ private
+
+ def validate_user_allowed_to_manage
+ unless model.project && user.allowed_to?(:manage_bcf, model.project)
+ errors.add :base, :error_unauthorized
+ end
+ end
end
end
diff --git a/modules/bcf/app/contracts/bcf/issues/create_contract.rb b/modules/bcf/app/contracts/bcf/issues/create_contract.rb
index dc46229c0b3..827ea1acfb3 100644
--- a/modules/bcf/app/contracts/bcf/issues/create_contract.rb
+++ b/modules/bcf/app/contracts/bcf/issues/create_contract.rb
@@ -28,12 +28,7 @@
module Bcf::Issues
class CreateContract < BaseContract
- private
-
- def validate_user_allowed_to_manage
- unless model.project && user.allowed_to?(:manage_bcf, model.project)
- errors.add :base, :error_unauthorized
- end
- end
+ attribute :uuid
+ attribute :work_package
end
end
diff --git a/modules/bcf/app/contracts/bcf/issues/update_contract.rb b/modules/bcf/app/contracts/bcf/issues/update_contract.rb
new file mode 100644
index 00000000000..69ea3124665
--- /dev/null
+++ b/modules/bcf/app/contracts/bcf/issues/update_contract.rb
@@ -0,0 +1,32 @@
+#-- 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 Bcf::Issues
+ class UpdateContract < BaseContract
+ end
+end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/create.rb b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/create.rb
index 4aab66915f3..b478a07e777 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/create.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/create.rb
@@ -40,11 +40,5 @@ module Bcf::API::V2_1::Endpoints
def postprocess_errors(call)
Bcf::API::V2_1::Errors::ErrorMapper.map(super)
end
-
- private
-
- def deduce_process_service
- "::Bcf::#{deduce_backend_namespace}::#{update_or_create}Service".constantize
- end
end
end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/modify_mixin.rb b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/modify_mixin.rb
index 7d5fd4fdd1a..eea4c8b98cc 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/modify_mixin.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/modify_mixin.rb
@@ -36,6 +36,10 @@ module Bcf::API::V2_1::Endpoints
Bcf::API::V2_1::ParseResourceParamsService
end
+ def deduce_process_service
+ "::Bcf::#{deduce_backend_namespace}::#{update_or_create}Service".constantize
+ end
+
def deduce_in_and_out_representer
"::Bcf::API::V2_1::#{deduce_api_namespace}::SingleRepresenter".constantize
end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
index 17d4e65b38d..ae59a86a9ce 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
@@ -50,7 +50,10 @@ module Bcf::API::V2_1
end
get &::Bcf::API::V2_1::Endpoints::Show.new(model: Project).mount
- put &::Bcf::API::V2_1::Endpoints::Update.new(model: Project).mount
+ put &::Bcf::API::V2_1::Endpoints::Update
+ .new(model: Project,
+ process_service: ::Projects::UpdateService)
+ .mount
mount Bcf::API::V2_1::TopicsAPI
mount Bcf::API::V2_1::ProjectExtensions::API
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
index c6e902d6830..34e035fc1d8 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
@@ -51,10 +51,8 @@ module Bcf::API::V2_1
.new(model: Bcf::Issue,
api_name: 'Topics',
params_modifier: ->(attributes) {
- attributes[:project_id] = @project.id
-
wp_attributes = Bcf::Issues::TransformAttributesService
- .new
+ .new(@project)
.call(attributes)
.result
@@ -63,6 +61,7 @@ module Bcf::API::V2_1
:index,
:labels)
.merge(wp_attributes)
+ .merge(project: @project)
})
.mount
@@ -76,10 +75,28 @@ module Bcf::API::V2_1
api_name: 'Topics')
.mount
+ put &::Bcf::API::V2_1::Endpoints::Update
+ .new(model: Bcf::Issue,
+ api_name: 'Topics',
+ params_modifier: ->(attributes) {
+ # TODO: avoid code duplication with create
+ wp_attributes = Bcf::Issues::TransformAttributesService
+ .new(@project)
+ .call(attributes)
+ .result
+
+ attributes
+ .slice(:stage,
+ :index,
+ :labels)
+ .merge(wp_attributes)
+ })
+ .mount
+
delete &::Bcf::API::V2_1::Endpoints::Delete
- .new(model: Bcf::Issue,
- api_name: 'Topics')
- .mount
+ .new(model: Bcf::Issue,
+ api_name: 'Topics')
+ .mount
mount Bcf::API::V2_1::Viewpoints::API
end
diff --git a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
index 76ac8cda681..40a403ab4ff 100644
--- a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
+++ b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
@@ -30,6 +30,10 @@
module Bcf::Issues
class TransformAttributesService
+ def initialize(project)
+ self.project = project
+ end
+
def call(attributes)
ServiceResult.new success: true,
result: work_package_attributes(attributes)
@@ -37,6 +41,8 @@ module Bcf::Issues
private
+ attr_accessor :project
+
##
# BCF issues might have empty titles. OP needs one.
def title(attributes)
@@ -51,8 +57,8 @@ module Bcf::Issues
find_user_in_project(project, attributes[:author]) || User.system
end
- def assignee(project, attributes)
- assignee = find_user_in_project(project, attributes[:assignee])
+ def assignee(attributes)
+ assignee = find_user(attributes[:assignee])
return assignee if assignee.present?
@@ -61,11 +67,11 @@ module Bcf::Issues
##
# Try to find the given user by mail in the project
- def find_user_in_project(project, mail)
+ def find_user(mail)
project.users.find_by(mail: mail)
end
- def type(project, attributes)
+ def type(attributes)
type_name = attributes[:type]
type = project.types.find_by(name: type_name)
@@ -100,12 +106,8 @@ module Bcf::Issues
# Get mapped and raw attributes from MarkupExtractor
# and return all values that are non-nil
def work_package_attributes(attributes)
- project = Project.find(attributes[:project_id])
-
{
- # Fixed attributes we know
- project: project,
- type: type(project, attributes),
+ type: type(attributes),
# Native attributes from the extractor
subject: title(attributes),
@@ -114,7 +116,7 @@ module Bcf::Issues
start_date: attributes[:start_date],
# Mapped attributes
- assigned_to: assignee(project, attributes),
+ assigned_to: assignee(attributes),
status: status(attributes),
priority: priority(attributes)
}.compact
diff --git a/modules/bcf/app/services/bcf/issues/update_service.rb b/modules/bcf/app/services/bcf/issues/update_service.rb
new file mode 100644
index 00000000000..657221c3cb7
--- /dev/null
+++ b/modules/bcf/app/services/bcf/issues/update_service.rb
@@ -0,0 +1,49 @@
+#-- encoding: UTF-8
+
+#-- 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.
+#++
+
+module Bcf::Issues
+ class UpdateService < ::BaseServices::Update
+ private
+
+ def before_perform(params)
+ wp_call = ::WorkPackages::UpdateService
+ .new(model: model.work_package, user: user)
+ .call(params)
+
+ if wp_call.success?
+ issue_params = params.slice(:stage, :labels, :index)
+
+ super(issue_params)
+ else
+ wp_call
+ end
+ end
+ end
+end
diff --git a/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb b/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
index e77d97bfb16..7d1b749a96d 100644
--- a/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
+++ b/modules/bcf/lib/open_project/bcf/bcf_xml/issue_reader.rb
@@ -92,7 +92,7 @@ module OpenProject::Bcf::BcfXml
## and return all values that are non-nil
def work_package_attributes
attributes = ::Bcf::Issues::TransformAttributesService
- .new
+ .new(project)
.call(extractor_attributes.merge(import_options: import_options))
.result
.merge(send_notifications: false)
@@ -104,15 +104,9 @@ module OpenProject::Bcf::BcfXml
end
def extractor_attributes
- attributes = {
- project_id: project.id
- }
-
- %i(type title description due_date assignee status priority).each do |key|
- attributes[key] = extractor.send(key)
- end
-
- attributes
+ %i(type title description due_date assignee status priority).map do |key|
+ [attributes[key], extractor.send(key)]
+ end.to_h
end
##
diff --git a/modules/bcf/spec/contracts/bcf/issues/create_contract_spec.rb b/modules/bcf/spec/contracts/bcf/issues/create_contract_spec.rb
index a5d70aeb9ff..a465bea7a0b 100644
--- a/modules/bcf/spec/contracts/bcf/issues/create_contract_spec.rb
+++ b/modules/bcf/spec/contracts/bcf/issues/create_contract_spec.rb
@@ -34,9 +34,7 @@ describe Bcf::Issues::CreateContract do
let(:issue) do
Bcf::Issue.new(uuid: issue_uuid,
work_package: issue_work_package,
- stage: issue_stage,
- index: issue_index,
- labels: issue_labels)
+ index: issue_index)
end
let(:permissions) { [:manage_bcf] }
diff --git a/modules/bcf/spec/contracts/bcf/issues/shared_contract_examples.rb b/modules/bcf/spec/contracts/bcf/issues/shared_contract_examples.rb
index d44560ac17f..17df1903350 100644
--- a/modules/bcf/spec/contracts/bcf/issues/shared_contract_examples.rb
+++ b/modules/bcf/spec/contracts/bcf/issues/shared_contract_examples.rb
@@ -51,8 +51,6 @@ shared_examples_for 'issues contract' do
id
end
- let(:issue_stage) { nil }
- let(:issue_labels) { [] }
let(:issue_index) { 8 }
before do
@@ -99,14 +97,10 @@ shared_examples_for 'issues contract' do
end
end
- context 'if the stage is nil' do
- let(:issue_stage) { nil }
-
- it_behaves_like 'is valid'
- end
-
context 'if the stage is written' do
- let(:issue_stage) { 'some stage' }
+ before do
+ issue.stage = 'some stage'
+ end
it 'is invalid' do
expect_valid(false, stage: %i(error_readonly))
@@ -114,7 +108,9 @@ shared_examples_for 'issues contract' do
end
context 'if labels is written' do
- let(:issue_labels) { %w(some labels) }
+ before do
+ issue.labels = %w(some labels)
+ end
it 'is invalid' do
expect_valid(false, labels: %i(error_readonly))
diff --git a/modules/bcf/spec/contracts/bcf/issues/update_contract_spec.rb b/modules/bcf/spec/contracts/bcf/issues/update_contract_spec.rb
new file mode 100644
index 00000000000..a3f3e16b1d6
--- /dev/null
+++ b/modules/bcf/spec/contracts/bcf/issues/update_contract_spec.rb
@@ -0,0 +1,55 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require_relative './shared_contract_examples'
+
+describe Bcf::Issues::UpdateContract do
+ it_behaves_like 'issues contract' do
+ let(:issue) do
+ FactoryBot.build_stubbed(:bcf_issue,
+ work_package: issue_work_package).tap do |i|
+ # in order to actually have something changed
+ i.index = issue_index
+ end
+ end
+ let(:permissions) { [:manage_bcf] }
+
+ subject(:contract) { described_class.new(issue, current_user) }
+
+ context 'if work_package is altered' do
+ before do
+ issue.work_package = FactoryBot.build_stubbed(:stubbed_work_package)
+ end
+
+ it 'is invalid' do
+ expect_valid(false, work_package_id: %i(error_readonly))
+ end
+ end
+ end
+end
diff --git a/modules/bcf/spec/factories/bcf_issue_factory.rb b/modules/bcf/spec/factories/bcf_issue_factory.rb
index 094993505a3..1ad4fd627f1 100644
--- a/modules/bcf/spec/factories/bcf_issue_factory.rb
+++ b/modules/bcf/spec/factories/bcf_issue_factory.rb
@@ -109,8 +109,8 @@ FactoryBot.define do
</Markup>
MARKUP
end
- stage { 'Construction stop' }
- labels { ['Structural', 'IT Development', 'Huge'] }
+ stage { nil }
+ labels { [] }
sequence(:index) { |n| n }
factory :bcf_issue_with_viewpoint do
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
index 24443a4a54e..8bd5d149953 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
@@ -53,7 +53,12 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
let(:edit_work_package_member_user) do
FactoryBot.create(:user,
member_in_project: project,
- member_with_permissions: %i[add_work_packages delete_work_packages])
+ member_with_permissions: %i[manage_bcf
+ add_work_packages
+ view_linked_issues
+ edit_work_packages
+ view_work_packages
+ delete_work_packages])
end
let(:non_member_user) do
FactoryBot.create(:user)
@@ -446,4 +451,79 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
end
end
end
+
+ describe 'PUT /api/bcf/2.1/projects/:project_id/topics/:guid' do
+ let(:path) { "/api/bcf/2.1/projects/#{project.id}/topics/#{bcf_issue.uuid}" }
+ let(:current_user) { edit_member_user }
+ let!(:type) do
+ FactoryBot.create(:type).tap do |t|
+ project.types << t
+ end
+ end
+ let(:status) do
+ FactoryBot.create(:status)
+ end
+ let!(:default_status) do
+ FactoryBot.create(:default_status)
+ end
+ let!(:default_type) do
+ FactoryBot.create(:type, is_default: true)
+ end
+ let!(:standard_type) do
+ FactoryBot.create(:type_standard, name: 'Standard type')
+ end
+ let!(:priority) do
+ FactoryBot.create(:priority)
+ end
+ let!(:default_priority) do
+ FactoryBot.create(:default_priority)
+ end
+ let(:description) { 'some description' }
+ let(:index) { 5 }
+ let(:params) do
+ {
+ topic_type: type.name,
+ topic_status: status.name,
+ priority: priority.name,
+ title: 'BCF topic 101',
+ index: index,
+ due_date: Date.today.iso8601,
+ assigned_to: view_only_user.mail,
+ description: description
+ }
+ end
+
+ before do
+ login_as(current_user)
+ put path, params.to_json
+ end
+
+ it_behaves_like 'bcf api successful response' do
+ let(:expected_body) do
+ issue = Bcf::Issue.last
+ work_package = WorkPackage.last
+
+ {
+ guid: issue&.uuid,
+ topic_type: type.name,
+ topic_status: status.name,
+ priority: priority.name,
+ title: 'BCF topic 101',
+ labels: [],
+ index: index,
+ reference_links: [
+ api_v3_paths.work_package(work_package&.id)
+ ],
+ assigned_to: view_only_user.mail,
+ due_date: Date.today.iso8601,
+ stage: nil,
+ creation_author: work_package.author.mail,
+ creation_date: work_package&.created_at&.iso8601,
+ modified_author: edit_member_user.mail,
+ modified_date: work_package&.updated_at&.iso8601,
+ description: description
+ }
+ end
+ end
+ end
end