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

add status to create project form

ulferts2366d128094b
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index b7671d2465b..1b1145e1684 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -43,6 +43,9 @@ module Projects
attribute :parent do
validate_parent_assignable
end
+ attribute :status do
+ validate_status_code_included
+ end
def validate
validate_user_allowed_to_manage
@@ -88,6 +91,10 @@ module Projects
errors.add :base, :error_unauthorized unless user.allowed_to?(manage_permission, model)
end
+ def validate_status_code_included
+ errors.add :status, :inclusion if model.status&.code && !Project::Status.codes.keys.include?(model.status.code.to_s)
+ end
+
def manage_permission
raise NotImplementedError
end
diff --git a/app/services/projects/set_attributes_service.rb b/app/services/projects/set_attributes_service.rb
index 43eb32d7583..5d3e1008ef8 100644
--- a/app/services/projects/set_attributes_service.rb
+++ b/app/services/projects/set_attributes_service.rb
@@ -32,6 +32,16 @@ module Projects
class SetAttributesService < ::BaseServices::SetAttributes
private
+ def set_attributes(attributes)
+ status_attributes = attributes.delete(:status)
+
+ ret = super(attributes)
+
+ update_status(status_attributes)
+
+ ret
+ end
+
def set_default_attributes(attributes)
attribute_keys = attributes.keys.map(&:to_s)
@@ -58,5 +68,42 @@ module Projects
def set_default_types(provided)
model.types = ::Type.default if !provided && model.types.empty?
end
+
+ def update_status(attributes)
+ with_hack_around_faulty_enum(attributes) do |safe_attributes|
+ if model.status
+ model.status.attributes = safe_attributes
+ else
+ model.build_status(safe_attributes)
+ end
+ end
+ end
+
+ # Hack around ArgumentError on faulty enum values
+ # https://github.com/rails/rails/issues/13971
+ def with_hack_around_faulty_enum(attributes)
+ faulty_code = if faulty_code?(attributes)
+ attributes.delete(:code)
+ end
+
+ yield(attributes)
+
+ if faulty_code
+ # set an arbitrary status code first to get rails internal into correct state
+ model.status.code = first_not_set_code
+ # hack into rails internals to set faulty code
+ code_attributes = model.status.instance_variable_get(:@attributes)['code']
+ code_attributes.instance_variable_set(:@value_before_type_cast, faulty_code)
+ code_attributes.instance_variable_set(:@value, faulty_code)
+ end
+ end
+
+ def faulty_code?(attributes)
+ attributes && attributes[:code] && !Project::Status.codes.keys.include?(attributes[:code])
+ end
+
+ def first_not_set_code
+ (Project::Status.codes.keys - [model.status.code]).first
+ end
end
end
diff --git a/lib/api/v3/projects/project_payload_representer.rb b/lib/api/v3/projects/project_payload_representer.rb
index 0e626b8d686..f141e24398e 100644
--- a/lib/api/v3/projects/project_payload_representer.rb
+++ b/lib/api/v3/projects/project_payload_representer.rb
@@ -33,6 +33,8 @@ module API
module Projects
class ProjectPayloadRepresenter < ProjectRepresenter
include ::API::Utilities::PayloadRepresenter
+
+ cached_representer disabled: true
end
end
end
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index d888123211a..ce3d1a6bbe0 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -41,7 +41,8 @@ module API
include API::Decorators::FormattableProperty
extend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass
- cached_representer key_parts: %i(status)
+ cached_representer key_parts: %i(status),
+ disabled: false
self_link
@@ -161,6 +162,15 @@ module API
getter: ->(*) {
::API::V3::Projects::Status::ProjectStatusRepresenter.create(represented.status || Project::Status.new,
current_user: current_user)
+ },
+ setter: ->(fragment:, **) {
+ status = fragment.with_indifferent_access.slice(:explanation)
+
+ if fragment['code']
+ status[:code] = fragment['code'].strip.tr(' ', '_').underscore.to_sym
+ end
+
+ represented.status = status
}
def _type
diff --git a/lib/api/v3/projects/status/project_status_representer.rb b/lib/api/v3/projects/status/project_status_representer.rb
index 34121f7897b..c34191b9c88 100644
--- a/lib/api/v3/projects/status/project_status_representer.rb
+++ b/lib/api/v3/projects/status/project_status_representer.rb
@@ -40,7 +40,10 @@ module API
formattable_property :explanation
property :code,
- render_nil: true
+ render_nil: true,
+ getter: ->(represented:, **) {
+ represented.code.to_s.tr('_', ' ') if represented.code
+ }
end
end
end
diff --git a/spec/contracts/projects/create_contract_spec.rb b/spec/contracts/projects/create_contract_spec.rb
index 9515957833d..8940b66c800 100644
--- a/spec/contracts/projects/create_contract_spec.rb
+++ b/spec/contracts/projects/create_contract_spec.rb
@@ -37,7 +37,8 @@ describe Projects::CreateContract do
description: project_description,
active: project_active,
public: project_public,
- parent: project_parent)
+ parent: project_parent,
+ status: project_status)
end
let(:permissions) { [:add_project] }
let!(:allowed_to) do
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 8afd41b2dda..ba46f899aa9 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -43,6 +43,7 @@ shared_examples_for 'project contract' do
let(:project_description) { 'Project description' }
let(:project_active) { true }
let(:project_public) { true }
+ let(:project_status) { FactoryBot.build_stubbed(:project_status) }
let(:project_parent) do
FactoryBot.build_stubbed(:project)
end
@@ -131,7 +132,7 @@ shared_examples_for 'project contract' do
end
end
- context 'if the status is nil' do
+ context 'if active is nil' do
let(:project_active) { nil }
it 'is invalid' do
@@ -139,6 +140,24 @@ shared_examples_for 'project contract' do
end
end
+ context 'if status is nil' do
+ let(:project_status) { nil }
+
+ it_behaves_like 'is valid'
+ end
+
+ context 'if status code is invalid' do
+ before do
+ allow(project_status)
+ .to receive(:code)
+ .and_return('bogus')
+ end
+
+ it 'is invalid' do
+ expect_valid(false, status: %i(inclusion))
+ end
+ end
+
context 'if the user lacks permission' do
let(:permissions) { [] }
diff --git a/spec/contracts/projects/update_contract_spec.rb b/spec/contracts/projects/update_contract_spec.rb
index 7c703d04310..09a2f4fbbbb 100644
--- a/spec/contracts/projects/update_contract_spec.rb
+++ b/spec/contracts/projects/update_contract_spec.rb
@@ -35,7 +35,8 @@ describe Projects::UpdateContract do
FactoryBot.build_stubbed(:project,
identifier: project_identifier,
active: project_active,
- public: project_public).tap do |p|
+ public: project_public,
+ status: project_status).tap do |p|
# in order to actually have something changed
p.name = project_name
p.parent = project_parent
diff --git a/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb
new file mode 100644
index 00000000000..e1edc112b52
--- /dev/null
+++ b/spec/lib/api/v3/projects/project_payload_representer_parsing_spec.rb
@@ -0,0 +1,110 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+
+describe ::API::V3::Projects::ProjectPayloadRepresenter, 'parsing' do
+ include ::API::V3::Utilities::PathHelper
+
+ let(:object) do
+ OpenStruct.new available_custom_fields: []
+ end
+ let(:user) { FactoryBot.build_stubbed(:user) }
+ let(:representer) do
+ described_class.create(object, current_user: user)
+ end
+
+ describe 'properties' do
+ context 'status' do
+ let(:hash) do
+ {
+ "status" => {
+ "code" => 'on track',
+ "explanation" => 'status code explanation'
+ }
+ }
+ end
+
+ it 'updates code' do
+ project = representer.from_hash(hash)
+ expect(project.status[:code])
+ .to eql(:on_track)
+ end
+
+ it 'updates explanation' do
+ project = representer.from_hash(hash)
+ expect(project.status[:explanation])
+ .to eql('status code explanation')
+ end
+
+ context 'with code not provided' do
+ let(:hash) do
+ {
+ "status" => {
+ "explanation" => 'status code explanation'
+ }
+ }
+ end
+
+ it 'does not set code' do
+ project = representer.from_hash(hash)
+ expect(project.status[:code])
+ .to be_nil
+ end
+
+ it 'updates explanation' do
+ project = representer.from_hash(hash)
+ expect(project.status[:explanation])
+ .to eql('status code explanation')
+ end
+ end
+
+ context 'with explanation not provided' do
+ let(:hash) do
+ {
+ "status" => {
+ "code" => 'off track'
+ }
+ }
+ end
+
+ it 'does not set code' do
+ project = representer.from_hash(hash)
+ expect(project.status[:code])
+ .to eql :off_track
+ end
+
+ it 'does not set explanation' do
+ project = representer.from_hash(hash)
+ expect(project.status[:explanation])
+ .to be_nil
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/v3/projects/project_representer_spec.rb b/spec/lib/api/v3/projects/project_representer_spec.rb
index 0c02d1a3082..2d5670cbd10 100644
--- a/spec/lib/api/v3/projects/project_representer_spec.rb
+++ b/spec/lib/api/v3/projects/project_representer_spec.rb
@@ -126,7 +126,7 @@ describe ::API::V3::Projects::ProjectRepresenter do
it 'includes the project status code' do
expect(subject)
- .to be_json_eql(status.code.to_json)
+ .to be_json_eql(status.code.tr('_', ' ').to_json)
.at_path('status/code')
end
diff --git a/spec/requests/api/v3/projects/create_form_resource_spec.rb b/spec/requests/api/v3/projects/create_form_resource_spec.rb
index f87fe767b0e..0a61fc27cd4 100644
--- a/spec/requests/api/v3/projects/create_form_resource_spec.rb
+++ b/spec/requests/api/v3/projects/create_form_resource_spec.rb
@@ -106,6 +106,10 @@ describe ::API::V3::Projects::CreateFormAPI, content_type: :json do
"customField#{text_custom_field.id}": {
"raw": "CF text"
},
+ status: {
+ code: "on track",
+ explanation: "A magic dwells in each beginning."
+ },
"_links": {
"customField#{list_custom_field.id}": {
"href": api_v3_paths.custom_option(list_custom_field.custom_options.first.id)
@@ -136,6 +140,18 @@ describe ::API::V3::Projects::CreateFormAPI, content_type: :json do
expect(body)
.to be_json_eql(api_v3_paths.custom_option(list_custom_field.custom_options.first.id).to_json)
.at_path("_embedded/payload/_links/customField#{list_custom_field.id}/href")
+
+ expect(body)
+ .to be_json_eql(
+ {
+ code: "on track",
+ explanation: {
+ "format": "markdown",
+ "html": "<p>A magic dwells in each beginning.</p>",
+ "raw": "A magic dwells in each beginning."
+ }
+ }.to_json
+ ).at_path("_embedded/payload/status")
end
it 'has a commit link' do
@@ -145,6 +161,31 @@ describe ::API::V3::Projects::CreateFormAPI, content_type: :json do
end
end
+ context 'with faulty status parameters' do
+ let(:params) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name',
+ status: {
+ code: "bogus"
+ }
+ }
+ end
+
+ it 'has 1 validation errors' do
+ expect(subject.body).to have_json_size(1).at_path('_embedded/validationErrors')
+ end
+
+ it 'has a validation error on status' do
+ expect(subject.body).to have_json_path('_embedded/validationErrors/status')
+ end
+
+ it 'has no commit link' do
+ expect(subject.body)
+ .not_to have_json_path('_links/commit')
+ end
+ end
+
context 'without the necessary permission' do
let(:permissions) { [] }
diff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rb
index 3ce4b4a46ec..6d1d4a8a718 100644
--- a/spec/services/projects/set_attributes_service_spec.rb
+++ b/spec/services/projects/set_attributes_service_spec.rb
@@ -254,6 +254,127 @@ describe Projects::SetAttributesService, type: :model do
end
end
end
+
+ context 'project status' do
+ context 'with a value provided' do
+ let(:call_attributes) do
+ {
+ status: {
+ code: 'on_track',
+ explanation: 'A magic dwells in each beginning.'
+ }
+ }
+ end
+
+ it 'set the project status code' do
+ expect(subject.result.status.code)
+ .to eql 'on_track'
+ end
+
+ it 'set the project status explanation' do
+ expect(subject.result.status.explanation)
+ .to eql 'A magic dwells in each beginning.'
+ end
+
+ it 'does not persist the status' do
+ expect(subject.result.status)
+ .to be_new_record
+ end
+ end
+ end
+ end
+
+ context 'for an existing project' do
+ context 'project status' do
+ context 'with the project not having a status before' do
+ context 'with a value provided' do
+ let(:call_attributes) do
+ {
+ status: {
+ code: 'on_track',
+ explanation: 'A magic dwells in each beginning.'
+ }
+ }
+ end
+
+ it 'set the project status code' do
+ expect(subject.result.status.code)
+ .to eql 'on_track'
+ end
+
+ it 'set the project status explanation' do
+ expect(subject.result.status.explanation)
+ .to eql 'A magic dwells in each beginning.'
+ end
+
+ it 'does not persist the status' do
+ expect(subject.result.status)
+ .to be_new_record
+ end
+ end
+
+ context 'with an invalid code' do
+ let(:call_attributes) do
+ {
+ status: {
+ code: 'bogus',
+ explanation: 'A magic dwells in each beginning.'
+ }
+ }
+ end
+
+ it 'set the project status code' do
+ expect(subject.result.status.code)
+ .to eql 'bogus'
+ end
+
+ it 'set the project status explanation' do
+ expect(subject.result.status.explanation)
+ .to eql 'A magic dwells in each beginning.'
+ end
+
+ it 'does not persist the status' do
+ expect(subject.result.status)
+ .to be_new_record
+ end
+ end
+ end
+
+ context 'with the project having a status before' do
+ let(:project_status) do
+ FactoryBot.build_stubbed(:project_status)
+ end
+ let(:project) do
+ FactoryBot.build_stubbed(:project, status: project_status)
+ end
+
+ context 'with a value provided' do
+ let(:call_attributes) do
+ {
+ status: {
+ code: 'at_risk',
+ explanation: 'Still some magic there.'
+ }
+ }
+ end
+
+ it 'set the project status code' do
+ expect(subject.result.status.code)
+ .to eql 'at_risk'
+ end
+
+ it 'set the project status explanation' do
+ expect(subject.result.status.explanation)
+ .to eql 'Still some magic there.'
+ end
+
+ it 'does not persist the status' do
+ expect(subject.result.status)
+ .to be_changed
+ end
+ end
+ end
+ end
end
end
end